6#include <coroio/notifier.hpp>
130using TEvent = std::pair<unsigned, TTime>;
150 using TPtr = std::unique_ptr<TActorContext>;
197 template<
typename T,
typename... Args>
211 template<
typename T,
typename... Args>
233 template<
typename T,
typename... Args>
256 template<
typename T,
typename... Args>
263 void Cancel(TEvent event);
298 template<
typename Rep,
typename Period>
318 template<
typename T,
typename TQuestion>
329 TAsync(TActorSystem* actorSystem, TLocalActorId actorId)
330 : ActorSystem_(actorSystem)
337 TLocalActorId ActorId_;
338 TActorSystem* ActorSystem_;
347 static void*
operator new(size_t) =
delete;
348 static void*
operator new(
size_t size, TActorSystem* actorSystem);
349 static void operator delete(
void* ptr);
355 , ActorSystem(actorSystem)
372class TMockActorContext :
public TActorContext {
374 TMockActorContext(
TActorId sender,
TActorId self, TActorSystem* actorSystem)
375 : TActorContext(sender, self, actorSystem)
391 using TPtr = std::unique_ptr<IActor>;
392 friend class TActorSystem;
395 virtual ~IActor() =
default;
407 virtual void Receive(TMessageId messageId,
TBlob blob, TActorContext::TPtr ctx) = 0;
455 void Receive(TMessageId messageId,
TBlob blob, TActorContext::TPtr ctx)
final;
490 virtual void Receive(TMessageId messageId,
TBlob blob, TActorContext::TPtr ctx) = 0;
524template<
typename TBaseBehavior,
typename... TMessages>
537 void Receive(TMessageId messageId,
TBlob blob, TActorContext::TPtr ctx)
final {
538 bool handled = (TryHandleMessage<TMessages>(
545 static_cast<TBaseBehavior*
>(
this)->HandleUnknownMessage(messageId, std::move(blob), std::move(ctx));
550 template<
typename TMessage>
551 bool TryHandleMessage(TMessageId messageId,
TBlob& blob, TActorContext::TPtr& ctx) {
552 if (TMessage::MessageId == messageId) {
554 auto&& mes = DeserializeNear<TMessage>(blob);
555 HandleMessage<TMessage>(
560 }
else if constexpr (is_local_message_v<TMessage>) {
563 auto&& mes = DeserializeFar<TMessage>(blob);
564 HandleMessage<TMessage>(
575 template<
typename TMessage>
576 void HandleMessage(TMessage&& message, TBlob blob, TActorContext::TPtr ctx)
578 using ReturnType =
decltype(
static_cast<TBaseBehavior*
>(
this)->Receive(
579 std::declval<TMessage>(),
580 std::declval<TBlob>(),
581 std::declval<TActorContext::TPtr>()
584 if constexpr (std::is_same_v<ReturnType, void>) {
585 static_cast<TBaseBehavior*
>(
this)->
Receive(
591 auto async = ctx->StartAsync();
592 auto future =
static_cast<TBaseBehavior*
>(
this)->
Receive(
597 if (!future.done()) {
598 async.Commit(std::move(future));
654 CurrentBehavior_ = behavior;
663 void Receive(TMessageId messageId,
TBlob blob, TActorContext::TPtr ctx)
final {
664 assert(CurrentBehavior_);
665 CurrentBehavior_->Receive(messageId, std::move(blob), std::move(ctx));
Base interface for all actors in the system.
Definition actor.hpp:389
virtual void BeforeUnregister(TActorContext::TPtr)
Called once on PoisonPill, right before the actor is destroyed.
Definition actor.hpp:423
virtual void AfterRegister(TActorContext::TPtr)
Called once right after the actor is registered in the system.
Definition actor.hpp:415
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:642
void Receive(TMessageId messageId, TBlob blob, TActorContext::TPtr ctx) final
Delegate message handling to the current behavior.
Definition actor.hpp:663
void Become(IBehavior *behavior)
Switch to a new behavior.
Definition actor.hpp:653
Base interface for actor behaviors.
Definition actor.hpp:480
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:447
void Receive(TMessageId messageId, TBlob blob, TActorContext::TPtr ctx) final
IActor bridge — invokes CoReceive and parks pending futures.
Definition actor.cpp:7
virtual TFuture< void > CoReceive(TMessageId messageId, TBlob blob, TActorContext::TPtr ctx)=0
Asynchronous message handler (override in subclass).
TActorId Self() const
Get this actor's ID.
Definition actor.hpp:158
void SendAs(TActorId from, TActorId to, TMessageId messageId, TBlob blob)
Send a message with an explicitly specified sender.
Definition actorsystem.cpp:41
void Forward(TActorId to, TMessageId messageId, TBlob blob)
Forward a message to another actor (preserves original sender).
Definition actorsystem.cpp:36
TFuture< void > Sleep(TTime until)
Suspend the current handler until a specific time.
Definition actorsystem.hpp:524
TActorId Register(std::unique_ptr< IActor > actor)
Registers a new actor in the same actor system.
Definition actorsystem.cpp:56
TFuture< T > Ask(TActorId recipient, TQuestion &&question)
Send a request and suspend until a reply of type T arrives.
Definition actorsystem.hpp:534
std::unique_ptr< INotifier > CreateNotifier()
Creates a notifier bound to the actor system's poller, on the poller thread.
Definition actorsystem.cpp:26
void Cancel(TEvent event)
Cancel a previously scheduled message.
Definition actorsystem.cpp:51
void Send(TActorId to, TMessageId messageId, TBlob blob)
Send a message to another actor.
Definition actorsystem.cpp:31
TAsync StartAsync()
Start an asynchronous operation context.
Definition actor.cpp:16
TEvent Schedule(TTime when, TActorId sender, TActorId recipient, TMessageId messageId, TBlob blob)
Schedule a message to be delivered at a specific time.
Definition actorsystem.cpp:46
TActorId Sender() const
Get the sender of the current message.
Definition actor.hpp:153
Globally unique identifier for actors across a distributed system.
Definition actorid.hpp:34
Single-threaded actor runtime.
Definition actorsystem.hpp:101
Template for type-safe behavior implementations.
Definition actor.hpp:526
void Receive(TMessageId messageId, TBlob blob, TActorContext::TPtr ctx) final
Process incoming message with automatic type dispatch.
Definition actor.hpp:537
Message envelope containing routing and payload information.
Definition actor.hpp:122
TActorId Recipient
Actor that should receive the message.
Definition actor.hpp:125
TBlob Blob
Serialized message data.
Definition actor.hpp:127
TActorId Sender
Actor that sent the message.
Definition actor.hpp:124
TMessageId MessageId
Type identifier of the message.
Definition actor.hpp:126
Mock actor context for testing purposes.
Definition actor.hpp:372
Implementation of a promise/future system for coroutines.
Helper class for managing asynchronous operations in actors.
Definition actor.hpp:328
Opaque message payload with Near/Far duality.
Definition messages.hpp:101
@ Near
Live object pointer — valid only within the same process.
Definition messages.hpp:106
Owned coroutine handle that carries a result of type T.
Definition corochain.hpp:185