Skip to content

Commit a1de84a

Browse files
fix socket close sequence
1 parent 28afcec commit a1de84a

2 files changed

Lines changed: 25 additions & 17 deletions

File tree

include/discord_ipc_cpp/discord_ipc_client.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,13 @@ class DiscordIPCClient {
211211
* setting \ref _stop_recv_thread to \c false. Lastly, the underlying socket
212212
* is closed.
213213
*
214+
* \param write_close Should send disconnect packet before closing socket.
215+
*
214216
* \return Success of the attempt to close connection.
215217
*
216218
* \see discord_ipc_cpp::websockets::SocketClient::close
217219
*/
218-
bool close();
220+
bool close(bool write_close);
219221

220222
/**
221223
* \brief Checks for successful authentication with socket.

src/discord_ipc_client.cpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ using discord_ipc_cpp::ipc_types::RichPresence;
3636
using discord_ipc_cpp::internal_ipc_types::AuthorizationRequest;
3737
using discord_ipc_cpp::internal_ipc_types::CommandRequest;
3838

39+
using discord_ipc_cpp::utils::find_discord_ipc_file;
40+
3941
std::vector<char> DiscordIPCClient::encode_packet(
4042
const Payload& payload
4143
) {
@@ -52,6 +54,8 @@ std::vector<char> DiscordIPCClient::encode_packet(
5254
}
5355

5456
void DiscordIPCClient::recv_thread() {
57+
_stop_recv_thread = false;
58+
5559
while (!_stop_recv_thread) {
5660
auto optional_payload = recv_packet();
5761

@@ -61,11 +65,6 @@ void DiscordIPCClient::recv_thread() {
6165

6266
Payload recv_payload = std::move(*optional_payload);
6367

64-
// std::cout << recv_payload.opcode
65-
// << ": "
66-
// << recv_payload.payload.to_string()
67-
// << std::endl;
68-
6968
switch (recv_payload.opcode) {
7069
case Opcode::op_ping:
7170
send_packet({ Opcode::op_pong, recv_payload.payload });
@@ -81,9 +80,12 @@ void DiscordIPCClient::recv_thread() {
8180
}
8281

8382
break;
84-
case Opcode::op_handshake:
8583
case Opcode::op_close:
86-
close();
84+
close(true);
85+
86+
break;
87+
case Opcode::op_quit:
88+
close(false);
8789

8890
break;
8991
default:
@@ -104,7 +106,7 @@ _stop_recv_thread(false),
104106
_successful_auth(false) {}
105107

106108
DiscordIPCClient::~DiscordIPCClient() {
107-
close();
109+
close(true);
108110
}
109111

110112
bool DiscordIPCClient::send_packet(const Payload& payload) {
@@ -143,9 +145,11 @@ std::optional<Payload> DiscordIPCClient::recv_packet() {
143145

144146
data = std::string(buffer.begin(), buffer.end());
145147

148+
bool has_data = data.length() > 0;
149+
146150
return Payload {
147-
static_cast<Opcode>(opcode),
148-
data.length() > 0 ? Parser::parse(data) : JSON()
151+
has_data ? static_cast<Opcode>(opcode) : Opcode::op_quit,
152+
has_data ? Parser::parse(data) : JSON()
149153
};
150154
}
151155

@@ -191,7 +195,6 @@ bool DiscordIPCClient::attempt_send_payload(
191195
return success;
192196
}
193197

194-
195198
bool DiscordIPCClient::connect() {
196199
bool ret = _socket.connect();
197200

@@ -228,13 +231,16 @@ bool DiscordIPCClient::has_successful_auth() {
228231
return _successful_auth;
229232
}
230233

231-
bool DiscordIPCClient::close() {
232-
send_packet({
233-
.opcode = Opcode::op_close,
234-
.payload = {}
235-
});
234+
bool DiscordIPCClient::close(bool write_close) {
235+
if (write_close) {
236+
send_packet({
237+
.opcode = Opcode::op_close,
238+
.payload = {}
239+
});
240+
}
236241

237242
_stop_recv_thread = true;
243+
_successful_auth = false;
238244

239245
std::this_thread::sleep_for(std::chrono::milliseconds(25));
240246

0 commit comments

Comments
 (0)