public void FixtureSetup() { rig = TestRig.CreateMultiFile(); id = new PeerId(new Peer(new string('a', 20), new Uri("tcp://BLAH")), rig.Manager); for (int i = 0; i < id.BitField.Length; i += 2) id.BitField[i] = true; }
private void ConnectionReceived(object sender, NewConnectionEventArgs e) { if (_engine.ConnectionManager.ShouldBanPeer(e.Peer)) { e.Connection.Dispose(); return; } var id = new PeerId(e.Peer, e.TorrentManager) {Connection = e.Connection}; Logger.Log(id.Connection, "ListenManager - ConnectionReceived"); if (id.Connection.IsIncoming) { var skeys = new List<InfoHash>(); ClientEngine.MainLoop.QueueWait(delegate { for (var i = 0; i < _engine.Torrents.Count; i++) skeys.Add(_engine.Torrents[i].InfoHash); }); EncryptorFactory.BeginCheckEncryption(id, HandshakeMessage.HandshakeLength, _endCheckEncryptionCallback, id, skeys.ToArray()); } else { ClientEngine.MainLoop.Queue(() => _engine.ConnectionManager.ProcessFreshConnection(id)); } }
internal PeerConnectionEventArgs( TorrentManager manager, PeerId id, Direction direction, String message ) : base(manager) { _peerConnectionId = id; _connectionDirection = direction; _message = message; }
public virtual void Unchoke(PeerId id) { id.AmChoking = false; id.TorrentManager.UploadingTo++; id.Enqueue(new UnchokeMessage()); id.LastUnchoked = DateTime.Now; }
public PeerExchangePeersAdded(TorrentManager manager, int count, int total, PeerId id) : base(manager, count, total) { if (id == null) throw new ArgumentNullException("id"); _id = id; }
internal static IAsyncResult BeginCheckEncryption(PeerId id, int bytesToReceive, AsyncCallback callback, object state, InfoHash[] sKeys) { EncryptorAsyncResult result = new EncryptorAsyncResult(id, callback, state); result.SKeys = sKeys; IConnection c = id.Connection; ClientEngine.MainLoop.QueueTimeout(TimeSpan.FromSeconds(10), delegate { if (id.Encryptor == null || id.Decryptor == null) id.CloseConnection(); return false; }); try { // If the connection is incoming, receive the handshake before // trying to decide what encryption to use if (id.Connection.IsIncoming) { result.Buffer = new byte[bytesToReceive]; NetworkIO.EnqueueReceive(c, result.Buffer, 0, result.Buffer.Length, null, null, null, HandshakeReceivedCallback, result); } else { EncryptionTypes usable = CheckRC4(id); bool hasPlainText = Toolbox.HasEncryption(usable, EncryptionTypes.PlainText); bool hasRC4 = Toolbox.HasEncryption(usable, EncryptionTypes.RC4Full) || Toolbox.HasEncryption(usable, EncryptionTypes.RC4Header); if (id.Engine.Settings.PreferEncryption) { if (hasRC4) { result.EncSocket = new PeerAEncryption(id.TorrentManager.InfoHash, usable); result.EncSocket.BeginHandshake(id.Connection, CompletedEncryptedHandshakeCallback, result); } else { result.Complete(); } } else { if (hasPlainText) { result.Complete(); } else { result.EncSocket = new PeerAEncryption(id.TorrentManager.InfoHash, usable); result.EncSocket.BeginHandshake(id.Connection, CompletedEncryptedHandshakeCallback, result); } } } } catch (Exception ex) { result.Complete(ex); } return result; }
public override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List<PeerId> otherPeers, int count, int startIndex, int endIndex) { // Invert 'bitfield' and AND it with the peers bitfield // Any pieces which are 'true' in the bitfield will not be downloaded temp.From(peerBitfield).NAnd(bitfield); if (temp.AllFalse) return null; return base.PickPiece(id, temp, otherPeers, count, startIndex, endIndex); }
internal Block(Piece piece, int startOffset, int requestLength) { this.requestedOff = null; this.piece = piece; this.received = false; this.requested = false; this.requestLength = requestLength; this.startOffset = startOffset; this.written = false; }
public override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List<PeerId> otherPeers, int count, int startIndex, int endIndex) { if (peerBitfield.AllFalse) return null; if (count > 1) return base.PickPiece(id, peerBitfield, otherPeers, count, startIndex, endIndex); int midpoint = random.Next(startIndex, endIndex); return base.PickPiece(id, peerBitfield, otherPeers, count, midpoint, endIndex) ?? base.PickPiece(id, peerBitfield, otherPeers, count, startIndex, midpoint); }
public void Choke(PeerId peer) { //Choke the supplied peer if (peer.AmChoking) //We're already choking this peer, nothing to do return; peer.AmChoking = true; _owningTorrent.UploadingTo--; RejectPendingRequests(peer); peer.EnqueueAt(new ChokeMessage(), 0); Logger.Log(peer.Connection, "Choking"); // Send2Log("Choking: " + PeerToChoke.Location); }
public override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List<PeerId> otherPeers, int count, int startIndex, int endIndex) { if (peerBitfield.AllFalse) return null; if (count > 1) return base.PickPiece(id, peerBitfield, otherPeers, count, startIndex, endIndex); GenerateRarestFirst(peerBitfield, otherPeers); while (rarest.Count > 0) { BitField current = rarest.Pop(); MessageBundle bundle = base.PickPiece(id, current, otherPeers, count, startIndex, endIndex); spares.Push(current); if (bundle != null) return bundle; } return null; }
public override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List<PeerId> otherPeers, int count, int startIndex, int endIndex) { PickPieceId.Add(id); BitField clone = new BitField(peerBitfield.Length); clone.Or(peerBitfield); PickPieceBitfield.Add(clone); PickPiecePeers.Add(otherPeers); PickPieceStartIndex.Add(startIndex); PickPieceEndIndex.Add(endIndex); PickPieceCount.Add(count); for (int i = startIndex; i < endIndex; i++) { if (PickedPieces.Contains(i)) continue; PickedPieces.Add(i); if (ReturnNoPiece) return null; else return new MessageBundle(); } return null; }
public RequestMessage PickPiece(PeerId peer, List<PeerId> otherPeers) { MessageBundle bundle = PickPiece(peer, otherPeers, 1); return bundle == null ? null : (RequestMessage)bundle.Messages[0]; }
public override bool ValidatePiece(PeerId peer, int pieceIndex, int startOffset, int length, out Piece piece) { piece = null; return false; }
public override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List<PeerId> otherPeers, int count, int startIndex, int endIndex) { return null; }
public override void CancelRequests(PeerId peer) { }
public override void CancelRequest(PeerId peer, int piece, int startOffset, int length) { }
public virtual RequestMessage ContinueExistingRequest(PeerId peer) { CheckOverriden(); return picker.ContinueExistingRequest(peer); }
public virtual void CancelRequests(PeerId peer) { CheckOverriden(); picker.CancelRequests(peer); }
public virtual void CancelRequest(PeerId peer, int piece, int startOffset, int length) { CheckOverriden(); picker.CancelRequest(peer, piece, startOffset, length); }
public void Setup() { bitfield = new BitField(40).SetAll(true) .Set(4, false) .Set(6, false) .Set(24, false) .Set(36, false); picker = new EndGamePicker(); pieces = new List<Piece>(new Piece[] { new Piece(4, rig.Torrent.PieceLength, rig.Torrent.Size), new Piece(6, rig.Torrent.PieceLength, rig.Torrent.Size), new Piece(24, rig.Torrent.PieceLength, rig.Torrent.Size), new Piece(36, rig.Torrent.PieceLength, rig.Torrent.Size) }); id = new PeerId(new Peer("peerid", new Uri("tcp://weburl.com")), rig.Manager); id.IsChoking = false; id.BitField.SetAll(false); other = new PeerId(new Peer("other", new Uri("tcp://other.com")), rig.Manager); other.IsChoking = false; other.BitField.SetAll(false); }
public void Setup() { requestedUrl.Clear(); partialData = false; int i; for (i = 0; i < 1000; i++) { try { listener = new HttpListener(); listener.Prefixes.Add(string.Format(listenerURL, i)); listener.Start(); break; } catch { } } listener.BeginGetContext(GotContext, null); rig = TestRig.CreateMultiFile(); connection = new HttpConnection(new Uri(string.Format(listenerURL, i))); connection.Manager = rig.Manager; id = new PeerId(new Peer("this is my id", connection.Uri), rig.Manager); id.Connection = connection; id.IsChoking = false; id.AmInterested = true; id.BitField.SetAll(true); id.MaxPendingRequests = numberOfPieces; requests = rig.Manager.PieceManager.Picker.PickPiece(id, new List<PeerId>(), numberOfPieces); }
public void FixtureSetup() { rig = TestRig.CreateMultiFile(); id = new PeerId(new Peer(new string('a', 20), new Uri("tcp://BLAH")), rig.Manager); id.BitField.SetAll(true); }
public void SingleFileTorrent() { rig.Dispose(); rig = TestRig.CreateSingleFile(); string url = rig.Torrent.GetRightHttpSeeds[0]; connection = new HttpConnection(new Uri (url)); connection.Manager = rig.Manager; id = new PeerId(new Peer("this is my id", connection.Uri), rig.Manager); id.Connection = connection; id.IsChoking = false; id.AmInterested = true; id.BitField.SetAll(true); id.MaxPendingRequests = numberOfPieces; requests = rig.Manager.PieceManager.Picker.PickPiece(id, new List<PeerId>(), numberOfPieces); RecieveFirst(); Assert.AreEqual(url, requestedUrl[0]); }
public virtual void Choke(PeerId id) { id.AmChoking = true; id.TorrentManager.UploadingTo--; id.Enqueue(new ChokeMessage()); }
public MessageBundle PickPiece(PeerId peer, List<PeerId> otherPeers, int count) { return PickPiece(peer, peer.BitField, otherPeers, count, 0, peer.BitField.Length); }
public void Setup(bool metadataMode, string metadataPath) { pair = new ConnectionPair(55432); rig = TestRig.CreateSingleFile(1024 * 1024 * 1024, 32768, metadataMode); rig.MetadataPath = metadataPath; rig.RecreateManager(); rig.Manager.HashChecked = true; rig.Manager.Start(); rig.AddConnection(pair.Outgoing); var connection = pair.Incoming; PeerId id = new PeerId(new Peer("", connection.Uri), rig.Manager); id.Connection = connection; byte[] data; EncryptorFactory.EndCheckEncryption(EncryptorFactory.BeginCheckEncryption(id, 68, null, null, new InfoHash[] { id.TorrentManager.InfoHash }), out data); decryptor = id.Decryptor; encryptor = id.Encryptor; }
public virtual MessageBundle PickPiece(PeerId id, BitField peerBitfield, List<PeerId> otherPeers, int count, int startIndex, int endIndex) { CheckOverriden(); return picker.PickPiece(id, peerBitfield, otherPeers, count, startIndex, endIndex); }
public virtual void Setup() { // Yes, this is horrible. Deal with it. rig = TestRig.CreateMultiFile(); peers = new List<PeerId>(); picker = new IgnoringPicker(rig.Manager.Bitfield, new StandardPicker()); picker.Initialise(rig.Manager.Bitfield, rig.Manager.Torrent.Files, new List<Piece>()); peer = new PeerId(new Peer(new string('a', 20), new Uri("tcp://BLAH")), rig.Manager); for (int i = 0; i < 20; i++) { PeerId p = new PeerId(new Peer(new string(i.ToString()[0], 20), new Uri("tcp://" + i)), rig.Manager); p.SupportsFastPeer = true; peers.Add(p); } }
public virtual bool ValidatePiece(PeerId peer, int pieceIndex, int startOffset, int length, out Piece piece) { CheckOverriden(); return picker.ValidatePiece(peer, pieceIndex, startOffset, length, out piece); }