public async Task HandleMessages() { var cts = new CancellationTokenSource(); while (!cts.IsCancellationRequested && _inputStream.CanRead && _outputStream.CanWrite) { try { var msg = await _parser.ParseAsync <BDictionary>(_reader, cts.Token); if (msg.TryGetValue("op", out IBObject op)) { var s = ((BString)op).ToString(); await HandleMessage(s, msg, cts); } } catch (BencodeNET.Exceptions.BencodeException ex) when/* HACK */ (ex.Message.Contains("but reached end of stream")) { // ^ This message filter appears to be the only way to check if the stream is closed, because // the BencodeNET does not expose an inner exception, specific code, etc when the stream closes. await SendException(null, "Reached end of stream"); return; } catch (OperationCanceledException) { } catch (Exception ex) { await SendException(null, ex.Message); } } }
public async Task HandleMessages() { var cts = new CancellationTokenSource(); while (!cts.IsCancellationRequested) { try { var msg = await _parser.ParseAsync <BDictionary>(_reader, cts.Token); if (msg.TryGetValue("op", out IBObject op)) { var s = ((BString)op).ToString(); await HandleMessage(s, msg, cts); } } catch (OperationCanceledException) { } catch (Exception ex) { await SendException(null, ex.Message); } } }