示例#1
0
        private void ReadHeaderCallback(IAsyncResult result)
        {
            int receivedBytes = 0;

            try
            {
                lock (_connection)
                {
                    receivedBytes = _connection.EndReceive(result);
                }
            }
            catch (ObjectDisposedException)
            {
                return;
            }
            catch (Exception)
            {
                HandleDisconnection();
                ReceiveBuffer.Release((ReceiveBuffer)result.AsyncState);
                return;
            }

            if (receivedBytes == 0)
            {
                HandleDisconnection();
                ReceiveBuffer.Release((ReceiveBuffer)result.AsyncState);
                return;
            }

            var buffer = (ReceiveBuffer)result.AsyncState;

            buffer.ReceivedBytes += receivedBytes;

            if (buffer.ReceivedBytes < buffer.Buffer.Length)
            {
                try
                {
                    ReceiveHeader(buffer);
                }
                catch (Exception)
                {
                    HandleDisconnection();
                    ReceiveBuffer.Release((ReceiveBuffer)result.AsyncState);
                    return;
                }
            }
            else
            {
                Debug.Assert(buffer.ReceivedBytes == 4);
                var bytes  = buffer.Buffer;
                int length = (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];
                ReceiveBody(length);
                ReceiveBuffer.Release((ReceiveBuffer)result.AsyncState);
            }
        }
示例#2
0
        private void ReadBodyCallback(IAsyncResult result)
        {
            int receivedBytes = 0;

            try
            {
                lock (_connection)
                {
                    receivedBytes = _connection.EndReceive(result);
                }
            }
            catch (ObjectDisposedException)
            {
                return;
            }
            catch (Exception)
            {
                HandleDisconnection();
                ReceiveBuffer.Release((ReceiveBuffer)result.AsyncState);
                return;
            }

            if (receivedBytes == 0)
            {
                HandleDisconnection();
                ReceiveBuffer.Release((ReceiveBuffer)result.AsyncState);
                return;
            }

            var buffer = (ReceiveBuffer)result.AsyncState;

            buffer.ReceivedBytes += receivedBytes;

            if (buffer.ReceivedBytes < buffer.Buffer.Length)
            {
                try
                {
                    ReceiveBody(buffer);
                }
                catch (Exception)
                {
                    HandleDisconnection();
                    ReceiveBuffer.Release((ReceiveBuffer)result.AsyncState);
                    return;
                }
            }
            else
            {
                ReceiveHeader();

                _onDataCallback?.Invoke(this, buffer.Buffer);
                ReceiveBuffer.Release((ReceiveBuffer)result.AsyncState);
            }
        }