add AdcGenericNetClient class

This commit is contained in:
Timur A. Fatkhullin
2024-11-14 23:16:08 +03:00
parent 78a9e53d18
commit 285f8de1f7
6 changed files with 130 additions and 16 deletions

View File

@@ -371,7 +371,20 @@ public:
};
void setAcceptTimeout(const traits::adc_time_duration_c auto& timeout)
{
_acceptTimeout = std::chrono::duration_cast<decltype(_acceptTimeout)>(timeout);
}
auto getAcceptTimeout() const
{
return _acceptTimeout;
}
// helper methods for logging
template <traits::formattable... Ts>
void logMessage(LoggerT::loglevel_t level, std::format_string<Ts...> fmt, Ts&&... args)
{
@@ -397,6 +410,8 @@ public:
}
protected:
std::chrono::seconds _acceptTimeout = std::chrono::seconds::max();
// template <interfaces::adc_netsession_c SessionT>
// inline static std::unordered_map<const AdcGenericNetServer*, bool> _isListening{};
template <interfaces::adc_netsession_c SessionT>
@@ -412,24 +427,28 @@ protected:
template <typename SessionT, typename AT, typename IDT, typename CTXT>
void doAccept(std::shared_ptr<AT> acceptor, IDT id, CTXT sess_ctx)
{
acceptor->asyncAccept([acceptor, id = std::move(id), sess_ctx = std::move(sess_ctx), this](
auto ec, typename SessionT::netservice_t srv) mutable {
if (!ec) {
logInfo(
"Client connection is succesfully accepted! Client endpoint: {} (server addr = {}, thread = {})",
srv.remoteEndpoint(), (void*)this, utils::AdcThisThreadId());
acceptor->asyncAccept(
[acceptor, id = std::move(id), sess_ctx = std::move(sess_ctx), this](
auto ec, typename SessionT::netservice_t srv) mutable {
if (!ec) {
logInfo(
"Client connection is succesfully accepted! Client endpoint: {} (server addr = {}, thread = "
"{})",
srv.remoteEndpoint(), (void*)this, utils::AdcThisThreadId());
auto sess = std::make_shared<SessionT>(id, std::move(srv), sess_ctx);
startSession(sess);
auto sess = std::make_shared<SessionT>(id, std::move(srv), sess_ctx);
startSession(sess);
_isListening<SessionT>[this][id] = true;
doAccept<SessionT>(acceptor, std::move(id), std::move(sess_ctx));
} else {
this->logError("Cannot start accepting connection: {}", SessionT::netservice_t::formattableError(ec));
_isListening<SessionT>[this][id] = true;
doAccept<SessionT>(acceptor, std::move(id), std::move(sess_ctx));
} else {
this->logError("Cannot start accepting connection: {}",
SessionT::netservice_t::formattableError(ec));
_isListening<SessionT>[this][id] = false;
}
});
_isListening<SessionT>[this][id] = false;
}
},
_acceptTimeout);
}
};