MccGenericNetworkServer: fix client session thread pool behavior in

destructor
This commit is contained in:
2025-11-27 09:20:42 +03:00
parent 43638f383f
commit a825a6935b
2 changed files with 51 additions and 7 deletions

View File

@@ -188,7 +188,7 @@ public:
std::stringstream st;
st << std::this_thread::get_id();
logInfo(std::format("Create generic network server instance (thread ID = {})", st.str()));
logInfo(std::format("Create MccGenericNetworkServer class instance (thread ID = {})", st.str()));
}
// MccNetworkServer(asio::io_context& ctx, const handle_message_func_t& func, LoggerT logger = MccNullLogger{})
@@ -208,6 +208,10 @@ public:
logInfo(std::format("Delete generic network server instance (thread ID = {}) ...", st.str()));
stopListening();
_sessionThreadPool.stop();
_sessionThreadPool.join();
disconnectClients();
}
@@ -585,8 +589,15 @@ public:
_stopSignal.async_wait([this](std::error_code, int signo) {
logInfo(std::format("Stop signal was received (signo = {})", signo));
stopListening();
disconnectClients();
// _handleMessageFunc(MCC_COMMPROTO_KEYWORD_STOP_STR);
// std::this_thread::sleep_for(std::chrono::milliseconds(100));
// stopListening();
// _sessionThreadPool.stop();
// _sessionThreadPool.join();
// disconnectClients();
_asioContext.stop();
});
@@ -913,10 +924,21 @@ class MccGenericMountNetworkServer : public MccGenericNetworkServer<LoggerT>
public:
using typename base_t::handle_message_func_result_t;
using base_t::logDebug;
using base_t::logError;
using base_t::logInfo;
using base_t::logTrace;
using base_t::logWarn;
template <mcc_generic_mount_c MountT, typename... LoggerCtorArgsTs>
MccGenericMountNetworkServer(asio::io_context& ctx, MountT& mount, LoggerCtorArgsTs&&... log_args)
: base_t(ctx, {}, std::forward<LoggerCtorArgsTs>(log_args)...)
{
std::stringstream st;
st << std::this_thread::get_id();
logInfo(std::format("Create MccGenericMountNetworkServer class instance (thread ID = {})", st.str()));
// to avoid possible compiler optimization (one needs to catch 'mount' strictly by reference)
auto* mount_ptr = &mount;
@@ -939,15 +961,27 @@ public:
return output_msg.template byteRepr<typename base_t::handle_message_func_result_t>();
};
// special functor (used in the destructor)
_stopMountFunc = [mount_ptr]() { mount_ptr->stopMount(); };
}
virtual ~MccGenericMountNetworkServer() {}
virtual ~MccGenericMountNetworkServer()
{
std::stringstream st;
st << std::this_thread::get_id();
_stopMountFunc();
logInfo(std::format("Delete MccGenericMountNetworkServer class instance (thread ID = {})", st.str()));
}
protected:
MccCoordinateSerializer::SerializedCoordFormat _coordFormat{
MccCoordinateSerializer::SerializedCoordFormat::CFMT_SGM};
MccCoordinateSerializer::SexagesimalCoordPrec _coordPrec{2, 1};
std::function<void()> _stopMountFunc{};
template <typename RESULT_MSG_T, typename INPUT_MSG_T, mcc_generic_mount_c MountT>
RESULT_MSG_T handleMessage(const INPUT_MSG_T& input_msg, MountT* mount_ptr)