public void SetBuffer_without_buffer_should_also_be_made_in_our_args()
        {
            var e = new SocketAsyncEventArgs();
            var buf = new byte[65535];

            var sut = new SocketAsyncEventArgsWrapper(e);
            sut.SetBuffer(buf, 1, 100);
            sut.SetBuffer(2, 101);

            e.Offset.Should().Be(2);
            e.Count.Should().Be(101);
            e.Buffer.Length.Should().Be(buf.Length);
            sut.BaseOffset.Should().Be(1);
            sut.Offset.Should().Be(2);
        }
예제 #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();
        }
예제 #3
0
        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();
        }