Esempio n. 1
0
        private void OnReadMessageLengthCompleted(IAsyncResult asyncResult_)
        {
            if (null == _readAsyncResult)
            {
                return; // Protocol has been reset, no further read/write data is of interest.
            }
            try
            {
                var bytesRead = Stream.EndRead(asyncResult_);
                if (0 == bytesRead)
                {
                    // 0 bytes read. Connection has been closed.
                    _readOutput = new NetProtocolReadOutput(NetProtocolReadType.EmptyRead, null);
                    _readAsyncResult.CompleteOperation();
                    return;
                }

                _bytesRead += bytesRead;
                if (0 == BytesRemainingToBeRead)
                {
                    BeginReadMessageContents();
                }
                else
                {
                    Stream.BeginRead(_readBuffer, _bytesRead, BytesRemainingToBeRead, OnReadMessageLengthCompleted, null);
                }
            }
            catch (Exception ex)
            {
                _readAsyncResult.CompleteOperation(ex);
            }
        }
Esempio n. 2
0
        private void BeginReadMessageContents()
        {
            var messageLength = BitConverter.ToInt32(_readBuffer, 0);

            if (0 == messageLength)
            {
                // Keep alive message. Nothing to do.
                _bytesRead  = 0;
                _readBuffer = new byte[0];
                _readOutput = new NetProtocolReadOutput(NetProtocolReadType.KeepAlive, _readBuffer);
                _readAsyncResult.CompleteOperation();
                return;
            }

            ValidateReadMessageLength(messageLength);
            _readBuffer = new byte[messageLength];
            _bytesRead  = 0;
            Stream.BeginRead(_readBuffer, _bytesRead, BytesRemainingToBeRead, OnReadMessageCompleted, null);
        }
Esempio n. 3
0
        private int ReadMessageLength()
        {
            _readBuffer = new byte[sizeof(int)];
            _bytesRead  = 0;
            FillBufferFromStreamSync();
            if (0 == _bytesRead)
            {
                _readOutput = new NetProtocolReadOutput(NetProtocolReadType.EmptyRead, null);
                return(0);
            }

            var messageLength = BitConverter.ToInt32(_readBuffer, 0);

            ValidateReadMessageLength(messageLength);
            if (0 == messageLength)
            {
                _readOutput = new NetProtocolReadOutput(NetProtocolReadType.KeepAlive, new byte[0]);
            }

            return(messageLength);
        }
Esempio n. 4
0
 private void OnReadMessageCompleted(IAsyncResult asyncResult_)
 {
     if (null == _readAsyncResult)
     {
         return; // Protocol has been reset, no further read/write data is of interest.
     }
     try
     {
         _bytesRead += Stream.EndRead(asyncResult_);
         if (0 == BytesRemainingToBeRead)
         {
             _readOutput = new NetProtocolReadOutput(NetProtocolReadType.Message, _readBuffer);
             _readAsyncResult.CompleteOperation();
         }
         else
         {
             Stream.BeginRead(_readBuffer, _bytesRead, BytesRemainingToBeRead, OnReadMessageCompleted, null);
         }
     }
     catch (Exception ex)
     {
         _readAsyncResult.CompleteOperation(ex);
     }
 }