@@ -131,8 +131,10 @@ namespace Discord.Net.WebSockets
}
}
else //Dont make an OS thread if we only want to capture one packet...
else //Dont make an OS thread if we only want to capture one packet...
tasks.Add(Task.Run(() => ReceiveVoiceAsync(_cancelToken)));
tasks.Add(Task.Run(() => ReceiveVoiceAsync(_cancelToken)));
#if !DNXCORE50
tasks.Add(WatcherAsync());
tasks.Add(WatcherAsync());
#endif
if (tasks.Count > 0)
if (tasks.Count > 0)
{
{
// We need to combine tasks into one because receiveThread is
// We need to combine tasks into one because receiveThread is
@@ -176,6 +178,7 @@ namespace Discord.Net.WebSockets
private void ReceiveVoiceAsync(CancellationToken cancelToken)
private void ReceiveVoiceAsync(CancellationToken cancelToken)
{
{
var closeTask = cancelToken.Wait();
try
try
{
{
byte[] packet, decodingBuffer = null, nonce = null, result;
byte[] packet, decodingBuffer = null, nonce = null, result;
@@ -193,7 +196,18 @@ namespace Discord.Net.WebSockets
Thread.Sleep(1);
Thread.Sleep(1);
if (_udp.Available > 0)
if (_udp.Available > 0)
{
{
#if !DNXCORE50
packet = _udp.Receive(ref endpoint);
packet = _udp.Receive(ref endpoint);
#else
//TODO: Is this really the only way to end a Receive call in DNXCore?
var receiveTask = _udp.ReceiveAsync();
var task = Task.WhenAny(closeTask, receiveTask).Result;
if (task == closeTask)
break;
var udpPacket = receiveTask.Result;
packet = udpPacket.Buffer;
endpoint = udpPacket.RemoteEndPoint;
#endif
packetLength = packet.Length;
packetLength = packet.Length;
if (packetLength > 0 && endpoint.Equals(_endpoint))
if (packetLength > 0 && endpoint.Equals(_endpoint))
@@ -367,6 +381,7 @@ namespace Discord.Net.WebSockets
catch (OperationCanceledException) { }
catch (OperationCanceledException) { }
catch (InvalidOperationException) { } //Includes ObjectDisposedException
catch (InvalidOperationException) { } //Includes ObjectDisposedException
}
}
#if !DNXCORE50
//Closes the UDP socket when _disconnectToken is triggered, since UDPClient doesn't allow passing a canceltoken
//Closes the UDP socket when _disconnectToken is triggered, since UDPClient doesn't allow passing a canceltoken
private Task WatcherAsync()
private Task WatcherAsync()
{
{
@@ -374,6 +389,7 @@ namespace Discord.Net.WebSockets
return cancelToken.Wait()
return cancelToken.Wait()
.ContinueWith(_ => _udp.Close());
.ContinueWith(_ => _udp.Close());
}
}
#endif
protected override async Task ProcessMessage(string json)
protected override async Task ProcessMessage(string json)
{
{