mirror of
https://github.com/Sani7/remote-ui.git
synced 2026-10-27 06:01:14 +01:00
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
This commit is contained in:
+13
-15
@@ -54,7 +54,6 @@ void Websocket::on_open(websocketpp::connection_hdl hdl)
|
|||||||
{
|
{
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> guard(m_action_lock);
|
std::lock_guard<std::mutex> guard(m_action_lock);
|
||||||
// std::cout << "on_open" << std::endl;
|
|
||||||
m_actions.push(action(SUBSCRIBE, hdl));
|
m_actions.push(action(SUBSCRIBE, hdl));
|
||||||
}
|
}
|
||||||
m_action_cond.notify_one();
|
m_action_cond.notify_one();
|
||||||
@@ -64,7 +63,6 @@ void Websocket::on_close(websocketpp::connection_hdl hdl)
|
|||||||
{
|
{
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> guard(m_action_lock);
|
std::lock_guard<std::mutex> guard(m_action_lock);
|
||||||
// std::cout << "on_close" << std::endl;
|
|
||||||
m_actions.push(action(UNSUBSCRIBE, hdl));
|
m_actions.push(action(UNSUBSCRIBE, hdl));
|
||||||
}
|
}
|
||||||
m_action_cond.notify_one();
|
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
|
// queue message up for sending by processing thread
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> guard(m_action_lock);
|
std::lock_guard<std::mutex> guard(m_action_lock);
|
||||||
// std::cout << "on_message" << std::endl;
|
|
||||||
m_actions.push(action(MESSAGE, hdl, msg));
|
m_actions.push(action(MESSAGE, hdl, msg));
|
||||||
}
|
}
|
||||||
m_action_cond.notify_one();
|
m_action_cond.notify_one();
|
||||||
@@ -121,8 +118,16 @@ void Websocket::process_messages()
|
|||||||
m_server.send(a.hdl, response, websocketpp::frame::opcode::TEXT);
|
m_server.send(a.hdl, response, websocketpp::frame::opcode::TEXT);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case BROADCAST:
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> guard(m_connection_lock);
|
||||||
|
for (auto it : m_connections)
|
||||||
|
{
|
||||||
|
m_server.send(it, a.payload, websocketpp::frame::opcode::TEXT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
// undefined.
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -132,23 +137,16 @@ void Websocket::process_broadcast()
|
|||||||
{
|
{
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
{
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||||
std::lock_guard<std::mutex> guard(m_connection_lock);
|
|
||||||
if (m_connections.empty())
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
std::string update = m_on_update();
|
std::string update = m_on_update();
|
||||||
if (update.empty() || update == "{}")
|
if (update.empty() || update == "{}")
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> guard(m_connection_lock);
|
std::lock_guard<std::mutex> guard(m_action_lock);
|
||||||
for (auto it = m_connections.begin(); it != m_connections.end(); ++it) {
|
m_actions.push(action(BROADCAST, update));
|
||||||
m_server.send(*it, update, websocketpp::frame::opcode::TEXT);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
m_action_cond.notify_one();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+4
-1
@@ -19,17 +19,20 @@ using websocketpp::lib::placeholders::_2;
|
|||||||
enum action_type {
|
enum action_type {
|
||||||
SUBSCRIBE,
|
SUBSCRIBE,
|
||||||
UNSUBSCRIBE,
|
UNSUBSCRIBE,
|
||||||
MESSAGE
|
MESSAGE,
|
||||||
|
BROADCAST
|
||||||
};
|
};
|
||||||
|
|
||||||
struct action {
|
struct action {
|
||||||
action(action_type t, websocketpp::connection_hdl h) : type(t), hdl(h) {}
|
action(action_type t, websocketpp::connection_hdl h) : type(t), hdl(h) {}
|
||||||
action(action_type t, websocketpp::connection_hdl h, Server::message_ptr m)
|
action(action_type t, websocketpp::connection_hdl h, Server::message_ptr m)
|
||||||
: type(t), hdl(h), msg(m) {}
|
: type(t), hdl(h), msg(m) {}
|
||||||
|
action(action_type t, std::string p) : type(t), payload(p) {}
|
||||||
|
|
||||||
action_type type;
|
action_type type;
|
||||||
websocketpp::connection_hdl hdl;
|
websocketpp::connection_hdl hdl;
|
||||||
Server::message_ptr msg;
|
Server::message_ptr msg;
|
||||||
|
std::string payload;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Websocket
|
class Websocket
|
||||||
|
|||||||
Reference in New Issue
Block a user