public ServerFactory( Lifetime lifetime, [NotNull] Func <WireParameters> wireParametersFactory, IPEndPoint endpoint = null ) { var serverSocket = Server.CreateServerSocket(lifetime, endpoint); LocalPort = ((IPEndPoint)serverSocket.LocalEndPoint).Port; void Rec() { lifetime.TryExecute(() => { var(scheduler, id) = wireParametersFactory(); var s = new Server(lifetime, scheduler, serverSocket, id); s.Connected.WhenTrue(lifetime, lt => { Connected.AddLifetimed(lt, s); Rec(); }); }); } Rec(); }
public ServerFactory( Lifetime lifetime, [NotNull] Func <WireParameters> wireParametersFactory, IPEndPoint endpoint = null ) { var serverSocket = Server.CreateServerSocket(endpoint); var serverSocketLifetimeDef = new LifetimeDefinition(lifetime); serverSocketLifetimeDef.Lifetime.OnTermination(() => { ourStaticLog.Verbose("closing server socket"); Base.CloseSocket(serverSocket); }); LocalPort = ((IPEndPoint)serverSocket.LocalEndPoint).Port; void Rec() { lifetime.TryExecute(() => { var(scheduler, id) = wireParametersFactory(); var s = new Server(lifetime, scheduler, serverSocket, id); // Each server will spawn a thread that will be waiting in serverSocket.Accept method. When lifetime // termination is invoked, these threads synchronously join the termination thread. Since these Thread.Join // calls are located deeper in the Lifetime termination stack we have to place this socket termination call // after each server creation. lifetime.OnTermination(() => serverSocketLifetimeDef.Terminate()); s.Connected.WhenTrue(lifetime, lt => { Connected.AddLifetimed(lt, s); Rec(); }); }); } Rec(); }