internal PeerConnectionEventArgs(TorrentManager manager, PeerId id, Direction direction, string message) : base(manager) { PeerID = id; ConnectionDirection = direction; Message = message; }
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; }
public EndGamePickerTests() { rig = TestRig.CreateMultiFile(); 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[] { 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 override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List<PeerId> otherPeers, int count, int startIndex, int endIndex) { // Only request 2 pieces at a time in endgame mode // to prevent a *massive* overshoot if (id.IsChoking || id.AmRequestingPiecesCount > 2) return null; LoadPieces(id, peerBitfield); // 1) See if there are any blocks which have not been requested at all. Request the block if the peer has it foreach (var p in pieces) { if (!peerBitfield[p.Index] || p.AllBlocksRequested) continue; for (var i = 0; i < p.BlockCount; i++) { if (p.Blocks[i].Requested) continue; p.Blocks[i].Requested = true; var request = new Request(id, p.Blocks[i]); requests.Add(request); return new MessageBundle(request.Block.CreateRequest(id)); } } // 2) For each block with an existing request, add another request. We do a search from the start // of the list to the end. So when we add a duplicate request, move both requests to the end of the list foreach (var p in pieces) { if (!peerBitfield[p.Index]) continue; for (var i = 0; i < p.BlockCount; i++) { if (p.Blocks[i].Received || AlreadyRequested(p.Blocks[i], id)) continue; var c = requests.Count; for (var j = 0; j < requests.Count - 1 && (c-- > 0); j++) { if (requests[j].Block.PieceIndex == p.Index && requests[j].Block.StartOffset == p.Blocks[i].StartOffset) { var r = requests[j]; requests.RemoveAt(j); requests.Add(r); j--; } } p.Blocks[i].Requested = true; var request = new Request(id, p.Blocks[i]); requests.Add(request); return new MessageBundle(request.Block.CreateRequest(id)); } } return null; }
/// <summary> /// Creates a new PeerMessageEventArgs /// </summary> /// <param name="message">The peer message involved</param> /// <param name="direction">The direction of the message</param> internal PeerMessageEventArgs(TorrentManager manager, PeerMessage message, Direction direction, PeerId id) : base(manager) { Direction = direction; ID = id; Message = message; }
internal PeerConnectionEventArgs(TorrentManager manager, PeerId id, Direction direction, String message) : base(manager) { peerConnectionId = id; connectionDirection = direction; this.message = message; }
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); }
/// <summary> /// Creates a new PeerMessageEventArgs /// </summary> /// <param name="message">The peer message involved</param> /// <param name="direction">The direction of the message</param> internal PeerMessageEventArgs(TorrentManager manager, PeerMessage message, Direction direction, PeerId id) : base(manager) { this.direction = direction; this.id = id; this.message = message; }
public EncryptorAsyncResult(PeerId id, AsyncCallback callback, object state) : base(callback, state) { Id = id; Decryptor = new PlainTextEncryption(); Encryptor = new PlainTextEncryption(); }
protected override void AppendBitfieldMessage(PeerId id, MessageBundle bundle) { if (id.SupportsFastPeer) bundle.Messages.Add(new HaveNoneMessage()); else bundle.Messages.Add(new BitfieldMessage(zero)); }
public SeededPiece(PeerId peer, int index, int totalBlocks) { Index = index; Peer = peer; SeededAt = DateTime.Now; TotalBlocks = totalBlocks; }
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); id.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(delegate { Engine.ConnectionManager.ProcessFreshConnection(id); }); } }
private void NextPeer() { var flag = false; foreach (var id in Manager.Peers.ConnectedPeers) { if (id.SupportsLTMessages && id.ExtensionSupports.Supports(LTMetadata.Support.Name)) { if (id == currentId) flag = true; else if (flag) { currentId = id; return; } } } //second pass without removing the currentid and previous ones foreach (var id in Manager.Peers.ConnectedPeers) { if (id.SupportsLTMessages && id.ExtensionSupports.Supports(LTMetadata.Support.Name)) { currentId = id; return; } } currentId = null; }
public virtual void Unchoke(PeerId id) { id.AmChoking = false; id.TorrentManager.UploadingTo++; id.Enqueue(new UnchokeMessage()); id.LastUnchoked = DateTime.Now; }
public void Setup() { rig.Manager.UploadingTo = 0; rig.Manager.Settings.UploadSlots = 4; peer = rig.CreatePeer(true); unchoker = new InitialSeedUnchoker(rig.Manager); unchoker.PeerConnected(peer); }
public PeerExchangePeersAdded(TorrentManager manager, int count, int total, PeerId id) :base(manager, count, total) { if (id == null) throw new ArgumentNullException("id"); this.id = id; }
public override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List<PeerId> otherPeers, int count, int startIndex, int endIndex) { var bundle = ActivePicker.PickPiece(id, peerBitfield, otherPeers, count, startIndex, endIndex); if (bundle == null && TryEnableEndgame()) return ActivePicker.PickPiece(id, peerBitfield, otherPeers, count, startIndex, endIndex); return bundle; }
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 PeerExchangeManager(PeerId id) { this.id = id; this.addedPeers = new List<Peer>(); this.droppedPeers = new List<Peer>(); id.TorrentManager.OnPeerFound += new EventHandler<PeerAddedEventArgs>(OnAdd); Start(); }
public override void CancelRequest(PeerId peer, int piece, int startOffset, int length) { CancelWhere(delegate (Request r) { return r.Block.PieceIndex == piece && r.Block.StartOffset == startOffset && r.Block.RequestLength == length && peer.Equals(r.Peer); }); }
internal PeerExchangeManager(PeerId id) { this.id = id; addedPeers = new List<Peer>(); droppedPeers = new List<Peer>(); id.TorrentManager.OnPeerFound += OnAdd; Start(); }
internal Block(Piece piece, int startOffset, int requestLength) { requestedOff = null; this.piece = piece; received = false; requested = false; this.requestLength = requestLength; this.startOffset = startOffset; written = false; }
public RandomisedPickerTests() { rig = TestRig.CreateMultiFile(); id = new PeerId(new Peer(new string('a', 20), new Uri("tcp://BLAH")), rig.Manager); for (var i = 0; i < id.BitField.Length; i += 2) id.BitField[i] = true; tester = new TestPicker(); picker = new RandomisedPicker(tester); }
public void HandleMessage(PeerId id, PeerMessage message) { if (message is IFastPeerMessage && !id.SupportsFastPeer) throw new MessageException("Peer shouldn't support fast peer messages"); if (message is ExtensionMessage && !id.SupportsLTMessages && !(message is ExtendedHandshakeMessage)) throw new MessageException("Peer shouldn't support extension messages"); if (message is HaveMessage) HandleHaveMessage(id, (HaveMessage)message); else if (message is RequestMessage) HandleRequestMessage(id, (RequestMessage)message); else if (message is PortMessage) HandlePortMessage(id, (PortMessage)message); else if (message is PieceMessage) HandlePieceMessage(id, (PieceMessage)message); else if (message is NotInterestedMessage) HandleNotInterested(id, (NotInterestedMessage)message); else if (message is KeepAliveMessage) HandleKeepAliveMessage(id, (KeepAliveMessage)message); else if (message is InterestedMessage) HandleInterestedMessage(id, (InterestedMessage)message); else if (message is ChokeMessage) HandleChokeMessage(id, (ChokeMessage)message); else if (message is CancelMessage) HandleCancelMessage(id, (CancelMessage)message); else if (message is BitfieldMessage) HandleBitfieldMessage(id, (BitfieldMessage)message); else if (message is UnchokeMessage) HandleUnchokeMessage(id, (UnchokeMessage)message); else if (message is HaveAllMessage) HandleHaveAllMessage(id, (HaveAllMessage)message); else if (message is HaveNoneMessage) HandleHaveNoneMessage(id, (HaveNoneMessage)message); else if (message is RejectRequestMessage) HandleRejectRequestMessage(id, (RejectRequestMessage)message); else if (message is SuggestPieceMessage) HandleSuggestedPieceMessage(id, (SuggestPieceMessage)message); else if (message is AllowedFastMessage) HandleAllowedFastMessage(id, (AllowedFastMessage)message); else if (message is ExtendedHandshakeMessage) HandleExtendedHandshakeMessage(id, (ExtendedHandshakeMessage)message); else if (message is LTMetadata) HandleLtMetadataMessage(id, (LTMetadata)message); else if (message is LTChat) HandleLtChat(id, (LTChat)message); else if (message is PeerExchangeMessage) HandlePeerExchangeMessage(id, (PeerExchangeMessage)message); else if (message is HandshakeMessage) HandleHandshakeMessage(id, (HandshakeMessage)message); else if (message is ExtensionMessage) HandleGenericExtensionMessage(id, (ExtensionMessage)message); else throw new MessageException(string.Format("Unsupported message found: {0}", message.GetType().Name)); }
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 override void Choke(PeerId id) { base.Choke(id); advertisedPieces.RemoveAll(delegate(SeededPiece p) { return p.Peer == id; }); // Place the peer at the end of the list so the rest of the peers // will get an opportunity to unchoke before this peer gets tried again var data = peers.Find(delegate(ChokeData d) { return d.Peer == id; }); peers.Remove(data); peers.Add(data); }
internal static IAsyncResult BeginCheckEncryption(PeerId id, AsyncCallback callback, object state, byte[][] sKeys) { EncryptorAsyncResult result = new EncryptorAsyncResult(id, callback, state); result.SKeys = sKeys; IConnection c = id.Connection; 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[id.BytesToRecieve]; NetworkIO.EnqueueReceive(c, result.Buffer, 0, result.Buffer.Length, 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.Torrent.infoHash, usable); result.EncSocket.BeginHandshake(id.Connection, CompletedEncryptedHandshakeCallback, result); } else { result.Complete(); } } else { if (hasPlainText) { result.Complete(); } else { result.EncSocket = new PeerAEncryption(id.TorrentManager.Torrent.infoHash, usable); result.EncSocket.BeginHandshake(id.Connection, CompletedEncryptedHandshakeCallback, result); } } } } catch (Exception ex) { result.Complete(ex); } return result; }
internal HdknPeer(PeerId peer) { _peer = peer; new Task(() => { try { _reverseDns = Dns.GetHostEntry(_peer.Uri.Host).HostName; } catch (SocketException) { } }).Start(); }
public PriorityPickerTests() { rig = TestRig.CreateMultiFile(); id = new PeerId(new Peer(new string('a', 20), new Uri("tcp://BLAH")), rig.Manager); id.BitField.SetAll(true); id.BitField.SetAll(true); tester = new TestPicker(); picker = new PriorityPicker(tester); picker.Initialise(rig.Manager.Bitfield, rig.Torrent.Files, new List<Piece>()); foreach (var file in rig.Torrent.Files) file.Priority = Priority.Normal; }
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 virtual RequestMessage ContinueExistingRequest(PeerId peer) { CheckOverriden(); return(picker.ContinueExistingRequest(peer)); }
public MessageBundle PickPiece(PeerId peer, List <PeerId> otherPeers, int count) { return(PickPiece(peer, peer.BitField, otherPeers, count, 0, peer.BitField.Length)); }
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)); }
public virtual void CancelRequest(PeerId peer, int piece, int startOffset, int length) { CheckOverriden(); picker.CancelRequest(peer, piece, startOffset, length); }
public override void HandlePeerConnected(PeerId id, MonoTorrent.Common.Direction direction) { id.CloseConnection(); }
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 RequestMessage PickPiece(PeerId peer, List <PeerId> otherPeers) { MessageBundle bundle = PickPiece(peer, otherPeers, 1); return(bundle == null ? null : (RequestMessage)bundle.Messages[0]); }
public virtual void CancelRequests(PeerId peer) { CheckOverriden(); picker.CancelRequests(peer); }