private async Task AcceptConnection(TcpListener tcpListener, RachisConsensus rachis) { rachis.OnDispose += (sender, args) => tcpListener.Stop(); while (true) { TcpClient tcpClient; try { tcpClient = await tcpListener.AcceptTcpClientAsync(); } catch (InvalidOperationException) { break; } var stream = tcpClient.GetStream(); rachis.AcceptNewConnection(stream, () => tcpClient.Client.Disconnect(false), tcpClient.Client.RemoteEndPoint, hello => { lock (this) { if (_rejectionList.TryGetValue(rachis.Url, out var set)) { if (set.Contains(hello.DebugSourceIdentifier)) { throw new InvalidComObjectException("Simulated failure"); } } var connections = _connections.GetOrAdd(rachis.Url, _ => new ConcurrentSet <Tuple <string, TcpClient> >()); connections.Add(Tuple.Create(hello.DebugSourceIdentifier, tcpClient)); } }); } }
private async Task AcceptConnection(TcpListener tcpListener, RachisConsensus rachis) { rachis.OnDispose += (sender, args) => tcpListener.Stop(); while (true) { TcpClient tcpClient; try { tcpClient = await tcpListener.AcceptTcpClientAsync(); } catch (InvalidOperationException) { break; } #pragma warning disable 4014 Task.Factory.StartNew(() => #pragma warning restore 4014 { try { var stream = tcpClient.GetStream(); var remoteConnection = new RemoteConnection(rachis.Tag, rachis.CurrentTerm, stream, features: new TcpConnectionHeaderMessage.SupportedFeatures.ClusterFeatures { MultiTree = true }, () => tcpClient.Client.Disconnect(false)); rachis.AcceptNewConnection(remoteConnection, tcpClient.Client.RemoteEndPoint, hello => { if (rachis.Url == null) { return; } lock (this) { if (_rejectionList.TryGetValue(rachis.Url, out var set)) { if (set.Contains(hello.DebugSourceIdentifier)) { throw new InvalidComObjectException("Simulated failure"); } } var connections = _connections.GetOrAdd(rachis.Url, _ => new ConcurrentSet <Tuple <string, TcpClient> >()); connections.Add(Tuple.Create(hello.DebugSourceIdentifier, tcpClient)); } }); } catch { // expected } }); } }