ADC/net/uwebsockets/adc_netservice_uws.h
2024-10-17 18:03:47 +03:00

73 lines
2.6 KiB
C++

#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)
{
// just call listen method
_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));
}
});
}
template <traits::adc_time_duration_c DT = decltype(DEFAULT_ACCEPT_TIMEOUT)>
auto accept(const DT& timeout = DEFAULT_ACCEPT_TIMEOUT)
{
}
private:
uWS::SSLApp& _uwsApp;
AdcNetServiceUWS::endpoint_t _endpoint;
};
AdcNetServiceUWS(uWS::SSLApp& app) {}
};
} // namespace adc::impl