Пример #1
0
        public override void Tick(int counter)
        {
            base.Tick(counter);
            if (!_unchoker.Complete)
                return;

            PeerMessage bitfieldMessage = new BitfieldMessage(Manager.Bitfield);
            PeerMessage haveAllMessage = new HaveAllMessage();
            foreach (var peer in Manager.Peers.ConnectedPeers)
            {
                var message = peer.SupportsFastPeer && Manager.Complete ? haveAllMessage : bitfieldMessage;
                peer.Enqueue(message);
            }
            Manager.Mode = new DownloadMode(Manager);
        }
        public void SwitchingModesSendsHaves()
        {
            Rig.Manager.Peers.ConnectedPeers.Add(Rig.CreatePeer(true, true));
            Rig.Manager.Peers.ConnectedPeers.Add(Rig.CreatePeer(true, false));

            var peer = Rig.CreatePeer(true);

            peer.MutableBitField.SetAll(true);
            Mode.HandlePeerConnected(peer);
            Mode.Tick(0);

            Assert.IsTrue(Rig.Manager.Peers.ConnectedPeers[0].MessageQueue.TryDequeue() is HaveAllMessage, "#1");
            BitfieldMessage m = (BitfieldMessage)Rig.Manager.Peers.ConnectedPeers[1].MessageQueue.TryDequeue();

            Assert.IsTrue(m.BitField.AllTrue, "#2");
        }
Пример #3
0
        public void BitFieldEncoding()
        {
            var data = new[] { true, false, false, true, false, true, false, true, false, true,
                               false, true, false, false, false, true, true, true, false, false,
                               false, true, false, true, false, false, true, false, true, false,
                               true, true, false, false, true, false, false, true, true, false };

            var encoded = new BitfieldMessage(new BitField(data)).Encode();

            var message = (BitfieldMessage)PeerMessage.DecodeMessage(encoded, 0, encoded.Length, _testRig.Manager);

            Assert.AreEqual(data.Length, message.BitField.Length, "#1");
            for (var i = 0; i < data.Length; i++)
            {
                Assert.AreEqual(data[i], message.BitField[i], "#2." + i);
            }
        }
Пример #4
0
        public override void Tick(int counter)
        {
            base.Tick(counter);
            if (!_unchoker.Complete)
            {
                return;
            }

            PeerMessage bitfieldMessage = new BitfieldMessage(Manager.Bitfield);
            PeerMessage haveAllMessage  = new HaveAllMessage();

            foreach (var peer in Manager.Peers.ConnectedPeers)
            {
                var message = peer.SupportsFastPeer && Manager.Complete ? haveAllMessage : bitfieldMessage;
                peer.Enqueue(message);
            }
            Manager.Mode = new DownloadMode(Manager);
        }
Пример #5
0
        public void BitFieldEncoding()
        {
            bool[] data = { true,  false, false, true,  false, true,  false, true,  false, true,
                            false, true,  false, false, false, true,  true,  true,  false, false,
                            false, true,  false, true,  false, false, true,  false, true,  false,
                            true,  true,  false, false, true,  false, false, true,  true,  false };

            Assert.AreEqual(data.Length, (int)Math.Ceiling((double)torrentData.Size / torrentData.PieceLength), "#0");
            byte[] encoded = new BitfieldMessage(new BitField(data)).Encode();

            BitfieldMessage m = (BitfieldMessage)PeerMessage.DecodeMessage(encoded, 0, encoded.Length, torrentData);

            Assert.AreEqual(data.Length, m.BitField.Length, "#1");
            for (int i = 0; i < data.Length; i++)
            {
                Assert.AreEqual(data[i], m.BitField[i], "#2." + i);
            }
        }
Пример #6
0
        public void BitFieldDecoding()
        {
            byte[]          buf = { 0x00, 0x00, 0x00, 0x04, 0x05, 0xff, 0x08, 0xAA, 0xE3, 0x00 };
            BitfieldMessage msg = (BitfieldMessage)PeerMessage.DecodeMessage(buf, 0, 8, torrentData);

            for (int i = 0; i < 8; i++)
            {
                Assert.IsTrue(msg.BitField[i], i.ToString());
            }

            for (int i = 8; i < 12; i++)
            {
                Assert.IsFalse(msg.BitField[i], i.ToString());
            }

            Assert.IsTrue(msg.BitField[12], 12.ToString ());
            for (int i = 13; i < 15; i++)
            {
                Assert.IsFalse(msg.BitField[i], i.ToString());
            }
            EncodeDecode(msg);
        }
Пример #7
0
        public void BitFieldDecoding()
        {
            byte[] buffer = new byte[] { 0x00, 0x00, 0x00, 0x04, 0x05, 0xff, 0x08, 0xAA, 0xE3, 0x00 };
            Console.WriteLine("Pieces: " + testRig.Manager.Torrent.Pieces.Count);
            BitfieldMessage msg = (BitfieldMessage)PeerMessage.DecodeMessage(buffer, 0, 8, this.testRig.Manager);

            for (int i = 0; i < 8; i++)
            {
                Assert.IsTrue(msg.BitField[i], i.ToString());
            }

            for (int i = 8; i < 12; i++)
            {
                Assert.IsFalse(msg.BitField[i], i.ToString());
            }

            Assert.IsTrue(msg.BitField[12], 12.ToString());
            for (int i = 13; i < 15; i++)
            {
                Assert.IsFalse(msg.BitField[i], i.ToString());
            }
            EncodeDecode(msg);
        }
Пример #8
0
 protected virtual void HandleBitfield(BitfieldMessage bitfield, PeerState peer)
 {
     bitfield.Bitfield.CopyTo(peer.Bitfield, 0, 0, Metadata.PieceCount);
 }
Пример #9
0
        protected virtual void HandleBitfieldMessage(PeerId id, BitfieldMessage message)
        {
            id.BitField = message.BitField;
            id.Peer.IsSeeder = (id.BitField.AllTrue);

            SetAmInterestedStatus(id, _manager.PieceManager.IsInteresting(id));
        }
Пример #10
0
        public void OnMessageReceived(IMessageReceivedContext context)
        {
            // Read message
            CommonPeerMessage message = MessageHandler.ReadMessage(context.Metainfo, context.Reader, context.MessageLength, (byte)context.MessageId);

            if (message == null)
            {
                // Something went wrong
                context.Peer.Disconnect();
                return;
            }

            var peer = context.Peer;

            // Process message
            switch (message.ID)
            {
            case ChokeMessage.MessageID:
                SetChokedByPeer(peer, true);
                break;

            case UnchokeMessage.MessageID:
                SetChokedByPeer(peer, false);
                break;

            case InterestedMessage.MessageID:
                SetPeerInterested(peer, true);
                UnchokePeer(peer);
                break;

            case NotInterestedMessage.MessageID:
                SetPeerInterested(peer, false);
                break;

            case HaveMessage.MessageId:
            {
                HaveMessage haveMessage = message as HaveMessage;
                SetPeerBitfield(context, peer, haveMessage.Piece.Index, true);
                break;
            }

            case BitfieldMessage.MessageId:
            {
                BitfieldMessage bitfieldMessage = message as BitfieldMessage;
                SetPeerBitfield(peer, bitfieldMessage.Bitfield);
                if (IsBitfieldInteresting(context, bitfieldMessage.Bitfield))
                {
                    peer.IsInterestedInRemotePeer = true;
                    peer.SendMessage(new InterestedMessage());
                }
                break;
            }

            case RequestMessage.MessageID:
            {
                RequestMessage requestMessage = message as RequestMessage;
                SetBlockRequestedByPeer(peer, requestMessage.Block);
                break;
            }

            case CancelMessage.MessageID:
            {
                CancelMessage cancelMessage = message as CancelMessage;
                SetBlockCancelledByPeer(peer, cancelMessage.Block);
                break;
            }

            case PieceMessage.MessageId:
            {
                PieceMessage pieceMessage = message as PieceMessage;
                BlockReceived(context, peer, pieceMessage.Block);
                break;
            }
            }
        }
Пример #11
0
        /// <summary>
        /// Send bitfield message asynchronously
        /// </summary>
        /// <param name="bitfield">The bitfield of the requested file</param>
        public void SendBitfieldMessageAsync(bool[] bitfield)
        {
            BitfieldMessage message = new BitfieldMessage(bitfield);

            SendMessageAsync(message);
        }