示例#1
0
        public bool AnnounceTCP(Int32 num_want = -1, Int64 downloaded = 0, Int64 left = 0, Int64 uploaded = 0)
        {
            if (!ConnectTCP())
            {
                return(false);
            }

            try
            {
                byte[] sendBuff = System.Text.Encoding.UTF8.GetBytes($"GET {uri.AbsolutePath}?info_hash={Utils.StringHexToUrlEncode(options.InfoHash)}&peer_id={Utils.StringHexToUrlEncode(BitConverter.ToString(options.PeerId).Replace("-",""))}&port=11111&left={left}&downloaded={downloaded}&uploaded={uploaded}&event=started&compact=1&numwant={num_want} HTTP/1.1\r\nHost: {host}:{port}\r\nConection: close\r\n\r\n");

                if (type == Type.HTTP)
                {
                    netStream.Write(sendBuff, 0, sendBuff.Length);
                }
                else
                {
                    sslStream.Write(sendBuff, 0, sendBuff.Length);
                }

                if (!ReceiveTCP())
                {
                    return(false);
                }

                BencodeParser parser = new BencodeParser();
                BDictionary   extDic = parser.Parse <BDictionary>(recvBuff);

                byte[] hashBytes = ((BString)extDic["peers"]).Value.ToArray();
                Dictionary <string, int> peers = new Dictionary <string, int>();

                for (int i = 0; i < hashBytes.Length; i += 6)
                {
                    IPAddress curIP   = new IPAddress(Utils.ArraySub(ref hashBytes, (uint)i, 4, false));
                    UInt16    curPort = (UInt16)BitConverter.ToInt16(Utils.ArraySub(ref hashBytes, (uint)i + 4, 2, true), 0);
                    if (curPort > 0)
                    {
                        peers[curIP.ToString()] = curPort;
                    }
                }

                if (options.Verbosity > 0)
                {
                    Log($"Success ({peers.Count} Peers)");
                }

                if (peers.Count > 0)
                {
                    Beggar.FillPeers(peers, BitSwarm.PeersStorage.TRACKER);
                }
            }
            catch (Exception e)
            {
                Log($"Failed {e.Message}\r\n{e.StackTrace}");
                return(false);
            }

            return(true);
        }