private async Task VerifyConnectionAsync(VoiceUpdatePacket packet, Task task) { if (task.VerifyTask()) { LogHandler <WsVoiceClient> .Log.Error(exception : task.Exception); return; } _receiveTask = _socket .ReceiveAsync <WsVoiceClient, BaseDiscordPayload>(_receiveCancel, ProcessPayloadAsync) .ContinueWith(DisposeAsync); var payload = new BaseDiscordPayload(VoiceOpType.Identify, new IdentifyPayload { ServerId = $"{packet.GuildId}", SessionId = packet.SessionId, UserId = $"{packet.UserId}", Token = packet.Token }); await _socket.SendAsync(payload) .ConfigureAwait(false); }
private async Task SendSpeakingAsync(bool isSpeaking) { var payload = new BaseDiscordPayload(VoiceOpType.Speaking, new { Speaking = isSpeaking, Delay = 0 }); await _socket.SendAsync(payload) .ConfigureAwait(false); }
private async Task HandleHeartbeatAsync(int interval) { while (!_heartBeatCancel.IsCancellationRequested) { var payload = new BaseDiscordPayload(VoiceOpType.Heartbeat, DateTimeOffset.UtcNow.ToUnixTimeSeconds()); await _socket.SendAsync(payload) .ConfigureAwait(false); await Task.Delay(interval, _heartBeatCancel.Token) .ConfigureAwait(false); } }
private async Task ProcessPayloadAsync(BaseDiscordPayload payload) { LogHandler <WsVoiceClient> .Log.Debug($"Received {Enum.GetName(typeof(VoiceOpType), payload.Op)} payload."); switch (payload.Op) { case VoiceOpType.Ready: Vrp = payload.Data.TryCast <VoiceReadyPayload>(); _udp = new UdpClient(Vrp.IpAddress, Vrp.Port); await _udp.SendDiscoveryAsync(Vrp.Ssrc).ConfigureAwait(false); LogHandler <WsVoiceClient> .Log.Debug($"Sent UDP discovery with {Vrp.Ssrc} ssrc."); _heartBeatTask = HandleHeartbeatAsync(Vrp.HeartbeatInterval); LogHandler <WsVoiceClient> .Log.Debug( $"Started heartbeat task with {Vrp.HeartbeatInterval} interval."); var selectProtocol = new BaseDiscordPayload(VoiceOpType.SelectProtocol, new SelectPayload(Vrp.IpAddress, Vrp.Port)); await _socket.SendAsync(selectProtocol) .ConfigureAwait(false); LogHandler <WsVoiceClient> .Log.Debug($"Sent select protocol with {Vrp.IpAddress}:{Vrp.Port}."); _ = VoiceSenderTask(); break; case VoiceOpType.SessionDescription: var sdp = payload.Data.TryCast <SessionDescriptionPayload>(); if (sdp.Mode != "xsalsa20_poly1305") { return; } _sdp = sdp; SodiumCodec = new SodiumCodec(_sdp.SecretKey); await _socket.SendAsync(new BaseDiscordPayload(VoiceOpType.Speaking, new { delay = 0, speaking = false })).ConfigureAwait(false); _ = SendKeepAliveAsync().ConfigureAwait(false); break; case VoiceOpType.Hello: var helloPayload = payload.Data.TryCast <HelloPayload>(); if (_heartBeatTask != null) { _heartBeatCancel.Cancel(false); _heartBeatTask.Dispose(); _heartBeatTask = null; } _heartBeatTask = HandleHeartbeatAsync(helloPayload.HeartBeatInterval); break; } }