2 #include <ossia/detail/config.hpp>
4 #include <ossia/detail/json.hpp>
6 #include <ossia/network/exceptions.hpp>
7 #include <ossia/network/sockets/websocket_reply.hpp>
9 #include <websocketpp/config/asio_no_tls.hpp>
10 #include <websocketpp/http/request.hpp>
11 #include <websocketpp/server.hpp>
12 #if defined(OSSIA_BENCHMARK)
22 using server_t = websocketpp::server<websocketpp::config::asio>;
23 using connection_handler = websocketpp::connection_hdl;
28 m_server.set_reuse_addr(
true);
29 m_server.clear_access_channels(websocketpp::log::alevel::all);
30 m_server.set_socket_init_handler(init_handler);
35 m_server.init_asio(&ctx);
36 m_server.set_reuse_addr(
true);
37 m_server.clear_access_channels(websocketpp::log::alevel::all);
38 m_server.set_socket_init_handler(init_handler);
41 static void init_handler(websocketpp::connection_hdl, boost::asio::ip::tcp::socket& s)
43 boost::asio::ip::tcp::no_delay option(
true);
54 template <
typename Handler>
55 void set_open_handler(Handler h)
57 m_server.set_open_handler(h);
60 template <
typename Handler>
61 void set_close_handler(Handler h)
63 m_server.set_close_handler(h);
66 template <
typename Handler>
67 void set_message_handler(Handler h)
69 m_server.set_message_handler(
70 [
this, h](connection_handler hdl, server_t::message_ptr msg) {
71 #if defined OSSIA_BENCHMARK
72 auto t1 = std::chrono::high_resolution_clock::now();
76 auto res = h(hdl, msg->get_opcode(), msg->get_raw_payload());
77 if(res.data.size() > 0)
79 send_message(hdl, res);
84 auto con = m_server.get_con_from_hdl(hdl);
86 "Node not found: {} ==> {}", con->get_uri()->get_resource(), e.what());
90 auto con = m_server.get_con_from_hdl(hdl);
92 "Error in request: {} ==> {}", con->get_uri()->get_resource(), e.what());
94 catch(
const std::exception& e)
96 auto con = m_server.get_con_from_hdl(hdl);
101 auto con = m_server.get_con_from_hdl(hdl);
105 #if defined OSSIA_BENCHMARK
106 auto t2 = std::chrono::high_resolution_clock::now();
109 std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count());
113 m_server.set_http_handler([
this, h](connection_handler hdl) {
114 auto con = m_server.get_con_from_hdl(hdl);
117 con->append_header(
"Access-Control-Allow-Origin",
"*");
121 ossia::net::server_reply str
122 = h(hdl, websocketpp::frame::opcode::TEXT, con->get_uri()->get_resource());
126 case server_reply::data_type::json: {
127 con->replace_header(
"Content-Type",
"application/json; charset=utf-8");
131 case server_reply::data_type::html: {
132 con->replace_header(
"Content-Type",
"text/html; charset=utf-8");
138 con->replace_header(
"Connection",
"close");
139 con->set_body(std::move(str.data));
140 con->set_status(websocketpp::http::status_code::ok);
144 con->set_status(websocketpp::http::status_code::not_found);
149 "Error in request: {} ==> {}", con->get_uri()->get_resource(), e.what());
150 con->set_status(websocketpp::http::status_code::bad_request);
152 catch(
const std::exception& e)
163 void listen(uint16_t port = 9002)
165 m_server.listen(boost::asio::ip::tcp::v4(), port);
166 m_server.start_accept();
187 void close(connection_handler hdl)
189 auto con = m_server.get_con_from_hdl(hdl);
190 con->close(websocketpp::close::status::going_away,
"Server shutdown");
193 void send_message(connection_handler hdl,
const std::string&
message)
195 auto con = m_server.get_con_from_hdl(hdl);
199 void send_message(connection_handler hdl,
const ossia::net::server_reply&
message)
201 auto con = m_server.get_con_from_hdl(hdl);
204 case server_reply::data_type::json:
205 case server_reply::data_type::html:
206 con->send(
message.data, websocketpp::frame::opcode::TEXT);
209 con->send(
message.data, websocketpp::frame::opcode::BINARY);
214 void send_message(connection_handler hdl,
const rapidjson::StringBuffer&
message)
216 auto con = m_server.get_con_from_hdl(hdl);
217 con->send(
message.GetString(),
message.GetSize(), websocketpp::frame::opcode::text);
220 void send_binary_message(connection_handler hdl,
const std::string&
message)
222 auto con = m_server.get_con_from_hdl(hdl);
223 con->send(
message.data(),
message.size(), websocketpp::frame::opcode::binary);
226 void send_binary_message(connection_handler hdl, std::string_view
message)
228 auto con = m_server.get_con_from_hdl(hdl);
229 con->send(
message.data(),
message.size(), websocketpp::frame::opcode::binary);
Low-level websocket & http server for oscquery.
Definition: websocket_server.hpp:20
spdlog::logger & logger() noexcept
Where the errors will be logged. Default is stderr.
Definition: context.cpp:104
Used when a bad network request is done on a local server.
Definition: network/exceptions.hpp:72
The message struct.
Definition: message.hpp:29
Used when a requested node could not be found.
Definition: network/exceptions.hpp:60