add udp keepalive

This commit is contained in:
ouwou
2022-09-29 22:47:00 -04:00
parent dc127d15fb
commit b735feb901
2 changed files with 21 additions and 1 deletions

View File

@@ -188,6 +188,7 @@ DiscordVoiceClient::~DiscordVoiceClient() {
void DiscordVoiceClient::Start() {
m_ws.StartConnection("wss://" + m_endpoint + "/?v=7");
m_heartbeat_waiter.revive();
m_keepalive_waiter.revive();
}
void DiscordVoiceClient::Stop() {
@@ -196,6 +197,8 @@ void DiscordVoiceClient::Stop() {
m_udp.Stop();
m_heartbeat_waiter.kill();
if (m_heartbeat_thread.joinable()) m_heartbeat_thread.join();
m_keepalive_waiter.kill();
if (m_keepalive_thread.joinable()) m_keepalive_thread.join();
m_connected = false;
m_signal_disconnected.emit();
}
@@ -271,6 +274,7 @@ void DiscordVoiceClient::HandleGatewayReady(const VoiceGatewayMessage &m) {
printf("connect to %s:%u ssrc %u\n", m_ip.c_str(), m_port, m_ssrc);
m_udp.Connect(m_ip, m_port);
m_keepalive_thread = std::thread(&DiscordVoiceClient::KeepaliveThread, this);
Discovery();
}
@@ -385,6 +389,18 @@ void DiscordVoiceClient::HeartbeatThread() {
}
}
void DiscordVoiceClient::KeepaliveThread() {
while (true) {
if (!m_keepalive_waiter.wait_for(std::chrono::seconds(10)))
break;
if (IsConnected()) {
const static uint8_t KEEPALIVE[] = { 0x13, 0x37 };
m_udp.Send(KEEPALIVE, sizeof(KEEPALIVE));
}
}
}
DiscordVoiceClient::type_signal_disconnected DiscordVoiceClient::signal_connected() {
return m_signal_connected;
}

View File

@@ -5,8 +5,8 @@
#include "snowflake.hpp"
#include "waiter.hpp"
#include "websocket.hpp"
#include <optional>
#include <mutex>
#include <optional>
#include <queue>
#include <string>
#include <glibmm/dispatcher.h>
@@ -211,6 +211,7 @@ private:
void OnUDPData(std::vector<uint8_t> data);
void HeartbeatThread();
void KeepaliveThread();
std::string m_session_id;
std::string m_endpoint;
@@ -242,6 +243,9 @@ private:
Waiter m_heartbeat_waiter;
std::thread m_heartbeat_thread;
Waiter m_keepalive_waiter;
std::thread m_keepalive_thread;
std::array<uint8_t, 1275> m_opus_buffer;
std::atomic<bool> m_connected = false;