protected virtual async Task ReceiveFromRemotePeerAsync(RemoteTcpPeer remoteTcpPeer, CancellationToken cancellationToken)
        {
            Defragmentation.ReadFrameResult readFrameResult = null;

            while (!cancellationToken.IsCancellationRequested)
            {
                using (var timeoutCts = this.Config.ConnectionTimeout == TimeSpan.Zero ? new CancellationTokenSource() : new CancellationTokenSource(this.Config.ConnectionTimeout))
                    using (var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(timeoutCts.Token, cancellationToken))
                    {
                        readFrameResult = await remoteTcpPeer.ProtocolFrameDefragmenter
                                          .ReadFrameAsync(remoteTcpPeer, readFrameResult?.LeftOvers, linkedCts.Token)
                                          .ConfigureAwait(false);
                    }

                if (readFrameResult.ReadFrameStatus == Defragmentation.ReadFrameStatus.StreamClosed)
                {
                    remoteTcpPeer.ConnectionCloseReason = ConnectionCloseReason.RemoteShutdown;
                    return;
                }
                else if (readFrameResult.ReadFrameStatus == Defragmentation.ReadFrameStatus.FrameDropped)
                {
                    readFrameResult = null;

                    continue;
                }

                var frameArrivedEventArgs = new TcpFrameArrivedEventArgs(remoteTcpPeer, readFrameResult.FrameData);

                this.OnFrameArrived(remoteTcpPeer, frameArrivedEventArgs);
            }

            remoteTcpPeer.ConnectionCloseReason = ConnectionCloseReason.LocalShutdown;
        }
Exemple #2
0
        protected virtual async Task HandleRemotePeerAsync(RemoteTcpPeer remoteTcpPeer, CancellationToken cancellationToken)
        {
            try
            {
                await this.ReceiveFromRemotePeerAsync(remoteTcpPeer, cancellationToken).ConfigureAwait(false);
            }
            catch (OperationCanceledException)
            {
                if (!cancellationToken.IsCancellationRequested)
                {
                    remoteTcpPeer.ConnectionCloseReason = ConnectionCloseReason.Timeout;
                    return;
                }
                else
                {
                    remoteTcpPeer.ConnectionCloseReason = ConnectionCloseReason.LocalShutdown;
                    return;
                }
            }
            catch (Exception ex)
            {
                var unhandledErrorEventArgs = new UnhandledErrorEventArgs(new ErrorData(ex));

                this.OnUnhandledError(unhandledErrorEventArgs);
            }

            var connectionClosedEventArgs = new ConnectionClosedEventArgs(new ConnectionClosedData(remoteTcpPeer, remoteTcpPeer.ConnectionCloseReason));

            this.OnConnectionClosed(remoteTcpPeer, connectionClosedEventArgs);
        }
Exemple #3
0
        protected virtual void OnConnectionClosed(RemoteTcpPeer remoteTcpPeer, ConnectionClosedEventArgs e)
        {
            try
            {
                remoteTcpPeer.OnConnectionClosed(e);
                this.ConnectionClosed?.Invoke(this, e);
            }
            catch (Exception ex)
            {
                var unhandledErrorEventArgs = new UnhandledErrorEventArgs(new ErrorData(ex));

                this.OnUnhandledError(unhandledErrorEventArgs);
            }
        }
        protected virtual void OnConnectionClosed(RemoteTcpPeer remoteTcpPeer, ConnectionClosedEventArgs e)
        {
            try
            {
                remoteTcpPeer.OnConnectionClosed(e);
            }
            catch (Exception ex)
            {
                var ue = new ExceptionEventArgs(ex);
                this.OnUnhandledException(ue);
            }

            try
            {
                this.ConnectionClosed?.Invoke(this, e);
            }
            catch (Exception ex)
            {
                var ue = new ExceptionEventArgs(ex);
                this.OnUnhandledException(ue);
            }
        }
        protected virtual void OnFrameArrived(RemoteTcpPeer remoteTcpPeer, TcpFrameArrivedEventArgs e)
        {
            try
            {
                remoteTcpPeer.OnFrameArrived(e);
            }
            catch (Exception ex)
            {
                var ue = new ExceptionEventArgs(ex);
                this.OnUnhandledException(ue);
            }

            try
            {
                this.FrameArrived?.Invoke(this, e);
            }
            catch (Exception ex)
            {
                var ue = new ExceptionEventArgs(ex);
                this.OnUnhandledException(ue);
            }
        }
        protected virtual async Task HandleRemotePeerAsync(RemoteTcpPeer remoteTcpPeer, CancellationToken cancellationToken)
        {
            try
            {
                await this.ReceiveFromRemotePeerAsync(remoteTcpPeer, cancellationToken).ConfigureAwait(false);
            }
            catch (OperationCanceledException ex)
            {
                if (!cancellationToken.IsCancellationRequested)
                {
                    remoteTcpPeer.ConnectionCloseReason = ConnectionCloseReason.Timeout;
                }
                else
                {
                    remoteTcpPeer.ConnectionCloseReason = ConnectionCloseReason.LocalShutdown;
                }

                remoteTcpPeer.ConnectionCloseException = ex;
            }
            catch (Exception ex)
            {
                remoteTcpPeer.ConnectionCloseReason    = ConnectionCloseReason.ExceptionOccured;
                remoteTcpPeer.ConnectionCloseException = ex;

                var ue = new ExceptionEventArgs(ex);

                this.OnUnhandledException(ue);
            }

            this.RemoveRemoteTcpPeerFromConnectedList(remoteTcpPeer.IPEndPoint);

            var connectionClosedEventArgs = new ConnectionClosedEventArgs(remoteTcpPeer)
            {
                ConnectionCloseException = remoteTcpPeer.ConnectionCloseException,
                ConnectionCloseReason    = remoteTcpPeer.ConnectionCloseReason
            };

            this.OnConnectionClosed(remoteTcpPeer, connectionClosedEventArgs);
        }
        protected virtual void AddRemoteTcpPeerToConnectedList(RemoteTcpPeer remoteTcpPeer)
        {
            this.RemoveRemoteTcpPeerFromConnectedList(remoteTcpPeer.IPEndPoint);

            this.connectedPeersDictionary.TryAdd(remoteTcpPeer.IPEndPoint, remoteTcpPeer);
        }
 public TcpFrameArrivedData(RemoteTcpPeer remoteTcpPeer, byte[] frameData)
 {
     this.RemoteTcpPeer = remoteTcpPeer;
     this.FrameData     = frameData;
 }