Fixed a couple crash bugs in DiscordVoiceSocket
This commit is contained in:
@@ -590,12 +590,10 @@ namespace Discord
|
||||
=> JoinVoiceServer(_channels[channelId]);
|
||||
public async Task JoinVoiceServer(Channel channel)
|
||||
{
|
||||
CheckReady();
|
||||
CheckVoice();
|
||||
CheckReady(checkVoice: true);
|
||||
if (channel == null) throw new ArgumentNullException(nameof(channel));
|
||||
|
||||
await LeaveVoiceServer().ConfigureAwait(false);
|
||||
//_currentVoiceServerId = channel.ServerId;
|
||||
_webSocket.JoinVoice(channel);
|
||||
#if !DNXCORE50
|
||||
await _voiceWebSocket.BeginConnect().ConfigureAwait(false);
|
||||
@@ -606,15 +604,13 @@ namespace Discord
|
||||
|
||||
public async Task LeaveVoiceServer()
|
||||
{
|
||||
CheckReady();
|
||||
CheckVoice();
|
||||
CheckReady(checkVoice: true);
|
||||
|
||||
#if !DNXCORE50
|
||||
await _voiceWebSocket.DisconnectAsync().ConfigureAwait(false);
|
||||
#else
|
||||
await Task.CompletedTask.ConfigureAwait(false);
|
||||
#endif
|
||||
//if (_voiceWebSocket.CurrentVoiceServerId != null)
|
||||
_webSocket.LeaveVoice();
|
||||
}
|
||||
|
||||
@@ -624,8 +620,7 @@ namespace Discord
|
||||
/// <remarks>Will block until</remarks>
|
||||
public void SendVoicePCM(byte[] data, int count)
|
||||
{
|
||||
CheckReady();
|
||||
CheckVoice();
|
||||
CheckReady(checkVoice: true);
|
||||
if (count == 0) return;
|
||||
|
||||
if (_isDebugMode)
|
||||
@@ -638,8 +633,7 @@ namespace Discord
|
||||
/// <summary> Clears the PCM buffer. </summary>
|
||||
public void ClearVoicePCM()
|
||||
{
|
||||
CheckReady();
|
||||
CheckVoice();
|
||||
CheckReady(checkVoice: true);
|
||||
|
||||
if (_isDebugMode)
|
||||
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>
|
||||
public async Task WaitVoice()
|
||||
{
|
||||
CheckReady();
|
||||
CheckVoice();
|
||||
CheckReady(checkVoice: true);
|
||||
|
||||
#if !DNXCORE50
|
||||
_voiceWebSocket.Wait();
|
||||
@@ -661,17 +654,16 @@ namespace Discord
|
||||
}
|
||||
|
||||
//Helpers
|
||||
private void CheckReady()
|
||||
private void CheckReady(bool checkVoice = false)
|
||||
{
|
||||
if (_isDisconnecting)
|
||||
throw new InvalidOperationException("The client is currently disconnecting.");
|
||||
else if (!_isConnected)
|
||||
throw new InvalidOperationException("The client is not currently connected to Discord");
|
||||
}
|
||||
private void CheckVoice()
|
||||
{
|
||||
#if !DNXCORE50
|
||||
if (!_config.EnableVoice)
|
||||
else if (checkVoice && !_config.EnableVoice)
|
||||
#else
|
||||
else if (checkVoice) //Always fail on DNXCORE50
|
||||
#endif
|
||||
throw new InvalidOperationException("Voice is not enabled for this client.");
|
||||
}
|
||||
|
||||
@@ -162,7 +162,7 @@ namespace Discord
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException) { }
|
||||
catch (ObjectDisposedException) { }
|
||||
catch (InvalidOperationException) { } //Includes ObjectDisposedException
|
||||
catch (Exception ex) { DisconnectInternal(ex); }
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
@@ -255,7 +255,7 @@ namespace Discord
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException) { }
|
||||
catch (ObjectDisposedException) { }
|
||||
catch (InvalidOperationException) { } //Includes ObjectDisposedException
|
||||
catch (Exception ex) { DisconnectInternal(ex); }
|
||||
#if !USE_THREAD
|
||||
}).ConfigureAwait(false);
|
||||
@@ -411,6 +411,8 @@ namespace Discord
|
||||
|
||||
public void SendPCMFrame(byte[] data, int count)
|
||||
{
|
||||
if (!_isReady)
|
||||
return;
|
||||
if (count != _encoder.FrameSize)
|
||||
throw new InvalidOperationException($"Invalid frame size. Got {count}, expected {_encoder.FrameSize}.");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user