Skip to content

Commit

Permalink
Fix locking, add bind address to config, alwayts pop front message, s…
Browse files Browse the repository at this point in the history
…tartup logging and optimize for likely errocode path
  • Loading branch information
cryptocode committed Mar 23, 2019
1 parent ce55125 commit 9ed979c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
3 changes: 2 additions & 1 deletion nano/node/node.cpp
Expand Up @@ -1050,7 +1050,8 @@ startup_time (std::chrono::steady_clock::now ())
{
if (config.websocket_config.enabled)
{
websocket_server = std::make_shared<nano::websocket::listener> (*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<nano::websocket::listener> (*this, endpoint_l);
this->websocket_server->run ();
}

Expand Down
15 changes: 8 additions & 7 deletions nano/node/websocket.cpp
Expand Up @@ -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 ()
Expand All @@ -19,14 +20,14 @@ void nano::websocket::session::handshake ()
{
std::lock_guard<std::mutex> 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 ());
}
});
}
Expand All @@ -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<std::mutex> lk (subscriptions_mutex);
std::unique_lock<std::mutex> 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 ();
Expand All @@ -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 ();
Expand Down
2 changes: 2 additions & 0 deletions nano/node/websocketconfig.cpp
Expand Up @@ -4,13 +4,15 @@
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 ();
}

nano::error nano::websocket::config::deserialize_json (nano::jsonconfig & json)
{
json.get<bool> ("enable", enabled);
json.get_required<boost::asio::ip::address_v6> ("address", address);
json.get<uint16_t> ("port", port);
return json.get_error ();
}
2 changes: 2 additions & 0 deletions nano/node/websocketconfig.hpp
@@ -1,5 +1,6 @@
#pragma once

#include <boost/asio.hpp>
#include <nano/lib/errors.hpp>
#include <string>

Expand All @@ -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 () };
};
}
}

0 comments on commit 9ed979c

Please sign in to comment.