private void EndAccept(IAsyncResult ar) { using(_Trace.Open()) { Socket client = null; try { client = socket.EndAccept(ar); if(_Cancel.IsCancellationRequested) return; NodeServerTrace.Information("Client connection accepted : " + client.RemoteEndPoint); var cancel = CancellationTokenSource.CreateLinkedTokenSource(_Cancel.Token); cancel.CancelAfter(TimeSpan.FromSeconds(10)); var stream = new Message.CustomNetworkStream(client, false); while(true) { cancel.Token.ThrowIfCancellationRequested(); var message = Message.ReadNext(stream, Network, Version, cancel.Token); _MessageProducer.PushMessage(new IncomingMessage() { Socket = client, Message = message, Node = null, }); if(message.Payload is VersionPayload) break; else NodeServerTrace.Error("The first message of the remote peer did not contained a Version payload", null); } } catch(OperationCanceledException) { Utils.SafeCloseSocket(client); if(!_Cancel.Token.IsCancellationRequested) { NodeServerTrace.Error("The remote connecting failed to send a message within 10 seconds, dropping connection", null); } } catch(Exception ex) { if(_Cancel.IsCancellationRequested) return; if(client == null) { NodeServerTrace.Error("Error while accepting connection ", ex); Thread.Sleep(3000); } else { Utils.SafeCloseSocket(client); NodeServerTrace.Error("Invalid message received from the remote connecting node", ex); } } BeginAccept(); } }
private void EndAccept(SocketAsyncEventArgs args) { using (_Trace.Open()) { Socket client = null; try { if (args.SocketError != SocketError.Success) { throw new SocketException((int)args.SocketError); } client = args.AcceptSocket; if (_Cancel.IsCancellationRequested) { return; } NodeServerTrace.Information("Client connection accepted : " + client.RemoteEndPoint); var cancel = CancellationTokenSource.CreateLinkedTokenSource(_Cancel.Token); cancel.CancelAfter(TimeSpan.FromSeconds(10)); var stream = new Message.CustomNetworkStream(client, false); while (true) { cancel.Token.ThrowIfCancellationRequested(); var message = Message.ReadNext(stream, Network, Version, cancel.Token); _MessageProducer.PushMessage(new IncomingMessage() { Socket = client, Message = message, Node = null, }); if (message.Payload is VersionPayload) { break; } else { NodeServerTrace.Error("The first message of the remote peer did not contained a Version payload", null); } } } catch (OperationCanceledException) { Utils.SafeCloseSocket(client); if (!_Cancel.Token.IsCancellationRequested) { NodeServerTrace.Error("The remote connecting failed to send a message within 10 seconds, dropping connection", null); } } catch (Exception ex) { if (_Cancel.IsCancellationRequested) { return; } if (client == null) { NodeServerTrace.Error("Error while accepting connection ", ex); Thread.Sleep(3000); } else { Utils.SafeCloseSocket(client); NodeServerTrace.Error("Invalid message received from the remote connecting node", ex); } } BeginAccept(); } }