ADC/net/adc_netservice.h
Timur A. Fatkhullin 8b2e8fcaaa ...
2024-06-11 00:45:50 +03:00

98 lines
2.4 KiB
C++

#pragma once
/*
ABSTRACT DEVICE COMPONENTS LIBRARY
*/
#include <chrono>
#include <utility>
namespace adc
{
template <typename NetMessageT, typename ImplT>
class AdcNetService
{
protected:
ImplT _impl;
public:
using typename ImplT::endpoint_t;
using timeout_clock_t = std::chrono::steady_clock;
using timeout_drtn_t = timeout_clock_t::duration;
static constexpr timeout_drtn_t defaultConnectTimeout = std::chrono::seconds(5);
// static constexpr std::chrono::duration defaultConnectTimeout = std::chrono::seconds(5);
static constexpr timeout_drtn_t defaultSendTimeout = std::chrono::seconds(5);
static constexpr timeout_drtn_t defaultRecvTimeout = std::chrono::seconds(5);
template <typename... ImplCtorArgTs>
AdcNetService(ImplCtorArgTs&&... ctor_args) : _impl(std::forward<ImplCtorArgTs>(ctor_args)...)
{
}
virtual ~AdcNetService() = default;
/* asynchronuos operations */
// open connection
auto asyncConnect(const endpoint_t& end_point, const timeout_drtn_t& timeout = defaultConnectTimeout)
{
return _impl.asyncConnect(end_point, timeout);
}
template <typename... ArgTs>
auto asyncSend(const NetMessageT& msg, const timeout_drtn_t& timeout = defaultSendTimeout, ArgTs&&... args)
{
return _impl.asyncSend(msg, timeout, std::forward<ArgTs>(args)...);
}
template <typename... ArgTs>
auto asyncReceive(const timeout_drtn_t& timeout = defaultRecvTimeout, ArgTs&&... args)
{
return _impl.asyncReceive(timeout, std::forward<ArgTs>(args)...);
}
/* blocking operations */
template <typename... ArgTs>
auto connect(const endpoint_t& endpoint, const timeout_drtn_t& timeout = defaultConnectTimeout, ArgTs&&... args)
{
return _impl.connect(endpoint, timeout, std::forward<ArgTs>(args)...);
}
template <typename... ArgTs>
auto send(const NetMessageT& msg, const timeout_drtn_t& timeout = defaultSendTimeout, ArgTs&&... args)
{
return _impl.send(msg, timeout, std::forward<ArgTs>(args)...);
}
template <typename... ArgTs>
NetMessageT receive(const timeout_drtn_t& timeout = defaultRecvTimeout, ArgTs&&... args)
{
return _impl.receive(timeout, std::forward<ArgTs>(args)...);
}
template <typename... ArgTs>
auto close(ArgTs&&... args)
{
return _impl.close(std::forward<ArgTs>(args)...);
}
};
} // namespace adc