COROIO: coroio/actors/actor.hpp File Reference
COROIO
Loading...
Searching...
No Matches
actor.hpp File Reference

Actor system implementation with message passing and behavior support. More...

#include <memory>
#include <coroio/corochain.hpp>
#include "messages.hpp"

Go to the source code of this file.

Classes

class  NNet::NActors::TActorId
 Unique identifier for actors in the system. More...
class  NNet::NActors::TEnvelope
 Message envelope containing routing and payload information. More...
class  NNet::NActors::TActorContext
 Context object providing actor communication and scheduling capabilities. More...
struct  NNet::NActors::TActorContext::TAsync
 Helper class for managing asynchronous operations in actors. More...
class  NNet::NActors::TMockActorContext
 Mock actor context for testing purposes. More...
class  NNet::NActors::IActor
 Base interface for all actors in the system. More...
class  NNet::NActors::ICoroActor
 Coroutine-based actor interface for asynchronous message processing. More...
class  NNet::NActors::IBehavior
 Base interface for actor behaviors. More...
class  NNet::NActors::TBehavior< TBaseBehavior, TMessages >
 Template for type-safe behavior implementations. More...
class  NNet::NActors::IBehaviorActor
 Actor that delegates message handling to a pluggable behavior. More...

Typedefs

using NNet::NActors::TLocalActorId = uint32_t
 Local actor identifier within a node.
using NNet::NActors::TNodeId = uint16_t
 Node identifier in a distributed system.
using NNet::NActors::TCookie = uint16_t
 Cookie for actor versioning and disambiguation.
using NNet::NActors::TMessageId = uint32_t
 Message type identifier.
using NNet::NActors::TEvent = std::pair<unsigned, TTime>

Detailed Description

Actor system implementation with message passing and behavior support.

This file contains the core components of an actor-based concurrent system. Actors are lightweight, isolated units of computation that communicate through message passing. The system supports both synchronous and asynchronous message handling, behavior switching, and coroutine-based actors.

Basic Usage Examples

Simple Actor Example

class MyActor : public IActor {
public:
void Receive(TMessageId messageId, TBlob blob, TActorContext::TPtr ctx) override {
if (messageId == MyMessage::MessageId) {
auto message = DeserializeNear<MyMessage>(blob);
// Process message
ctx->Send(ctx->Sender(), ResponseMessage{});
}
}
};

Coroutine Actor Example

class MyCoroActor : public ICoroActor {
public:
TFuture<void> CoReceive(TMessageId messageId, TBlob blob, TActorContext::TPtr ctx) override {
if (messageId == MyMessage::MessageId) {
auto message = DeserializeNear<MyMessage>(blob);
// Async operations
co_await ctx->Sleep(std::chrono::seconds(1));
auto response = co_await ctx->Ask<ResponseMessage>(
someActor, QueryMessage{}
);
ctx->Send(ctx->Sender(), response);
}
}
};

Behavior-Based Actor Example

class MyBehaviorActor : public IBehaviorActor,
public TBehavior<MyBehaviorActor, StartMessage, DataMessage> {
public:
MyBehaviorActor() {
Become(this); // Set initial behavior
}
void Receive(StartMessage&& msg, TBlob blob, TActorContext::TPtr ctx) {
// Handle start message
state_ = State::Started;
}
void Receive(DataMessage&& msg, TBlob blob, TActorContext::TPtr ctx) {
// Handle data message
processData(msg.data);
}
void HandleUnknownMessage(TMessageId messageId, TBlob blob, TActorContext::TPtr ctx) {
// Handle unknown messages
}
private:
enum class State { Idle, Started };
State state_ = State::Idle;
};

Async Behavior Example

class AsyncBehaviorActor : public IBehaviorActor,
public TBehavior<AsyncBehaviorActor, ProcessMessage> {
public:
AsyncBehaviorActor() { Become(this); }
TFuture<void> Receive(ProcessMessage&& msg, TBlob blob, TActorContext::TPtr ctx) {
// This method returns a future, so it will be handled asynchronously
co_await ctx->Sleep(std::chrono::milliseconds(100));
auto result = co_await processAsync(msg.data);
ctx->Send(ctx->Sender(), ResultMessage{result});
}
void HandleUnknownMessage(TMessageId messageId, TBlob blob, TActorContext::TPtr ctx) {
// Handle unknown messages
}
};