86 lines
3.1 KiB
C++
86 lines
3.1 KiB
C++
#pragma once
|
|
|
|
#include <asio/io_context.hpp>
|
|
#include <asio/ip/tcp.hpp>
|
|
#include <asio/ip/udp.hpp>
|
|
#include <asio/local/seq_packet_protocol.hpp>
|
|
#include <asio/local/stream_protocol.hpp>
|
|
|
|
#include "../adc_device_netserver.h"
|
|
#include "../adc_endpoint.h"
|
|
#include "adc_netservice_asio.h"
|
|
|
|
namespace adc::impl
|
|
{
|
|
|
|
class AdcDeviceNetServerASIO : public AdcDeviceNetServer
|
|
{
|
|
public:
|
|
template <traits::adc_input_char_range R>
|
|
AdcDeviceNetServerASIO(const R& id, asio::io_context& io_context) : AdcDeviceNetServer(id), _ioContext(io_context)
|
|
{
|
|
}
|
|
|
|
|
|
template <interfaces::adc_netsession_proto_c SessProtoT, std::derived_from<AdcEndpointParser> EptT>
|
|
void start(const EptT& endpoint)
|
|
{
|
|
if (!endpoint.isValid()) {
|
|
return;
|
|
}
|
|
|
|
|
|
// may thow here!
|
|
#ifdef USE_OPENSSL_WITH_ASIO
|
|
if (endpoint.isTCP() || endpoint.isTLS()) {
|
|
asio::ip::tcp::endpoint ept(asio::ip::make_address(endpoint.host()), endpoint.port());
|
|
if (endpoint.isTCP()) {
|
|
using srv_t = AdcNetServiceASIOBase<asio::ip::tcp, SessProtoT>;
|
|
AdcDeviceNetServer::start<Session<srv_t>>("TCP", this, _ioContext, ept);
|
|
} else {
|
|
}
|
|
#else
|
|
if (endpoint.isTCP()) {
|
|
asio::ip::tcp::endpoint ept(asio::ip::make_address(endpoint.host()), endpoint.port());
|
|
using srv_t = AdcNetServiceASIOBase<asio::ip::tcp, AdcStopSeqSessionProto<>>;
|
|
AdcDeviceNetServer::start<Session<srv_t>>("TCP", this, _ioContext, ept);
|
|
#endif
|
|
} else if (endpoint.isLocal()) {
|
|
if (endpoint.isLocalStream()) {
|
|
asio::local::stream_protocol::endpoint ept(endpoint.template path<std::string>());
|
|
using srv_t = AdcNetServiceASIOBase<asio::local::stream_protocol, SessProtoT>;
|
|
AdcDeviceNetServer::start<Session<srv_t>>("LOCAL STREAM", this, _ioContext, ept);
|
|
} else if (endpoint.isLocalDatagram()) {
|
|
asio::local::datagram_protocol::endpoint ept(endpoint.template path<std::string>());
|
|
using srv_t = AdcNetServiceASIOBase<asio::local::datagram_protocol, SessProtoT>;
|
|
AdcDeviceNetServer::start<Session<srv_t>>("LOCAL DGRAM", this, _ioContext, ept);
|
|
} else if (endpoint.isLocalSeqpacket()) {
|
|
asio::local::seq_packet_protocol::endpoint ept(endpoint.template path<std::string>());
|
|
using srv_t = AdcNetServiceASIOBase<asio::local::seq_packet_protocol, SessProtoT>;
|
|
AdcDeviceNetServer::start<Session<srv_t>>("LOCAL SEQPACK", this, _ioContext, ept);
|
|
}
|
|
} else {
|
|
throw std::system_error(std::make_error_code(std::errc::protocol_not_supported));
|
|
}
|
|
}
|
|
|
|
// some default endpoint?!!
|
|
void start() {}
|
|
|
|
protected:
|
|
asio::io_context& _ioContext;
|
|
|
|
// demonizing ASIO-related methods
|
|
virtual void daemonizePrepare()
|
|
{
|
|
_ioContext.notify_fork(asio::execution_context::fork_prepare);
|
|
}
|
|
|
|
virtual void daemonizeFinalize()
|
|
{
|
|
_ioContext.notify_fork(asio::io_context::fork_child);
|
|
}
|
|
};
|
|
|
|
} // namespace adc::impl
|