private StageActorRef.Receive Connecting(Outbound outbound) { return(args => { var sender = args.Item1; var msg = args.Item2; msg.Match() .With <Terminated>(() => FailStage(new StreamTcpException("The IO manager actor (TCP) has terminated. Stopping now."))) .With <Tcp.CommandFailed>(failed => FailStage(new StreamTcpException($"Tcp command {failed.Cmd} failed"))) .With <Tcp.Connected>(c => { ((Outbound)_role).LocalAddressPromise.TrySetResult(c.LocalAddress); _connection = sender; SetHandler(_bytesOut, _readHandler); StageActorRef.Unwatch(outbound.Manager); StageActorRef.Become(Connected); StageActorRef.Watch(_connection); _connection.Tell(new Tcp.Register(StageActorRef, keepOpenonPeerClosed: true, useResumeWriting: false), StageActorRef); if (IsAvailable(_bytesOut)) { _connection.Tell(Tcp.ResumeReading.Instance, StageActorRef); } Pull(_bytesIn); }); }); }
private void Receive(Tuple <IActorRef, object> args) { var sender = args.Item1; var msg = args.Item2; msg.Match() .With <Tcp.Bound>(bound => { _listener = sender; StageActorRef.Watch(_listener); if (IsAvailable(_stage._out)) { _listener.Tell(new Tcp.ResumeAccepting(1), StageActorRef); } var target = StageActorRef; _bindingPromise.TrySetResult(new StreamTcp.ServerBinding(bound.LocalAddress, () => { target.Tell(Tcp.Unbind.Instance, StageActorRef); return(_unbindPromise.Task); })); }) .With <Tcp.CommandFailed>(() => { var ex = BindFailedException.Instance; _bindingPromise.TrySetException(ex); _unbindPromise.TrySetResult(NotUsed.Instance); FailStage(ex); }) .With <Tcp.Connected>(c => { Push(_stage._out, ConnectionFor(c, sender)); }) .With <Tcp.Unbind>(() => { if (!IsClosed(_stage._out) && _listener != null) { TryUnbind(); } }) .With <Tcp.Unbound>(() => // If we're unbound then just shut down { if (_stage._connectionFlowsAwaitingInitialization.Current == 0) { CompleteStage(); } else { ScheduleOnce(BindShutdownTimer, _stage._bindShutdownTimeout); } }) .With <Terminated>(terminated => { if (Equals(terminated.ActorRef, _listener)) { FailStage(new IllegalStateException("IO Listener actor terminated unexpectedly")); } }); }
private void Receive(Tuple <IActorRef, object> args) { var sender = args.Item1; var msg = args.Item2; if (msg is Tcp.Bound) { var bound = (Tcp.Bound)msg; _listener = sender; StageActorRef.Watch(_listener); if (IsAvailable(_stage._out)) { _listener.Tell(new Tcp.ResumeAccepting(1), StageActorRef); } var thisStage = StageActorRef; _bindingPromise.TrySetResult(new StreamTcp.ServerBinding(bound.LocalAddress, () => { // Beware, sender must be explicit since stageActor.ref will be invalid to access after the stage stopped thisStage.Tell(Tcp.Unbind.Instance, thisStage); return(_unbindPromise.Task); })); } else if (msg is Tcp.CommandFailed) { var ex = BindFailedException.Instance; _bindingPromise.TrySetException(ex); _unbindPromise.TrySetResult(NotUsed.Instance); FailStage(ex); } else if (msg is Tcp.Connected) { var connected = (Tcp.Connected)msg; Push(_stage._out, ConnectionFor(connected, sender)); } else if (msg is Tcp.Unbind) { if (!IsClosed(_stage._out) && !ReferenceEquals(_listener, null)) { TryUnbind(); } } else if (msg is Tcp.Unbound) { UnbindCompleted(); } else if (msg is Terminated) { if (_unbindStarted) { UnbindCompleted(); } else { FailStage(new IllegalStateException("IO Listener actor terminated unexpectedly")); } } }
public override void PreStart() { SetKeepGoing(true); _self = GetStageActorRef(Receive); _self.Watch(_stage._actorRef); _stage._actorRef.Tell(_stage._onInitMessage, _self); Pull(_stage._inlet); }
private void Behaviour(Tuple <IActorRef, object> args) { var msg = args.Item2; var sender = args.Item1; msg.Match() .With <Add>(a => _sum += a.N) .With <PullNow>(() => Pull(_stage._inlet)) .With <CallInitStageActorRef>(() => sender.Tell(GetStageActorRef(Behaviour), _self)) .With <BecomeStringEcho>(() => GetStageActorRef(tuple => tuple.Item1.Tell(tuple.Item2.ToString()))) .With <StopNow>(() => { _promise.TrySetResult(_sum); CompleteStage(); }).With <AddAndTell>(a => { _sum += a.N; sender.Tell(_sum, _self); }) .With <WatchMe>(w => _self.Watch(w.Watchee)) .With <Terminated>(t => _stage._probe.Tell(new WatcheeTerminated(t.ActorRef))); }