Example #1
0
        private Peer(Torrent torrent, string localId)
        {
            LocalId = localId;
            Torrent = torrent;

            LastActive        = DateTime.UtcNow;
            IsPieceDownloaded = new bool[Torrent.PieceCount];
            IsBlockRequested  = new bool[Torrent.PieceCount][];
            for (int i = 0; i < Torrent.PieceCount; i++)
            {
                IsBlockRequested[i] = new bool[Torrent.GetBlockCount(i)];
            }
        }
Example #2
0
        private void ProcessDownloads()
        {
            if (Interlocked.Exchange(ref isProcessDownloads, 1) == 1)
            {
                return;
            }

            DataPackage incomingBlock;

            while (IncomingBlocks.TryDequeue(out incomingBlock))
            {
                Torrent.WriteBlock(incomingBlock.Piece, incomingBlock.Block, incomingBlock.Data);
            }

            if (Torrent.IsCompleted)
            {
                Interlocked.Exchange(ref isProcessDownloads, 0);
                return;
            }

            int[] ranked = GetRankedPieces();

            foreach (var piece in ranked)
            {
                if (Torrent.IsPieceVerified[piece])
                {
                    continue;
                }

                foreach (var peer in GetRankedSeeders())
                {
                    if (!peer.IsPieceDownloaded[piece])
                    {
                        continue;
                    }

                    // just request blocks in order
                    for (int block = 0; block < Torrent.GetBlockCount(piece); block++)
                    {
                        if (downloadThrottle.IsThrottled)
                        {
                            continue;
                        }

                        if (Torrent.IsBlockAcquired[piece][block])
                        {
                            continue;
                        }

                        // only request one block from each peer at a time
                        if (peer.BlocksRequested > 0)
                        {
                            continue;
                        }

                        // only request from 1 peer at a time
                        if (Peers.Count(x => x.Value.IsBlockRequested[piece][block]) > 0)
                        {
                            continue;
                        }

                        int size = Torrent.GetBlockSize(piece, block);
                        peer.SendRequest(piece, block * Torrent.BlockSize, size);
                        downloadThrottle.Add(size);
                        peer.IsBlockRequested[piece][block] = true;
                    }
                }
            }

            Interlocked.Exchange(ref isProcessDownloads, 0);
        }
        private Peer(Torrent torrent, string localId)
        {
            LocalId = localId;
            Torrent = torrent;

            LastActive = DateTime.UtcNow;
            IsPieceDownloaded = new bool[Torrent.PieceCount];
            IsBlockRequested = new bool[Torrent.PieceCount][];
            for (int i = 0; i < Torrent.PieceCount; i++)
                IsBlockRequested[i] = new bool[Torrent.GetBlockCount(i)];
        }