Exemplo n.º 1
0
        public void AddInSteps()
        {
            var slize  = CreateSlice(@"GET / HTTP/1.1
Connection: Keep-Alive
HOST: localhost
Content-Length: 0

");
            var stream = new SliceStream(slize, 1);

            _parser.Parse(stream);

            stream.SetLength(10);
            _parser.Parse(stream);
            Assert.False(_completed);

            stream.SetLength(16);
            _parser.Parse(stream);
            Assert.False(_completed);

            stream.SetLength(78);
            _parser.Parse(stream);
            Assert.True(_completed);
            Assert.Equal("Keep-Alive", _request.Headers["Connection"].Value);
            Assert.Equal("localhost", _request.Headers["HOST"].Value);
            Assert.Equal("0", _request.Headers["Content-Length"].Value);
        }
        public void InitUsingWrittenAndWriteMore()
        {
            var initial = "Hello world!";
            var addition = "Something more..";
            var buffer = new byte[65535];
            var text = Encoding.ASCII.GetBytes(initial);
            Buffer.BlockCopy(text, 0, buffer, 0, text.Length);
            var slice = new BufferSlice(buffer, 0, buffer.Length);
            var stream = new SliceStream(slice, text.Length);
            stream.SetLength(initial.Length);
            stream.Position = stream.Length;

            var writeable = Encoding.ASCII.GetBytes(addition);
            stream.Write(writeable, 0, writeable.Length);

            stream.Position = 0;
            var reader = new StreamReader(stream);
            var actual = reader.ReadToEnd();
            Assert.Equal(initial + addition, actual);
        }
Exemplo n.º 3
0
        public void InitUsingWrittenAndWriteMore()
        {
            var initial  = "Hello world!";
            var addition = "Something more..";
            var buffer   = new byte[65535];
            var text     = Encoding.ASCII.GetBytes(initial);

            Buffer.BlockCopy(text, 0, buffer, 0, text.Length);
            var slice  = new BufferSlice(buffer, 0, buffer.Length);
            var stream = new SliceStream(slice, text.Length);

            stream.SetLength(initial.Length);
            stream.Position = stream.Length;

            var writeable = Encoding.ASCII.GetBytes(addition);

            stream.Write(writeable, 0, writeable.Length);

            stream.Position = 0;
            var reader = new StreamReader(stream);
            var actual = reader.ReadToEnd();

            Assert.Equal(initial + addition, actual);
        }
        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;
                    }
                }

                try
                {
                    bool isPending = _socket.ReceiveAsync(_readArgs);
                    if (!isPending)
                    {
                        OnReadCompleted(_socket, _readArgs);
                    }
                }
                catch (ObjectDisposedException)
                {
                    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;

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