Skip to content

Commit

Permalink
Fix/voice socketengine (#1337)
Browse files Browse the repository at this point in the history
  • Loading branch information
Neko-Life authored Nov 27, 2024
1 parent f21c6b9 commit 6830a83
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion include/dpp/cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class DPP_EXPORT cluster {
* @brief Used to spawn the socket engine into its own thread if
* the cluster is started with dpp::st_return. It is unused otherwise.
*/
std::unique_ptr<std::thread> engine_thread{nullptr};
std::thread engine_thread;

/**
* @brief Protection mutex for timers
Expand Down
10 changes: 7 additions & 3 deletions src/dpp/cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ void cluster::start(bool return_after) {
});

if (return_after) {
engine_thread = std::make_unique<std::thread>([event_loop]() {
engine_thread = std::thread([event_loop]() {
dpp::utility::set_thread_name("event_loop");
event_loop();
});
Expand All @@ -310,9 +310,12 @@ void cluster::start(bool return_after) {
void cluster::shutdown() {
/* Signal termination */
terminating = true;
if (engine_thread) {
engine_thread->join();

if (engine_thread.joinable()) {
/* Join engine_thread if it ever started */
engine_thread.join();
}

{
std::lock_guard<std::mutex> l(timer_guard);
/* Free memory for active timers */
Expand All @@ -322,6 +325,7 @@ void cluster::shutdown() {
timer_list.clear();
next_timer.clear();
}

/* Terminate shards */
for (const auto& sh : shards) {
delete sh.second;
Expand Down
1 change: 1 addition & 0 deletions src/dpp/socketengines/epoll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ struct DPP_EXPORT socket_engine_epoll : public socket_engine_base {
}

if ((ev.events & EPOLLOUT) != 0U) {
/* Should we have a flag to allow keeping WANT_WRITE? Maybe like WANT_WRITE_ONCE or GREEDY_WANT_WRITE, eh */
eh->flags = modify_event(epoll_handle, eh, eh->flags & ~WANT_WRITE);
if (eh->on_write) {
eh->on_write(fd, *eh);
Expand Down
4 changes: 4 additions & 0 deletions src/dpp/sslclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,10 @@ void ssl_client::on_read(socket fd, const struct socket_events& ev) {
}

void ssl_client::on_write(socket fd, const struct socket_events& e) {
/* We wanted write before so keep it */
socket_events se{e};
se.flags |= WANT_WRITE;
owner->socketengine->update_socket(se);

if (!tcp_connect_done) {
tcp_connect_done = true;
Expand Down
4 changes: 0 additions & 4 deletions src/dpp/voice/enabled/read_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ void discord_voice_client::send(const char* packet, size_t len, uint64_t duratio
} else [[unlikely]] {
this->udp_send(packet, len);
}
if (!this->sent_stop_frames) {
udp_events.flags = WANT_READ | WANT_WRITE | WANT_ERROR;
owner->socketengine->update_socket(udp_events);
}
}

int discord_voice_client::udp_send(const char* data, size_t length) {
Expand Down
14 changes: 9 additions & 5 deletions src/dpp/voice/enabled/write_ready.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
namespace dpp {

void discord_voice_client::write_ready() {
/*
* WANT_WRITE has been reset everytime this method is being called,
* ALWAYS set it again no matter what we're gonna do.
*/
udp_events.flags = WANT_READ | WANT_WRITE | WANT_ERROR;
owner->socketengine->update_socket(udp_events);

uint64_t duration = 0;
bool track_marker_found = false;
uint64_t bufsize = 0;
Expand All @@ -54,15 +61,12 @@ void discord_voice_client::write_ready() {
}
}
if (!outbuf.empty()) {
if (this->udp_send(outbuf[0].packet.data(), outbuf[0].packet.length()) == (int)outbuf[0].packet.length()) {
int sent_siz = this->udp_send(outbuf[0].packet.data(), outbuf[0].packet.length());
if (sent_siz == (int)outbuf[0].packet.length()) {
duration = outbuf[0].duration * timescale;
bufsize = outbuf[0].packet.length();
outbuf.erase(outbuf.begin());
}
if (!outbuf.empty()) {
udp_events.flags = WANT_READ | WANT_WRITE | WANT_ERROR;
owner->socketengine->update_socket(udp_events);
}
}
}
}
Expand Down

0 comments on commit 6830a83

Please sign in to comment.