23 using config = websocketpp::config::asio;
24 using transport_type = websocketpp::config::asio::transport_type;
25 using server_t = websocketpp::server<websocketpp::config::asio>;
26 using type = server_t;
27 using connection_handler = websocketpp::connection_hdl;
29 typedef typename config::concurrency_type concurrency_type;
30 typedef websocketpp::connection<config> connection_type;
31 typedef typename connection_type::ptr connection_ptr;
32 typedef typename transport_type::transport_con_type transport_con_type;
33 typedef typename transport_con_type::ptr transport_con_ptr;
34 typedef websocketpp::endpoint<connection_type, config> endpoint_type;
37 : m_server{std::make_shared<server_t>()}
38 , m_owns_context{
true}
40 m_server->init_asio();
41 m_server->set_reuse_addr(
true);
42 m_server->clear_access_channels(websocketpp::log::alevel::all);
43 m_server->set_socket_init_handler(init_handler);
47 : m_server{std::make_shared<server_t>()}
49 , m_owns_context{
false}
51 m_server->init_asio(&ctx->context);
52 m_server->set_reuse_addr(
true);
53 m_server->clear_access_channels(websocketpp::log::alevel::all);
54 m_server->set_socket_init_handler(init_handler);
57 static void init_handler(websocketpp::connection_hdl, boost::asio::ip::tcp::socket& s)
59 boost::asio::ip::tcp::no_delay option(
true);
70 template <
typename Handler>
71 void set_open_handler(Handler h)
73 m_server->set_open_handler(h);
76 template <
typename Handler>
77 void set_close_handler(Handler h)
79 m_server->set_close_handler(h);
82 template <
typename Handler>
83 void set_message_handler(Handler h)
85 m_server->set_message_handler(
86 [
this, h](connection_handler hdl, server_t::message_ptr msg) {
87#if defined OSSIA_BENCHMARK
88 auto t1 = std::chrono::high_resolution_clock::now();
92 auto res = h(hdl, msg->get_opcode(), msg->get_raw_payload());
93 if(res.data.size() > 0)
95 send_message(hdl, res);
100 auto con = m_server->get_con_from_hdl(hdl);
102 "Node not found: {} ==> {}", con->get_uri()->get_resource(), e.what());
106 auto con = m_server->get_con_from_hdl(hdl);
108 "Error in request: {} ==> {}", con->get_uri()->get_resource(), e.what());
110 catch(
const std::exception& e)
112 auto con = m_server->get_con_from_hdl(hdl);
117 auto con = m_server->get_con_from_hdl(hdl);
121#if defined OSSIA_BENCHMARK
122 auto t2 = std::chrono::high_resolution_clock::now();
125 std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count());
129 m_server->set_http_handler([
this, h](connection_handler hdl) {
130 auto con = m_server->get_con_from_hdl(hdl);
133 con->append_header(
"Access-Control-Allow-Origin",
"*");
137 ossia::net::server_reply str
138 = h(hdl, websocketpp::frame::opcode::TEXT, con->get_uri()->get_resource());
142 case server_reply::data_type::json: {
143 con->replace_header(
"Content-Type",
"application/json; charset=utf-8");
147 case server_reply::data_type::html: {
148 con->replace_header(
"Content-Type",
"text/html; charset=utf-8");
154 con->replace_header(
"Connection",
"close");
155 con->set_body(std::move(str.data));
156 con->set_status(websocketpp::http::status_code::ok);
161 con->set_status(websocketpp::http::status_code::not_found);
166 "Error in request: {} ==> {}", con->get_uri()->get_resource(), e.what());
167 con->set_status(websocketpp::http::status_code::bad_request);
169 catch(
const std::exception& e)
177 con->set_status(websocketpp::http::status_code::internal_server_error);
181 void listen(uint16_t port = 9002)
183 m_server->listen(boost::asio::ip::tcp::v4(), port);
185 boost::system::error_code ec;
191 ec = boost::system::error_code();
192 auto con = m_server->get_connection(ec);
200 namespace lib = websocketpp::lib;
201 m_server->transport_type::async_accept(
202 lib::static_pointer_cast<transport_con_type>(con),
203 [server = m_server, con](lib::error_code ec) {
207 ec = websocketpp::error::http_connection_ended;
208 server->handle_accept_legacy(con, ec);
216 con->terminate(lib::error_code());
220 void run() { m_server->run(); }
232 boost::system::error_code ec;
233 if(m_server->is_listening())
234 m_server->stop_listening(ec);
247 m_context->context, [s = m_server]()
mutable { new decltype(s){s}; });
251 void close(connection_handler hdl)
253 auto con = m_server->get_con_from_hdl(hdl);
254 con->close(websocketpp::close::status::going_away,
"Server shutdown");
257 void send_message(connection_handler hdl,
const std::string& message)
259 auto con = m_server->get_con_from_hdl(hdl);
263 void send_message(connection_handler hdl,
const ossia::net::server_reply& message)
265 auto con = m_server->get_con_from_hdl(hdl);
268 case server_reply::data_type::json:
269 case server_reply::data_type::html:
270 con->send(message.data, websocketpp::frame::opcode::TEXT);
273 con->send(message.data, websocketpp::frame::opcode::BINARY);
278 void send_message(connection_handler hdl,
const rapidjson::StringBuffer& message)
280 auto con = m_server->get_con_from_hdl(hdl);
281 con->send(message.GetString(), message.GetSize(), websocketpp::frame::opcode::text);
284 void send_binary_message(connection_handler hdl,
const std::string& message)
286 auto con = m_server->get_con_from_hdl(hdl);
287 con->send(message.data(), message.size(), websocketpp::frame::opcode::binary);
290 void send_binary_message(connection_handler hdl, std::string_view message)
292 auto con = m_server->get_con_from_hdl(hdl);
293 con->send(message.data(), message.size(), websocketpp::frame::opcode::binary);
296 server_t& impl() {
return *m_server; }
299 std::shared_ptr<server_t> m_server;
300 ossia::net::network_context_ptr m_context;
301 bool m_owns_context{};