public static BitTorrentCollection<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);
            BitTorrentCollection<Peer> list = new BitTorrentCollection<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 ListenManager(ClientEngine engine)
 {
     Engine    = engine;
     listeners = new BitTorrentCollection <PeerListener>();
     endCheckEncryptionCallback = ClientEngine.MainLoop.Wrap(EndCheckEncryption);
     handshakeReceivedCallback  = (a, b, c) => ClientEngine.MainLoop.Queue(() => onPeerHandshakeReceived(a, b, c));
 }
        internal static BitTorrentCollection <int> Calculate(byte[] addressBytes, InfoHash infohash, int count, UInt32 numberOfPieces)
        {
            byte[] hashBuffer = new byte[24];                                           // The hash buffer to be used in hashing
            BitTorrentCollection <int> results = new BitTorrentCollection <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);
                    }
                }
            }
        }
 public static BitTorrentCollection<Peer> Decode(BEncodedList peers)
 {
     BitTorrentCollection<Peer> list = new BitTorrentCollection<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;
 }
Exemple #5
0
        internal PeerId(Peer peer, TorrentManager manager)
        {
            if (peer == null)
            {
                throw new ArgumentNullException("peer");
            }

            this.suggestedPieces = new BitTorrentCollection <int>();
            this.amChoking       = true;
            this.isChoking       = true;

            this.isAllowedFastPieces = new BitTorrentCollection <int>();
            this.amAllowedFastPieces = new BitTorrentCollection <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 BitTorrentCollection <PeerMessage>(12);
            ExtensionSupports = new ExtensionSupports();
            TorrentManager    = manager;
            InitializeTyrant();
        }