From 221f595bcb7858d746140fafe0b3fcd617fff22d Mon Sep 17 00:00:00 2001 From: "Timur A. Fatkhullin" Date: Fri, 15 Nov 2024 12:49:11 +0300 Subject: [PATCH] ... --- CMakeLists.txt | 7 ++++++- common/adc_spdlog.h | 10 +++++----- net/adc_netserver.h | 4 ---- net/asio/adc_device_netserver_asio.h | 5 +++-- net/asio/adc_netservice_asio.h | 11 ++++++++--- tests/adc_asio_netserver_test.cpp | 2 +- 6 files changed, 23 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ca5f433..b65c014 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,6 +53,7 @@ set(ADC_NETWORK_HEADERS # net/adc_netservice.h net/adc_endpoint.h net/adc_netserver.h + net/adc_netclient.h net/adc_net_concepts.h net/adc_device_netmsg.h net/adc_device_netserver.h @@ -199,8 +200,12 @@ if (BUILD_TESTS) # add_test(VALUE_HOLDER ${VALUEHOLDER_TEST_APP}) add_test(VALUE_HOLDER ${DEVATTR_TEST_APP}) add_test(NETMSG_TEST ${NETMSG_TEST_APP}) + add_test(ASIO_NETSRV_TEST ${ASIO_NETSERVER_TEST_APP}) - target_link_libraries(${ASIO_NETSERVER_TEST_APP} PRIVATE fmt::fmt) + if (SPDLOG_LIBRARY) + target_link_libraries(${ASIO_NETSERVER_TEST_APP} PRIVATE fmt::fmt) + endif() + enable_testing() endif(BUILD_TESTS) diff --git a/common/adc_spdlog.h b/common/adc_spdlog.h index 9247804..66c60a3 100644 --- a/common/adc_spdlog.h +++ b/common/adc_spdlog.h @@ -16,7 +16,7 @@ namespace adc /* SPDLOG-library based advanced single/multithreaded logger */ -class AdcSPDLOGLogger +class AdcSpdlogLogger { public: // [year-month-day time.millisecs][log-level]: log-message @@ -25,7 +25,7 @@ public: typedef spdlog::level::level_enum loglevel_t; template - AdcSPDLOGLogger(std::shared_ptr logger, const R& pattern = LOGGER_DEFAULT_FORMAT) + AdcSpdlogLogger(std::shared_ptr logger, const R& pattern = LOGGER_DEFAULT_FORMAT) : _loggerSPtr(logger), _currentLogPattern() { std::ranges::copy(pattern, std::back_inserter(_currentLogPattern)); @@ -33,7 +33,7 @@ public: } - virtual ~AdcSPDLOGLogger() = default; + virtual ~AdcSpdlogLogger() = default; void setLogLevel(loglevel_t log_level) @@ -144,6 +144,7 @@ protected: }; +/* template class AdcSpdlogGenericDecorator : public BaseT @@ -285,7 +286,6 @@ protected: } }; - template class AdcSpdlogGenericMarkDecorator : public AdcSpdlogGenericDecorator { @@ -317,7 +317,7 @@ public: } }; - +*/ } // namespace adc #endif diff --git a/net/adc_netserver.h b/net/adc_netserver.h index 6f23b20..82b1353 100644 --- a/net/adc_netserver.h +++ b/net/adc_netserver.h @@ -10,7 +10,6 @@ ABSTRACT DEVICE COMPONENTS LIBRARY #include #include #include -#include #include #if __has_include() // POSIX @@ -52,8 +51,6 @@ public: 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); @@ -82,7 +79,6 @@ public: close(1); close(2); -// _ioContext.notify_fork(asio::io_context::fork_child); #endif daemonizeFinalize(); } diff --git a/net/asio/adc_device_netserver_asio.h b/net/asio/adc_device_netserver_asio.h index f090e86..40c2660 100644 --- a/net/asio/adc_device_netserver_asio.h +++ b/net/asio/adc_device_netserver_asio.h @@ -106,7 +106,7 @@ public: std::vector vsg; std::ranges::transform(sig_range, std::back_inserter(vsg), - [](auto s) { return std::format("'{}'", sigdescr_np(s)); }); + [](auto s) { return std::format("'{}' (No = {})", sigdescr_np(s), s); }); utils::AdcJoinRange(vsg, std::string_view(", "), sgs); #else @@ -184,7 +184,8 @@ protected: virtual void signalReceived(std::error_code ec, int signo) { #ifdef _GNU_SOURCE - this->logInfo("The server received the signal: '{}' (ec = {})", sigdescr_np(signo), ec.message()); + this->logInfo("The server received the signal: '{}' (No = {}, ec = {})", sigdescr_np(signo), signo, + ec.message()); #else this->logInfo("The server received the signal: {} (ec = {})", signo, ec.message()); #endif diff --git a/net/asio/adc_netservice_asio.h b/net/asio/adc_netservice_asio.h index 36e8454..ac3e9f0 100644 --- a/net/asio/adc_netservice_asio.h +++ b/net/asio/adc_netservice_asio.h @@ -259,6 +259,8 @@ public: timer->cancel(); } + srv->_socket.set_option(asio::socket_base::keep_alive(true)); + self.complete(ec, std::move(*srv)); srv.reset(); @@ -574,7 +576,7 @@ public: if (!ec) { if (do_read) { - do_read = false; + // do_read = false; if (_receiveQueue.size()) { // return message from queue timer->cancel(); auto imsg = _receiveQueue.front(); @@ -593,6 +595,8 @@ public: } auto buff = _streamBuffer.prepare(n_avail ? n_avail : 1); + do_read = false; + if constexpr (isTLS) { return asio::async_read(_sessSocket, std::move(buff), asio::transfer_at_least(1), std::move(self)); @@ -638,8 +642,9 @@ public: auto net_pack = this->search(std::span(start_ptr, _streamBuffer.size())); if (net_pack.empty()) { do_read = true; - asio::post(std::move(self)); // initiate consequence socket's read operation - return; + return _socket.async_wait(asio::ip::tcp::socket::wait_read, std::move(self)); + // asio::post(std::move(self)); // initiate consequence socket's read operation + // return; } timer->cancel(); // there were no errors in the asynchronous read-operation, so stop timer diff --git a/tests/adc_asio_netserver_test.cpp b/tests/adc_asio_netserver_test.cpp index bb485b7..b3adf3a 100644 --- a/tests/adc_asio_netserver_test.cpp +++ b/tests/adc_asio_netserver_test.cpp @@ -137,7 +137,7 @@ int main(int argc, char* argv[]) asio::signal_set signals(io_ctx, SIGINT, SIGTERM); signals.async_wait([&](std::error_code, int) { io_ctx.stop(); }); - using server_t = adc::impl::AdcDeviceNetServerASIO; + using server_t = adc::impl::AdcDeviceNetServerASIO; std::shared_ptr logger = spdlog::stdout_color_mt("console"); logger->set_level(spdlog::level::debug); // server_t server("TEST SRV", io_ctx, logger, "[%Y-%m-%d %T.%e][%l]: %v");