public static void EnqueueSendMessage(IConnection connection, IEncryption encryptor, PeerMessage message, IRateLimiter rateLimiter, ConnectionMonitor peerMonitor, ConnectionMonitor managerMonitor, AsyncIOCallback callback, object state)
        {
            int count  = message.ByteLength;
            var buffer = ClientEngine.BufferManager.GetBuffer(count);

            message.Encode(buffer, 0);
            encryptor.Encrypt(buffer, 0, count);

            var data = sendCache.Dequeue().Initialise(buffer, callback, state);

            NetworkIO.EnqueueSend(connection, buffer, 0, count, rateLimiter, peerMonitor, managerMonitor, EndSendCallback, data);
        }
        public void ZeroSentClosesConnection()
        {
            bool           connectionOpen = true;
            AutoResetEvent handle         = new AutoResetEvent(false);

            Incoming.ManualBytesSent = 0;
            NetworkIO.EnqueueSend(Incoming, data, 0, 100, null, null, null, (successful, count, state) => {
                connectionOpen = successful;
                handle.Set();
            }, null);

            NetworkIO.EnqueueReceive(Outgoing, data, 0, 100, null, null, null, delegate { }, null);
            Assert.True(handle.WaitOne(TimeSpan.FromSeconds(4)), "#1");
            Assert.False(connectionOpen, "#2");
        }
        public void InvalidMessage()
        {
            bool             success = true;
            ManualResetEvent handle  = new ManualResetEvent(false);

            Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(16)), 0, data, 0, 4);
            for (int i = 4; i < 16; i++)
            {
                data [i] = byte.MaxValue;
            }
            PeerIO.EnqueueReceiveMessage(Incoming, new PlainTextEncryption(), null, null, null, (successful, count, state) => {
                success = successful;
                handle.Set();
            }, null);

            NetworkIO.EnqueueSend(Outgoing, data, 0, 20, null, null, null, delegate { }, null);
            Assert.True(handle.WaitOne(TimeSpan.FromSeconds(4)), "#Should have closed");;
            Assert.False(success, "#1");
        }
        public void ReceiveTwoKeepAlives()
        {
            var message = new KeepAliveMessage();
            var buffer  = message.Encode();
            var handle  = new AutoResetEvent(false);

            NetworkIO.EnqueueSend(Outgoing, buffer, 0, buffer.Length, null, null, null, delegate { }, null);
            NetworkIO.EnqueueSend(Outgoing, buffer, 0, buffer.Length, null, null, null, delegate { }, null);

            AsyncMessageReceivedCallback callback = (s, m, state) => {
                if (s && m is KeepAliveMessage)
                {
                    handle.Set();
                }
            };

            PeerIO.EnqueueReceiveMessage(Incoming, new PlainTextEncryption(), null, null, null, callback, null);
            Assert.True(handle.WaitOne(TimeSpan.FromSeconds(2)), "#Should receive first message");


            PeerIO.EnqueueReceiveMessage(Incoming, new PlainTextEncryption(), null, null, null, callback, null);
            Assert.True(handle.WaitOne(TimeSpan.FromSeconds(2)), "#Should receive second message");
        }
        public void DoSend(bool slowOutgoing, bool slowIncoming)
        {
            Incoming.SlowConnection = slowIncoming;
            Outgoing.SlowConnection = slowOutgoing;

            var handle = new ManualResetEvent(false);

            NetworkIO.EnqueueSend(Outgoing, data, 0, data.Length, null, null, null, delegate {
                handle.Set();
            }, null);

            int received = 0;

            byte[] buffer = new byte [data.Length];
            while (received != buffer.Length)
            {
                int r = Incoming.Receive(buffer, received, buffer.Length - received);
                Assert.NotEqual(0, r);
                received += r;
            }
            Assert.True(handle.WaitOne(TimeSpan.FromSeconds(1)), "Data should be all sent");
            Assert.True(Toolbox.ByteMatch(buffer, data), "Data matches");
        }