From 9ed979c79a3527e42634d56c74e59cff2b95b459 Mon Sep 17 00:00:00 2001 From: cryptocode Date: Sat, 23 Mar 2019 01:08:23 +0100 Subject: [PATCH] Fix locking, add bind address to config, alwayts pop front message, startup logging and optimize for likely errocode path --- nano/node/node.cpp | 3 ++- nano/node/websocket.cpp | 15 ++++++++------- nano/node/websocketconfig.cpp | 2 ++ nano/node/websocketconfig.hpp | 2 ++ 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/nano/node/node.cpp b/nano/node/node.cpp index 733ade0765..5fef6b2c16 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -1050,7 +1050,8 @@ startup_time (std::chrono::steady_clock::now ()) { if (config.websocket_config.enabled) { - websocket_server = std::make_shared (*this, nano::tcp_endpoint (boost::asio::ip::address_v6::any (), config.websocket_config.port)); + auto endpoint_l (nano::tcp_endpoint (config.websocket_config.address, config.websocket_config.port)); + websocket_server = std::make_shared (*this, endpoint_l); this->websocket_server->run (); } diff --git a/nano/node/websocket.cpp b/nano/node/websocket.cpp index f9ea801e6c..9c5c54148b 100644 --- a/nano/node/websocket.cpp +++ b/nano/node/websocket.cpp @@ -8,6 +8,7 @@ nano::websocket::session::session (nano::websocket::listener & listener_a, boost ws_listener (listener_a), ws (std::move (socket_a)), write_strand (ws.get_executor ()) { ws.text (true); + ws_listener.get_node ().logger.try_log ("websocket session started"); } nano::websocket::session::~session () @@ -19,14 +20,14 @@ void nano::websocket::session::handshake () { std::lock_guard lk (io_mutex); ws.async_accept ([self_l = shared_from_this ()](boost::system::error_code const & ec) { - if (ec) + if (!ec) { - self_l->ws_listener.get_node ().logger.always_log ("websocket handshake failed: ", ec.message ()); + // Start reading incoming messages + self_l->read (); } else { - // Start reading incoming messages - self_l->read (); + self_l->ws_listener.get_node ().logger.always_log ("websocket handshake failed: ", ec.message ()); } }); } @@ -44,10 +45,10 @@ void nano::websocket::session::close () void nano::websocket::session::write (nano::websocket::message message_a) { // clang-format off - std::lock_guard lk (subscriptions_mutex); + std::unique_lock lk (subscriptions_mutex); if (message_a.topic == nano::websocket::topic::ack || subscriptions.find (message_a.topic) != subscriptions.end ()) { - subscriptions_mutex.unlock (); + lk.unlock (); boost::asio::post (write_strand, [message_a, self_l = shared_from_this ()]() { bool write_in_progress = !self_l->send_queue.empty (); @@ -71,9 +72,9 @@ void nano::websocket::session::write_queued_messages () ws.async_write (boost::asio::buffer (msg_str.data (), msg_str.size ()), boost::asio::bind_executor (write_strand, [msg, self_l = shared_from_this ()](boost::system::error_code ec, std::size_t bytes_transferred) { + self_l->send_queue.pop_front (); if (!ec) { - self_l->send_queue.pop_front (); if (!self_l->send_queue.empty ()) { self_l->write_queued_messages (); diff --git a/nano/node/websocketconfig.cpp b/nano/node/websocketconfig.cpp index 3ba9badcc6..3f797b75a3 100644 --- a/nano/node/websocketconfig.cpp +++ b/nano/node/websocketconfig.cpp @@ -4,6 +4,7 @@ nano::error nano::websocket::config::serialize_json (nano::jsonconfig & json) const { json.put ("enable", enabled); + json.put ("address", address.to_string ()); json.put ("port", port); return json.get_error (); } @@ -11,6 +12,7 @@ nano::error nano::websocket::config::serialize_json (nano::jsonconfig & json) co nano::error nano::websocket::config::deserialize_json (nano::jsonconfig & json) { json.get ("enable", enabled); + json.get_required ("address", address); json.get ("port", port); return json.get_error (); } diff --git a/nano/node/websocketconfig.hpp b/nano/node/websocketconfig.hpp index 125510768a..0f987b1685 100644 --- a/nano/node/websocketconfig.hpp +++ b/nano/node/websocketconfig.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -16,6 +17,7 @@ namespace websocket nano::error serialize_json (nano::jsonconfig & json) const; bool enabled{ false }; uint16_t port{ 7078 }; + boost::asio::ip::address_v6 address{ boost::asio::ip::address_v6::loopback () }; }; } }