170 lines
3.4 KiB
C++
170 lines
3.4 KiB
C++
#pragma once
|
|
|
|
/*
|
|
|
|
ABSTRACT DEVICE COMPONENTS LIBRARY
|
|
|
|
*/
|
|
|
|
#include <filesystem>
|
|
#if __has_include(<unistd.h>) // POSIX
|
|
#define FORK_EXISTS 1
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
#include <cerrno>
|
|
#endif
|
|
|
|
#include <memory>
|
|
|
|
|
|
namespace adc
|
|
{
|
|
|
|
|
|
/* Server session and its abstract decorator */
|
|
|
|
template <typename ImplT>
|
|
class AdcNetServerSession : std::enable_shared_from_this<AdcNetServerSession<ImplT>>
|
|
{
|
|
public:
|
|
using shared_ptr_t = std::shared_ptr<AdcNetServerSession>;
|
|
using weak_ptr_t = std::weak_ptr<AdcNetServerSession>;
|
|
|
|
template <typename... ImplCtorArgTs>
|
|
AdcNetServerSession(ImplCtorArgTs&&... ctor_args) : _implUptr(std::forward<ImplCtorArgTs>(ctor_args)...)
|
|
{
|
|
}
|
|
|
|
|
|
virtual ~AdcNetServerSession() = default;
|
|
|
|
|
|
auto sessionIdent() const
|
|
{
|
|
//
|
|
return _implUptr->_sessionIdent();
|
|
}
|
|
|
|
|
|
virtual void start()
|
|
{
|
|
//
|
|
_implUptr->start();
|
|
}
|
|
|
|
virtual void stop()
|
|
{
|
|
//
|
|
_implUptr->stop();
|
|
}
|
|
|
|
protected:
|
|
std::unique_ptr<ImplT> _implUptr;
|
|
};
|
|
|
|
|
|
template <typename ImplT, std::derived_from<AdcNetServerSession<ImplT>> SessionT>
|
|
class AdcNetServerSessionDecorator : public SessionT
|
|
{
|
|
public:
|
|
using SessionT::sessionIdent;
|
|
|
|
template <typename... ImplCtorArgTs>
|
|
AdcNetServerSessionDecorator(ImplCtorArgTs&&... ctor_args) : SessionT(std::forward<ImplCtorArgTs>(ctor_args)...)
|
|
{
|
|
}
|
|
|
|
virtual ~AdcNetServerSessionDecorator() = default;
|
|
|
|
virtual std::invoke_result_t<decltype(sessionIdent)> sessionIdent() const = 0;
|
|
|
|
virtual void start() = 0;
|
|
|
|
virtual void stop() = 0;
|
|
};
|
|
|
|
|
|
|
|
/* Server */
|
|
|
|
template <typename ImplT>
|
|
class AdcNetServer
|
|
{
|
|
protected:
|
|
std::unique_ptr<ImplT> _implUptr;
|
|
|
|
public:
|
|
template <typename... ImplCtorArgTs>
|
|
AdcNetServer(ImplCtorArgTs&&... ctor_args) : _implUptr(std::make_unique<ImplT>(ctor_args)...)
|
|
{
|
|
}
|
|
|
|
|
|
virtual ~AdcNetServer() = default;
|
|
|
|
|
|
virtual void start() = 0;
|
|
|
|
virtual void stop() = 0;
|
|
|
|
template <typename SessionT>
|
|
void start() {};
|
|
|
|
template <typename SessionT>
|
|
void stop() {};
|
|
|
|
// run server as daemon (still only on POSIX OSes)
|
|
void daemonize()
|
|
{
|
|
daemonizePrepare();
|
|
|
|
#ifdef FORK_EXISTS
|
|
// get TEMP directory in OS
|
|
|
|
auto tmp_path = std::filesystem::temp_directory_path();
|
|
if (tmp_path.empty()) {
|
|
tmp_path = std::filesystem::current_path().root_path();
|
|
}
|
|
|
|
// _ioContext.notify_fork(asio::execution_context::fork_prepare);
|
|
|
|
if (pid_t pid = fork()) {
|
|
if (pid > 0) {
|
|
exit(0);
|
|
} else {
|
|
throw std::system_error(errno, std::generic_category(), "CANNOT FORK 1-STAGE");
|
|
}
|
|
}
|
|
|
|
if (setsid() == -1) {
|
|
throw std::system_error(errno, std::generic_category(), "CANNOT FORK SETSID");
|
|
}
|
|
|
|
std::filesystem::current_path(tmp_path);
|
|
|
|
umask(0);
|
|
|
|
if (pid_t pid = fork()) {
|
|
if (pid > 0) {
|
|
exit(0);
|
|
} else {
|
|
throw std::system_error(errno, std::generic_category(), "CANNOT FORK 2-STAGE");
|
|
}
|
|
}
|
|
|
|
close(0);
|
|
close(1);
|
|
close(2);
|
|
|
|
// _ioContext.notify_fork(asio::io_context::fork_child);
|
|
#endif
|
|
daemonizeFinalize();
|
|
}
|
|
|
|
virtual void daemonizePrepare() = 0;
|
|
|
|
virtual void daemonizeFinalize() = 0;
|
|
};
|
|
|
|
} // namespace adc
|