This commit is contained in:
Timur A. Fatkhullin 2024-10-16 18:06:10 +03:00
parent e57d8ed546
commit 64ded5f3f3
2 changed files with 71 additions and 1 deletions

View File

@ -92,7 +92,7 @@ if (USE_UWEBSOCKET)
FetchContent_MakeAvailable(uwebsockets) FetchContent_MakeAvailable(uwebsockets)
FetchContent_GetProperties(uwebsockets SOURCE_DIR uwebsockets_SOURCE_DIR) 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 FetchContent_Declare(usockets
SOURCE_DIR ${uwebsockets_SOURCE_DIR}/uSockets SOURCE_DIR ${uwebsockets_SOURCE_DIR}/uSockets
@ -177,3 +177,4 @@ target_include_directories(
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include> $<INSTALL_INTERFACE:include>
) )

View File

@ -0,0 +1,69 @@
#pragma once
#include "../../common/adc_traits.h"
#include <App.h>
namespace adc::impl
{
template <traits::adc_output_char_range RMSGT =
std::vector<char>> // 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<char> 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<std::string, int> endpoint_t; // {host, port}
// typedefs for completion tokens (callbacks, required by 'adc_netservice_c' concept)
typedef std::function<void(std::error_code)> async_connect_callback_t;
typedef std::function<void(std::error_code)> async_send_callback_t;
typedef std::function<void(std::error_code, RMSGT)> 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<netservice_t> sptr_netservice_t;
typedef std::function<void(std::error_code, sptr_netservice_t)> async_accept_callback_t;
template <std::convertible_to<async_accept_callback_t> 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<TokenT>(token)), this](auto) {
std::get<0>(wrapper)(std::error_code{}, std::make_shared<AdcNetServiceUWS>(_uwsApp));
}});
_uwsApp.listen(_endpoint.first, _endpoint.second,
[wrapper = traits::adc_pf_wrapper(std::forward<TokenT>(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<AdcNetServiceUWS>(_uwsApp));
}
});
}
private:
uWS::SSLApp& _uwsApp;
AdcNetServiceUWS::endpoint_t _endpoint;
};
AdcNetServiceUWS(uWS::SSLApp& app) {}
};
} // namespace adc::impl