From d67108542ed22d5c63ef7a4d278064d059588bcf Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 19 Aug 2024 21:29:22 +0200 Subject: [PATCH] Add websocket class --- tools/CMakeLists.txt | 3 ++- tools/main.cpp | 36 +++++++++++++++++++++++++- tools/websocket.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++ tools/websocket.hpp | 24 ++++++++++++++++++ 4 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 tools/websocket.cpp create mode 100644 tools/websocket.hpp diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 40f549a..a7616aa 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -3,6 +3,7 @@ target_sources(simulators PRIVATE ${CMAKE_CURRENT_LIST_DIR}/main.cpp ${CMAKE_CURRENT_LIST_DIR}/simulator_base.cpp ${CMAKE_CURRENT_LIST_DIR}/simulators.cpp + ${CMAKE_CURRENT_LIST_DIR}/websocket.cpp ) target_compile_options(simulators PRIVATE $<$: @@ -13,7 +14,7 @@ target_include_directories(simulators PUBLIC ${CMAKE_CURRENT_LIST_DIR} PRIVATE - ${PROJECT_DIR}/external/** + ${PROJECT_DIR}/external/websocketpp ) target_link_libraries(simulators PRIVATE diff --git a/tools/main.cpp b/tools/main.cpp index a4228bf..07b5852 100644 --- a/tools/main.cpp +++ b/tools/main.cpp @@ -1,5 +1,39 @@ +#include +#include +#include +#include +#include + +#include "simulators.hpp" +#include "websocket.hpp" + +Simulators simulators; +Websocket* g_web_socket; + +void int_handler(int s) +{ + UNUSED(s); + std::cout << "\nCaught Ctrl + C\nShutting down." << std::endl; + g_web_socket->stop(); + exit(1); + +} int main(void) { - return 0; + struct sigaction sigIntHandler; + + sigIntHandler.sa_handler = int_handler; + sigemptyset(&sigIntHandler.sa_mask); + sigIntHandler.sa_flags = 0; + + sigaction(SIGINT, &sigIntHandler, NULL); + + Websocket web_socket(9002, [](std::string message) { + return message; + }); + g_web_socket = &web_socket; + web_socket.run(); + + } \ No newline at end of file diff --git a/tools/websocket.cpp b/tools/websocket.cpp new file mode 100644 index 0000000..3cea934 --- /dev/null +++ b/tools/websocket.cpp @@ -0,0 +1,60 @@ +#include "websocket.hpp" + + +Websocket::Websocket(uint16_t port, std::function on_message) + : p_on_message(on_message) +{ + try { + // Set logging settings + server.set_access_channels(websocketpp::log::alevel::all); + server.clear_access_channels(websocketpp::log::alevel::frame_payload); + + // Initialize Asio + server.init_asio(); + + // Register our message handler + server.set_message_handler(bind(&Websocket::on_message, this, &server, ::_1, ::_2)); + + // Listen on port 9002 + server.listen(port); + } catch (websocketpp::exception const & e) { + std::cout << e.what() << std::endl; + } catch (...) { + std::cout << "other exception" << std::endl; + } +} + +void Websocket::run() +{ + try + { + // Start the server accept loop + server.start_accept(); + + // Start the ASIO io_service run loop + server.run(); + } catch (websocketpp::exception const & e) { + std::cout << e.what() << std::endl; + } catch (...) { + std::cout << "other exception" << std::endl; + } +} + +void Websocket::stop() +{ + server.stop_listening(); + server.stop(); +} + +void Websocket::on_message(Server* s, websocketpp::connection_hdl hdl, Server::message_ptr msg) { + std::cout << "on_message called with hdl: " << hdl.lock().get() + << " and message: " << msg->get_payload() + << std::endl; + std::string response = p_on_message(msg->get_payload()); + try { + s->send(hdl, response, msg->get_opcode()); + } catch (websocketpp::exception const & e) { + std::cout << "Response failed because: " + << "(" << e.what() << ")" << std::endl; + } +} \ No newline at end of file diff --git a/tools/websocket.hpp b/tools/websocket.hpp new file mode 100644 index 0000000..4a30f60 --- /dev/null +++ b/tools/websocket.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include + +#include +#include + +typedef websocketpp::server Server; + +using websocketpp::lib::placeholders::_1; +using websocketpp::lib::placeholders::_2; +using websocketpp::lib::bind; + +class Websocket +{ +public: + Websocket(uint16_t port, std::function on_message); + void run(); + void stop(); +private: + void on_message(Server* s, websocketpp::connection_hdl hdl, Server::message_ptr msg); + Server server; + std::function p_on_message; +}; \ No newline at end of file