...
This commit is contained in:
@@ -18,203 +18,19 @@ ABSTRACT DEVICE COMPONENTS LIBRARY
|
||||
#include <cerrno>
|
||||
#endif
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "adc_net_concepts.h"
|
||||
|
||||
namespace adc
|
||||
{
|
||||
|
||||
|
||||
namespace traits
|
||||
{
|
||||
|
||||
// network server session implementation concept
|
||||
template <typename T>
|
||||
concept adc_netserver_session_impl_c = requires(T t, const T t_const) {
|
||||
typename T::session_ident_t;
|
||||
|
||||
{ t_const.sessionIdent() } -> std::same_as<typename T::session_ident_t>;
|
||||
{ t.start() } -> std::same_as<void>;
|
||||
{ t.stop() } -> std::same_as<void>;
|
||||
};
|
||||
|
||||
} // namespace traits
|
||||
|
||||
|
||||
|
||||
/* Server session */
|
||||
|
||||
class AdcNetServerGenericSession
|
||||
{
|
||||
public:
|
||||
struct Opt {
|
||||
std::function<void()> startSess;
|
||||
std::function<void()> stopSess;
|
||||
std::function<void()> run;
|
||||
};
|
||||
|
||||
AdcNetServerGenericSession(Opt&& opts) : _opts(std::move(opts)) {}
|
||||
|
||||
virtual void start()
|
||||
{
|
||||
_opts.startSess();
|
||||
}
|
||||
|
||||
virtual void stop()
|
||||
{
|
||||
_opts.stopSess();
|
||||
}
|
||||
|
||||
protected:
|
||||
Opt _opts;
|
||||
|
||||
virtual void run()
|
||||
{
|
||||
_opts.run();
|
||||
}
|
||||
};
|
||||
|
||||
template <traits::adc_netserver_session_impl_c ImplT>
|
||||
class AdcNetServerSession : std::enable_shared_from_this<AdcNetServerSession<ImplT>>
|
||||
{
|
||||
protected:
|
||||
ImplT _impl;
|
||||
|
||||
public:
|
||||
typedef ImplT session_impl_t;
|
||||
typedef std::shared_ptr<AdcNetServerSession> shared_ptr_t;
|
||||
typedef std::weak_ptr<AdcNetServerSession> weak_ptr_t;
|
||||
|
||||
using typename ImplT::session_ident_t;
|
||||
|
||||
template <typename... ImplCtorArgTs>
|
||||
AdcNetServerSession(ImplCtorArgTs&&... ctor_args) : _impl(std::forward<ImplCtorArgTs>(ctor_args)...)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
virtual ~AdcNetServerSession() = default;
|
||||
|
||||
|
||||
virtual session_ident_t sessionIdent() const
|
||||
{
|
||||
//
|
||||
return _impl._sessionIdent();
|
||||
}
|
||||
|
||||
|
||||
virtual void start()
|
||||
{
|
||||
//
|
||||
_impl.start();
|
||||
}
|
||||
|
||||
virtual void stop()
|
||||
{
|
||||
//
|
||||
_impl.stop();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
namespace traits
|
||||
{
|
||||
|
||||
// network server session concept
|
||||
template <typename T>
|
||||
concept adc_netserver_session_c = requires {
|
||||
typename T::session_impl_t;
|
||||
|
||||
std::derived_from<T, AdcNetServerSession<typename T::session_impl_t>>;
|
||||
};
|
||||
|
||||
} // namespace traits
|
||||
|
||||
|
||||
/* network server */
|
||||
|
||||
namespace traits
|
||||
{
|
||||
|
||||
template <typename T>
|
||||
concept adc_generic_netserver_impl_c = requires(T t, const T t_const) {
|
||||
typename T::server_ident_t;
|
||||
|
||||
{ t_const.serverIdent() } -> std::same_as<typename T::server_ident_t>;
|
||||
{ t.start() } -> std::same_as<void>;
|
||||
{ t.stop() } -> std::same_as<void>;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
concept adc_netserver_impl_c = requires(T t, const T t_const) {
|
||||
typename T::server_ident_t;
|
||||
|
||||
{ t_const.serverIdent() } -> std::same_as<typename T::server_ident_t>;
|
||||
{ t.start() } -> std::same_as<void>;
|
||||
{ t.stop() } -> std::same_as<void>;
|
||||
{ t.daemonize() } -> std::same_as<void>;
|
||||
{ t.daemonizePrepare() } -> std::same_as<void>;
|
||||
{ t.daemonizeFinalize() } -> std::same_as<void>;
|
||||
};
|
||||
|
||||
} // namespace traits
|
||||
|
||||
|
||||
/* VERY GENERIC NETWORK SERVER INTERFACE */
|
||||
|
||||
template <traits::adc_generic_netserver_impl_c ImplT>
|
||||
class AdcGenericNetServer
|
||||
{
|
||||
protected:
|
||||
ImplT _impl;
|
||||
|
||||
public:
|
||||
typedef ImplT server_impl_t;
|
||||
using typename ImplT::server_ident_t;
|
||||
|
||||
template <typename... ImplCtorArgTs>
|
||||
AdcGenericNetServer(ImplCtorArgTs&&... ctor_args) : _impl(std::make_unique<ImplT>(ctor_args)...)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
virtual ~AdcGenericNetServer() = default;
|
||||
|
||||
|
||||
virtual server_ident_t serverIdent() const
|
||||
{
|
||||
//
|
||||
return _impl.serverIdent();
|
||||
}
|
||||
|
||||
virtual void start()
|
||||
{
|
||||
//
|
||||
_impl.start();
|
||||
};
|
||||
|
||||
virtual void stop()
|
||||
{
|
||||
//
|
||||
_impl.stop();
|
||||
};
|
||||
};
|
||||
|
||||
template <traits::adc_netserver_impl_c ImplT>
|
||||
class AdcNetServer
|
||||
{
|
||||
protected:
|
||||
ImplT _impl;
|
||||
|
||||
public:
|
||||
typedef ImplT server_impl_t;
|
||||
using typename ImplT::server_ident_t;
|
||||
|
||||
template <typename... ImplCtorArgTs>
|
||||
AdcNetServer(ImplCtorArgTs&&... ctor_args) : _impl(std::make_unique<ImplT>(ctor_args)...)
|
||||
{
|
||||
}
|
||||
typedef std::string server_ident_t;
|
||||
|
||||
|
||||
virtual ~AdcNetServer() = default;
|
||||
@@ -222,21 +38,13 @@ public:
|
||||
|
||||
virtual server_ident_t serverIdent() const
|
||||
{
|
||||
//
|
||||
return _impl.serverIdent();
|
||||
return _serverIdent;
|
||||
}
|
||||
|
||||
virtual void start()
|
||||
{
|
||||
//
|
||||
_impl.start();
|
||||
};
|
||||
template <interfaces::adc_netservice_c SRVT>
|
||||
void start(SRVT&& netservice, const typename SRVT::endpoint_t& endpoint) {};
|
||||
|
||||
virtual void stop()
|
||||
{
|
||||
//
|
||||
_impl.stop();
|
||||
};
|
||||
virtual void stop() {};
|
||||
|
||||
|
||||
// run server as daemon (still only on POSIX OSes)
|
||||
@@ -290,12 +98,15 @@ public:
|
||||
|
||||
|
||||
protected:
|
||||
server_ident_t _serverIdent;
|
||||
|
||||
|
||||
// started sessions weak pointers
|
||||
template <traits::adc_netserver_session_c SessionT>
|
||||
template <interfaces::adc_netsession_c SessionT>
|
||||
static std::unordered_map<const AdcNetServer*, std::set<typename SessionT::weak_ptr_t>> _serverSessions;
|
||||
std::vector<std::function<void()>> _stopSessionFunc;
|
||||
|
||||
template <traits::adc_netserver_session_c SessionT>
|
||||
template <interfaces::adc_netsession_c SessionT>
|
||||
void startSession(const typename SessionT::shared_ptr_t& sess_ptr)
|
||||
{
|
||||
auto res = _serverSessions<SessionT>[this].emplace(sess_ptr);
|
||||
|
||||
Reference in New Issue
Block a user