internal void ConnectToPeer(TorrentManager manager, Peer peer)
        {
            // Connect to the peer.
            IConnection connection = ConnectionFactory.Create(peer.ConnectionUri);

            if (connection == null)
            {
                return;
            }

            peer.LastConnectionAttempt = DateTime.Now;
            AsyncConnectState c = new AsyncConnectState(manager, peer, connection);

            pendingConnects.Add(c);

            manager.Peers.ConnectingToPeers.Add(peer);
            NetworkIO.EnqueueConnect(connection, endCreateConnectionCallback, c);
        }
        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");
        }
Esempio n. 5
0
        static void MessageLengthReceived(bool successful, int transferred, object state)
        {
            var data          = (ReceiveMessageState)state;
            int messageLength = -1;

            if (successful)
            {
                data.Decryptor.Decrypt(data.Buffer, 0, transferred);
                messageLength = IPAddress.HostToNetworkOrder(BitConverter.ToInt32(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);
        }