Skip to content

Commit

Permalink
fix: use exceptions to bail out of parseheader
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Dec 12, 2024
1 parent 5a06ebc commit 4ee7abb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/dpp/discordclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,17 @@ bool discord_client::handle_frame(const std::string &buffer, ws_opcode opcode)
case Z_STREAM_ERROR:
this->error(err_compression_stream);
this->close();
return false;
return true;
case Z_DATA_ERROR:
this->error(err_compression_data);
this->close();
return false;
return true;
case Z_MEM_ERROR:
this->error(err_compression_memory);
this->close();
return false;
return true;
case Z_OK:
this->decompressed.append((const char*)decomp_buffer.data(), have);
Expand Down Expand Up @@ -353,9 +356,8 @@ bool discord_client::handle_frame(const std::string &buffer, ws_opcode opcode)
}
break;
case ft_reconnect:
log(dpp::ll_debug, "Reconnection requested, closing socket " + sessionid);
message_queue.clear();
this->close();
throw dpp::connection_exception("Reconnection requested, closing socket " + sessionid);
break;
/* Heartbeat ack */
case ft_heartbeat_ack:
Expand All @@ -369,8 +371,7 @@ bool discord_client::handle_frame(const std::string &buffer, ws_opcode opcode)
case ft_resume:
case ft_request_guild_members:
case ft_request_soundboard_sounds:
log(dpp::ll_error, "Received invalid opcode on websocket for session " + sessionid);
break;
throw dpp::connection_exception("Received invalid opcode on websocket for session " + sessionid);
}
}
return true;
Expand Down
12 changes: 10 additions & 2 deletions src/dpp/wsclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,13 @@ bool websocket_client::handle_buffer(std::string& buffer)
}
} else if (state == CONNECTED) {
/* Process packets until we can't (buffer will erase data until parseheader returns false) */
while (this->parseheader(buffer)) { }
try {
while (this->parseheader(buffer)) { }
}
catch (const std::exception &e) {
log(ll_debug, "Receiving exception: " + std::string(e.what()));
return false;
}
}

return true;
Expand Down Expand Up @@ -274,7 +280,9 @@ bool websocket_client::parseheader(std::string& data)
handle_ping(data.substr(payloadstartoffset, len));
} else if ((opcode & ~WS_FINBIT) != OP_PONG) { /* Otherwise, handle everything else apart from a PONG. */
/* Pass this frame to the deriving class */
this->handle_frame(data.substr(payloadstartoffset, len), static_cast<ws_opcode>(opcode & ~WS_FINBIT));
if (!this->handle_frame(data.substr(payloadstartoffset, len), static_cast<ws_opcode>(opcode & ~WS_FINBIT))) {
return false;
}
}

/* Remove this frame from the input buffer */
Expand Down

0 comments on commit 4ee7abb

Please sign in to comment.