Browse Source

Fixed a couple crash bugs in DiscordVoiceSocket

tags/docs-0.9
RogueException 9 years ago
parent
commit
769be07c56
2 changed files with 13 additions and 19 deletions
  1. +9
    -17
      src/Discord.Net/DiscordClient.cs
  2. +4
    -2
      src/Discord.Net/DiscordVoiceSocket.cs

+ 9
- 17
src/Discord.Net/DiscordClient.cs View File

@@ -590,12 +590,10 @@ namespace Discord
=> JoinVoiceServer(_channels[channelId]); => JoinVoiceServer(_channels[channelId]);
public async Task JoinVoiceServer(Channel channel) public async Task JoinVoiceServer(Channel channel)
{ {
CheckReady();
CheckVoice();
CheckReady(checkVoice: true);
if (channel == null) throw new ArgumentNullException(nameof(channel)); if (channel == null) throw new ArgumentNullException(nameof(channel));


await LeaveVoiceServer().ConfigureAwait(false); await LeaveVoiceServer().ConfigureAwait(false);
//_currentVoiceServerId = channel.ServerId;
_webSocket.JoinVoice(channel); _webSocket.JoinVoice(channel);
#if !DNXCORE50 #if !DNXCORE50
await _voiceWebSocket.BeginConnect().ConfigureAwait(false); await _voiceWebSocket.BeginConnect().ConfigureAwait(false);
@@ -606,15 +604,13 @@ namespace Discord


public async Task LeaveVoiceServer() public async Task LeaveVoiceServer()
{ {
CheckReady();
CheckVoice();
CheckReady(checkVoice: true);


#if !DNXCORE50 #if !DNXCORE50
await _voiceWebSocket.DisconnectAsync().ConfigureAwait(false); await _voiceWebSocket.DisconnectAsync().ConfigureAwait(false);
#else #else
await Task.CompletedTask.ConfigureAwait(false); await Task.CompletedTask.ConfigureAwait(false);
#endif #endif
//if (_voiceWebSocket.CurrentVoiceServerId != null)
_webSocket.LeaveVoice(); _webSocket.LeaveVoice();
} }


@@ -624,8 +620,7 @@ namespace Discord
/// <remarks>Will block until</remarks> /// <remarks>Will block until</remarks>
public void SendVoicePCM(byte[] data, int count) public void SendVoicePCM(byte[] data, int count)
{ {
CheckReady();
CheckVoice();
CheckReady(checkVoice: true);
if (count == 0) return; if (count == 0) return;


if (_isDebugMode) if (_isDebugMode)
@@ -638,8 +633,7 @@ namespace Discord
/// <summary> Clears the PCM buffer. </summary> /// <summary> Clears the PCM buffer. </summary>
public void ClearVoicePCM() public void ClearVoicePCM()
{ {
CheckReady();
CheckVoice();
CheckReady(checkVoice: true);


if (_isDebugMode) if (_isDebugMode)
RaiseOnDebugMessage(DebugMessageType.VoiceOutput, $"Cleared the voice buffer."); RaiseOnDebugMessage(DebugMessageType.VoiceOutput, $"Cleared the voice buffer.");
@@ -651,8 +645,7 @@ namespace Discord
/// <summary> Returns a task that completes once the voice output buffer is empty. </summary> /// <summary> Returns a task that completes once the voice output buffer is empty. </summary>
public async Task WaitVoice() public async Task WaitVoice()
{ {
CheckReady();
CheckVoice();
CheckReady(checkVoice: true);


#if !DNXCORE50 #if !DNXCORE50
_voiceWebSocket.Wait(); _voiceWebSocket.Wait();
@@ -661,17 +654,16 @@ namespace Discord
} }


//Helpers //Helpers
private void CheckReady()
private void CheckReady(bool checkVoice = false)
{ {
if (_isDisconnecting) if (_isDisconnecting)
throw new InvalidOperationException("The client is currently disconnecting."); throw new InvalidOperationException("The client is currently disconnecting.");
else if (!_isConnected) else if (!_isConnected)
throw new InvalidOperationException("The client is not currently connected to Discord"); throw new InvalidOperationException("The client is not currently connected to Discord");
}
private void CheckVoice()
{
#if !DNXCORE50 #if !DNXCORE50
if (!_config.EnableVoice)
else if (checkVoice && !_config.EnableVoice)
#else
else if (checkVoice) //Always fail on DNXCORE50
#endif #endif
throw new InvalidOperationException("Voice is not enabled for this client."); throw new InvalidOperationException("Voice is not enabled for this client.");
} }


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

@@ -162,7 +162,7 @@ namespace Discord
} }
} }
catch (OperationCanceledException) { } catch (OperationCanceledException) { }
catch (ObjectDisposedException) { }
catch (InvalidOperationException) { } //Includes ObjectDisposedException
catch (Exception ex) { DisconnectInternal(ex); } catch (Exception ex) { DisconnectInternal(ex); }
}).ConfigureAwait(false); }).ConfigureAwait(false);
} }
@@ -255,7 +255,7 @@ namespace Discord
} }
} }
catch (OperationCanceledException) { } catch (OperationCanceledException) { }
catch (ObjectDisposedException) { }
catch (InvalidOperationException) { } //Includes ObjectDisposedException
catch (Exception ex) { DisconnectInternal(ex); } catch (Exception ex) { DisconnectInternal(ex); }
#if !USE_THREAD #if !USE_THREAD
}).ConfigureAwait(false); }).ConfigureAwait(false);
@@ -411,6 +411,8 @@ namespace Discord


public void SendPCMFrame(byte[] data, int count) public void SendPCMFrame(byte[] data, int count)
{ {
if (!_isReady)
return;
if (count != _encoder.FrameSize) if (count != _encoder.FrameSize)
throw new InvalidOperationException($"Invalid frame size. Got {count}, expected {_encoder.FrameSize}."); throw new InvalidOperationException($"Invalid frame size. Got {count}, expected {_encoder.FrameSize}.");




Loading…
Cancel
Save