public override async void Announce(AnnounceParameters parameters, object state) { try { await _semaphoreSlim.WaitAsync(); try { if (!_hasConnected) { await ConnectAsync(); } await DoAnnounceAsync(parameters, state); } finally { _semaphoreSlim.Release(); } } catch (Exception ex) { FailureMessage = ex.Message; DoAnnounceComplete(false, state, new List <Peer>()); } }
public override async void Announce(AnnounceParameters parameters, object state) { var peers = new List <Peer>(); var requestUri = CreateAnnounceString(parameters); try { using (var httpClient = new HttpClient()) { httpClient.Timeout = RequestTimeout; var request = new HttpRequestMessage(HttpMethod.Get, requestUri); request.Headers.UserAgent.ParseAdd(VersionInfo.ClientVersion); RaiseBeforeAnnounce(); var httpResponseMessage = await httpClient.SendAsync(request); if (httpResponseMessage.StatusCode == HttpStatusCode.OK) { FailureMessage = ""; WarningMessage = ""; try { var stream = await httpResponseMessage.Content.ReadAsStreamAsync(); if (stream.Length > 0L && parameters.ClientEvent != TorrentEvent.Stopped) { HandleAnnounce(DecodeResponse(stream), peers); } Status = TrackerState.Ok; } catch { Status = TrackerState.InvalidResponse; FailureMessage = "Failed to open tracker response."; } } else { FailureMessage = string.Format("The tracker could not be contacted. {0}", httpResponseMessage.StatusCode); Status = TrackerState.Offline; } } } catch (WebException) { Status = TrackerState.Offline; FailureMessage = "The tracker could not be contacted"; } catch { Status = TrackerState.InvalidResponse; FailureMessage = "The tracker returned an invalid or incomplete response"; } finally { RaiseAnnounceComplete(new AnnounceResponseEventArgs(this, state, string.IsNullOrEmpty(FailureMessage), peers)); } }
public override async void Announce(AnnounceParameters parameters, object state) { var peers = new List<Peer>(); var requestUri = CreateAnnounceString(parameters); try { using (var httpClient = new HttpClient()) { httpClient.Timeout = RequestTimeout; var request = new HttpRequestMessage(HttpMethod.Get, requestUri); request.Headers.UserAgent.ParseAdd(VersionInfo.ClientVersion); RaiseBeforeAnnounce(); var httpResponseMessage = await httpClient.SendAsync(request); if (httpResponseMessage.StatusCode == HttpStatusCode.OK) { FailureMessage = ""; WarningMessage = ""; try { var stream = await httpResponseMessage.Content.ReadAsStreamAsync(); if (stream.Length > 0L && parameters.ClientEvent != TorrentEvent.Stopped) HandleAnnounce(DecodeResponse(stream), peers); Status = TrackerState.Ok; } catch { Status = TrackerState.InvalidResponse; FailureMessage = "Failed to open tracker response."; } } else { FailureMessage = string.Format("The tracker could not be contacted. {0}", httpResponseMessage.StatusCode); Status = TrackerState.Offline; } } } catch (WebException) { Status = TrackerState.Offline; FailureMessage = "The tracker could not be contacted"; } catch { Status = TrackerState.InvalidResponse; FailureMessage = "The tracker returned an invalid or incomplete response"; } finally { RaiseAnnounceComplete(new AnnounceResponseEventArgs(this, state, string.IsNullOrEmpty(FailureMessage), peers)); } }
private Uri CreateAnnounceString(AnnounceParameters parameters) { var b = new UriQueryBuilder(Uri); b.Add("info_hash", parameters.InfoHash.UrlEncode()) .Add("peer_id", parameters.PeerId) .Add("port", parameters.Port) .Add("uploaded", parameters.BytesUploaded) .Add("downloaded", parameters.BytesDownloaded) .Add("left", parameters.BytesLeft) .Add("compact", 1) .Add("numwant", 100); if (parameters.SupportsEncryption) { b.Add("supportcrypto", 1); } if (parameters.RequireEncryption) { b.Add("requirecrypto", 1); } if (!b.Contains("key")) { b.Add("key", Key); } if (!string.IsNullOrEmpty(parameters.Ipaddress)) { b.Add("ip", parameters.Ipaddress); } // If we have not successfully sent the started event to this tier, override the passed in started event // Otherwise append the event if it is not "none" //if (!parameters.Id.Tracker.Tier.SentStartedEvent) //{ // sb.Append("&event=started"); // parameters.Id.Tracker.Tier.SendingStartedEvent = true; //} if (parameters.ClientEvent != TorrentEvent.None) { b.Add("event", parameters.ClientEvent.ToString().ToLower()); } if (!string.IsNullOrEmpty(_trackerId)) { b.Add("trackerid", _trackerId); } return(b.ToUri()); }
private async Task DoAnnounceAsync(AnnounceParameters parameter, object state) { var announceMessage = new AnnounceMessage(DateTime.Now.GetHashCode(), _connectionId, parameter); var responseBytes = await SendAndReceiveAsync(announceMessage); var msg = Receive(announceMessage, responseBytes); if (!(msg is AnnounceResponseMessage)) { throw new Exception(FailureMessage); } MinUpdateInterval = ((AnnounceResponseMessage)msg).Interval; CompleteAnnounce(msg, state); }
public AnnounceMessage(int transactionId, long connectionId, AnnounceParameters parameters) : base(1, transactionId) { ConnectionId = connectionId; if (parameters == null) return; Downloaded = parameters.BytesDownloaded; Infohash = parameters.InfoHash; Ip = 0; Key = (uint) DateTime.Now.GetHashCode(); // FIXME: Don't do this! It should be constant Left = parameters.BytesLeft; NumWanted = 50; PeerId = parameters.PeerId; Port = (ushort) parameters.Port; TorrentEvent = parameters.ClientEvent; Uploaded = parameters.BytesUploaded; }
public override async void Announce(AnnounceParameters parameters, object state) { try { await _semaphoreSlim.WaitAsync(); try { if (!_hasConnected) await ConnectAsync(); await DoAnnounceAsync(parameters, state); } finally { _semaphoreSlim.Release(); } } catch (Exception ex) { FailureMessage = ex.Message; DoAnnounceComplete(false, state, new List<Peer>()); } }
public abstract void Announce(AnnounceParameters parameters, object state);
private Uri CreateAnnounceString(AnnounceParameters parameters) { var b = new UriQueryBuilder(Uri); b.Add("info_hash", parameters.InfoHash.UrlEncode()) .Add("peer_id", parameters.PeerId) .Add("port", parameters.Port) .Add("uploaded", parameters.BytesUploaded) .Add("downloaded", parameters.BytesDownloaded) .Add("left", parameters.BytesLeft) .Add("compact", 1) .Add("numwant", 100); if (parameters.SupportsEncryption) b.Add("supportcrypto", 1); if (parameters.RequireEncryption) b.Add("requirecrypto", 1); if (!b.Contains("key")) b.Add("key", Key); if (!string.IsNullOrEmpty(parameters.Ipaddress)) b.Add("ip", parameters.Ipaddress); // If we have not successfully sent the started event to this tier, override the passed in started event // Otherwise append the event if it is not "none" //if (!parameters.Id.Tracker.Tier.SentStartedEvent) //{ // sb.Append("&event=started"); // parameters.Id.Tracker.Tier.SendingStartedEvent = true; //} if (parameters.ClientEvent != TorrentEvent.None) b.Add("event", parameters.ClientEvent.ToString().ToLower()); if (!string.IsNullOrEmpty(_trackerId)) b.Add("trackerid", _trackerId); return b.ToUri(); }
private async Task DoAnnounceAsync(AnnounceParameters parameter, object state) { var announceMessage = new AnnounceMessage(DateTime.Now.GetHashCode(), _connectionId, parameter); var responseBytes = await SendAndReceiveAsync(announceMessage); var msg = Receive(announceMessage, responseBytes); if (!(msg is AnnounceResponseMessage)) throw new Exception(FailureMessage); MinUpdateInterval = ((AnnounceResponseMessage) msg).Interval; CompleteAnnounce(msg, state); }