Skip to content

Commit 6830a83

Browse files
authored
Fix/voice socketengine (#1337)
1 parent f21c6b9 commit 6830a83

File tree

6 files changed

+22
-13
lines changed

6 files changed

+22
-13
lines changed

include/dpp/cluster.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ class DPP_EXPORT cluster {
183183
* @brief Used to spawn the socket engine into its own thread if
184184
* the cluster is started with dpp::st_return. It is unused otherwise.
185185
*/
186-
std::unique_ptr<std::thread> engine_thread{nullptr};
186+
std::thread engine_thread;
187187

188188
/**
189189
* @brief Protection mutex for timers

src/dpp/cluster.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ void cluster::start(bool return_after) {
298298
});
299299

300300
if (return_after) {
301-
engine_thread = std::make_unique<std::thread>([event_loop]() {
301+
engine_thread = std::thread([event_loop]() {
302302
dpp::utility::set_thread_name("event_loop");
303303
event_loop();
304304
});
@@ -310,9 +310,12 @@ void cluster::start(bool return_after) {
310310
void cluster::shutdown() {
311311
/* Signal termination */
312312
terminating = true;
313-
if (engine_thread) {
314-
engine_thread->join();
313+
314+
if (engine_thread.joinable()) {
315+
/* Join engine_thread if it ever started */
316+
engine_thread.join();
315317
}
318+
316319
{
317320
std::lock_guard<std::mutex> l(timer_guard);
318321
/* Free memory for active timers */
@@ -322,6 +325,7 @@ void cluster::shutdown() {
322325
timer_list.clear();
323326
next_timer.clear();
324327
}
328+
325329
/* Terminate shards */
326330
for (const auto& sh : shards) {
327331
delete sh.second;

src/dpp/socketengines/epoll.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ struct DPP_EXPORT socket_engine_epoll : public socket_engine_base {
111111
}
112112

113113
if ((ev.events & EPOLLOUT) != 0U) {
114+
/* Should we have a flag to allow keeping WANT_WRITE? Maybe like WANT_WRITE_ONCE or GREEDY_WANT_WRITE, eh */
114115
eh->flags = modify_event(epoll_handle, eh, eh->flags & ~WANT_WRITE);
115116
if (eh->on_write) {
116117
eh->on_write(fd, *eh);

src/dpp/sslclient.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,10 @@ void ssl_client::on_read(socket fd, const struct socket_events& ev) {
373373
}
374374

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

377381
if (!tcp_connect_done) {
378382
tcp_connect_done = true;

src/dpp/voice/enabled/read_write.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ void discord_voice_client::send(const char* packet, size_t len, uint64_t duratio
3939
} else [[unlikely]] {
4040
this->udp_send(packet, len);
4141
}
42-
if (!this->sent_stop_frames) {
43-
udp_events.flags = WANT_READ | WANT_WRITE | WANT_ERROR;
44-
owner->socketengine->update_socket(udp_events);
45-
}
4642
}
4743

4844
int discord_voice_client::udp_send(const char* data, size_t length) {

src/dpp/voice/enabled/write_ready.cpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@
3131
namespace dpp {
3232

3333
void discord_voice_client::write_ready() {
34+
/*
35+
* WANT_WRITE has been reset everytime this method is being called,
36+
* ALWAYS set it again no matter what we're gonna do.
37+
*/
38+
udp_events.flags = WANT_READ | WANT_WRITE | WANT_ERROR;
39+
owner->socketengine->update_socket(udp_events);
40+
3441
uint64_t duration = 0;
3542
bool track_marker_found = false;
3643
uint64_t bufsize = 0;
@@ -54,15 +61,12 @@ void discord_voice_client::write_ready() {
5461
}
5562
}
5663
if (!outbuf.empty()) {
57-
if (this->udp_send(outbuf[0].packet.data(), outbuf[0].packet.length()) == (int)outbuf[0].packet.length()) {
64+
int sent_siz = this->udp_send(outbuf[0].packet.data(), outbuf[0].packet.length());
65+
if (sent_siz == (int)outbuf[0].packet.length()) {
5866
duration = outbuf[0].duration * timescale;
5967
bufsize = outbuf[0].packet.length();
6068
outbuf.erase(outbuf.begin());
6169
}
62-
if (!outbuf.empty()) {
63-
udp_events.flags = WANT_READ | WANT_WRITE | WANT_ERROR;
64-
owner->socketengine->update_socket(udp_events);
65-
}
6670
}
6771
}
6872
}

0 commit comments

Comments
 (0)