Back to C++20 standard!
Logging is worked (AdcOstreamLogger and AdcSPDLOGLogger classes)
This commit is contained in:
@@ -14,20 +14,26 @@
|
||||
namespace adc::impl
|
||||
{
|
||||
|
||||
template <typename IdentT = std::string>
|
||||
class AdcDeviceNetServerASIO : public AdcDeviceNetServer<IdentT>
|
||||
template <typename IdentT = std::string, interfaces::adc_logger_c LoggerT = utils::AdcOstreamLogger<char>>
|
||||
class AdcDeviceNetServerASIO : public AdcDeviceNetServer<IdentT, LoggerT>
|
||||
{
|
||||
typedef AdcDeviceNetServer<IdentT> base_t;
|
||||
typedef AdcDeviceNetServer<IdentT, LoggerT> base_t;
|
||||
|
||||
public:
|
||||
using typename base_t::logger_t;
|
||||
using typename base_t::server_ident_t;
|
||||
|
||||
typedef std::string session_ident_t;
|
||||
|
||||
template <typename ServiceT>
|
||||
using Session = typename base_t::template Session<ServiceT, session_ident_t>;
|
||||
|
||||
AdcDeviceNetServerASIO(const server_ident_t& id, asio::io_context& io_context)
|
||||
: base_t(id), _ioContext(io_context), _stopSignal(io_context), _restartSignal(io_context)
|
||||
template <typename... LoggerCtorArgTs>
|
||||
AdcDeviceNetServerASIO(const server_ident_t& id, asio::io_context& io_context, LoggerCtorArgTs&&... ctor_args)
|
||||
: base_t(id, std::forward<LoggerCtorArgTs>(ctor_args)...),
|
||||
_ioContext(io_context),
|
||||
_stopSignal(io_context),
|
||||
_restartSignal(io_context)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -93,6 +99,26 @@ public:
|
||||
requires(std::convertible_to<std::ranges::range_value_t<RST>, int> &&
|
||||
std::convertible_to<std::ranges::range_value_t<RRT>, int>)
|
||||
{
|
||||
auto sig_list = [](const auto& sig_range) {
|
||||
std::string sgs;
|
||||
#ifdef _GNU_SOURCE
|
||||
std::vector<std::string> vsg;
|
||||
|
||||
std::ranges::transform(sig_range, std::back_inserter(vsg),
|
||||
[](auto s) { return std::format("'{}'", sigdescr_np(s)); });
|
||||
|
||||
utils::AdcJoinRange(vsg, std::string_view(", "), sgs);
|
||||
#else
|
||||
sgs = utils::AdcDefaultValueConverter<utils::constants::DEFAULT_CONVERTER_DELIMITER_COMA>::serialize<
|
||||
std::string>(sig_range);
|
||||
#endif
|
||||
return sgs;
|
||||
};
|
||||
|
||||
this->logDebug("Setup 'stop-server' signal to: {}", sig_list(stop_sig_num));
|
||||
this->logDebug("Setup 'restart-server' signal to: {}", sig_list(restart_sig_num));
|
||||
|
||||
|
||||
for (const int sig : stop_sig_num) {
|
||||
_stopSignal.add(sig);
|
||||
}
|
||||
@@ -119,6 +145,9 @@ public:
|
||||
{
|
||||
_sessionRecvTimeout = recv_timeout;
|
||||
_sessionSendTimeout = send_timeout;
|
||||
|
||||
this->logDebug("Set session timeouts: recv = {} msec, send = {} msec", _sessionRecvTimeout.count(),
|
||||
_sessionSendTimeout.count());
|
||||
}
|
||||
|
||||
protected:
|
||||
@@ -129,25 +158,41 @@ protected:
|
||||
std::chrono::milliseconds _sessionRecvTimeout = std::chrono::hours(12);
|
||||
std::chrono::milliseconds _sessionSendTimeout = std::chrono::seconds(5);
|
||||
|
||||
void daemonize()
|
||||
{
|
||||
this->logInfo("Daemonize server process (server addr: {})", (void*)this);
|
||||
|
||||
base_t::daemonize();
|
||||
}
|
||||
|
||||
// demonizing ASIO-related methods
|
||||
virtual void daemonizePrepare()
|
||||
{
|
||||
this->logDebug("ASIO-related call of daemonizePrepare()");
|
||||
|
||||
_ioContext.notify_fork(asio::execution_context::fork_prepare);
|
||||
}
|
||||
|
||||
virtual void daemonizeFinalize()
|
||||
{
|
||||
this->logDebug("ASIO-related call of daemonizeFinalize()");
|
||||
|
||||
_ioContext.notify_fork(asio::io_context::fork_child);
|
||||
}
|
||||
|
||||
virtual void signalReceived(std::error_code, int signo)
|
||||
virtual void signalReceived(std::error_code ec, int signo)
|
||||
{
|
||||
std::cout << "SIGNAL: " << signo << "\n";
|
||||
#ifdef _GNU_SOURCE
|
||||
this->logInfo("The server received the signal: '{}' (ec = {})", sigdescr_np(signo), ec.message());
|
||||
#else
|
||||
this->logInfo("The server received the signal: {} (ec = {})", signo, ec.message());
|
||||
#endif
|
||||
};
|
||||
|
||||
virtual void restart()
|
||||
{
|
||||
this->logInfo("Restart server (server addr: {})", (void*)this);
|
||||
|
||||
this->stopAllSessions();
|
||||
|
||||
_restartSignal.async_wait([this](std::error_code ec, int signo) {
|
||||
|
||||
@@ -296,6 +296,15 @@ public:
|
||||
_acceptor.close(ec);
|
||||
}
|
||||
|
||||
|
||||
std::string localEndpoint() const
|
||||
{
|
||||
std::stringstream st;
|
||||
st << _acceptor.local_endpoint();
|
||||
|
||||
return st.str();
|
||||
}
|
||||
|
||||
private:
|
||||
asio::io_context& _ioContext;
|
||||
srv_acceptor_t _acceptor;
|
||||
@@ -758,11 +767,17 @@ public:
|
||||
}
|
||||
|
||||
|
||||
// static void formatError(std::error_code err, std::string& result_str)
|
||||
// {
|
||||
// std::format_to(std::back_inserter(result_str), "{} (Err category: {}) (Err msg: {})", err.value(),
|
||||
// err.category().name(), err.message());
|
||||
// }
|
||||
std::string remoteEndpoint() const
|
||||
{
|
||||
std::stringstream st;
|
||||
st << _socket.remote_endpoint();
|
||||
|
||||
if (st.str().empty()) {
|
||||
return "<local>";
|
||||
}
|
||||
|
||||
return st.str();
|
||||
}
|
||||
|
||||
static std::string formattableError(std::error_code ec)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user