internal ListenManager(ClientEngine engine) { Engine = engine; Listeners = new MonoTorrentCollection<PeerListener>(); endCheckEncryptionCallback = ClientEngine.MainLoop.Wrap(EndCheckEncryption); handshakeReceivedCallback = (a, b, c) => ClientEngine.MainLoop.Queue(() => onPeerHandshakeReceived(a, b, c)); }
internal ListenManager(ClientEngine engine) { Engine = engine; listeners = new MonoTorrentCollection <PeerListener>(); endCheckEncryptionCallback = ClientEngine.MainLoop.Wrap(EndCheckEncryption); handshakeReceivedCallback = (a, b, c) => ClientEngine.MainLoop.Queue(() => onPeerHandshakeReceived(a, b, c)); }
public static MonoTorrentCollection <Peer> Decode(BEncodedList peers) { MonoTorrentCollection <Peer> list = new MonoTorrentCollection <Peer>(peers.Count); foreach (BEncodedValue value in peers) { try { if (value is BEncodedDictionary) { list.Add(DecodeFromDict((BEncodedDictionary)value)); } else if (value is BEncodedString) { foreach (Peer p in Decode((BEncodedString)value)) { list.Add(p); } } } catch { // If something is invalid and throws an exception, ignore it // and continue decoding the rest of the peers } } return(list); }
public static MonoTorrentCollection <Peer> Decode(BEncodedString peers) { // "Compact Response" peers are encoded in network byte order. // IP's are the first four bytes // Ports are the following 2 bytes byte[] byteOrderedData = peers.TextBytes; int i = 0; UInt16 port; StringBuilder sb = new StringBuilder(27); MonoTorrentCollection <Peer> list = new MonoTorrentCollection <Peer>((byteOrderedData.Length / 6) + 1); while ((i + 5) < byteOrderedData.Length) { sb.Remove(0, sb.Length); sb.Append("tcp://"); sb.Append(byteOrderedData[i++]); sb.Append('.'); sb.Append(byteOrderedData[i++]); sb.Append('.'); sb.Append(byteOrderedData[i++]); sb.Append('.'); sb.Append(byteOrderedData[i++]); port = (UInt16)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(byteOrderedData, i)); i += 2; sb.Append(':'); sb.Append(port); Uri uri = new Uri(sb.ToString()); list.Add(new Peer("", uri, EncryptionTypes.All)); } return(list); }
internal static MonoTorrentCollection <int> Calculate(byte[] addressBytes, InfoHash infohash, int count, UInt32 numberOfPieces) { byte[] hashBuffer = new byte[24]; // The hash buffer to be used in hashing MonoTorrentCollection <int> results = new MonoTorrentCollection <int>(count); // The results array which will be returned // 1) Convert the bytes into an int32 and make them Network order int ip = IPAddress.HostToNetworkOrder(BitConverter.ToInt32(addressBytes, 0)); // 2) binary AND this value with 0xFFFFFF00 to select the three most sigificant bytes int ipMostSignificant = (int)(0xFFFFFF00 & ip); // 3) Make ipMostSignificant into NetworkOrder UInt32 ip2 = (UInt32)IPAddress.HostToNetworkOrder(ipMostSignificant); // 4) Copy ip2 into the hashBuffer Buffer.BlockCopy(BitConverter.GetBytes(ip2), 0, hashBuffer, 0, 4); // 5) Copy the infohash into the hashbuffer Buffer.BlockCopy(infohash.Hash, 0, hashBuffer, 4, 20); // 6) Keep hashing and cycling until we have AllowedFastPieceCount number of results // Then return that result while (true) { lock (hasher) hashBuffer = hasher.ComputeHash(hashBuffer); for (int i = 0; i < 20; i += 4) { UInt32 result = (UInt32)IPAddress.HostToNetworkOrder(BitConverter.ToInt32(hashBuffer, i)); result = result % numberOfPieces; if (result > int.MaxValue) { return(results); } results.Add((int)result); if (count == results.Count) { return(results); } } } }
internal static MonoTorrentCollection<int> Calculate(byte[] addressBytes, InfoHash infohash, int count, uint numberOfPieces) { var hashBuffer = new byte[24]; // The hash buffer to be used in hashing var results = new MonoTorrentCollection<int>(count); // The results array which will be returned // 1) Convert the bytes into an int32 and make them Network order var ip = IPAddress.HostToNetworkOrder(BitConverter.ToInt32(addressBytes, 0)); // 2) binary AND this value with 0xFFFFFF00 to select the three most sigificant bytes var ipMostSignificant = (int) (0xFFFFFF00 & ip); // 3) Make ipMostSignificant into NetworkOrder var ip2 = (uint) IPAddress.HostToNetworkOrder(ipMostSignificant); // 4) Copy ip2 into the hashBuffer Buffer.BlockCopy(BitConverter.GetBytes(ip2), 0, hashBuffer, 0, 4); // 5) Copy the infohash into the hashbuffer Buffer.BlockCopy(infohash.Hash, 0, hashBuffer, 4, 20); // 6) Keep hashing and cycling until we have AllowedFastPieceCount number of results // Then return that result while (true) { lock (hasher) hashBuffer = hasher.ComputeHash(hashBuffer); for (var i = 0; i < 20; i += 4) { var result = (uint) IPAddress.HostToNetworkOrder(BitConverter.ToInt32(hashBuffer, i)); result = result%numberOfPieces; if (result > int.MaxValue) return results; results.Add((int) result); if (count == results.Count) return results; } } }
internal PeerId(Peer peer, TorrentManager manager) { Peer = peer ?? throw new ArgumentNullException(nameof(peer)); TorrentManager = manager; SuggestedPieces = new MonoTorrentCollection <int>(); AmChoking = true; IsChoking = true; IsAllowedFastPieces = new MonoTorrentCollection <int>(); AmAllowedFastPieces = new MonoTorrentCollection <int>(); MaxPendingRequests = 2; MaxSupportedPendingRequests = 50; Monitor = new ConnectionMonitor(); sendQueue = new MonoTorrentCollection <PeerMessage>(12); ExtensionSupports = new ExtensionSupports(); InitializeTyrant(); }
public static MonoTorrentCollection<Peer> Decode(BEncodedList peers) { var list = new MonoTorrentCollection<Peer>(peers.Count); foreach (var value in peers) { try { if (value is BEncodedDictionary) list.Add(DecodeFromDict((BEncodedDictionary) value)); else if (value is BEncodedString) foreach (var p in Decode((BEncodedString) value)) list.Add(p); } catch { // If something is invalid and throws an exception, ignore it // and continue decoding the rest of the peers } } return list; }
internal PeerId(Peer peer, TorrentManager manager) { if (peer == null) throw new ArgumentNullException(nameof(peer)); SuggestedPieces = new MonoTorrentCollection<int>(); AmChoking = true; IsChoking = true; IsAllowedFastPieces = new MonoTorrentCollection<int>(); AmAllowedFastPieces = new MonoTorrentCollection<int>(); LastMessageReceived = DateTime.Now; LastMessageSent = DateTime.Now; Peer = peer; MaxPendingRequests = 2; MaxSupportedPendingRequests = 50; Monitor = new ConnectionMonitor(); _sendQueue = new MonoTorrentCollection<PeerMessage>(12); ExtensionSupports = new ExtensionSupports(); TorrentManager = manager; InitializeTyrant(); }
internal PeerId(Peer peer, TorrentManager manager) { if (peer == null) { throw new ArgumentNullException("peer"); } this.suggestedPieces = new MonoTorrentCollection <int>(); this.amChoking = true; this.isChoking = true; this.isAllowedFastPieces = new MonoTorrentCollection <int>(); this.amAllowedFastPieces = new MonoTorrentCollection <int>(); this.lastMessageReceived = DateTime.Now; this.lastMessageSent = DateTime.Now; this.peer = peer; this.maxPendingRequests = 2; this.maxSupportedPendingRequests = 50; this.monitor = new ConnectionMonitor(); this.sendQueue = new MonoTorrentCollection <PeerMessage>(12); ExtensionSupports = new ExtensionSupports(); TorrentManager = manager; InitializeTyrant(); }
internal PeerId(Peer peer, TorrentManager manager) { LastReviewUploadRate = 0; LastReviewDownloadRate = 0; BytesUploadedAtLastReview = 0; BytesDownloadedAtLastReview = 0; if (peer == null) throw new ArgumentNullException("peer"); suggestedPieces = new MonoTorrentCollection<int>(); amChoking = true; isChoking = true; IsAllowedFastPieces = new MonoTorrentCollection<int>(); AmAllowedFastPieces = new MonoTorrentCollection<int>(); lastMessageReceived = DateTime.Now; lastMessageSent = DateTime.Now; this.peer = peer; MaxPendingRequests = 2; MaxSupportedPendingRequests = 50; monitor = new ConnectionMonitor(); sendQueue = new MonoTorrentCollection<PeerMessage>(12); ExtensionSupports = new ExtensionSupports(); TorrentManager = manager; InitializeTyrant(); }
internal ListenManager(ClientEngine engine) { Engine = engine; Listeners = new MonoTorrentCollection <PeerListener>(); }
public ClientEngine(EngineSettings settings, PeerListener listener, PieceWriter writer) { Check.Settings(settings); Check.Listener(listener); Check.Writer(writer); this.listener = listener; this.settings = settings; this.connectionManager = new ConnectionManager(this); RegisterDht (new NullDhtEngine()); this.diskManager = new DiskManager(this, writer); this.listenManager = new ListenManager(this); MainLoop.QueueTimeout(TimeSpan.FromMilliseconds(TickLength), delegate { if (IsRunning && !disposed) LogicTick(); return !disposed; }); this.torrents = new MonoTorrentCollection<TorrentManager>(); CreateRateLimiters(); this.peerId = GeneratePeerId(); localPeerListener = new LocalPeerListener(this); localPeerManager = new LocalPeerManager(); LocalPeerSearchEnabled = SupportsLocalPeerDiscovery; listenManager.Register(listener); // This means we created the listener in the constructor if (listener.Endpoint.Port == 0) listener.ChangeEndpoint(new IPEndPoint(IPAddress.Any, settings.ListenPort)); }
internal PeerId(Peer peer, TorrentManager manager) { if (peer == null) throw new ArgumentNullException("peer"); this.suggestedPieces = new MonoTorrentCollection<int>(); this.amChoking = true; this.isChoking = true; this.isAllowedFastPieces = new MonoTorrentCollection<int>(); this.amAllowedFastPieces = new MonoTorrentCollection<int>(); this.lastMessageReceived = DateTime.Now; this.lastMessageSent = DateTime.Now; this.peer = peer; this.monitor = new ConnectionMonitor(); this.sendQueue = new MonoTorrentCollection<PeerMessage>(12); TorrentManager = manager; InitializeTyrant(); }
///<summary> /// Calculates the approximate size of the final .torrent in bytes ///</summary> public long GetSize() { var paths = new MonoTorrentCollection<string>(); if (Directory.Exists(path)) GetAllFilePaths(path, paths); else if (File.Exists(path)) paths.Add(path); else return 64*1024; long size = 0; for (var i = 0; i < paths.Count; i++) size += new FileInfo(paths[i]).Length; return size; }
/// <summary> /// /// </summary> /// <param name="settings"></param> public ConnectionManager(ClientEngine engine) { this.engine = engine; this.endCheckEncryptionCallback = delegate(IAsyncResult result) { ClientEngine.MainLoop.Queue(delegate { EndCheckEncryption(result); }); }; this.endSendMessageCallback = delegate(bool s, int c, object o) { ClientEngine.MainLoop.Queue(delegate { EndSendMessage(s, c, o); }); }; this.endCreateConnectionCallback = delegate(bool succeeded, object state) { ClientEngine.MainLoop.Queue(delegate { EndCreateConnection(succeeded, state); }); }; this.incomingConnectionAcceptedCallback = delegate(bool s, int c, object o) { ClientEngine.MainLoop.Queue(delegate { IncomingConnectionAccepted(s, c, o); }); }; this.bitfieldSentCallback = new MessagingCallback(PeerBitfieldSent); this.handshakeSentCallback = new MessagingCallback(this.PeerHandshakeSent); this.handshakeReceievedCallback = delegate(bool s, int c, object o) { ClientEngine.MainLoop.Queue(delegate { PeerHandshakeReceived(s, c, o); }); }; this.messageSentCallback = new MessagingCallback(this.PeerMessageSent); this.torrents = new MonoTorrentCollection<TorrentManager>(); }
public static MonoTorrentCollection<Peer> Decode(BEncodedString peers) { // "Compact Response" peers are encoded in network byte order. // IP's are the first four bytes // Ports are the following 2 bytes byte[] byteOrderedData = peers.TextBytes; int i = 0; UInt16 port; StringBuilder sb = new StringBuilder(27); MonoTorrentCollection<Peer> list = new MonoTorrentCollection<Peer>((byteOrderedData.Length / 6) + 1); while ((i + 5) < byteOrderedData.Length) { sb.Remove(0, sb.Length); sb.Append("tcp://"); sb.Append(byteOrderedData[i++]); sb.Append('.'); sb.Append(byteOrderedData[i++]); sb.Append('.'); sb.Append(byteOrderedData[i++]); sb.Append('.'); sb.Append(byteOrderedData[i++]); port = (UInt16)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(byteOrderedData, i)); i += 2; sb.Append(':'); sb.Append(port); Uri uri = new Uri(sb.ToString()); list.Add(new Peer("", uri, EncryptionTypes.All)); } return list; }
public void UnsupportedTrackers () { MonoTorrentCollection<string> tier = new MonoTorrentCollection<string> (); tier.Add ("fake://123.123.123.2:5665"); rig.Torrent.AnnounceUrls.Add (tier); TorrentManager manager = new TorrentManager (rig.Torrent, "", new TorrentSettings()); foreach (MonoTorrent.Client.Tracker.TrackerTier t in manager.TrackerManager) { Assert.IsTrue (t.Trackers.Count > 0, "#1"); } }
void GetAllFilePaths(string directory, MonoTorrentCollection<string> paths) { var subs = Directory.GetDirectories(directory); foreach (var path in subs) { if (ignoreHiddenFiles) { var info = new DirectoryInfo(path); if ((info.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue; } GetAllFilePaths(path, paths); } subs = Directory.GetFiles(directory); foreach (var path in subs) { if (ignoreHiddenFiles) { var info = new FileInfo(path); if ((info.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue; } paths.Add(path); } }
public ClientEngine(EngineSettings settings, PeerListener listener, PieceWriter writer) { Check.Settings(settings); Check.Listener(listener); Check.Writer(writer); this.listener = listener; this.settings = settings; this.connectionManager = new ConnectionManager(this); this.dhtListener = new UdpListener(new IPEndPoint(IPAddress.Any, settings.ListenPort)); this.dhtEngine = new DhtEngine(dhtListener); this.diskManager = new DiskManager(this, writer); this.listenManager = new ListenManager(this); MainLoop.QueueTimeout(TimeSpan.FromMilliseconds(TickLength), delegate { if (IsRunning && !disposed) LogicTick(); return !disposed; }); this.torrents = new MonoTorrentCollection<TorrentManager>(); this.downloadLimiter = new RateLimiter(); this.uploadLimiter = new RateLimiter(); this.peerId = GeneratePeerId(); listenManager.Register(listener); dhtEngine.StateChanged += delegate { if (dhtEngine.State != State.Ready) return; MainLoop.Queue(delegate { foreach (TorrentManager manager in torrents) { if (!manager.CanUseDht) continue; dhtEngine.Announce(manager.Torrent.infoHash, Listener.Endpoint.Port); dhtEngine.GetPeers(manager.Torrent.infoHash); } }); }; // This means we created the listener in the constructor if (listener.Endpoint.Port == 0) listener.ChangeEndpoint(new IPEndPoint(IPAddress.Any, settings.ListenPort)); listener.Start(); }