public Connection(TcpRpcServer server, TcpClient client, FramePump pump, OutboundTcpEndpoint outboundEp, RpcEngine.RpcEndpoint inboundEp) { Client = client; Pump = pump; OutboundEp = outboundEp; InboundEp = inboundEp; PumpRunner = new Thread(o => { try { Thread.CurrentThread.Name = $"TCP RPC Server Thread {Thread.CurrentThread.ManagedThreadId}"; Pump.Run(); } finally { OutboundEp.Dismiss(); InboundEp.Dismiss(); Pump.Dispose(); lock (server._reentrancyBlocker) { --server.ConnectionCount; server._connections.Remove(this); } } }); }
async Task Connect(string host, int port) { await ConnectAsync(host, port); _pump = new FramePump(_client.GetStream()); _outboundEndpoint = new OutboundTcpEndpoint(this, _pump); _inboundEndpoint = _rpcEngine.AddEndpoint(_outboundEndpoint); _pumpThread = new Thread(() => { try { Thread.CurrentThread.Name = $"TCP RPC Client Thread {Thread.CurrentThread.ManagedThreadId}"; _pump.Run(); } finally { _outboundEndpoint.Dismiss(); _inboundEndpoint.Dismiss(); _pump.Dispose(); } }); _pump.FrameReceived += _inboundEndpoint.Forward; _pumpThread.Start(); }
public Connection(TcpRpcServer server, TcpClient client, FramePump pump, OutboundTcpEndpoint outboundEp, RpcEngine.RpcEndpoint inboundEp) { _server = server; Client = client; Pump = pump; OutboundEp = outboundEp; InboundEp = inboundEp; }
public void Start() { Pump = new FramePump(_stream); foreach (var tracer in _tracers) { Pump.AttachTracer(tracer); } _tracers.Clear(); OutboundEp = new OutboundTcpEndpoint(_server, Pump); InboundEp = _server._rpcEngine.AddEndpoint(OutboundEp); Pump.FrameReceived += InboundEp.Forward; State = ConnectionState.Active; PumpRunner = new Thread(o => { try { Thread.CurrentThread.Name = $"TCP RPC Server Thread {Thread.CurrentThread.ManagedThreadId}"; Pump.Run(); } catch (ThreadInterruptedException) { Logger.LogError($"{Thread.CurrentThread.Name} interrupted at {Environment.StackTrace}"); } finally { OutboundEp.Dismiss(); InboundEp.Dismiss(); Pump.Dispose(); Client.Dispose(); lock (_server._reentrancyBlocker) { --_server.ConnectionCount; _server._connections.Remove(this); State = ConnectionState.Down; _server.OnConnectionChanged?.Invoke(_server, new ConnectionEventArgs(this)); } } }); PumpRunner.Start(); }
void AcceptClients() { try { if (Thread.CurrentThread.Name == null) { Thread.CurrentThread.Name = $"TCP RPC Acceptor Thread {Thread.CurrentThread.ManagedThreadId}"; } while (true) { var client = _listener.AcceptTcpClient(); var pump = new FramePump(client.GetStream()); var outboundEndpoint = new OutboundTcpEndpoint(this, pump); var inboundEndpoint = _rpcEngine.AddEndpoint(outboundEndpoint); pump.FrameReceived += inboundEndpoint.Forward; var connection = new Connection(this, client, pump, outboundEndpoint, inboundEndpoint); lock (_reentrancyBlocker) { ++ConnectionCount; _connections.Add(connection); OnConnectionChanged?.Invoke(this, new ConnectionEventArgs(connection)); connection.Start(); } connection.PumpRunner.Start(); } } catch (SocketException) { // Listener was stopped. Maybe a little bit rude, but this is // our way of shutting down the acceptor thread. } catch (System.Exception exception) { // Any other exception might be due to some other problem. Logger.LogError(exception.Message); } }