Beispiel #1
0
 protected virtual void OnClientErrorOccured(TcpClientErrorEventArgs e)
 {
     this.ClientErrorOccured?.Invoke(this, e);
 }
Beispiel #2
0
        protected virtual async Task HandleTcpClientAsync(TcpClient tcpClient, CancellationToken token)
        {
            using (tcpClient)
                using (var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(token))
                {
                    var sendQueue = this.CreateSendQueueActionBlock(linkedCts.Token);

                    RemoteTcpPeer remoteTcpPeer;
                    SslStream     sslStream = null;

                    try
                    {
                        if (this.Config.UseSsl)
                        {
                            sslStream = this.CreateSslStream(tcpClient);

                            await this.AuthenticateSslStream(tcpClient, sslStream, linkedCts.Token)
                            .ConfigureAwait(false);

                            remoteTcpPeer = this.CreateRemoteTcpPeer(tcpClient, sslStream, sendQueue, linkedCts);
                        }
                        else
                        {
                            remoteTcpPeer = this.CreateRemoteTcpPeer(tcpClient, sendQueue, linkedCts);
                        }
                    }
                    catch (AuthenticationException ex)
                    {
                        var clientErrorEventArgs = new TcpClientErrorEventArgs(new ErrorData(ex));
                        this.OnClientErrorOccured(clientErrorEventArgs);

                        sendQueue.Complete();
                        sslStream?.Dispose();

                        return;
                    }
                    catch (Exception)
                    {
                        sendQueue.Complete();
                        return;
                    }

                    using (remoteTcpPeer)
                    {
                        var connectionEstablishedEventArgs = new ConnectionEstablishedEventArgs(new ConnectionEstablishedData(remoteTcpPeer));
                        this.OnConnectionEstablished(connectionEstablishedEventArgs);

                        try
                        {
                            await this.HandleRemotePeerAsync(remoteTcpPeer, linkedCts.Token).ConfigureAwait(false);
                        }
                        catch (Exception ex)
                        {
                            var unhandledErrorEventArgs = new UnhandledErrorEventArgs(new ErrorData(ex));

                            this.OnUnhandledError(unhandledErrorEventArgs);
                        }
                        finally
                        {
                            sendQueue.Complete();
                            sslStream?.Dispose();
                        }
                    }
                }
        }