Browse Source

Added an EnabledVoice option to the config, disabled by default.

tags/docs-0.9
RogueException 9 years ago
parent
commit
29328d5370
3 changed files with 64 additions and 36 deletions
  1. +56
    -32
      src/Discord.Net/DiscordClient.cs
  2. +5
    -0
      src/Discord.Net/DiscordClientConfig.cs
  3. +3
    -4
      src/Discord.Net/DiscordVoiceSocket.cs

+ 56
- 32
src/Discord.Net/DiscordClient.cs View File

@@ -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;
}

/// <summary> Mutes a user on the provided server. </summary>
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;
}

/// <summary> Sends a PCM frame to the voice server. </summary>
/// <param name="data">PCM frame to send.</param>
/// <param name="count">Number of bytes in this frame. </param>
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);
}

/// <summary> Clears the PCM buffer. </summary>
public void ClearVoicePCM()
{
if (!_config.EnableVoice)
throw new InvalidOperationException("Voice is not enabled for this client (see DiscordClientConfig).");

_voiceWebSocket.ClearPCMFrames();
}
#endif


+ 5
- 0
src/Discord.Net/DiscordClientConfig.cs View File

@@ -2,6 +2,11 @@
{
public class DiscordClientConfig
{
#if !DNXCORE50
/// <summary> Enables the voice websocket and UDP client (Experimental!). </summary>
/// <remarks> This option requires the opus .dll or .so be in the local lib/ folder. </remarks>
public bool EnableVoice { get; set; } = false;
#endif
/// <summary> Max time in milliseconds to wait for the web socket to connect. </summary>
public int ConnectionTimeout { get; set; } = 5000;
/// <summary> Max time in milliseconds to wait for the voice web socket to connect. </summary>


+ 3
- 4
src/Discord.Net/DiscordVoiceSocket.cs View File

@@ -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();
}
}
}
}
#endif

Loading…
Cancel
Save