Ejemplo n.º 1
0
        public override PieceRequest ContinueExistingRequest(PeerId id)
        {
            for (int req = 0; req < requests.Count; req++)
            {
                Piece p = requests[req];
                // For each piece that was assigned to this peer, try to request a block from it
                // A piece is 'assigned' to a peer if he is the first person to request a block from that piece
                if (p.AllBlocksRequested || !id.Equals(p.Blocks[0].RequestedOff))
                {
                    continue;
                }

                for (int i = 0; i < p.BlockCount; i++)
                {
                    if (p.Blocks[i].Requested || p.Blocks[i].Received)
                    {
                        continue;
                    }

                    return(p.Blocks[i].CreateRequest(id));
                }
            }

            // If we get here it means all the blocks in the pieces being downloaded by the peer are already requested
            return(null);
        }
Ejemplo n.º 2
0
 public override void CancelRequest(PeerId peer, int piece, int startOffset, int length)
 {
     CancelWhere(r => r.Block.PieceIndex == piece &&
                 r.Block.StartOffset == startOffset &&
                 r.Block.RequestLength == length &&
                 peer.Equals(r.Peer), false);
 }
Ejemplo n.º 3
0
 public override void CancelRequest(PeerId peer, int piece, int startOffset, int length)
 {
     CancelWhere(b => b.StartOffset == startOffset &&
                 b.RequestLength == length &&
                 b.PieceIndex == piece &&
                 peer.Equals(b.RequestedOff));
 }
Ejemplo n.º 4
0
        public override bool Equals(object obj)
        {
            var args = obj as BlockEventArgs;

            return(args != null &&
                   _piece.Equals(args._piece) &&
                   _id.Equals(args._id) &&
                   _block.Equals(args._block));
        }
Ejemplo n.º 5
0
        private void ConsumeReceivedPublicKey(INetworkConnection connection, byte[] publicKey)
        {
            if (publicKey == null)
            {
                return;
            }

            var newKey = PublicKey.Unmarshal(publicKey);

            if (newKey == null)
            {
                return;
            }

            var np = new PeerId(newKey);

            if (np == null)
            {
                return;
            }

            if (!np.Equals(connection.RemotePeer))
            {
                if (connection.RemotePeer == "" && np != "")
                {
                    Host.Peerstore.AddPublicKey(connection.RemotePeer, newKey);
                }

                return;
            }

            var currentKey = Host.Peerstore.PublicKey(connection.RemotePeer);

            if (currentKey == null)
            {
                Host.Peerstore.AddPublicKey(connection.RemotePeer, newKey);
                return;
            }

            if (currentKey.Equals(newKey))
            {
                return;
            }

            var cp = new PeerId(currentKey);

            if (cp == null)
            {
                return;
            }

            if (!cp.Equals(connection.RemotePeer))
            {
                return;
            }
        }
Ejemplo n.º 6
0
 public bool Equals(UpdatedPeer?other)
 {
     if (ReferenceEquals(null, other))
     {
         return(false);
     }
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     return(PeerId.Equals(other.PeerId) && UpdateAction == other.UpdateAction);
 }
Ejemplo n.º 7
0
        public override bool Equals(object?obj)
        {
            if (!(obj is HandshakeMessage msg))
            {
                return(false);
            }

            if (InfoHash != msg.InfoHash)
            {
                return(false);
            }

            return((PeerId is null ? msg.PeerId is null : PeerId.Equals(msg.PeerId)) &&
                   ProtocolString == msg.ProtocolString &&
                   SupportsFastPeer == msg.SupportsFastPeer);
        }
Ejemplo n.º 8
0
        public override bool Equals(object obj)
        {
            HandshakeMessage msg = obj as HandshakeMessage;

            if (msg == null)
            {
                return(false);
            }

            if (InfoHash != msg.InfoHash)
            {
                return(false);
            }

            return(PeerId.Equals(msg.PeerId) &&
                   ProtocolString == msg.ProtocolString &&
                   SupportsFastPeer == msg.SupportsFastPeer);
        }
Ejemplo n.º 9
0
        public override bool ValidatePiece(PeerId id, int pieceIndex, int startOffset, int length, out Piece piece)
        {
            Comparer.Index = pieceIndex;
            int pIndex = requests.BinarySearch(null, Comparer);

            if (pIndex < 0)
            {
                piece = null;

                Logger.Log(null, "Validating: {0} - {1}: ", pieceIndex, startOffset);
                Logger.Log(null, "No piece");
                return(false);
            }
            piece = requests[pIndex];
            // Pick out the block that this piece message belongs to
            int blockIndex = Block.IndexOf(piece.Blocks, startOffset, length);

            if (blockIndex == -1 || !id.Equals(piece.Blocks[blockIndex].RequestedOff))
            {
                Logger.Log(null, "Validating: {0} - {1}: ", pieceIndex, startOffset);
                Logger.Log(null, "no block");
                return(false);
            }
            if (piece.Blocks[blockIndex].Received)
            {
                Logger.Log(null, "Validating: {0} - {1}: ", pieceIndex, startOffset);
                Logger.Log(null, "received");
                return(false);
            }
            if (!piece.Blocks[blockIndex].Requested)
            {
                Logger.Log(null, "Validating: {0} - {1}: ", pieceIndex, startOffset);
                Logger.Log(null, "not requested");
                return(false);
            }
            id.AmRequestingPiecesCount--;
            piece.Blocks[blockIndex].Received = true;

            if (piece.AllBlocksReceived)
            {
                requests.RemoveAt(pIndex);
            }
            return(true);
        }
Ejemplo n.º 10
0
        public override RequestMessage ContinueExistingRequest(PeerId id)
        {
            foreach (var p in Requests.Where(p => !p.AllBlocksRequested && id.Equals(p.Blocks[0].RequestedOff)))
            {
                for (var i = 0; i < p.BlockCount; i++)
                {
                    if (p.Blocks[i].Requested || p.Blocks[i].Received)
                    {
                        continue;
                    }

                    p.Blocks[i].Requested = true;
                    return(p.Blocks[i].CreateRequest(id));
                }
            }

            // If we get here it means all the blocks in the pieces being downloaded by the peer are already requested
            return(null);
        }
Ejemplo n.º 11
0
        public override bool ValidatePiece(PeerId id, int pieceIndex, int startOffset, int length, out Piece piece)
        {
            //Comparer.index = pieceIndex;
            var pIndex = Requests.BinarySearch(null, new BinaryIndexComparer(pieceIndex));

            if (pIndex < 0)
            {
                piece = null;
                Debug.WriteLine("Validating: {0} - {1}: ", pieceIndex, startOffset);
                Debug.WriteLine("No piece");
                return(false);
            }
            piece = Requests[pIndex];
            // Pick out the block that this piece message belongs to
            var blockIndex = Block.IndexOf(piece.Blocks, startOffset, length);

            if (blockIndex == -1 || !id.Equals(piece.Blocks[blockIndex].RequestedOff))
            {
                Debug.WriteLine("Validating: {0} - {1}: ", pieceIndex, startOffset);
                Debug.WriteLine("no block");
                return(false);
            }
            if (piece.Blocks[blockIndex].Received)
            {
                Debug.WriteLine("Validating: {0} - {1}: ", pieceIndex, startOffset);
                Debug.WriteLine("received");
                return(false);
            }
            if (!piece.Blocks[blockIndex].Requested)
            {
                Debug.WriteLine("Validating: {0} - {1}: ", pieceIndex, startOffset);
                Debug.WriteLine("not requested");
                return(false);
            }
            id.AmRequestingPiecesCount--;
            piece.Blocks[blockIndex].Received = true;

            if (piece.AllBlocksReceived)
            {
                Requests.RemoveAt(pIndex);
            }
            return(true);
        }
Ejemplo n.º 12
0
 public bool Equals(MessageKey other)
 {
     return(_peerId.Equals(other._peerId) && _messageId.Equals(other._messageId));
 }
Ejemplo n.º 13
0
 public override void CancelRequests(PeerId peer)
 {
     CancelWhere(b => peer.Equals(b.RequestedOff));
 }
Ejemplo n.º 14
0
 public bool Equals(PeerAddress other)
 {
     return(other != null && PeerId.Equals(other.PeerId));
 }