@@ -33,8 +33,7 @@ namespace Discord
private ManualResetEventSlim _connectWaitOnLogin;
private ManualResetEventSlim _connectWaitOnLogin;
private uint _ssrc;
private uint _ssrc;
private readonly Random _rand = new Random();
private readonly Random _rand = new Random();
#if !DNXCORE50
private OpusEncoder _encoder;
private OpusEncoder _encoder;
private ConcurrentQueue<Packet> _sendQueue;
private ConcurrentQueue<Packet> _sendQueue;
private UdpClient _udp;
private UdpClient _udp;
@@ -46,20 +45,16 @@ namespace Discord
private string _mode;
private string _mode;
#if USE_THREAD
#if USE_THREAD
private Thread _sendThread;
private Thread _sendThread;
#endif
#endif
#endif
public DiscordVoiceSocket(DiscordClient client, int timeout, int interval, bool isDebug)
public DiscordVoiceSocket(DiscordClient client, int timeout, int interval, bool isDebug)
: base(client, timeout, interval, isDebug)
: base(client, timeout, interval, isDebug)
{
{
_connectWaitOnLogin = new ManualResetEventSlim(false);
_connectWaitOnLogin = new ManualResetEventSlim(false);
#if !DNXCORE50
_sendQueue = new ConcurrentQueue<Packet>();
_sendQueue = new ConcurrentQueue<Packet>();
_encoder = new OpusEncoder(48000, 1, 20, Application.Audio);
_encoder = new OpusEncoder(48000, 1, 20, Application.Audio);
#endif
}
}
#if !DNXCORE50
protected override void OnConnect()
protected override void OnConnect()
{
{
_udp = new UdpClient(new IPEndPoint(IPAddress.Any, 0));
_udp = new UdpClient(new IPEndPoint(IPAddress.Any, 0));
@@ -77,21 +72,18 @@ namespace Discord
_sendThread = null;
_sendThread = null;
#endif
#endif
}
}
#endif
protected override Task[] CreateTasks()
protected override Task[] CreateTasks()
{
{
#if !DNXCORE50 && USE_THREAD
#if USE_THREAD
_sendThread = new Thread(new ThreadStart(() => SendAsync(_disconnectToken)));
_sendThread = new Thread(new ThreadStart(() => SendAsync(_disconnectToken)));
_sendThread.Start();
_sendThread.Start();
#endif
#endif
return new Task[]
return new Task[]
{
{
#if !DNXCORE50
ReceiveAsync(),
ReceiveAsync(),
#if !USE_THREAD
#if !USE_THREAD
SendAsync(),
SendAsync(),
#endif
#endif
#endif
WatcherAsync()
WatcherAsync()
}.Concat(base.CreateTasks()).ToArray();
}.Concat(base.CreateTasks()).ToArray();
@@ -122,8 +114,7 @@ namespace Discord
SetConnected();
SetConnected();
}
}
#if !DNXCORE50
private async Task ReceiveAsync()
private async Task ReceiveAsync()
{
{
var cancelSource = _disconnectToken;
var cancelSource = _disconnectToken;
@@ -152,7 +143,6 @@ namespace Discord
var cancelSource = _disconnectToken;
var cancelSource = _disconnectToken;
var cancelToken = cancelSource.Token;
var cancelToken = cancelSource.Token;
await Task.Yield();
await Task.Yield();
#endif
Packet packet;
Packet packet;
try
try
@@ -231,30 +221,21 @@ namespace Discord
await Task.Delay(-1, cancelToken);
await Task.Delay(-1, cancelToken);
}
}
catch (TaskCanceledException) { }
catch (TaskCanceledException) { }
#if !DNXCORE50
finally { _udp.Close(); }
finally { _udp.Close(); }
#endif
}
}
#if DNXCORE50
protected override Task ProcessMessage(string json)
#else
protected override async Task ProcessMessage(string json)
protected override async Task ProcessMessage(string json)
#endif
{
{
var msg = JsonConvert.DeserializeObject<WebSocketMessage>(json);
var msg = JsonConvert.DeserializeObject<WebSocketMessage>(json);
switch (msg.Operation)
switch (msg.Operation)
{
{
case 2: //READY
case 2: //READY
{
{
#if !DNXCORE50
if (!_isReady)
if (!_isReady)
{
{
#endif
var payload = (msg.Payload as JToken).ToObject<VoiceWebSocketEvents.Ready>();
var payload = (msg.Payload as JToken).ToObject<VoiceWebSocketEvents.Ready>();
_heartbeatInterval = payload.HeartbeatInterval;
_heartbeatInterval = payload.HeartbeatInterval;
_ssrc = payload.SSRC;
_ssrc = payload.SSRC;
#if !DNXCORE50
_endpoint = new IPEndPoint((await Dns.GetHostAddressesAsync(_host)).FirstOrDefault(), payload.Port);
_endpoint = new IPEndPoint((await Dns.GetHostAddressesAsync(_host)).FirstOrDefault(), payload.Port);
//_mode = payload.Modes.LastOrDefault();
//_mode = payload.Modes.LastOrDefault();
_mode = "plain";
_mode = "plain";
@@ -273,12 +254,8 @@ namespace Discord
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }, 70);
0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }, 70);
}
}
#else
_connectWaitOnLogin.Set();
#endif
}
}
break;
break;
#if !DNXCORE50
case 4: //SESSION_DESCRIPTION
case 4: //SESSION_DESCRIPTION
{
{
var payload = (msg.Payload as JToken).ToObject<VoiceWebSocketEvents.JoinServer>();
var payload = (msg.Payload as JToken).ToObject<VoiceWebSocketEvents.JoinServer>();
@@ -287,17 +264,13 @@ namespace Discord
_connectWaitOnLogin.Set();
_connectWaitOnLogin.Set();
}
}
break;
break;
#endif
default:
default:
if (_isDebug)
if (_isDebug)
RaiseOnDebugMessage(DebugMessageType.WebSocketUnknownOpCode, "Unknown Opcode: " + msg.Operation);
RaiseOnDebugMessage(DebugMessageType.WebSocketUnknownOpCode, "Unknown Opcode: " + msg.Operation);
break;
break;
}
}
#if DNXCORE50
return Task.CompletedTask;
#endif
}
}
#if !DNXCORE50
private void ProcessUdpMessage(UdpReceiveResult msg)
private void ProcessUdpMessage(UdpReceiveResult msg)
{
{
if (msg.Buffer.Length > 0 && msg.RemoteEndPoint.Equals(_endpoint))
if (msg.Buffer.Length > 0 && msg.RemoteEndPoint.Equals(_endpoint))
@@ -369,7 +342,6 @@ namespace Discord
if (length < 36) //12 + 24
if (length < 36) //12 + 24
throw new Exception($"Unexpected message length. Expected >= 36, got {length}.");
throw new Exception($"Unexpected message length. Expected >= 36, got {length}.");
#if !DNXCORE50
byte[] nonce = new byte[24]; //16 bytes static, 8 bytes incrementing?
byte[] nonce = new byte[24]; //16 bytes static, 8 bytes incrementing?
Buffer.BlockCopy(buffer, 12, nonce, 0, 24);
Buffer.BlockCopy(buffer, 12, nonce, 0, 24);
@@ -377,7 +349,6 @@ namespace Discord
Buffer.BlockCopy(buffer, 36, cipherText, 0, cipherText.Length);
Buffer.BlockCopy(buffer, 36, cipherText, 0, cipherText.Length);
Sodium.SecretBox.Open(cipherText, nonce, _secretKey);
Sodium.SecretBox.Open(cipherText, nonce, _secretKey);
#endif
}
}
else //Plain
else //Plain
{
{
@@ -427,7 +398,6 @@ namespace Discord
isTalking.Payload.Delay = 0;
isTalking.Payload.Delay = 0;
QueueMessage(isTalking);
QueueMessage(isTalking);
}
}
#endif
protected override object GetKeepAlive()
protected override object GetKeepAlive()
{
{