protected void SendMessage(byte[] toSend)
 {
     try
     {
         if (toSend.Length > 0)
         {
             NetworkIO.EnqueueSend(socket, toSend, 0, toSend.Length, null, null, null, doneSendCallback, null);
         }
     }
     catch (Exception ex)
     {
         asyncResult.Complete(ex);
     }
 }
Exemple #2
0
        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.IsTrue(handle.WaitOne(TimeSpan.FromSeconds(4)), "#1");
            Assert.IsFalse(connectionOpen, "#2");
        }
Exemple #3
0
        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.IsTrue(handle.WaitOne(TimeSpan.FromSeconds(4)), "#Should have closed");;
            Assert.IsFalse(success, "#1");
        }
Exemple #4
0
        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.IsTrue(handle.WaitOne(TimeSpan.FromSeconds(2)), "#Should receive first message");


            PeerIO.EnqueueReceiveMessage(Incoming, new PlainTextEncryption(), null, null, null, callback, null);
            Assert.IsTrue(handle.WaitOne(TimeSpan.FromSeconds(2)), "#Should receive second message");
        }
Exemple #5
0
        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.AreNotEqual(0, r, "#Received data");
                received += r;
            }
            Assert.IsTrue(handle.WaitOne(TimeSpan.FromSeconds(1)), "Data should be all sent");
            Assert.IsTrue(Toolbox.ByteMatch(buffer, data), "Data matches");
        }