コード例 #1
0
ファイル: HttpTracker.cs プロジェクト: Whinarn/TorrentSwifter
        private static PeerInfo[] DecodePeers(BEncoding.List peerList)
        {
            var peerInfoList = new List <PeerInfo>(peerList.Count);

            foreach (var peer in peerList)
            {
                if (peer is BEncoding.Dictionary)
                {
                    var    peerDict = (peer as BEncoding.Dictionary);
                    byte[] peerIDBytes;
                    if (!peerDict.TryGetByteArray("peer id", out peerIDBytes) && !peerDict.TryGetByteArray("peer_id", out peerIDBytes))
                    {
                        peerIDBytes = null;
                    }

                    string peerIP;
                    long   peerPort;
                    if (peerDict.TryGetString("ip", out peerIP) && peerDict.TryGetInteger("port", out peerPort) && peerPort > 0 && peerPort <= ushort.MaxValue)
                    {
                        IPAddress ipAddress;
                        if (IPAddress.TryParse(peerIP, out ipAddress))
                        {
                            var endPoint = new IPEndPoint(ipAddress, (int)peerPort);
                            if (peerIDBytes != null && peerIDBytes.Length == 20)
                            {
                                var peerID = new PeerID(peerIDBytes);
                                peerInfoList.Add(new PeerInfo(peerID, endPoint));
                            }
                            else
                            {
                                peerInfoList.Add(new PeerInfo(endPoint));
                            }
                        }
                        else
                        {
                            Log.LogError("[HTTP Tracker] Unable to parse peer IP: {0}", peerIP);
                        }
                    }
                }
                else if (peer is byte[])
                {
                    var peerBytes    = peer as byte[];
                    var decodedPeers = DecodePeers(peerBytes);
                    if (decodedPeers != null)
                    {
                        peerInfoList.AddRange(decodedPeers);
                    }
                    else
                    {
                        Log.LogError("[HTTP Tracker] Unable to decode peer information from {0} bytes of compact data.", peerBytes.Length);
                    }
                }
                else
                {
                    Log.LogError("[HTTP Tracker] Unable decode get peer information with invalid BEncode type: {0}", (peer != null ? peer.GetType().Name : "<null>"));
                }
            }

            return(peerInfoList.ToArray());
        }
コード例 #2
0
        internal static bool TryGetPeerClientInfo(PeerID peerID, out string clientName, out string clientVersion)
        {
            clientName    = null;
            clientVersion = null;

            byte[] idBytes = peerID.ID;
            if (idBytes == null || idBytes.Length != 20)
            {
                return(false);
            }

            char[] idChars = new char[20];
            for (int i = 0; i < idChars.Length; i++)
            {
                idChars[i] = (char)idBytes[i];
            }

            if (TryGetAzureusStyleClientInfo(idChars, out clientName, out clientVersion))
            {
                return(true);
            }
            else if (TryGetOtherClientInfo(idChars, out clientName, out clientVersion))
            {
                return(true);
            }
            else if (TryGetShadowStyleClientInfo(idChars, out clientName, out clientVersion))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
コード例 #3
0
 private static Player GetPlayerForPeerID(PeerID who)
 {
     if (player1.NetworkID == who)
     {
         return(player1);
     }
     if (player2.NetworkID == who)
     {
         return(player2);
     }
     return(null);
 }
コード例 #4
0
        /// <summary>
        /// Возвращает словарь параметров.
        /// </summary>
        public override Dictionary <string, string> GetParameters()
        {
            var parameters = base.GetParameters();

            parameters["message_ids"] = String.Join(",", MessageIDs);
            if (PeerID != 0)
            {
                parameters["peer_id"] = PeerID.ToString();
            }
            if (StartMessageID != 0)
            {
                parameters["start_message_id"] = StartMessageID.ToString();
            }

            return(parameters);
        }
コード例 #5
0
        /// <summary>
        /// Creates a new tracker announce request.
        /// </summary>
        /// <param name="infoHash">The info hash.</param>
        /// <param name="peerID">The peer ID.</param>
        /// <param name="port">The port number used to listen for client connections on.</param>
        public AnnounceRequest(InfoHash infoHash, PeerID peerID, int port)
        {
            if (infoHash.Hash == null)
            {
                throw new ArgumentException("The info hash is invalid.", "infoHash");
            }
            else if (peerID.ID == null)
            {
                throw new ArgumentException("The peer ID is invalid.", "infoHash");
            }
            else if (port <= 0 || port > ushort.MaxValue)
            {
                throw new ArgumentOutOfRangeException("port");
            }

            this.infoHash = infoHash;
            this.peerID   = peerID;
            this.port     = port;
        }
コード例 #6
0
        private static void ThreadWorker()
        {
            while (!workerStop)
            {
                var recv = peer.Receive();
                while (recv != null)
                {
                    // store the server's PeerID, so we can pass it to Send() later
                    if (recv.GetCommand() == PacketCommand.NOTIFY_CONNECT_SUCCESS)
                    {
                        serverID = recv.GetSender();
                    }

                    OnMessageReceived?.Invoke(recv);

                    // Keep checking until the queue is empty
                    recv = peer.Receive();
                }

                Thread.Sleep(1);
            }
        }
コード例 #7
0
 /// <summary>
 /// Returns the hash code for this HandshakeMessage instance.
 /// </summary>
 /// <returns>An integer representing the hash code of this instace of the HandshakeMessage class.</returns>
 public override int GetHashCode()
 {
     return(Protocol.GetHashCode() ^ BitConverter.ToString(Reserved).GetHashCode() ^
            BitConverter.ToString(InfoHash).GetHashCode() ^ PeerID.GetHashCode());
 }
コード例 #8
0
 /// <summary>
 /// ResHost初始化
 /// </summary>
 /// <param name="ipep">主机的IPEndPoint</param>
 /// <param name="hostName">主机名</param>
 public ResHost(IPEndPoint ipep, string hostName)
 {
     this.hostIPEP = ipep;
     this.id       = NetUtils.GetIDByAddress(ipep.Address);
     this.hostName = hostName;
 }
コード例 #9
0
ファイル: HostInfo.cs プロジェクト: burstinair/burst.net
 /// <summary>
 /// 初始化HostInfo
 /// </summary>
 public HostInfo()
 {
     this.id            = HoldingServer.Self.id;
     this.SearchService = HoldingServer.Self.searchService;
     this.groups        = HoldingServer.Self.groups.ToArray();
 }