private async Task ListenLoop() { try { while (!_Cts.IsCancellationRequested) { string line = await _Reader.ReadLineAsync().WithCancellation(_Cts.Token); if (line != null) { if (line.StartsWith("{\"result\":", StringComparison.OrdinalIgnoreCase)) { var invoiceString = JObject.Parse(line)["result"].ToString(); LnrpcInvoice parsedInvoice = _Parent.Deserialize <LnrpcInvoice>(invoiceString); await _Invoices.Writer.WriteAsync(ConvertLndInvoice(parsedInvoice), _Cts.Token); } else if (line.StartsWith("{\"error\":", StringComparison.OrdinalIgnoreCase)) { var errorString = JObject.Parse(line)["error"].ToString(); var error = _Parent.Deserialize <LndError>(errorString); throw new LndException(error); } else { throw new LndException("Unknown result from LND: " + line); } } } } catch when(_Cts.IsCancellationRequested) { }
private async Task ListenLoop(HttpRequestMessage request) { try { _Response = await _Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, _Cts.Token); _Body = await _Response.Content.ReadAsStreamAsync(); _Reader = new StreamReader(_Body); while (!_Cts.IsCancellationRequested) { string line = await _Reader.ReadLineAsync().WithCancellation(_Cts.Token); if (line != null) { if (line.StartsWith("{\"result\":", StringComparison.OrdinalIgnoreCase)) { var invoiceString = JObject.Parse(line)["result"].ToString(); LnrpcInvoice parsedInvoice = _Parent.Deserialize <LnrpcInvoice>(invoiceString); await _Invoices.Writer.WriteAsync(ConvertLndInvoice(parsedInvoice), _Cts.Token); } else if (line.StartsWith("{\"error\":", StringComparison.OrdinalIgnoreCase)) { var errorString = JObject.Parse(line)["error"].ToString(); var error = _Parent.Deserialize <LndError>(errorString); throw new LndException(error); } else { throw new LndException("Unknown result from LND: " + line); } } } } catch when(_Cts.IsCancellationRequested) { }
private async Task ListenLoop() { try { while (!_Cts.IsCancellationRequested) { string line = await _Reader.ReadLineAsync().WithCancellation(_Cts.Token); if (line != null && line.StartsWith("{\"result\":", StringComparison.OrdinalIgnoreCase)) { var invoiceString = JObject.Parse(line)["result"].ToString(); LnrpcInvoice parsedInvoice = _Parent.Deserialize <LnrpcInvoice>(invoiceString); await _Invoices.Writer.WriteAsync(ConvertLndInvoice(parsedInvoice), _Cts.Token); } } } catch when(_Cts.IsCancellationRequested) { }
public async void StartListening() { var urlBuilder = new StringBuilder(); urlBuilder.Append(_Parent.BaseUrl).Append("/v1/invoices/subscribe"); try { using (var client = _Parent.CreateHttpClient()) { client.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite); var request = new HttpRequestMessage(HttpMethod.Get, urlBuilder.ToString()); using (var response = await client.SendAsync( request, HttpCompletionOption.ResponseHeadersRead, _Cts.Token)) { using (var body = await response.Content.ReadAsStreamAsync()) using (var reader = new StreamReader(body)) { while (!_Cts.IsCancellationRequested) { string line = await reader.ReadLineAsync().WithCancellation(_Cts.Token); if (line != null && line.StartsWith("{\"result\":", StringComparison.OrdinalIgnoreCase)) { var invoiceString = JObject.Parse(line)["result"].ToString(); LnrpcInvoice parsedInvoice = _Parent.Deserialize <LnrpcInvoice>(invoiceString); await _Invoices.Writer.WriteAsync(ConvertLndInvoice(parsedInvoice)); } } } } } } catch when(_Cts.IsCancellationRequested) { } finally { _Stopped.Set(); } }