示例#1
0
        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;
        }
示例#2
0
 internal void Enqueue(PeerMessage msg)
 {
     sendQueue.Add(msg);
     if (!processingQueue)
     {
         processingQueue = true;
         ConnectionManager.ProcessQueue(this);
     }
 }
示例#3
0
 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;
 }
        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);
                    }
                }
            }
        }
        private void SendMessage()
        {
            SendDetails?send = null;

            if (CanSend)
            {
                send = sendQueue.Dequeue();
            }

            if (send != null)
            {
                SendMessage(send.Value.Message, send.Value.Destination);
                SendDetails details = send.Value;
                details.SentAt = DateTime.UtcNow;
                if (details.Message is QueryMessage)
                {
                    waitingResponse.Add(details);
                }
            }
        }