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); }
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); }
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)); }
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)); }
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; } }
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); }
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); }
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); }
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); }
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); }
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); }
public bool Equals(MessageKey other) { return(_peerId.Equals(other._peerId) && _messageId.Equals(other._messageId)); }
public override void CancelRequests(PeerId peer) { CancelWhere(b => peer.Equals(b.RequestedOff)); }
public bool Equals(PeerAddress other) { return(other != null && PeerId.Equals(other.PeerId)); }