示例#1
0
        void SendHaveMessagesToAll()
        {
            for (int i = 0; i < Manager.Peers.ConnectedPeers.Count; i++)
            {
                if (Manager.Peers.ConnectedPeers[i].Connection == null)
                {
                    continue;
                }

                MessageBundle bundle = new MessageBundle();

                foreach (var haveMessage in Manager.finishedPieces)
                {
                    // If the peer has the piece already, we need to recalculate his "interesting" status.
                    bool hasPiece = Manager.Peers.ConnectedPeers[i].BitField[haveMessage.PieceIndex];
                    if (hasPiece)
                    {
                        bool isInteresting = Manager.PieceManager.IsInteresting(Manager.Peers.ConnectedPeers[i]);
                        SetAmInterestedStatus(Manager.Peers.ConnectedPeers[i], isInteresting);
                    }

                    // Check to see if have supression is enabled and send the have message accordingly
                    if (!hasPiece || (hasPiece && !Settings.AllowHaveSuppression))
                    {
                        bundle.Messages.Add(haveMessage);
                    }
                }

                Manager.Peers.ConnectedPeers[i].Enqueue(bundle);
            }
            Manager.finishedPieces.Clear();
        }
        public void PickBundle5()
        {
            rig.Manager.Bitfield.SetAll(true);

            for (int i = 0; i < 20; i++)
            {
                rig.Manager.Bitfield[i % 2]  = false;
                rig.Manager.Bitfield[10 + i] = false;
            }

            peers[0].IsChoking = false;
            peers[0].BitField.SetAll(true);

            for (int i = 0; i < rig.BlocksPerPiece; i++)
            {
                picker.PickPiece(peers[0], peers[0].BitField, new List <PeerId>(), 1, 3, 3);
            }
            for (int i = 0; i < rig.BlocksPerPiece; i++)
            {
                picker.PickPiece(peers[0], peers[0].BitField, new List <PeerId>(), 1, 6, 6);
            }

            MessageBundle b = picker.PickPiece(peers[0], new List <PeerId>(), 20 * rig.BlocksPerPiece);

            Assert.Equal(20 * rig.BlocksPerPiece, b.Messages.Count);
            foreach (RequestMessage m in b.Messages)
            {
                Assert.True(m.PieceIndex >= 10 && m.PieceIndex < 30);
            }
        }
        // Fixes the language display of the game
        internal static void FixLangDisplay(MessageDirector dir)
        {
            string[] bundles =
            {
                "achieve", "exchange"
            };

            foreach (string bundle in bundles)
            {
                MessageBundle  actor  = dir.GetBundle(bundle);
                ResourceBundle rActor = actor.GetPrivateField <ResourceBundle>("bundle");

                FileInfo fActor = new FileInfo(Application.dataPath + $"/{bundle}.yaml");

                using (StreamWriter writer = fActor.CreateText())
                {
                    writer.WriteLine("#=====================================");
                    writer.WriteLine("# AUTO GENERATED FROM THE GAME");
                    writer.WriteLine("#=====================================");
                    writer.WriteLine("");

                    foreach (string key in rActor.GetKeys())
                    {
                        writer.WriteLine($"{bundle}:" + key + ": \"" +
                                         actor.Get(key).Replace("\"", "\\\"").Replace("\n", "\\n") + "\"");
                    }
                }
            }
        }
        public override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List <PeerId> otherPeers, int count, int startIndex, int endIndex)
        {
            if (peerBitfield.AllFalse)
            {
                return(null);
            }

            if (count > 1)
            {
                return(base.PickPiece(id, peerBitfield, otherPeers, count, startIndex, endIndex));
            }

            GenerateRarestFirst(peerBitfield, otherPeers);

            while (rarest.Count > 0)
            {
                BitField      current = rarest.Pop();
                MessageBundle bundle  = base.PickPiece(id, current, otherPeers, count, startIndex, endIndex);
                spares.Push(current);

                if (bundle != null)
                {
                    return(bundle);
                }
            }

            return(null);
        }
示例#5
0
        protected virtual MessageBundle GetStandardRequest(PeerId id, BitField current, List <PeerId> otherPeers, int startIndex, int endIndex, int count)
        {
            int piecesNeeded = (count * Piece.BlockSize) / id.TorrentManager.Torrent.PieceLength;

            if ((count * Piece.BlockSize) % id.TorrentManager.Torrent.PieceLength != 0)
            {
                piecesNeeded++;
            }
            int checkIndex = CanRequest(current, startIndex, endIndex, ref piecesNeeded);

            // Nothing to request.
            if (checkIndex == -1)
            {
                return(null);
            }

            MessageBundle bundle = new MessageBundle();

            for (int i = 0; bundle.Messages.Count < count && i < piecesNeeded; i++)
            {
                // Request the piece
                Piece p = new Piece(checkIndex + i, id.TorrentManager.Torrent.PieceLength, id.TorrentManager.Torrent.Size);
                requests.Add(p);

                for (int j = 0; j < p.Blocks.Length && bundle.Messages.Count < count; j++)
                {
                    p.Blocks[j].Requested = true;
                    bundle.Messages.Add(p.Blocks[j].CreateRequest(id));
                }
            }
            return(bundle);
        }
        internal void RaisePeerMessageTransferred(PeerMessageEventArgs e)
        {
            if (PeerMessageTransferred == null)
            {
                return;
            }

            ThreadPool.QueueUserWorkItem(delegate
            {
                EventHandler <PeerMessageEventArgs> h = PeerMessageTransferred;
                if (h == null)
                {
                    return;
                }

                if (!(e.Message is MessageBundle))
                {
                    h(e.TorrentManager, e);
                }
                else
                {
                    // Message bundles are only a convience for internal usage!
                    MessageBundle b = (MessageBundle)e.Message;
                    foreach (PeerMessage message in b.Messages)
                    {
                        PeerMessageEventArgs args = new PeerMessageEventArgs(e.TorrentManager, message, e.Direction, e.ID);
                        h(args.TorrentManager, args);
                    }
                }
            });
        }
示例#7
0
        private void SendHaveMessagesToAll()
        {
            foreach (var peerId in _manager.Peers.ConnectedPeers)
            {
                if (peerId.Connection == null)
                {
                    continue;
                }

                var bundle = new MessageBundle();

                foreach (var pieceIndex in _manager.FinishedPieces)
                {
                    // If the peer has the piece already, we need to recalculate his "interesting" status.
                    var hasPiece = peerId.BitField[pieceIndex];
                    if (hasPiece)
                    {
                        var isInteresting = _manager.PieceManager.IsInteresting(peerId);
                        SetAmInterestedStatus(peerId, isInteresting);
                    }

                    // Check to see if have supression is enabled and send the have message accordingly
                    if (!hasPiece || (hasPiece && !_manager.Engine.Settings.HaveSupressionEnabled))
                    {
                        bundle.Messages.Add(new HaveMessage(pieceIndex));
                    }
                }

                peerId.Enqueue(bundle);
            }
            _manager.FinishedPieces.Clear();
        }
        public MessageBundle getBundle(GadgetSpec spec, Locale locale, bool ignoreCache)
        {
            if (ignoreCache)
            {
                return(getNestedBundle(spec, locale, true));
            }

            String key = spec.getUrl().ToString() + '.' + locale.ToString();

            MessageBundle cached = HttpRuntime.Cache[key] as MessageBundle;

            MessageBundle bundle;

            if (cached == null)
            {
                try
                {
                    bundle = getNestedBundle(spec, locale, ignoreCache);
                }
                catch (GadgetException)
                {
                    // Enforce negative caching.
                    bundle = cached ?? MessageBundle.EMPTY;
                }
                HttpRuntime.Cache.Insert(key, bundle, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(refresh));
            }
            else
            {
                bundle = cached;
            }

            return(bundle);
        }
示例#9
0
        public override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List <PeerId> otherPeers, int count, int startIndex, int endIndex)
        {
            // Fast Path - the peer has nothing to offer
            if (peerBitfield.AllFalse)
            {
                return(null);
            }

            if (files.Count == 1)
            {
                if (files[0].File.Priority == Priority.DoNotDownload)
                {
                    return(null);
                }
                else
                {
                    return(base.PickPiece(id, peerBitfield, otherPeers, count, startIndex, endIndex));
                }
            }

            files.Sort();

            // Fast Path - all the files have been set to DoNotDownload
            if (files[0].File.Priority == Priority.DoNotDownload)
            {
                return(null);
            }

            // Fast Path - If all the files are the same priority, call straight into the base picker
            if (files.TrueForAll(AllSamePriority))
            {
                return(base.PickPiece(id, peerBitfield, otherPeers, count, startIndex, endIndex));
            }

            temp.From(files[0].Selector);
            for (int i = 1; i < files.Count && files[i].File.Priority != Priority.DoNotDownload; i++)
            {
                if (files[i].File.Priority != files[i - 1].File.Priority)
                {
                    temp.And(peerBitfield);
                    if (!temp.AllFalse)
                    {
                        MessageBundle message = base.PickPiece(id, temp, otherPeers, count, startIndex, endIndex);
                        if (message != null)
                        {
                            return(message);
                        }
                        temp.SetAll(false);
                    }
                }

                temp.Or(files[i].Selector);
            }

            if (temp.AllFalse || temp.And(peerBitfield).AllFalse)
            {
                return(null);
            }
            return(base.PickPiece(id, temp, otherPeers, count, startIndex, endIndex));
        }
示例#10
0
        public void PickBundle6()
        {
            rig.Manager.Bitfield.SetAll(false);

            peers[0].IsChoking = false;
            peers[0].BitField.SetAll(true);

            for (int i = 0; i < rig.BlocksPerPiece; i++)
            {
                picker.PickPiece(peers[0], peers[0].BitField, new List <PeerId>(), 1, 0, 0);
            }
            for (int i = 0; i < rig.BlocksPerPiece; i++)
            {
                picker.PickPiece(peers[0], peers[0].BitField, new List <PeerId>(), 1, 1, 1);
            }
            for (int i = 0; i < rig.BlocksPerPiece; i++)
            {
                picker.PickPiece(peers[0], peers[0].BitField, new List <PeerId>(), 1, 3, 3);
            }
            for (int i = 0; i < rig.BlocksPerPiece; i++)
            {
                picker.PickPiece(peers[0], peers[0].BitField, new List <PeerId>(), 1, 6, 6);
            }

            MessageBundle b = picker.PickPiece(peers[0], new List <PeerId>(), 2 * rig.BlocksPerPiece);

            Assert.AreEqual(2 * rig.BlocksPerPiece, b.Messages.Count);
            foreach (RequestMessage m in b.Messages)
            {
                Assert.IsTrue(m.PieceIndex >= 4 && m.PieceIndex < 6);
            }
        }
示例#11
0
        public void Setup()
        {
            requestedUrl.Clear();
            partialData = false;
            int i;

            for (i = 0; i < 1000; i++)
            {
                try
                {
                    listener = new HttpListener();
                    listener.Prefixes.Add(string.Format(listenerURL, i));
                    listener.Start();
                    break;
                }
                catch
                {
                }
            }
            listener.BeginGetContext(GotContext, null);
            rig                = TestRig.CreateMultiFile();
            connection         = new HttpConnection(new Uri(string.Format(listenerURL, i)));
            connection.Manager = rig.Manager;

            id              = new PeerId(new Peer("this is my id", connection.Uri), rig.Manager);
            id.Connection   = connection;
            id.IsChoking    = false;
            id.AmInterested = true;
            id.BitField.SetAll(true);
            id.MaxPendingRequests = numberOfPieces;

            requests = rig.Manager.PieceManager.Picker.PickPiece(id, new List <PeerId>(), numberOfPieces);
        }
示例#12
0
        void SendHaveMessagesToAll()
        {
            for (int i = 0; i < manager.Peers.ConnectedPeers.Count; i++)
            {
                var connectedPeer = manager.Peers.ConnectedPeers[i];
                if (connectedPeer.Connection == null)
                {
                    continue;
                }

                MessageBundle bundle = new MessageBundle();

                foreach (int pieceIndex in manager.finishedPieces)
                {
                    // If the peer has the piece already, we need to recalculate his "interesting" status.
                    bool hasPiece = connectedPeer.BitField[pieceIndex];
                    if (hasPiece)
                    {
                        bool isInteresting = manager.PieceManager.IsInteresting(manager.Peers.ConnectedPeers[i]);
                        SetAmInterestedStatus(connectedPeer, isInteresting);
                    }

                    // Check to see if have supression is enabled and send the have message accordingly
                    if (!hasPiece || (hasPiece && !manager.Engine.Settings.HaveSupressionEnabled))
                    {
                        bundle.Messages.Add(new HaveMessage(pieceIndex));
                    }
                }

                connectedPeer.Enqueue(bundle);
            }
            manager.finishedPieces.Clear();
        }
 protected override void AppendBitfieldMessage(PeerId id, MessageBundle bundle)
 {
     if (id.SupportsFastPeer)
         bundle.Messages.Add(new HaveNoneMessage());
     else
         bundle.Messages.Add(new BitfieldMessage(zero));
 }
示例#14
0
 protected virtual void AppendExtendedHandshake(PeerId id, MessageBundle bundle)
 {
     if (id.SupportsLTMessages && ClientEngine.SupportsExtended)
     {
         bundle.Messages.Add(new ExtendedHandshakeMessage(Manager.Torrent?.IsPrivate ?? false, Manager.HasMetadata ? Manager.Torrent.InfoMetadata.Length : 0, Settings.ListenPort));
     }
 }
示例#15
0
        void SendHaveMessagesToAll ()
        {
            if (Manager.finishedPieces.Count == 0)
                return;

            if (Settings.AllowHaveSuppression) {
                for (int i = 0; i < Manager.Peers.ConnectedPeers.Count; i++) {
                    var bundle = new MessageBundle ();
                    foreach (HaveMessage haveMessage in Manager.finishedPieces) {
                        // If the peer has the piece already, we need to recalculate his "interesting" status.
                        bool hasPiece = Manager.Peers.ConnectedPeers[i].BitField[haveMessage.PieceIndex];
                        if (!hasPiece)
                            bundle.Messages.Add (haveMessage);
                    }

                    Manager.Peers.ConnectedPeers[i].Enqueue (bundle);
                }
            } else {
                var bundle = new MessageBundle (Manager.finishedPieces.Count);
                foreach (HaveMessage haveMessage in Manager.finishedPieces)
                    bundle.Messages.Add (haveMessage);

                foreach (PeerId peer in Manager.Peers.ConnectedPeers)
                    peer.Enqueue (bundle);
            }

            foreach (PeerId peer in Manager.Peers.ConnectedPeers) {
                bool isInteresting = Manager.PieceManager.IsInteresting (peer);
                SetAmInterestedStatus (peer, isInteresting);
            }
            Manager.finishedPieces.Clear ();
        }
示例#16
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);
        }
示例#17
0
 protected virtual void AppendExtendedHandshake(PeerId id, MessageBundle bundle)
 {
     if (id.SupportsLTMessages && ClientEngine.SupportsExtended)
     {
         bundle.Messages.Add(new ExtendedHandshakeMessage(manager.HasMetadata ? manager.Torrent.Metadata.Length : 0));
     }
 }
示例#18
0
        protected virtual void AppendFastPieces (PeerId id, MessageBundle bundle)
        {
            // Now we will enqueue a FastPiece message for each piece we will allow the peer to download
            // even if they are choked
            if (ClientEngine.SupportsFastPeer && id.SupportsFastPeer)
                for (int i = 0; i < id.AmAllowedFastPieces.Count; i++)
                    bundle.Messages.Add (new AllowedFastMessage (id.AmAllowedFastPieces[i]));

        }
示例#19
0
        public virtual void HandlePeerConnected(PeerId id, Direction direction)
        {
            MessageBundle bundle = new MessageBundle();

            AppendBitfieldMessage(id, bundle);
            AppendExtendedHandshake(id, bundle);
            AppendFastPieces(id, bundle);

            id.Enqueue(bundle);
        }
示例#20
0
        public override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List <PeerId> otherPeers, int count, int startIndex, int endIndex)
        {
            MessageBundle bundle = ActivePicker.PickPiece(id, peerBitfield, otherPeers, count, startIndex, endIndex);

            if (bundle == null && TryEnableEndgame())
            {
                return(ActivePicker.PickPiece(id, peerBitfield, otherPeers, count, startIndex, endIndex));
            }
            return(bundle);
        }
示例#21
0
 protected override void AppendBitfieldMessage(PeerId id, MessageBundle bundle)
 {
     if (id.SupportsFastPeer)
     {
         bundle.Add(HaveNoneMessage.Instance, default);
     }
     else
     {
         bundle.Add(new BitfieldMessage(zero), default);
     }
 }
示例#22
0
 protected override void AppendBitfieldMessage(PeerId id, MessageBundle bundle)
 {
     if (id.SupportsFastPeer)
     {
         bundle.Messages.Add(new HaveNoneMessage());
     }
     else
     {
         bundle.Messages.Add(new BitfieldMessage(zero));
     }
 }
示例#23
0
 public void RequestBlock()
 {
     peer.IsChoking = false;
     peer.BitField.SetAll(true);
     for (int i = 0; i < 1000; i++)
     {
         MessageBundle b = picker.PickPiece(peer, peers, i);
         Assert.AreEqual(Math.Min(i, rig.TotalBlocks), b.Messages.Count);
         picker.CancelRequests(peer);
     }
 }
示例#24
0
 protected override void AppendBitfieldMessage(PeerId id, MessageBundle bundle)
 {
     if (ClientEngine.SupportsFastPeer && id.SupportsFastPeer)
     {
         bundle.Messages.Add(new HaveNoneMessage());
     }
     // If the fast peer extensions are not supported we must not send a
     // bitfield message because we don't know how many pieces the torrent
     // has. We could probably send an invalid one and force the connection
     // to close.
 }
示例#25
0
 protected virtual void AppendFastPieces(PeerId id, MessageBundle bundle)
 {
     // Now we will enqueue a FastPiece message for each piece we will allow the peer to download
     // even if they are choked
     if (ClientEngine.SupportsFastPeer && id.SupportsFastPeer)
     {
         foreach (var pieceIndex in id.AmAllowedFastPieces)
         {
             bundle.Messages.Add(new AllowedFastMessage(pieceIndex));
         }
     }
 }
示例#26
0
        public void DoesntHaveSuggestedPiece()
        {
            peer.IsChoking        = false;
            peer.SupportsFastPeer = true;
            peer.SuggestedPieces.AddRange(new int[] { 1, 2, 3, 4 });
            peer.BitField.SetAll(true);
            picker = new StandardPicker();
            picker.Initialise(rig.Manager.Bitfield, rig.Torrent.Files, new List <Piece>());
            MessageBundle bundle = picker.PickPiece(peer, new MonoTorrent.Common.BitField(peer.BitField.Length), peers, 1, 0, peer.BitField.Length);

            Assert.IsNull(bundle);
        }
示例#27
0
        public override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List <PeerId> otherPeers, int count, int startIndex, int endIndex)
        {
            RequestMessage message;
            MessageBundle  bundle = null;

            // If there is already a request on this peer, try to request the next block. If the peer is choking us, then the only
            // requests that could be continued would be existing "Fast" pieces.
            if ((message = ContinueExistingRequest(id)) != null)
            {
                return(bundle = new MessageBundle(message));
            }

            // Then we check if there are any allowed "Fast" pieces to download
            if (id.IsChoking && (message = GetFromList(id, peerBitfield, id.IsAllowedFastPieces)) != null)
            {
                return(bundle = new MessageBundle(message));
            }

            // If the peer is choking, then we can't download from them as they had no "fast" pieces for us to download
            if (id.IsChoking)
            {
                return(null);
            }

            // If we are only requesting 1 piece, then we can continue any existing. Otherwise we should try
            // to request the full amount first, then try to continue any existing.
            if (count == 1 && (message = ContinueAnyExisting(id)) != null)
            {
                return(bundle = new MessageBundle(message));
            }

            // We see if the peer has suggested any pieces we should request
            if ((message = GetFromList(id, peerBitfield, id.SuggestedPieces)) != null)
            {
                return(bundle = new MessageBundle(message));
            }

            // Now we see what pieces the peer has that we don't have and try and request one
            if ((bundle = GetStandardRequest(id, peerBitfield, otherPeers, startIndex, endIndex, count)) != null)
            {
                return(bundle);
            }

            // If all else fails, ignore how many we're requesting and try to continue any existing
            if ((message = ContinueAnyExisting(id)) != null)
            {
                return(bundle = new MessageBundle(message));
            }

            return(null);
        }
        /// <summary>Constructor that creates an extraction error message for the specified message.</summary>
        /// <param name="messageId">the id of the message to create an extraction error for.</param>
        /// <param name="destination">the endpoint identifier that the extraction error message will be sent to.</param>
        /// <param name="source">the endpoint identifier that the extraction error message will be sent from.</param>
        public ExtractionErrorMessage(string messageId, string destination, string source = "http://nchs.cdc.gov/vrdr_submission") : base("http://nchs.cdc.gov/vrdr_extraction_error")
        {
            Header.Source.Endpoint  = source;
            this.MessageDestination = destination;
            MessageHeader.ResponseComponent resp = new MessageHeader.ResponseComponent();
            resp.Identifier = messageId;
            resp.Code       = MessageHeader.ResponseType.FatalError;
            Header.Response = resp;

            this.details    = new OperationOutcome();
            this.details.Id = Guid.NewGuid().ToString();
            MessageBundle.AddResourceEntry(this.details, "urn:uuid:" + this.details.Id);
            Header.Response.Details = new ResourceReference("urn:uuid:" + this.details.Id);
        }
示例#29
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();
            var    sendTask   = Send(sendBuffer, 0, sendBuffer.Length, 1);

            Assert.ThrowsAsync <ArgumentException>(() => sendTask);
        }
示例#30
0
        /**
         * Injects message bundles into the gadget output.
         * @throws GadgetException If we are unable to retrieve the message bundle.
         */
        private void InjectMessageBundles(Gadget gadget, Node scriptTag)
        {
            GadgetContext context = gadget.getContext();
            MessageBundle bundle  = messageBundleFactory.getBundle(
                gadget.getSpec(), context.getLocale(), context.getIgnoreCache());

            String msgs = bundle.ToJSONString();

            Text text = scriptTag.getOwnerDocument().createTextNode("gadgets.Prefs.setMessages_(");

            text.appendData(msgs);
            text.appendData(");");
            scriptTag.appendChild(text);
        }
示例#31
0
        public override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List <PeerId> otherPeers, int count, int startIndex, int endIndex)
        {
            MessageBundle bundle = base.PickPiece(id, peerBitfield, otherPeers, count, startIndex, endIndex);

            if (bundle != null)
            {
                foreach (RequestMessage m in bundle.Messages)
                {
                    HandleRequest(id, m);
                }
            }

            return(bundle);
        }
示例#32
0
 protected override void AppendBitfieldMessage(PeerId id, MessageBundle bundle)
 {
     // We can't send a bitfield message in metadata mode as
     // we don't know what size the bitfield is
 }
示例#33
0
        public virtual void HandlePeerConnected(PeerId id, Direction direction)
        {
            var bundle = new MessageBundle();

            AppendBitfieldMessage(id, bundle);
            AppendExtendedHandshake(id, bundle);
            AppendFastPieces(id, bundle);

            id.Enqueue(bundle);
        }
示例#34
0
        protected virtual void AppendBitfieldMessage(PeerId id, MessageBundle bundle)
        {
            if (id.SupportsFastPeer && ClientEngine.SupportsFastPeer)
            {
                if (_manager.Bitfield.AllFalse)
                    bundle.Messages.Add(new HaveNoneMessage());

                else if (_manager.Bitfield.AllTrue)
                    bundle.Messages.Add(new HaveAllMessage());

                else
                    bundle.Messages.Add(new BitfieldMessage(_manager.Bitfield));
            }
            else
            {
                bundle.Messages.Add(new BitfieldMessage(_manager.Bitfield));
            }
        }
示例#35
0
 protected virtual void AppendExtendedHandshake(PeerId id, MessageBundle bundle)
 {
     if (id.SupportsLTMessages && ClientEngine.SupportsExtended)
         bundle.Messages.Add(
             new ExtendedHandshakeMessage(_manager.HasMetadata ? _manager.Torrent.Metadata.Length : 0));
 }
示例#36
0
 protected virtual void AppendFastPieces(PeerId id, MessageBundle bundle)
 {
     // Now we will enqueue a FastPiece message for each piece we will allow the peer to download
     // even if they are choked
     if (ClientEngine.SupportsFastPeer && id.SupportsFastPeer)
         foreach (var pieceIndex in id.AmAllowedFastPieces)
             bundle.Messages.Add(new AllowedFastMessage(pieceIndex));
 }
示例#37
0
        private void SendHaveMessagesToAll()
        {
            foreach (var peerId in _manager.Peers.ConnectedPeers)
            {
                if (peerId.Connection == null)
                    continue;

                var bundle = new MessageBundle();

                foreach (var pieceIndex in _manager.finishedPieces)
                {
                    // If the peer has the piece already, we need to recalculate his "interesting" status.
                    var hasPiece = peerId.BitField[pieceIndex];
                    if (hasPiece)
                    {
                        var isInteresting = _manager.PieceManager.IsInteresting(peerId);
                        SetAmInterestedStatus(peerId, isInteresting);
                    }

                    // Check to see if have supression is enabled and send the have message accordingly
                    if (!hasPiece || (hasPiece && !_manager.Engine.Settings.HaveSupressionEnabled))
                        bundle.Messages.Add(new HaveMessage(pieceIndex));
                }

                peerId.Enqueue(bundle);
            }
            _manager.finishedPieces.Clear();
        }