Browse Source

Voice state improvements

tags/docs-0.9
RogueException 9 years ago
parent
commit
47fbd1efff
5 changed files with 26 additions and 21 deletions
  1. +8
    -8
      src/Discord.Net/DiscordClient.Voice.cs
  2. +8
    -5
      src/Discord.Net/DiscordClient.cs
  3. +1
    -1
      src/Discord.Net/DiscordClientConfig.cs
  4. +3
    -3
      src/Discord.Net/Net/WebSockets/DataWebSocket.cs
  5. +6
    -4
      src/Discord.Net/Net/WebSockets/VoiceWebSocket.cs

+ 8
- 8
src/Discord.Net/DiscordClient.Voice.cs View File

@@ -6,24 +6,25 @@ namespace Discord
{
public partial class DiscordClient
{
public Task JoinVoiceServer(string channelId)
=> JoinVoiceServer(_channels[channelId]);
public async Task JoinVoiceServer(Channel channel)
public Task JoinVoiceServer(Channel channel)
=> JoinVoiceServer(channel.ServerId, channel.Id);
public async Task JoinVoiceServer(string serverId, string channelId)
{
CheckReady(checkVoice: true);
if (channel == null) throw new ArgumentNullException(nameof(channel));
if (serverId == null) throw new ArgumentNullException(nameof(serverId));
if (channelId == null) throw new ArgumentNullException(nameof(channelId));

await LeaveVoiceServer().ConfigureAwait(false);
_dataSocket.SendJoinVoice(channel);
_voiceSocket.SetServer(serverId);
_dataSocket.SendJoinVoice(serverId, channelId);
//await _voiceSocket.WaitForConnection().ConfigureAwait(false);
//TODO: Add another ManualResetSlim to wait on here, base it off of DiscordClient's setup
}

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

if (_voiceSocket.CurrentVoiceServerId != null)
if (_voiceSocket.State != Net.WebSockets.WebSocketState.Disconnected)
{
await _voiceSocket.Disconnect().ConfigureAwait(false);
await TaskHelper.CompletedTask.ConfigureAwait(false);
@@ -43,7 +44,6 @@ namespace Discord
_voiceSocket.SendPCMFrames(data, count);
}

/// <summary> Clears the PCM buffer. </summary>
public void ClearVoicePCM()
{


+ 8
- 5
src/Discord.Net/DiscordClient.cs View File

@@ -121,7 +121,7 @@ namespace Discord
};
_voiceSocket.IsSpeaking += (s, e) =>
{
if (_voiceSocket.CurrentVoiceServerId != null)
if (_voiceSocket.State == WebSocketState.Connected)
{
var member = _members[e.UserId, _voiceSocket.CurrentVoiceServerId];
bool value = e.IsSpeaking;
@@ -586,11 +586,14 @@ namespace Discord
case "VOICE_SERVER_UPDATE":
{
var data = e.Payload.ToObject<Events.VoiceServerUpdate>(_serializer);
var server = _servers[data.GuildId];
if (_config.EnableVoice)
if (data.GuildId == _voiceSocket.CurrentVoiceServerId)
{
_voiceSocket.Host = "wss://" + data.Endpoint.Split(':')[0];
await _voiceSocket.Login(data.GuildId, _currentUserId, _dataSocket.SessionId, data.Token, _cancelToken).ConfigureAwait(false);
var server = _servers[data.GuildId];
if (_config.EnableVoice)
{
_voiceSocket.Host = "wss://" + data.Endpoint.Split(':')[0];
await _voiceSocket.Login(_currentUserId, _dataSocket.SessionId, data.Token, _cancelToken).ConfigureAwait(false);
}
}
}
break;


+ 1
- 1
src/Discord.Net/DiscordClientConfig.cs View File

@@ -29,7 +29,7 @@ namespace Discord
private int _messageQueueInterval = 100;
/// <summary> Gets or sets the max buffer length (in milliseconds) for outgoing voice packets. This value is the target maximum but is not guaranteed, the buffer will often go slightly above this value. </summary>
public int VoiceBufferLength { get { return _voiceBufferLength; } set { SetValue(ref _voiceBufferLength, value); } }
private int _voiceBufferLength = 3000;
private int _voiceBufferLength = 1000;

//Experimental Features
#if !DNXCORE50


+ 3
- 3
src/Discord.Net/Net/WebSockets/DataWebSocket.cs View File

@@ -112,11 +112,11 @@ namespace Discord.Net.WebSockets
return new Commands.KeepAlive();
}

public void SendJoinVoice(Channel channel)
public void SendJoinVoice(string serverId, string channelId)
{
var joinVoice = new Commands.JoinVoice();
joinVoice.Payload.ServerId = channel.ServerId;
joinVoice.Payload.ChannelId = channel.Id;
joinVoice.Payload.ServerId = serverId;
joinVoice.Payload.ChannelId = channelId;
QueueMessage(joinVoice);
}
public void SendLeaveVoice()


+ 6
- 4
src/Discord.Net/Net/WebSockets/VoiceWebSocket.cs View File

@@ -53,16 +53,19 @@ namespace Discord.Net.WebSockets
_targetAudioBufferLength = client.Config.VoiceBufferLength / 20; //20 ms frames
}

public async Task Login(string serverId, string userId, string sessionId, string token, CancellationToken cancelToken)
public void SetServer(string serverId)
{
if (_serverId == serverId && _userId == userId && _sessionId == sessionId && _token == token)
_serverId = serverId;
}
public async Task Login(string userId, string sessionId, string token, CancellationToken cancelToken)
{
if ((WebSocketState)_state != WebSocketState.Disconnected)
{
//Adjust the host and tell the system to reconnect
await DisconnectInternal(new Exception("Server transfer occurred."), isUnexpected: false);
return;
}
_serverId = serverId;
_userId = userId;
_sessionId = sessionId;
_token = token;
@@ -135,7 +138,6 @@ namespace Discord.Net.WebSockets
ClearPCMFrames();
if (!_wasDisconnectUnexpected)
{
_serverId = null;
_userId = null;
_sessionId = null;
_token = null;


Loading…
Cancel
Save