Passing CancelToken and properly parsing invoice response

This commit is contained in:
rockstardev 2018-05-31 15:53:14 -05:00
parent ce9189caf8
commit 279de1b869
2 changed files with 16 additions and 8 deletions

View File

@ -73,7 +73,9 @@ namespace BTCPayServer.Payments.Lightning.Lnd
public Task<ILightningListenInvoiceSession> Listen(CancellationToken cancellation = default(CancellationToken))
{
Task.Run(_rpcClient.StartSubscribeInvoiceThread);
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
_rpcClient.StartSubscribeInvoiceThread(cancellation);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
return Task.FromResult<ILightningListenInvoiceSession>(this);
}

View File

@ -11,6 +11,8 @@ using System.Text;
using System.Threading;
using System.Threading.Tasks;
using NBitcoin;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace BTCPayServer.Payments.Lightning.Lnd
{
@ -115,7 +117,7 @@ namespace BTCPayServer.Payments.Lightning.Lnd
public TaskCompletionSource<LndSwaggerClient> SubscribeLost = new TaskCompletionSource<LndSwaggerClient>();
// TODO: Refactor swagger generated wrapper to include this method directly
public async Task StartSubscribeInvoiceThread()
public async Task StartSubscribeInvoiceThread(CancellationToken token)
{
var urlBuilder = new StringBuilder();
urlBuilder.Append(BaseUrl).Append("/v1/invoices/subscribe");
@ -127,8 +129,7 @@ namespace BTCPayServer.Payments.Lightning.Lnd
var request = new HttpRequestMessage(HttpMethod.Get, urlBuilder.ToString());
using (var response = await client.SendAsync(
request,
HttpCompletionOption.ResponseHeadersRead))
request, HttpCompletionOption.ResponseHeadersRead, token))
{
using (var body = await response.Content.ReadAsStreamAsync())
using (var reader = new StreamReader(body))
@ -138,16 +139,21 @@ namespace BTCPayServer.Payments.Lightning.Lnd
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
LnrpcInvoice parsedInvoice = Newtonsoft.Json.JsonConvert.DeserializeObject<LnrpcInvoice>(line, _settings.Value);
InvoiceResponse?.SetResult(parsedInvoice);
if (line != null && line.Contains("\"result\":"))
{
dynamic parsedJson = JObject.Parse(line);
var result = parsedJson.result;
var invoiceString = result.ToString();
LnrpcInvoice parsedInvoice = JsonConvert.DeserializeObject<LnrpcInvoice>(invoiceString, _settings.Value);
InvoiceResponse.SetResult(parsedInvoice);
}
}
}
catch (Exception e)
{
// TODO: check that the exception type is actually from a closed stream.
Debug.WriteLine(e.Message);
SubscribeLost?.SetResult(this);
SubscribeLost.SetResult(this);
}
}
}