Encode() public method

public Encode ( byte buffer, int offset ) : int
buffer byte
offset int
return int
Esempio n. 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);
        }
Esempio n. 2
0
        public void AddConnectionToStoppedManager()
        {
            MessageBundle 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));
            byte[] 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.IsFalse(conn.Incoming.Connected, "#1");
            //                Assert.IsFalse(conn.Outgoing.Connected, "#2");
                return;
            }

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