示例#1
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);
        }
示例#2
0
        private void HandleRequest(PeerId id, RequestMessage m)
        {
            Request r = new Request();

            r.PieceIndex    = m.PieceIndex;
            r.RequestedOff  = id;
            r.RequestLength = m.RequestLength;
            r.StartOffset   = m.StartOffset;
            List <Request> current = requests.FindAll(delegate(Request req) { return(req.CompareTo(r) == 0); });

            if (current.Count > 0)
            {
                foreach (Request request in current)
                {
                    if (request.Verified)
                    {
                        if (id.TorrentManager.Bitfield[request.PieceIndex])
                        {
                            Logger.Log(null, "Double request: {0}", m);
                            Logger.Log(null, "From: {0} and {1}", id.PeerID, r.RequestedOff.PeerID);
                        }
                        else
                        {
                            // The piece failed a hashcheck, so ignore it this time
                            requests.Remove(request);
                        }
                    }
                }
            }
            requests.Add(r);
        }