private void OnReadCompleted(IAsyncResult ar)
        {
            var readBytes = 0;

            try
            {
                readBytes = _stream.EndRead(ar);
            }
            catch (Exception ex)
            {
                HandleDisconnect(ex);
            }

            if (readBytes == 0)
            {
                HandleDisconnect(SocketError.ConnectionReset);
                return;
            }

            _readBuffer.BytesTransferred = readBytes;
            _readBuffer.Offset           = _readBuffer.BaseOffset;
            _readBuffer.Count            = readBytes;

            try
            {
                _decoder.ProcessReadBytes(_readBuffer);
            }
            catch (Exception exception)
            {
                ChannelFailure(this, exception);
                return;
            }

            ReadAsync();
        }
Example #2
0
        private void OnReadCompleted(object sender, SocketAsyncEventArgs e)
        {
            if (e.BytesTransferred == 0 || e.SocketError != SocketError.Success)
            {
                try
                {
                    _socket.Close();
                }
                catch
                {
                    //close it.
                }
                HandleRemoteDisconnect(e.SocketError, null);
                return;
            }

            if (BufferPreProcessor != null)
            {
                var read = BufferPreProcessor(this, _readArgsWrapper);
                if (read > 0)
                {
                    var newCount  = _readArgsWrapper.BytesTransferred - read;
                    var newOffset = _readArgsWrapper.Offset + read;
                    _readArgsWrapper.SetBuffer(newOffset, newCount);
                    _readArgsWrapper.BytesTransferred -= read;
                }
            }

            try
            {
                // pre processor can have read everything
                if (_readArgsWrapper.BytesTransferred > 0)
                {
                    _decoder.ProcessReadBytes(_readArgsWrapper);
                }
            }
            catch (Exception exception)
            {
                ChannelFailure(this, exception);

                // Cleanup before both pending operations have exited.
                try
                {
                    if (!_socket.Connected)
                    {
                        return;
                    }
                }
                catch (NullReferenceException)
                {
                    //rare case of race condition during cleanup.
                    return;
                }
            }

            ReadAsync();
        }
Example #3
0
        private void OnClientConnected(object sender, ClientConnectedEventArgs args)
        {
            //Logger.WriteDebug(this, "Pipeline => OnClientRequest");

            try
            {
                _decoder.ProcessReadBytes(args.Channel, args.ChannelBuffer);
            }
            catch (Exception ex)
            {
                SendFailure(args.Channel, ex);
            }
        }
        /// <summary>
        /// Helper method which sends the websocket upgrade request
        /// </summary>
        /// <param name="decoder"></param>
        private static void sendWebSocketUpgradeRequest(IMessageDecoder decoder)
        {
            var buffer = new SocketBufferFake();
            buffer.Buffer = Encoding.ASCII.GetBytes(@"GET / HTTP/1.1
host: localhost
connection: upgrade
upgrade: websocket

");
            buffer.BytesTransferred = buffer.Buffer.Length;

            decoder.ProcessReadBytes(buffer);
        }
Example #5
0
        /// <summary>
        /// Helper method which sends the websocket upgrade request
        /// </summary>
        /// <param name="decoder"></param>
        private static void sendWebSocketUpgradeRequest(IMessageDecoder decoder)
        {
            var buffer = new SocketBufferFake();

            buffer.Buffer           = Encoding.ASCII.GetBytes(@"GET / HTTP/1.1
host: localhost
connection: upgrade
upgrade: websocket

");
            buffer.BytesTransferred = buffer.Buffer.Length;

            decoder.ProcessReadBytes(buffer);
        }
        private void OnReadCompleted(object sender, SocketAsyncEventArgs e)
        {
            if (e.BytesTransferred == 0 || e.SocketError != SocketError.Success)
            {
                HandleDisconnect(e.SocketError);
                return;
            }

            if (_bufferPreProcessor != null)
            {
                var read = _bufferPreProcessor(this, _readArgsWrapper);
                if (read > 0)
                {
                    var newCount  = _readArgsWrapper.BytesTransferred - read;
                    var newOffset = _readArgsWrapper.Offset + read;
                    _readArgsWrapper.SetBuffer(newOffset, newCount);
                    _readArgsWrapper.BytesTransferred -= read;
                }
            }

            try
            {
                // pre processor can have read everything
                if (_readArgsWrapper.BytesTransferred > 0)
                {
                    _decoder.ProcessReadBytes(_readArgsWrapper);
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
                ChannelFailure(this, exception);

                // event handler closed the socket.
                if (!_socket.Connected)
                {
                    return;
                }
            }

            ReadAsync();
        }
Example #7
0
        private void OnReadCompleted(object sender, SocketAsyncEventArgs e)
        {
            if (e.BytesTransferred == 0 || e.SocketError != SocketError.Success)
            {
                HandleDisconnect(e.SocketError);
                return;
            }

            try
            {
                _decoder.ProcessReadBytes(_readArgsWrapper);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
                ChannelFailure(this, exception);
            }

            ReadAsync();
        }