private void DatagramSocketOnMessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args) { try { var dataReader = args.GetDataReader(); var receiveBytes = new byte[dataReader.UnconsumedBufferLength]; dataReader.ReadBytes(receiveBytes); var receiveString = Encoding.ASCII.GetString(receiveBytes); var exp = new Regex( "BT-SEARCH \\* HTTP/1.1\\r\\nHost: 239.192.152.143:6771\\r\\nPort: (?<port>[^@]+)\\r\\nInfohash: (?<hash>[^@]+)\\r\\n\\r\\n\\r\\n"); var match = exp.Match(receiveString); if (!match.Success) { return; } var portcheck = Convert.ToInt32(match.Groups["port"].Value); if (portcheck < 0 || portcheck > 65535) { return; } TorrentManager manager = null; var matchHash = InfoHash.FromHex(match.Groups["hash"].Value); for (var i = 0; manager == null && i < _engine.Torrents.Count; i++) { if (_engine.Torrents[i].InfoHash == matchHash) { manager = _engine.Torrents[i]; } } if (manager == null) { return; } var uri = new Uri("tcp://" + args.RemoteAddress.RawName + ':' + match.Groups["port"].Value); var peer = new Peer("", uri, EncryptionTypes.All); // Add new peer to matched Torrent if (manager.HasMetadata && manager.Torrent.IsPrivate) { return; } ClientEngine.MainLoop.Queue(delegate { var count = manager.AddPeersCore(peer); manager.RaisePeersFound(new LocalPeersAdded(manager, count, 1)); }); } catch { // ignored } }
private void OnAnnounceComplete(object sender, AnnounceResponseEventArgs e) { try { this.updateSucceeded = e.Successful; if (manager.Engine == null) { e.Id.WaitHandle.Set(); return; } if (e.Successful) { manager.Peers.BusyPeers.Clear(); int count = manager.AddPeersCore(e.Peers); manager.RaisePeersFound(new TrackerPeersAdded(manager, count, e.Peers.Count, e.Tracker)); TrackerTier tier = trackerTiers.Find(delegate(TrackerTier t) { return(t.Trackers.Contains(e.Tracker)); }); if (tier != null) { Toolbox.Switch <Tracker>(tier.Trackers, 0, tier.IndexOf(e.Tracker)); Toolbox.Switch <TrackerTier>(trackerTiers, 0, trackerTiers.IndexOf(tier)); } e.Id.WaitHandle.Set(); } else { TrackerTier tier; Tracker tracker; if (!e.Id.TrySubsequent || !GetNextTracker(e.Tracker, out tier, out tracker)) { e.Id.WaitHandle.Set(); } else { Announce(tracker, e.Id.TorrentEvent, true, e.Id.WaitHandle); } } } catch (ObjectDisposedException) { } }
private void OnAnnounceComplete(object sender, AnnounceResponseEventArgs e) { _updateSucceeded = e.Successful; if (_manager.Engine == null) { e.Id.WaitHandle.Set(); return; } if (e.Successful) { _manager.Peers.BusyPeers.Clear(); var count = _manager.AddPeersCore(e.Peers); _manager.RaisePeersFound(new TrackerPeersAdded(_manager, count, e.Peers.Count, e.Tracker)); var tier = _trackerTiers.Find(t => t.Trackers.Contains(e.Tracker)); if (tier != null) { Toolbox.Switch(tier.Trackers, 0, tier.IndexOf(e.Tracker)); Toolbox.Switch(_trackerTiers, 0, _trackerTiers.IndexOf(tier)); } e.Id.WaitHandle.Set(); } else { TrackerTier tier; Tracker tracker; if (!e.Id.TrySubsequent || !GetNextTracker(e.Tracker, out tier, out tracker)) { e.Id.WaitHandle.Set(); } else { Announce(tracker, e.Id.TorrentEvent, true, e.Id.WaitHandle); } } }