private void OnReadCompleted(object sender, SocketAsyncEventArgs e)
        {
            _logger.Trace(string.Format("Received {0} from {1}", e.BytesTransferred, _remoteEndPoint));
            if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
            {
                _readStream.Position = 0;
                _readStream.SetLength(e.BytesTransferred);

                try
                {
                    HandleRead(_readBuffer, e.BytesTransferred);
                }
                catch (Exception err)
                {
                    _logger.Warning("Unhandled exception", err);

                    var buffer = new BufferSlice(_readBuffer.Buffer, _readBuffer.Offset, e.BytesTransferred);
                    var context = new ServiceExceptionContext(err, buffer);
                    _client.OnUnhandledException(context);

                    if (context.CanExceptionBePropagated)
                    {
                        var args = new ClientExceptionEventArgs(this, err, buffer);
                        UnhandledExceptionCaught(this, args);
                        if (!args.CanContinue)
                        {
                            _logger.Debug("Signalled to stop processing");
                            return;
                        }
                    }

                    if (!context.MayContinue)
                    {
                        _logger.Debug("ClientService signaled to stop processing");
                        Cleanup();
                        return;
                    }
                }

                bool isPending = _socket.ReceiveAsync(_readArgs);
                if (!isPending)
                    OnReadCompleted(_socket, _readArgs);
            }
            else
            {
                // read = 0 bytes = SocketError.Success
                // but we want to use it to indicate that localhost have closed the socket.
                // hence the rewrite
                var error = e.SocketError == SocketError.Success
                                ? SocketError.ConnectionReset
                                : e.SocketError;

                Cleanup();
                if (e.SocketError != SocketError.OperationAborted)
                    OnDisconnect(error);
            }
        }
Exemplo n.º 2
0
 private void OnClientException(object sender, ClientExceptionEventArgs e)
 {
     UnhandledClientExceptionCaught(this, e);
 }
Exemplo n.º 3
0
 private void OnClientException(object sender, ClientExceptionEventArgs e)
 {
     UnhandledClientExceptionCaught(this, e);
 }
Exemplo n.º 4
0
        private void OnReadCompleted(object sender, SocketAsyncEventArgs e)
        {
            this.logger.Trace(string.Format("Received {0} from {1}", e.BytesTransferred, this.remoteEndPoint));
            if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
            {
                this.readStream.Position = 0;
                this.readStream.SetLength(e.BytesTransferred);

                try
                {
                    this.HandleRead(this.readBuffer, e.BytesTransferred);
                }
                catch (Exception err)
                {
                    this.logger.Warning("Unhandled exception", err);

                    var buffer  = new BufferSlice(this.readBuffer.Buffer, this.readBuffer.Offset, e.BytesTransferred);
                    var context = new ServiceExceptionContext(err, buffer);
                    this.client.OnUnhandledException(context);

                    if (context.CanExceptionBePropagated)
                    {
                        var args = new ClientExceptionEventArgs(this, err, buffer);
                        this.UnhandledExceptionCaught(this, args);
                        if (!args.CanContinue)
                        {
                            this.logger.Debug("Signalled to stop processing");
                            return;
                        }
                    }

                    if (!context.MayContinue)
                    {
                        this.logger.Debug("ClientService signaled to stop processing");
                        this.Cleanup();
                        return;
                    }
                }

                try
                {
                    bool isPending = this.socket.ReceiveAsync(this.readArgs);
                    if (!isPending)
                    {
                        this.OnReadCompleted(this.socket, this.readArgs);
                    }
                }
                catch (ObjectDisposedException)
                {
                    this.Cleanup();
                    return;
                }
            }
            else
            {
                // read = 0 bytes = SocketError.Success
                // but we want to use it to indicate that localhost have closed the socket.
                // hence the rewrite
                var error = e.SocketError == SocketError.Success
                                ? SocketError.ConnectionReset
                                : e.SocketError;

                this.Cleanup();
                if (e.SocketError != SocketError.OperationAborted)
                {
                    this.OnDisconnect(error);
                }
            }
        }