Inheritance: PeerMessage
 public void PieceEncoding()
 {
     var message = new PieceMessage(15, 10, Piece.BlockSize)
                       {
                           Data = new byte[Piece.BlockSize]
                       };
     message.Encode(_buffer, Offset);
 }
 public void PieceDecoding()
 {
     var message = new PieceMessage(15, 10, Piece.BlockSize)
                                {
                                    Data = new byte[Piece.BlockSize]
                                };
     EncodeDecode(message);
 }
 public HttpRequestData(RequestMessage request)
 {
     Request = request;
     PieceMessage m = new PieceMessage(request.PieceIndex, request.StartOffset, request.RequestLength);
     TotalToReceive = m.ByteLength;
 }
 public void PieceEncoding()
 {
     PieceMessage message = new PieceMessage(15, 10, Piece.BlockSize);
     message.Data = new byte[Piece.BlockSize];
     message.Encode(buffer, offset);
 }
 public void PieceDecoding()
 {
     PieceMessage message = new PieceMessage(15, 10, Piece.BlockSize);
     message.Data = new byte[Piece.BlockSize];
     EncodeDecode(message);
 }
Exemple #6
0
        public void PieceDataReceived(PeerId peer, PieceMessage message)
        {
            Piece piece;
            if (picker.ValidatePiece(peer, message.PieceIndex, message.StartOffset, message.RequestLength, out piece))
            {
                PeerId id = peer;
                TorrentManager manager = id.TorrentManager;
                Block block = piece.Blocks [message.StartOffset / Piece.BlockSize];
                long offset = (long) message.PieceIndex * id.TorrentManager.Torrent.PieceLength + message.StartOffset;

                id.LastBlockReceived = DateTime.Now;
                id.TorrentManager.PieceManager.RaiseBlockReceived(new BlockEventArgs(manager, block, piece, id));
                id.TorrentManager.Engine.DiskManager.QueueWrite (manager, offset, message.Data, message.RequestLength , delegate {
                    piece.Blocks[message.StartOffset/ Piece.BlockSize].Written = true;
                    ClientEngine.BufferManager.FreeBuffer(ref message.Data);
                    // If we haven't written all the pieces to disk, there's no point in hash checking
                    if (!piece.AllBlocksWritten)
                        return;

                    // Hashcheck the piece as we now have all the blocks.
                    id.Engine.DiskManager.BeginGetHash (id.TorrentManager, piece.Index, delegate (object o) {
                        byte[] hash = (byte[]) o;
                        bool result = hash == null ? false : id.TorrentManager.Torrent.Pieces.IsValid(hash, piece.Index);
                        id.TorrentManager.Bitfield[message.PieceIndex] = result;

                        ClientEngine.MainLoop.Queue(delegate
                        {
                            id.TorrentManager.PieceManager.UnhashedPieces[piece.Index] = false;

                            id.TorrentManager.HashedPiece(new PieceHashedEventArgs(id.TorrentManager, piece.Index, result));
                            List<PeerId> peers = new List<PeerId>(piece.Blocks.Length);
                            for (int i = 0; i < piece.Blocks.Length; i++)
                                if (piece.Blocks[i].RequestedOff != null && !peers.Contains(piece.Blocks[i].RequestedOff))
                                    peers.Add(piece.Blocks[i].RequestedOff);

                            for (int i = 0; i < peers.Count; i++) {
                                if (peers[i].Connection != null) {
                                    peers[i].Peer.HashedPiece(result);
                                    if (peers [i].Peer.TotalHashFails == 5)
                                        peers[i].ConnectionManager.CleanupSocket (id, "Too many hash fails");
                                }
                            }

                            // If the piece was successfully hashed, enqueue a new "have" message to be sent out
                            if (result)
                                id.TorrentManager.finishedPieces.Enqueue(piece.Index);
                        });
                    });
                });

                if (piece.AllBlocksReceived)
                    this.unhashedPieces[message.PieceIndex] = true;
            }
            else
            {
            }
        }