示例#1
0
        private static void MessageLengthReceived(bool successful, int transferred, object state)
        {
            var data          = (ReceiveMessageState)state;
            var messageLength = -1;

            if (successful)
            {
                data.Decryptor.Decrypt(data.Buffer, 0, transferred);
                messageLength = Message.ReadInt(data.Buffer, 0);
            }

            if (!successful || messageLength < 0 || messageLength > MaxMessageLength)
            {
                ClientEngine.BufferManager.FreeBuffer(data.Buffer);
                data.Callback(false, null, data.State);
                ReceiveCache.Enqueue(data);
                return;
            }

            if (messageLength == 0)
            {
                ClientEngine.BufferManager.FreeBuffer(data.Buffer);
                data.Callback(true, new KeepAliveMessage(), data.State);
                ReceiveCache.Enqueue(data);
                return;
            }

            var buffer = ClientEngine.BufferManager.GetBuffer(messageLength + transferred);

            Buffer.BlockCopy(data.Buffer, 0, buffer, 0, transferred);
            ClientEngine.BufferManager.FreeBuffer(data.Buffer);
            data.Buffer = buffer;

            NetworkIO.EnqueueReceive(data.Connection, buffer, transferred, messageLength, data.RateLimiter,
                                     data.PeerMonitor,
                                     data.ManagerMonitor, MessageBodyReceivedCallback, data);
        }