From 64ded5f3f378069867151f5882ace539475bca02 Mon Sep 17 00:00:00 2001 From: "Timur A. Fatkhullin" Date: Wed, 16 Oct 2024 18:06:10 +0300 Subject: [PATCH] ... --- CMakeLists.txt | 3 +- net/uwebsockets/adc_netservice_uws.h | 69 ++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 net/uwebsockets/adc_netservice_uws.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b1d70d..0ed81d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -92,7 +92,7 @@ if (USE_UWEBSOCKET) FetchContent_MakeAvailable(uwebsockets) FetchContent_GetProperties(uwebsockets SOURCE_DIR uwebsockets_SOURCE_DIR) - set(UWEBSOCKET_INCLUDE_DIR ${uwebsockets_SOURCE_DIR}/src) + set(UWEBSOCKETS_INCLUDE_DIR ${uwebsockets_SOURCE_DIR}/src) FetchContent_Declare(usockets SOURCE_DIR ${uwebsockets_SOURCE_DIR}/uSockets @@ -177,3 +177,4 @@ target_include_directories( $ $ ) + diff --git a/net/uwebsockets/adc_netservice_uws.h b/net/uwebsockets/adc_netservice_uws.h new file mode 100644 index 0000000..55a57d5 --- /dev/null +++ b/net/uwebsockets/adc_netservice_uws.h @@ -0,0 +1,69 @@ +#pragma once + +#include "../../common/adc_traits.h" + +#include + +namespace adc::impl +{ + +template > // used only for inner storing of message byte sequence +class AdcNetServiceUWS +{ +public: + // typedefs to satisfy 'adc_netservice_c' concept + typedef std::string_view netservice_ident_t; + + typedef std::vector send_msg_t; // in general, only one of several possible + typedef RMSGT recv_msg_t; // in general, only one of several possible (see class template arguments declaration) + typedef traits::adc_common_duration_t timeout_t; + typedef std::pair endpoint_t; // {host, port} + + // typedefs for completion tokens (callbacks, required by 'adc_netservice_c' concept) + typedef std::function async_connect_callback_t; + typedef std::function async_send_callback_t; + typedef std::function async_receive_callback_t; + + + class acceptor_t + { + public: + static constexpr std::chrono::duration DEFAULT_ACCEPT_TIMEOUT = std::chrono::seconds::max(); + + acceptor_t(uWS::SSLApp& app, const AdcNetServiceUWS::endpoint_t& endpoint) : _uwsApp(app), _endpoint(endpoint) + { + } + + typedef AdcNetServiceUWS netservice_t; + typedef std::shared_ptr sptr_netservice_t; + + typedef std::function async_accept_callback_t; + + template TokenT> + auto asyncAccept(TokenT&& token, const traits::adc_time_duration_c auto& timeout = DEFAULT_ACCEPT_TIMEOUT) + { + _uwsApp.ws("/*", {.open = [wrapper = traits::adc_pf_wrapper(std::forward(token)), this](auto) { + std::get<0>(wrapper)(std::error_code{}, std::make_shared(_uwsApp)); + }}); + + _uwsApp.listen(_endpoint.first, _endpoint.second, + [wrapper = traits::adc_pf_wrapper(std::forward(token)), this](auto* sock) { + std::error_code ec; + if (!sock) { + std::get<0>(wrapper)(std::make_error_code(std::errc::not_connected), + std::make_shared(_uwsApp)); + } + }); + } + + private: + uWS::SSLApp& _uwsApp; + + AdcNetServiceUWS::endpoint_t _endpoint; + }; + + AdcNetServiceUWS(uWS::SSLApp& app) {} +}; + +} // namespace adc::impl