ADC/net/adc_netservice.h
Timur A. Fatkhullin 346a5e6965 ...
2024-06-09 00:27:40 +03:00

102 lines
2.6 KiB
C++

#pragma once
/*
ABSTRACT DEVICE COMPONENTS LIBRARY
*/
#include <chrono>
#include <memory>
#include <utility>
namespace adc
{
template <typename NetMessageT>
class AdcNetServiceAbstractImplementation;
template <typename NetMessageT, typename ImplT>
class AdcNetService
{
protected:
std::unique_ptr<ImplT> _implUptr;
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 timeout_drtn_t defaultSendTimeout = std::chrono::seconds(5);
static constexpr timeout_drtn_t defaultRecvTimeout = std::chrono::seconds(5);
AdcNetService(ImplT* impl) : _implUptr(impl) {}
AdcNetService(std::unique_ptr<ImplT> impl) : _implUptr(std::move(impl)) {}
virtual ~AdcNetService() = default;
/* asynchronuos operations */
// open connection
template <typename... ArgTs>
auto asyncConnect(const endpoint_t& end_point,
const timeout_drtn_t& timeout = defaultConnectTimeout,
ArgTs&&... args)
{
return _implUptr->asyncConnect(end_point, timeout, std::forward<ArgTs>(args)...);
}
template <typename... ArgTs>
auto asyncSend(const NetMessageT& msg, const timeout_drtn_t& timeout = defaultSendTimeout, ArgTs&&... args)
{
return _implUptr->asyncSend(msg, timeout, std::forward<ArgTs>(args)...);
}
template <typename... ArgTs>
auto asyncReceive(const timeout_drtn_t& timeout = defaultRecvTimeout, ArgTs&&... args)
{
return _implUptr->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 _implUptr->connect(endpoint, timeout, std::forward<ArgTs>(args)...);
}
template <typename... ArgTs>
auto send(const NetMessageT& msg, const timeout_drtn_t& timeout = defaultSendTimeout, ArgTs&&... args)
{
return _implUptr->send(msg, timeout, std::forward<ArgTs>(args)...);
}
template <typename... ArgTs>
NetMessageT receive(const timeout_drtn_t& timeout = defaultRecvTimeout, ArgTs&&... args)
{
return _implUptr->receive(timeout, std::forward<ArgTs>(args)...);
}
template <typename... ArgTs>
auto close(ArgTs&&... args)
{
return _implUptr->close(std::forward<ArgTs>(args)...);
}
};
} // namespace adc