This commit is contained in:
Timur A. Fatkhullin
2024-06-09 00:27:40 +03:00
parent 06959e8746
commit 346a5e6965
2 changed files with 208 additions and 19 deletions

View File

@@ -7,49 +7,64 @@ ABSTRACT DEVICE COMPONENTS LIBRARY
*/
#include <chrono>
#include <memory>
#include <utility>
namespace adc
{
template <typename IOImplT, typename NetMessageT>
class AdcNetServiceInterface : public IOImplT
template <typename NetMessageT>
class AdcNetServiceAbstractImplementation;
template <typename NetMessageT, typename ImplT>
class AdcNetService
{
protected:
AdcNetServiceInterface::socket _socket;
template <typename... IOImplCtorArgTs>
AdcNetServiceInterface(AdcNetServiceInterface::socket&& sock, IOImplCtorArgTs&&... args)
: IOImplT(std::forward<IOImplCtorArgTs>(args)...), _socket(sock)
{
}
std::unique_ptr<ImplT> _implUptr;
public:
using typename IOImplT::endpoint;
using typename IOImplT::socket;
using typename ImplT::endpoint_t;
virtual ~AdcNetServiceInterface() = default;
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(AdcNetServiceInterface::endpoint&& end_point, ArgTs&&... args)
auto asyncConnect(const endpoint_t& end_point,
const timeout_drtn_t& timeout = defaultConnectTimeout,
ArgTs&&... args)
{
return IOImplT::asyncConnect(end_point);
return _implUptr->asyncConnect(end_point, timeout, std::forward<ArgTs>(args)...);
}
template <typename... ArgTs>
auto asyncSend(const NetMessageT& msg, ArgTs&&... args)
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(ArgTs&&... args)
auto asyncReceive(const timeout_drtn_t& timeout = defaultRecvTimeout, ArgTs&&... args)
{
return _implUptr->asyncReceive(timeout, std::forward<ArgTs>(args)...);
}
@@ -57,25 +72,30 @@ public:
/* blocking operations */
template <typename... ArgTs>
auto connect(const AdcNetServiceInterface::endpoint& end_point, ArgTs&&... args)
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, ArgTs&&... args)
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(ArgTs&&... args)
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