From bba27d9e86a62f1a1c5d9517c303b7920e60fe74 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 26 Aug 2024 23:11:44 +0200 Subject: [PATCH] websocket reducing cpu % from 12% to 3% this by adding this_thread sleep for 1ms and moving sending of data to the process_messages thread --- tools/websocket.cpp | 28 +++++++++++++--------------- tools/websocket.hpp | 5 ++++- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/tools/websocket.cpp b/tools/websocket.cpp index 6519b48..9cd7dd9 100644 --- a/tools/websocket.cpp +++ b/tools/websocket.cpp @@ -54,7 +54,6 @@ void Websocket::on_open(websocketpp::connection_hdl hdl) { { std::lock_guard guard(m_action_lock); - // std::cout << "on_open" << std::endl; m_actions.push(action(SUBSCRIBE, hdl)); } m_action_cond.notify_one(); @@ -64,7 +63,6 @@ void Websocket::on_close(websocketpp::connection_hdl hdl) { { std::lock_guard guard(m_action_lock); - // std::cout << "on_close" << std::endl; m_actions.push(action(UNSUBSCRIBE, hdl)); } m_action_cond.notify_one(); @@ -75,7 +73,6 @@ void Websocket::on_message(websocketpp::connection_hdl hdl, Server::message_ptr // queue message up for sending by processing thread { std::lock_guard guard(m_action_lock); - // std::cout << "on_message" << std::endl; m_actions.push(action(MESSAGE, hdl, msg)); } m_action_cond.notify_one(); @@ -121,8 +118,16 @@ void Websocket::process_messages() m_server.send(a.hdl, response, websocketpp::frame::opcode::TEXT); } break; + case BROADCAST: + { + std::lock_guard guard(m_connection_lock); + for (auto it : m_connections) + { + m_server.send(it, a.payload, websocketpp::frame::opcode::TEXT); + } + } + break; default: - // undefined. break; } } @@ -132,23 +137,16 @@ void Websocket::process_broadcast() { while (1) { - { - std::lock_guard guard(m_connection_lock); - if (m_connections.empty()) - { - continue; - } - } + std::this_thread::sleep_for(std::chrono::milliseconds(1)); std::string update = m_on_update(); if (update.empty() || update == "{}") { continue; } { - std::lock_guard guard(m_connection_lock); - for (auto it = m_connections.begin(); it != m_connections.end(); ++it) { - m_server.send(*it, update, websocketpp::frame::opcode::TEXT); - } + std::lock_guard guard(m_action_lock); + m_actions.push(action(BROADCAST, update)); } + m_action_cond.notify_one(); } } \ No newline at end of file diff --git a/tools/websocket.hpp b/tools/websocket.hpp index dbcd2e5..b65760d 100644 --- a/tools/websocket.hpp +++ b/tools/websocket.hpp @@ -19,17 +19,20 @@ using websocketpp::lib::placeholders::_2; enum action_type { SUBSCRIBE, UNSUBSCRIBE, - MESSAGE + MESSAGE, + BROADCAST }; struct action { action(action_type t, websocketpp::connection_hdl h) : type(t), hdl(h) {} action(action_type t, websocketpp::connection_hdl h, Server::message_ptr m) : type(t), hdl(h), msg(m) {} + action(action_type t, std::string p) : type(t), payload(p) {} action_type type; websocketpp::connection_hdl hdl; Server::message_ptr msg; + std::string payload; }; class Websocket