From 29328d5370d140edb9f8bee7ca976bada44f436c Mon Sep 17 00:00:00 2001 From: RogueException Date: Tue, 25 Aug 2015 15:06:50 -0300 Subject: [PATCH] Added an EnabledVoice option to the config, disabled by default. --- src/Discord.Net/DiscordClient.cs | 88 ++++++++++++++++---------- src/Discord.Net/DiscordClientConfig.cs | 5 ++ src/Discord.Net/DiscordVoiceSocket.cs | 7 +- 3 files changed, 64 insertions(+), 36 deletions(-) diff --git a/src/Discord.Net/DiscordClient.cs b/src/Discord.Net/DiscordClient.cs index bf7e0622d..c371de0ab 100644 --- a/src/Discord.Net/DiscordClient.cs +++ b/src/Discord.Net/DiscordClient.cs @@ -19,7 +19,9 @@ namespace Discord { private readonly DiscordClientConfig _config; private readonly DiscordTextWebSocket _webSocket; +#if !DNXCORE50 private readonly DiscordVoiceSocket _voiceWebSocket; +#endif private readonly ManualResetEventSlim _blockEvent; private readonly Regex _userRegex, _channelRegex; private readonly MatchEvaluator _userRegexEvaluator, _channelRegexEvaluator; @@ -311,15 +313,20 @@ namespace Discord } }; _webSocket.OnDebugMessage += (s, e) => RaiseOnDebugMessage(e.Message); - - _voiceWebSocket = new DiscordVoiceSocket(this, _config.VoiceConnectionTimeout, _config.WebSocketInterval); - _voiceWebSocket.Connected += (s, e) => RaiseVoiceConnected(); - _voiceWebSocket.Disconnected += (s, e) => + +#if !DNXCORE50 + if (_config.EnableVoice) { + _voiceWebSocket = new DiscordVoiceSocket(this, _config.VoiceConnectionTimeout, _config.WebSocketInterval); + _voiceWebSocket.Connected += (s, e) => RaiseVoiceConnected(); + _voiceWebSocket.Disconnected += (s, e) => + { //TODO: Reconnect if we didn't cause the disconnect RaiseVoiceDisconnected(); - }; - _voiceWebSocket.OnDebugMessage += (s, e) => RaiseOnVoiceDebugMessage(e.Message); + }; + _voiceWebSocket.OnDebugMessage += (s, e) => RaiseOnVoiceDebugMessage(e.Message); + } +#endif #pragma warning disable CS1998 //Disable unused async keyword warning _webSocket.GotEvent += async (s, e) => @@ -573,14 +580,16 @@ namespace Discord var server = _servers[data.ServerId]; server.VoiceServer = data.Endpoint; try { RaiseVoiceServerUpdated(server, data.Endpoint); } catch { } - - if (data.ServerId == _currentVoiceServerId) + +#if !DNXCORE50 + if (_config.EnableVoice && data.ServerId == _currentVoiceServerId) { _currentVoiceEndpoint = data.Endpoint.Split(':')[0]; _currentVoiceToken = data.Token; await _voiceWebSocket.ConnectAsync(_currentVoiceEndpoint); await _voiceWebSocket.Login(_currentVoiceServerId, UserId, SessionId, _currentVoiceToken); } +#endif } break; @@ -901,7 +910,10 @@ namespace Discord _tasks = _tasks.ContinueWith(async x => { await _webSocket.DisconnectAsync(); - await _voiceWebSocket.DisconnectAsync(); +#if !DNXCORE50 + if (_config.EnableVoice) + await _voiceWebSocket.DisconnectAsync(); +#endif //Clear send queue Message ignored; @@ -1233,29 +1245,6 @@ namespace Discord //Voice - public Task JoinVoiceServer(Server server, Channel channel) - => JoinVoiceServer(server.Id, channel.Id); - public Task JoinVoiceServer(Server server, string channelId) - => JoinVoiceServer(server.Id, channelId); - public Task JoinVoiceServer(string serverId, Channel channel) - => JoinVoiceServer(serverId, channel.Id); - public async Task JoinVoiceServer(string serverId, string channelId) - { - await LeaveVoiceServer(); - _currentVoiceServerId = serverId; - _webSocket.JoinVoice(serverId, channelId); - } - - public async Task LeaveVoiceServer() - { - await _voiceWebSocket.DisconnectAsync(); - if (_currentVoiceEndpoint != null) - _webSocket.LeaveVoice(); - _currentVoiceEndpoint = null; - _currentVoiceServerId = null; - _currentVoiceToken = null; - } - /// Mutes a user on the provided server. public Task Mute(Server server, User user) => Mute(server.Id, user.Id); @@ -1321,17 +1310,52 @@ namespace Discord } #if !DNXCORE50 + public Task JoinVoiceServer(Server server, Channel channel) + => JoinVoiceServer(server.Id, channel.Id); + public Task JoinVoiceServer(Server server, string channelId) + => JoinVoiceServer(server.Id, channelId); + public Task JoinVoiceServer(string serverId, Channel channel) + => JoinVoiceServer(serverId, channel.Id); + public async Task JoinVoiceServer(string serverId, string channelId) + { + if (!_config.EnableVoice) + throw new InvalidOperationException("Voice is not enabled for this client (see DiscordClientConfig)."); + + await LeaveVoiceServer(); + _currentVoiceServerId = serverId; + _webSocket.JoinVoice(serverId, channelId); + } + + public async Task LeaveVoiceServer() + { + if (!_config.EnableVoice) + throw new InvalidOperationException("Voice is not enabled for this client (see DiscordClientConfig)."); + + await _voiceWebSocket.DisconnectAsync(); + if (_currentVoiceEndpoint != null) + _webSocket.LeaveVoice(); + _currentVoiceEndpoint = null; + _currentVoiceServerId = null; + _currentVoiceToken = null; + } + /// Sends a PCM frame to the voice server. /// PCM frame to send. /// Number of bytes in this frame. public void SendVoicePCM(byte[] data, int count) { + if (!_config.EnableVoice) + throw new InvalidOperationException("Voice is not enabled for this client (see DiscordClientConfig)."); + _voiceWebSocket.SendPCMFrame(data, count); } /// Clears the PCM buffer. public void ClearVoicePCM() { + if (!_config.EnableVoice) + throw new InvalidOperationException("Voice is not enabled for this client (see DiscordClientConfig)."); + _voiceWebSocket.ClearPCMFrames(); } #endif diff --git a/src/Discord.Net/DiscordClientConfig.cs b/src/Discord.Net/DiscordClientConfig.cs index 5ebcd4987..f97405657 100644 --- a/src/Discord.Net/DiscordClientConfig.cs +++ b/src/Discord.Net/DiscordClientConfig.cs @@ -2,6 +2,11 @@ { public class DiscordClientConfig { +#if !DNXCORE50 + /// Enables the voice websocket and UDP client (Experimental!). + /// This option requires the opus .dll or .so be in the local lib/ folder. + public bool EnableVoice { get; set; } = false; +#endif /// Max time in milliseconds to wait for the web socket to connect. public int ConnectionTimeout { get; set; } = 5000; /// Max time in milliseconds to wait for the voice web socket to connect. diff --git a/src/Discord.Net/DiscordVoiceSocket.cs b/src/Discord.Net/DiscordVoiceSocket.cs index 14d84d971..584a2bc26 100644 --- a/src/Discord.Net/DiscordVoiceSocket.cs +++ b/src/Discord.Net/DiscordVoiceSocket.cs @@ -1,5 +1,5 @@ //#define USE_THREAD - +#if !DNXCORE50 using Discord.API.Models; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -13,9 +13,7 @@ using System.Threading; using System.Threading.Tasks; using System.Text; using WebSocketMessage = Discord.API.Models.VoiceWebSocketCommands.WebSocketMessage; -#if !DNXCORE50 using Opus.Net; -#endif namespace Discord { @@ -410,4 +408,5 @@ namespace Discord return new VoiceWebSocketCommands.KeepAlive(); } } -} \ No newline at end of file +} +#endif \ No newline at end of file