public async Task Simple_server_connection_must_bind_and_unbind() { #region echo-server-simple-bind // define an incoming request processing logic Flow <ByteString, ByteString, NotUsed> echo = Flow.Create <ByteString>(); Tcp.ServerBinding binding = await Sys.TcpStream() .BindAndHandle(echo, Materializer, "localhost", 9000); Console.WriteLine($"Server listening at {binding.LocalAddress}"); // close server after everything is done await binding.Unbind(); #endregion }
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; var binding = 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); }); _bindingPromise.NonBlockingTrySetResult(binding); } else if (msg is Tcp.CommandFailed) { var ex = BindFailedException.Instance; _bindingPromise.NonBlockingTrySetException(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")); } } }