138 operator bool()
const {
139 return !((NodeId_ == 0) & (ActorId_ == 0) & (Cookie_ == 0));
164 + std::to_string(NodeId_) +
":"
165 + std::to_string(ActorId_) +
":"
166 + std::to_string(Cookie_);
202using TEvent = std::pair<unsigned, TTime>;
218 using TPtr = std::unique_ptr<TActorContext>;
307 template<
typename Rep,
typename Period>
318 template<
typename T,
typename TQuestion>
330 : ActorSystem_(actorSystem)
338 TActorSystem* ActorSystem_;
347 static void*
operator new(
size_t size, TActorSystem* actorSystem);
348 static void operator delete(
void* ptr);
354 , ActorSystem(actorSystem)
371class TMockActorContext :
public TActorContext {
373 TMockActorContext(
TActorId sender,
TActorId self, TActorSystem* actorSystem)
374 : TActorContext(sender, self, actorSystem)
390 using TPtr = std::unique_ptr<IActor>;
391 friend class TActorSystem;
394 virtual ~IActor() =
default;
488template<
typename TBaseBehavior,
typename... TMessages>
502 bool handled = (TryHandleMessage<TMessages>(
509 static_cast<TBaseBehavior*
>(
this)->HandleUnknownMessage(messageId, std::move(blob), std::move(ctx));
514 template<
typename TMessage>
515 bool TryHandleMessage(
TMessageId messageId,
TBlob& blob, TActorContext::TPtr& ctx) {
516 if (TMessage::MessageId == messageId) {
517 if (blob.Type == TBlob::PointerType::Near) {
518 auto&& mes = DeserializeNear<TMessage>(blob);
519 HandleMessage<TMessage>(
525 auto&& mes = DeserializeFar<TMessage>(blob);
526 HandleMessage<TMessage>(
537 template<
typename TMessage>
538 void HandleMessage(TMessage&& message, TBlob blob, TActorContext::TPtr ctx)
540 using ReturnType =
decltype(
static_cast<TBaseBehavior*
>(
this)->Receive(
541 std::declval<TMessage>(),
542 std::declval<TBlob>(),
543 std::declval<TActorContext::TPtr>()
546 if constexpr (std::is_same_v<ReturnType, void>) {
547 static_cast<TBaseBehavior*
>(
this)->
Receive(
553 auto async = ctx->StartAsync();
554 auto future =
static_cast<TBaseBehavior*
>(
this)->
Receive(
559 if (!future.done()) {
560 async.Commit(std::move(future));
609 CurrentBehavior_ = behavior;
619 CurrentBehavior_->Receive(messageId, std::move(blob), std::move(ctx));
uint16_t TNodeId
Node identifier in a distributed system.
Definition actor.hpp:113
uint32_t TMessageId
Message type identifier.
Definition actor.hpp:119
uint16_t TCookie
Cookie for actor versioning and disambiguation.
Definition actor.hpp:116
uint32_t TLocalActorId
Local actor identifier within a node.
Definition actor.hpp:110
virtual void Receive(TMessageId messageId, TBlob blob, TActorContext::TPtr ctx)=0
Process an incoming message.
Actor that delegates message handling to a pluggable behavior.
Definition actor.hpp:599
void Receive(TMessageId messageId, TBlob blob, TActorContext::TPtr ctx) override
Delegate message handling to the current behavior.
Definition actor.hpp:618
void Become(IBehavior *behavior)
Switch to a new behavior.
Definition actor.hpp:608
Base interface for actor behaviors.
Definition actor.hpp:447
virtual void Receive(TMessageId messageId, TBlob blob, TActorContext::TPtr ctx)=0
Process an incoming message according to this behavior.
Coroutine-based actor interface for asynchronous message processing.
Definition actor.hpp:417
void Receive(TMessageId messageId, TBlob blob, TActorContext::TPtr ctx) override
Synchronous receive method (calls CoReceive internally)
virtual TFuture< void > CoReceive(TMessageId messageId, TBlob blob, TActorContext::TPtr ctx)=0
Asynchronous message processing method.
TActorId Self() const
Get this actor's ID.
Definition actor.hpp:226
void Forward(TActorId to, TMessageId messageId, TBlob blob)
Forward a message to another actor (preserves original sender)
TFuture< void > Sleep(TTime until)
Sleep until a specific time.
Definition actorsystem.hpp:312
TFuture< T > Ask(TActorId recipient, TQuestion &&question)
Send a message and wait for a response.
Definition actorsystem.hpp:322
void Cancel(TEvent event)
Cancel a previously scheduled message.
void Send(TActorId to, TMessageId messageId, TBlob blob)
Send a message to another actor.
TAsync StartAsync()
Start an asynchronous operation context.
TEvent Schedule(TTime when, TActorId sender, TActorId recipient, TMessageId messageId, TBlob blob)
Schedule a message to be delivered at a specific time.
TActorId Sender() const
Get the sender of the current message.
Definition actor.hpp:221
Unique identifier for actors in the system.
Definition actor.hpp:129
TNodeId NodeId() const
Get the node ID component.
Definition actor.hpp:143
TCookie Cookie() const
Get the cookie component.
Definition actor.hpp:154
TActorId()=default
Default constructor creates an invalid actor ID.
TActorId(TNodeId nodeId, TLocalActorId actorId, TCookie cookie)
Construct actor ID with specific components.
Definition actor.hpp:175
std::string ToString() const
Convert actor ID to string representation.
Definition actor.hpp:162
TLocalActorId ActorId() const
Get the local actor ID component.
Definition actor.hpp:149
Definition actorsystem.hpp:62
Template for type-safe behavior implementations.
Definition actor.hpp:490
void Receive(TMessageId messageId, TBlob blob, TActorContext::TPtr ctx) override
Process incoming message with automatic type dispatch.
Definition actor.hpp:501
Message envelope containing routing and payload information.
Definition actor.hpp:194
TActorId Recipient
Actor that should receive the message.
Definition actor.hpp:197
TBlob Blob
Serialized message data.
Definition actor.hpp:199
TActorId Sender
Actor that sent the message.
Definition actor.hpp:196
TMessageId MessageId
Type identifier of the message.
Definition actor.hpp:198
Mock actor context for testing purposes.
Definition actor.hpp:371
Implementation of a promise/future system for coroutines.
Helper class for managing asynchronous operations in actors.
Definition actor.hpp:328
Definition messages.hpp:11
Future type for coroutines returning a value of type T.
Definition corochain.hpp:182