diff --git a/net/adc_netserver.h b/net/adc_netserver.h index 7cddb8b..dcb9404 100644 --- a/net/adc_netserver.h +++ b/net/adc_netserver.h @@ -103,29 +103,13 @@ protected: AdcNetSessionManager(const AdcNetSessionManager&) = delete; AdcNetSessionManager(AdcNetSessionManager&& other) { - if (this == &other) { - return; - } - - for (auto& func : _moveCtorFunc) { - func(this); - } - - _stopSessionFunc = std::move(other._stopSessionFunc); - _moveCtorFunc = std::move(other._moveCtorFunc); + moveInstFunc(&other, this); } AdcNetSessionManager& operator=(const AdcNetSessionManager&) = delete; AdcNetSessionManager& operator=(AdcNetSessionManager&& other) { - if (this != &other) { - for (auto& func : _moveCtorFunc) { - func(this); - } - - _stopSessionFunc = std::move(other._stopSessionFunc); - _moveCtorFunc = std::move(other._moveCtorFunc); - } + moveInstFunc(&other, this); return *this; }; @@ -139,11 +123,10 @@ protected: // started sessions weak pointers template - // static std::unordered_map>> - // _serverSessions; static inline std::unordered_map>> _serverSessions{}; - std::vector> _stopSessionFunc; - std::vector> _moveCtorFunc; + + std::vector> _stopSessionFunc; + std::vector> _moveCtorFunc; template void startSession(std::shared_ptr& sess_ptr) @@ -152,11 +135,11 @@ protected: it = _serverSessions[this].emplace(it, sess_ptr); sess_ptr->start(); - _stopSessionFunc.emplace_back([it, this]() { + _stopSessionFunc.emplace_back([it](const AdcNetSessionManager* inst) { if (!it->expired()) { // session is still existing auto sess = it->lock(); sess->stop(); - _serverSessions[this].erase(it); + _serverSessions[inst].erase(it); return true; } else { return false; @@ -164,27 +147,12 @@ protected: }); - // auto res = _serverSessions[this].emplace(sess_ptr); - // if (res.second) { - // sess_ptr->start(); - - // _stopSessionFunc.emplace_back([res, this]() { - // if (!res.first->expired()) { // session is still existing - // auto sess = res.first->lock(); - // sess->stop(); - // _serverSessions[this].erase(res.first); - // return true; - // } else { - // return false; - // } - // }); - // } - // define move-function only once per SessionT! if (_serverSessions[this].size() == 1) { - _moveCtorFunc.emplace_back([this](const AdcNetSessionManager* new_instance) { - _serverSessions[new_instance] = std::move(_serverSessions[this]); - }); + _moveCtorFunc.emplace_back( + [](const AdcNetSessionManager* new_instance, const AdcNetSessionManager* from_inst) { + _serverSessions[new_instance] = std::move(_serverSessions[from_inst]); + }); } } @@ -229,7 +197,7 @@ protected: size_t N = 0; for (auto& func : _stopSessionFunc) { - func() ? ++N : 0; + func(this) ? ++N : 0; } _stopSessionFunc.clear(); @@ -237,6 +205,19 @@ protected: return N; } + + + void moveInstFunc(const AdcNetSessionManager* to, const AdcNetSessionManager* from) + { + if (from != to) { + for (auto& func : _moveCtorFunc) { + func(to, from); + } + + _stopSessionFunc = std::move(from->_stopSessionFunc); + _moveCtorFunc = std::move(from->_moveCtorFunc); + } + } }; @@ -271,7 +252,7 @@ public: _stopListenFunc = std::move(other._stopListenFunc); for (auto& func : other._moveCtorFunc) { - func(this); + func(&other, this); } _moveCtorFunc = std::move(other._moveCtorFunc); @@ -290,7 +271,7 @@ public: _stopListenFunc = std::move(other._stopListenFunc); for (auto& func : other._moveCtorFunc) { - func(this); + func(&other, this); } _moveCtorFunc = std::move(other._moveCtorFunc); @@ -313,11 +294,11 @@ public: auto acceptor = std::make_shared( std::forward(ctor_args)...); - _stopListenFunc.emplace_back([acceptor, id, this]() { + _stopListenFunc.emplace_back([acceptor, id](const AdcGenericNetServer* inst) { std::error_code ec; acceptor->close(ec); - _isListening[this][id] = false; + _isListening[inst][id] = false; }); doAccept(acceptor, std::move(id), std::move(sess_ctx)); @@ -325,9 +306,10 @@ public: // only once per SessionT if (_isListening[this].size() == 1) { - _moveCtorFunc.emplace_back([this](const AdcGenericNetServer* new_instance) { - _isListening[new_instance] = std::move(_isListening[this]); - }); + _moveCtorFunc.emplace_back( + [](const AdcGenericNetServer* new_instance, const AdcGenericNetServer* from_inst) { + _isListening[new_instance] = std::move(_isListening[from_inst]); + }); } }; @@ -343,7 +325,7 @@ public: virtual void stop() { for (auto& func : _stopListenFunc) { - func(); + func(this); } _stopListenFunc.clear(); @@ -361,8 +343,8 @@ protected: std::map> _isListening{}; - std::vector> _stopListenFunc; - std::vector> _moveCtorFunc; + std::vector> _stopListenFunc; + std::vector> _moveCtorFunc; server_ident_t _serverIdent;