public async Task <IAudioClient> Join(Channel channel) { if (channel == null) { throw new ArgumentNullException(nameof(channel)); } var server = channel.Server; using (await _asyncLock.LockAsync().ConfigureAwait(false)) { if (Config.EnableMultiserver) { AudioClient client; if (!_voiceClients.TryGetValue(server.Id, out client)) { client = new AudioClient(Client, server, unchecked (++_nextClientId)); _voiceClients[server.Id] = client; await client.Connect().ConfigureAwait(false); /*voiceClient.VoiceSocket.FrameReceived += (s, e) => * { * OnFrameReceieved(e); * }; * voiceClient.VoiceSocket.UserIsSpeaking += (s, e) => * { * var user = server.GetUser(e.UserId); * OnUserIsSpeakingUpdated(user, e.IsSpeaking); * };*/ } await client.Join(channel).ConfigureAwait(false); return(client); } else { if (_defaultClient.Server != server) { await _defaultClient.Disconnect().ConfigureAwait(false); _defaultClient.VoiceSocket.Server = server; await _defaultClient.Connect().ConfigureAwait(false); } var client = new VirtualClient(_defaultClient, server); _currentClient = client; await client.Join(channel).ConfigureAwait(false); return(client); } } }
private async Task<IAudioClient> CreateClient(Server server) { var client = _voiceClients.GetOrAdd(server.Id, _ => null); //Placeholder, so we can't have two clients connecting at once if (client == null) { int id = unchecked(++_nextClientId); var gatewayLogger = Client.Log.CreateLogger($"Gateway #{id}"); var voiceLogger = Client.Log.CreateLogger($"Voice #{id}"); var gatewaySocket = new GatewaySocket(Client, gatewayLogger); var voiceClient = new AudioClient(this, id, server, Client.GatewaySocket, voiceLogger); await voiceClient.Connect(true).ConfigureAwait(false); /*voiceClient.VoiceSocket.FrameReceived += (s, e) => { OnFrameReceieved(e); }; voiceClient.VoiceSocket.UserIsSpeaking += (s, e) => { var user = server.GetUser(e.UserId); OnUserIsSpeakingUpdated(user, e.IsSpeaking); };*/ //Update the placeholder only it still exists (RemoveClient wasnt called) if (!_voiceClients.TryUpdate(server.Id, voiceClient, null)) { //If it was, cleanup await voiceClient.Disconnect().ConfigureAwait(false); ; await gatewaySocket.Disconnect().ConfigureAwait(false); ; } } return client; }