示例#1
0
        public void ChunkedRequest()
        {
            if (requests.Messages.Count != 0)
                rig.Manager.PieceManager.Picker.CancelRequests(id);

            requests = rig.Manager.PieceManager.Picker.PickPiece(id, new List<PeerId>(), 256);

            byte[] sendBuffer = requests.Encode();
            int offset = 0;
            amountSent = Math.Min(sendBuffer.Length - offset, 2048);
            IAsyncResult sendResult = connection.BeginSend(sendBuffer, offset, amountSent, null, null);
            while (sendResult.AsyncWaitHandle.WaitOne(10, true))
            {
                Assert.AreEqual(amountSent, connection.EndSend(sendResult), "#1." + amountSent);
                offset += amountSent;
                amountSent = Math.Min(sendBuffer.Length - offset, 2048);
                if (amountSent == 0)
                    Assert.Fail("This should never happen");
                sendResult = connection.BeginSend(sendBuffer, offset, amountSent, null, null);
            }

            byte[] buffer = new byte[1024 * 1024 * 3];
            IAsyncResult receiveResult = connection.BeginReceive(buffer, 0, 4, null, null);

            CompleteSendOrReceiveFirst(buffer, receiveResult, sendResult);
        }
        public void AddConnectionToStoppedManager()
        {
            var bundle = new MessageBundle();

            // Create the handshake and bitfield message
            bundle.Messages.Add(new HandshakeMessage(rig.Manager.InfoHash, "11112222333344445555",
                VersionInfo.ProtocolStringV100));
            bundle.Messages.Add(new BitfieldMessage(rig.Torrent.Pieces.Count));
            var data = bundle.Encode();

            // Add the 'incoming' connection to the engine and send our payload
            rig.Listener.Add(rig.Manager, conn.Incoming);
            conn.Outgoing.EndSend(conn.Outgoing.BeginSend(data, 0, data.Length, null, null));

            try
            {
                conn.Outgoing.EndReceive(conn.Outgoing.BeginReceive(data, 0, data.Length, null, null));
            }
            catch
            {
                Assert.False(conn.Incoming.Connected);
//                Assert.False(conn.Outgoing.Connected);
                return;
            }

            Assert.True(false, "The outgoing connection should've thrown an exception");
        }