|
|
@ -2,6 +2,7 @@ |
|
|
|
#include "Log.h" |
|
|
|
#include "Log.h" |
|
|
|
|
|
|
|
|
|
|
|
#include <set> |
|
|
|
#include <set> |
|
|
|
|
|
|
|
#include <functional> |
|
|
|
|
|
|
|
|
|
|
|
#include <websocketpp/config/asio_no_tls.hpp> |
|
|
|
#include <websocketpp/config/asio_no_tls.hpp> |
|
|
|
#include <websocketpp/server.hpp> |
|
|
|
#include <websocketpp/server.hpp> |
|
|
@ -27,7 +28,11 @@ namespace i2p |
|
|
|
typedef ServerImpl::message_ptr MessagePtr; |
|
|
|
typedef ServerImpl::message_ptr MessagePtr; |
|
|
|
public: |
|
|
|
public: |
|
|
|
|
|
|
|
|
|
|
|
WebsocketServerImpl(const std::string & addr, int port) : m_run(false), m_thread(nullptr) |
|
|
|
WebsocketServerImpl(const std::string & addr, int port) : |
|
|
|
|
|
|
|
m_run(false), |
|
|
|
|
|
|
|
m_ws_thread(nullptr), |
|
|
|
|
|
|
|
m_ev_thread(nullptr), |
|
|
|
|
|
|
|
m_WebsocketTicker(m_Service) |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_server.init_asio(); |
|
|
|
m_server.init_asio(); |
|
|
|
m_server.set_open_handler(std::bind(&WebsocketServerImpl::ConnOpened, this, std::placeholders::_1)); |
|
|
|
m_server.set_open_handler(std::bind(&WebsocketServerImpl::ConnOpened, this, std::placeholders::_1)); |
|
|
@ -44,7 +49,7 @@ namespace i2p |
|
|
|
void Start() { |
|
|
|
void Start() { |
|
|
|
m_run = true; |
|
|
|
m_run = true; |
|
|
|
m_server.start_accept(); |
|
|
|
m_server.start_accept(); |
|
|
|
m_thread = new std::thread([&] () { |
|
|
|
m_ws_thread = new std::thread([&] () { |
|
|
|
while(m_run) { |
|
|
|
while(m_run) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
m_server.run(); |
|
|
|
m_server.run(); |
|
|
@ -53,16 +58,35 @@ namespace i2p |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
m_ev_thread = new std::thread([&] () { |
|
|
|
|
|
|
|
while(m_run) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
m_Service.run(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} catch (std::exception & e ) { |
|
|
|
|
|
|
|
LogPrint(eLogError, "Websocket service: ", e.what()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
ScheduleTick(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Stop() { |
|
|
|
void Stop() { |
|
|
|
m_run = false; |
|
|
|
m_run = false; |
|
|
|
|
|
|
|
m_Service.stop(); |
|
|
|
m_server.stop(); |
|
|
|
m_server.stop(); |
|
|
|
if(m_thread) { |
|
|
|
|
|
|
|
m_thread->join(); |
|
|
|
if(m_ev_thread) { |
|
|
|
delete m_thread; |
|
|
|
m_ev_thread->join(); |
|
|
|
|
|
|
|
delete m_ev_thread; |
|
|
|
} |
|
|
|
} |
|
|
|
m_thread = nullptr; |
|
|
|
m_ev_thread = nullptr; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(m_ws_thread) { |
|
|
|
|
|
|
|
m_ws_thread->join(); |
|
|
|
|
|
|
|
delete m_ws_thread; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
m_ws_thread = nullptr; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void ConnOpened(ServerConn c) |
|
|
|
void ConnOpened(ServerConn c) |
|
|
@ -82,11 +106,40 @@ namespace i2p |
|
|
|
(void) conn; |
|
|
|
(void) conn; |
|
|
|
(void) msg; |
|
|
|
(void) msg; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void HandleTick(const boost::system::error_code & ec) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(ec != boost::asio::error::operation_aborted) |
|
|
|
|
|
|
|
LogPrint(eLogError, "Websocket ticker: ", ec.message()); |
|
|
|
|
|
|
|
// pump collected events to us
|
|
|
|
|
|
|
|
i2p::event::core.PumpCollected(this); |
|
|
|
|
|
|
|
ScheduleTick(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ScheduleTick() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LogPrint(eLogDebug, "Websocket schedule tick"); |
|
|
|
|
|
|
|
boost::posix_time::seconds dlt(1); |
|
|
|
|
|
|
|
m_WebsocketTicker.expires_from_now(dlt); |
|
|
|
|
|
|
|
m_WebsocketTicker.async_wait(std::bind(&WebsocketServerImpl::HandleTick, this, std::placeholders::_1)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief called from m_ev_thread */ |
|
|
|
|
|
|
|
void HandlePumpEvent(const EventType & ev, const uint64_t & val) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
EventType e; |
|
|
|
|
|
|
|
for (const auto & i : ev) |
|
|
|
|
|
|
|
e[i.first] = i.second; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
e["number"] = std::to_string(val); |
|
|
|
|
|
|
|
HandleEvent(e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief called from m_ws_thread */ |
|
|
|
void HandleEvent(const EventType & ev) |
|
|
|
void HandleEvent(const EventType & ev) |
|
|
|
{ |
|
|
|
{ |
|
|
|
std::lock_guard<std::mutex> lock(m_connsMutex); |
|
|
|
std::lock_guard<std::mutex> lock(m_connsMutex); |
|
|
|
LogPrint(eLogDebug, "websocket event"); |
|
|
|
|
|
|
|
boost::property_tree::ptree event; |
|
|
|
boost::property_tree::ptree event; |
|
|
|
for (const auto & item : ev) { |
|
|
|
for (const auto & item : ev) { |
|
|
|
event.put(item.first, item.second); |
|
|
|
event.put(item.first, item.second); |
|
|
@ -105,10 +158,13 @@ namespace i2p |
|
|
|
private: |
|
|
|
private: |
|
|
|
typedef std::set<ServerConn, std::owner_less<ServerConn> > ConnList; |
|
|
|
typedef std::set<ServerConn, std::owner_less<ServerConn> > ConnList; |
|
|
|
bool m_run; |
|
|
|
bool m_run; |
|
|
|
std::thread * m_thread; |
|
|
|
std::thread * m_ws_thread; |
|
|
|
|
|
|
|
std::thread * m_ev_thread; |
|
|
|
std::mutex m_connsMutex; |
|
|
|
std::mutex m_connsMutex; |
|
|
|
ConnList m_conns; |
|
|
|
ConnList m_conns; |
|
|
|
ServerImpl m_server; |
|
|
|
ServerImpl m_server; |
|
|
|
|
|
|
|
boost::asio::io_service m_Service; |
|
|
|
|
|
|
|
boost::asio::deadline_timer m_WebsocketTicker; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|