Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
abhaybd committed Oct 3, 2023
1 parent 35b33ce commit c72a977
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/network/websocket/WebSocketProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class WebSocketProtocol {
* If the heartbeat is reestablished after timing out, and then times out again, this
* handler will be called again.
*
* @param timeout The heartbeat timeout.
* @param timeout The duration after which the heartbeat times out.
* @param handler The handler to call when timed out.
*/
void setHeartbeatTimedOutHandler(std::chrono::milliseconds timeout,
Expand Down
30 changes: 18 additions & 12 deletions src/network/websocket/WebSocketServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,24 +186,30 @@ void SingleClientWSServer::onMessage(connection_hdl hdl, message_t message) {
auto conn = server.get_con_from_hdl(hdl);
std::string path = conn->get_resource();

assert(protocolMap.find(path) != protocolMap.end());

std::string jsonStr = message->get_payload();
log(LOG_TRACE, "Message on %s: %s\n", path.c_str(), jsonStr.c_str());
json obj = json::parse(jsonStr);
protocolMap.at(path).protocol->processMessage(obj);
auto it = protocolMap.find(path);
if (it != protocolMap.end()) {
std::string jsonStr = message->get_payload();
log(LOG_TRACE, "Message on %s: %s\n", path.c_str(), jsonStr.c_str());
json obj = json::parse(jsonStr);
it->second.protocol->processMessage(obj);
} else {
log(LOG_WARN, "Received message on unknown protocol path %s\n", path.c_str());
}
}

void SingleClientWSServer::onPong(connection_hdl hdl, const std::string& payload) {
log(LOG_DEBUG, "Pong from %s\n", payload.c_str());
auto conn = server.get_con_from_hdl(hdl);

assert(protocolMap.find(payload) != protocolMap.end());

auto& pd = protocolMap.at(payload);
std::lock_guard lock(pd.mutex);
if (pd.heartbeatInfo.has_value()) {
pd.heartbeatInfo->second.feed();
auto it = protocolMap.find(payload);
if (it != protocolMap.end()) {
auto& pd = it->second;
std::lock_guard lock(pd.mutex);
if (pd.heartbeatInfo.has_value()) {
pd.heartbeatInfo->second.feed();
}
} else {
log(LOG_WARN, "Received pong on unknown protocol path %s\n", payload.c_str());
}
}
} // namespace websocket
Expand Down

0 comments on commit c72a977

Please sign in to comment.