Пример #1
0
        private static void HandshakeMessageBase(ReceiveMessageState data)
        {
            var buffer = new byte[HandshakeMessage.Length];

            data.Buffer = buffer;
            NetworkIo.Receive(data.Socket, data.Buffer, 0, buffer.Length, data, EndReceiveHandshakeCallback);
        }
Пример #2
0
        private static void ReceiveMessageBase(ReceiveMessageState data)
        {
            var buffer = new byte[4];

            data.Buffer = buffer;
            NetworkIo.Receive(data.Socket, data.Buffer, 0, 4, data, EndReceiveLengthCallback);
        }
Пример #3
0
        private static void EndReceiveLength(bool success, int read, object state)
        {
            var data = (ReceiveMessageState)state;

            try
            {
                if (success)
                {
                    var messageLength = IPAddress.HostToNetworkOrder(BitConverter.ToInt32(data.Buffer, 0));
                    if (messageLength > MaxMessageLength)
                    {
                        data.Callback(false, null, data.State);
                        ReceiveCache.Put(data);
                        return;
                    }
                    if (messageLength == 0)
                    {
                        data.Callback(true, new KeepAliveMessage(), data.State);
                        ReceiveCache.Put(data);
                        return;
                    }
                    var newBuffer = new byte[read + messageLength];
                    Buffer.BlockCopy(newBuffer, 0, data.Buffer, 0, read);
                    data.Buffer = newBuffer;
                    NetworkIo.Receive(data.Socket, data.Buffer, read, messageLength, data, EndReceiveCallback);
                }
                else
                {
                    data.Callback(false, null, data.State);
                    ReceiveCache.Put(data);
                }
            }
            catch (Exception e)
            {
                data.Callback(false, null, data.State);
                ReceiveCache.Put(data);
                Debug.WriteLine(e, "MessageIO.EndReceiveLength");
            }
        }