Browse Source

Reverted to previous implementation & now using continuewith instead

pull/1199/head
ComputerMaster1st 7 years ago
parent
commit
80be364093
1 changed files with 15 additions and 9 deletions
  1. +15
    -9
      src/Discord.Net.WebSocket/Net/DefaultUdpSocket.cs

+ 15
- 9
src/Discord.Net.WebSocket/Net/DefaultUdpSocket.cs View File

@@ -113,20 +113,26 @@ namespace Discord.Net.Udp

private async Task RunAsync(CancellationToken cancelToken)
{
var closeTask = Task.Delay(-1, cancelToken);
while (!cancelToken.IsCancellationRequested)
{
try
{
var result = await _udp.ReceiveAsync()
.ConfigureAwait(false);
var receiveTask = _udp.ReceiveAsync();

await ReceivedDatagram(result.Buffer, 0, result.Buffer.Length).ConfigureAwait(false);
}
catch (ObjectDisposedException)
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
receiveTask.ContinueWith((recieveResult) =>
{
//if we get this the UDP socket has been closed (by dispose, so it has been cancelled)
//observe the exception as to not recieve as unhandled exception
_ = recieveResult.Exception;

}, TaskContinuationOptions.OnlyOnFaulted);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

var task = await Task.WhenAny(closeTask, receiveTask).ConfigureAwait(false);
if (task == closeTask)
break;
}

var result = receiveTask.Result;
await ReceivedDatagram(result.Buffer, 0, result.Buffer.Length).ConfigureAwait(false);
}
}
}


Loading…
Cancel
Save