예제 #1
0
        public TrackerResponse Announce()
        {
            for (int i = 0; i < Trackers.Count; i++)
            {
                Tracker         tracker  = Trackers[i];
                TrackerResponse response = tracker.Announce(_torrent);

                if (!response.Success)
                {
                    continue;
                }
                if (i == 0)
                {
                    return(response);
                }
                Trackers.RemoveAt(i);
                Trackers.Insert(0, tracker);
                return(response);
            }
            return(new TrackerResponse("Tier Error: No trackers responded"));
        }
예제 #2
0
        public TrackerResponse Announce(Torrent torrent)
        {
            string url = AnnounceUrl;

            UriHelper.AddParam(ref url, "info_hash", UriHelper.UrlEncode(torrent.InfoHashBytes));
            UriHelper.AddParam(ref url, "peer_id", _client.PeerId);
            if (_client.ExternalIp != null)
            {
                UriHelper.AddParam(ref url, "ip", _client.ExternalIp);
            }
            UriHelper.AddParam(ref url, "port", _client.ExternalPort.ToString());
            UriHelper.AddParam(ref url, "uploaded", "0");
            UriHelper.AddParam(ref url, "downloaded", "0");
            UriHelper.AddParam(ref url, "left", torrent.TotalSize.ToString());
            UriHelper.AddParam(ref url, "compact", Compact ? "1" : "0");
            UriHelper.AddParam(ref url, "event", "started");
            if (TrackerId != null)
            {
                UriHelper.AddParam(ref url, "trackerid", TrackerId);
            }

            HttpRequestMessage  request     = new HttpRequestMessage(HttpMethod.Get, url);
            HttpResponseMessage rawResponse = Task.Run(() => _httpClient.SendAsync(request)).Result;

            byte[] rawBytes = Task.Run(() => rawResponse.Content.ReadAsByteArrayAsync()).Result;

            if (!rawResponse.IsSuccessStatusCode)
            {
                return(new TrackerResponse(
                           $"HTTP Error - {rawResponse.StatusCode}: {rawResponse.ReasonPhrase} - {Encoding.UTF8.GetString(rawBytes)}"));
            }

            TrackerResponse response = new TrackerResponse((BDictionary)_client.BencodeParser.Parse(rawBytes));

            if (response.TrackerId != null)
            {
                TrackerId = response.TrackerId;
            }
            return(response);
        }