X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/bc6cac0e474dfe2feb4983aef98f99e23a98ffc4..73bee151e048054bde9a39c5ad2efec25af9c77b:/projects/stargazer/traffcounter_impl.h diff --git a/projects/stargazer/traffcounter_impl.h b/projects/stargazer/traffcounter_impl.h index d23f6306..03c118df 100644 --- a/projects/stargazer/traffcounter_impl.h +++ b/projects/stargazer/traffcounter_impl.h @@ -23,14 +23,12 @@ #include "stg/traffcounter.h" #include "stg/logger.h" #include "stg/raw_ip_packet.h" -#include "stg/noncopyable.h" -#include "stg/notifer.h" -#include "actions.h" -#include "eventloop.h" +#include "stg/subscriptions.h" #include "user_impl.h" -#include #include +#include +#include #include #include #include @@ -39,6 +37,7 @@ #include #pragma GCC diagnostic pop #include +#include #define PROTOMAX (5) @@ -83,92 +82,7 @@ struct PacketExtraData { uint32_t lenD; // Download length }; //----------------------------------------------------------------------------- -class TraffCounterImpl; -//----------------------------------------------------------------------------- -class TRF_IP_BEFORE: public PropertyNotifierBase { -public: - TRF_IP_BEFORE(TraffCounterImpl & t, UserImpl * u) - : PropertyNotifierBase(), - traffCnt(t), - user(u) - {} - TRF_IP_BEFORE(const TRF_IP_BEFORE & rvalue) - : PropertyNotifierBase(), - traffCnt(rvalue.traffCnt), - user(rvalue.user) - {} - void notify(const uint32_t & oldValue, const uint32_t & newValue) override; - void SetUser(UserImpl * u) { user = u; } - UserImpl * GetUser() const { return user; } - -private: - TRF_IP_BEFORE & operator=(const TRF_IP_BEFORE & rvalue); - - TraffCounterImpl & traffCnt; - UserImpl * user; -}; -//----------------------------------------------------------------------------- -class TRF_IP_AFTER: public PropertyNotifierBase { -public: - TRF_IP_AFTER(TraffCounterImpl & t, UserImpl * u) - : PropertyNotifierBase(), - traffCnt(t), - user(u) - {} - TRF_IP_AFTER(const TRF_IP_AFTER & rvalue) - : PropertyNotifierBase(), - traffCnt(rvalue.traffCnt), - user(rvalue.user) - {} - void notify(const uint32_t & oldValue, const uint32_t & newValue) override; - void SetUser(UserImpl * u) { user = u; } - UserImpl * GetUser() const { return user; } -private: - TRF_IP_AFTER & operator=(const TRF_IP_AFTER & rvalue); - - TraffCounterImpl & traffCnt; - UserImpl * user; -}; - -using UserImplPtr = UserImpl*; -//----------------------------------------------------------------------------- -class ADD_USER_NONIFIER: public NotifierBase { -public: - explicit ADD_USER_NONIFIER(TraffCounterImpl & t) : - NotifierBase(), - traffCnt(t) - {} - virtual ~ADD_USER_NONIFIER() {} - void notify(const UserImplPtr & user) override; - -private: - ADD_USER_NONIFIER(const ADD_USER_NONIFIER & rvalue); - ADD_USER_NONIFIER & operator=(const ADD_USER_NONIFIER & rvalue); - - TraffCounterImpl & traffCnt; -}; -//----------------------------------------------------------------------------- -class DEL_USER_NONIFIER: public NotifierBase { -public: - explicit DEL_USER_NONIFIER(TraffCounterImpl & t) : - NotifierBase(), - traffCnt(t) - {} - virtual ~DEL_USER_NONIFIER() {} - void notify(const UserImplPtr & user) override; - -private: - DEL_USER_NONIFIER(const DEL_USER_NONIFIER & rvalue); - DEL_USER_NONIFIER & operator=(const DEL_USER_NONIFIER & rvalue); - - TraffCounterImpl & traffCnt; -}; -//----------------------------------------------------------------------------- class TraffCounterImpl : public TraffCounter { - friend class ADD_USER_NONIFIER; - friend class DEL_USER_NONIFIER; - friend class TRF_IP_BEFORE; - friend class TRF_IP_AFTER; public: TraffCounterImpl(UsersImpl * users, const std::string & rulesFileName); ~TraffCounterImpl(); @@ -198,8 +112,8 @@ class TraffCounterImpl : public TraffCounter { void AddUser(UserImpl * user); void DelUser(uint32_t uip); - void SetUserNotifiers(UserImpl * user); - void UnSetUserNotifiers(UserImpl * user); + void SetUserNotifiers(UserImpl* user); + void UnSetUserNotifiers(UserImpl* user); typedef std::list::iterator rule_iter; @@ -230,44 +144,14 @@ class TraffCounterImpl : public TraffCounter { std::mutex m_mutex; std::jthread m_thread; - std::list ipBeforeNotifiers; - std::list ipAfterNotifiers; + ScopedConnection m_onAddUserConn; + ScopedConnection m_onDelUserConn; - ADD_USER_NONIFIER addUserNotifier; - DEL_USER_NONIFIER delUserNotifier; + using OnIPConns = std::tuple; + std::vector m_onIPConns; + void beforeIPChange(uint32_t oldVal); + void afterIPChange(UserImpl* user, uint32_t newVal); }; -//----------------------------------------------------------------------------- -inline -void TRF_IP_BEFORE::notify(const uint32_t & oldValue, const uint32_t &) -{ -// User changes his address. Remove old IP -if (!oldValue) - return; - -EVENT_LOOP::instance().Enqueue(traffCnt, &TraffCounterImpl::DelUser, oldValue); -} -//----------------------------------------------------------------------------- -inline -void TRF_IP_AFTER::notify(const uint32_t &, const uint32_t & newValue) -{ -// User changes his address. Add new IP -if (!newValue) - return; -EVENT_LOOP::instance().Enqueue(traffCnt, &TraffCounterImpl::AddUser, user); -} -//----------------------------------------------------------------------------- -inline -void ADD_USER_NONIFIER::notify(const UserImplPtr & user) -{ -EVENT_LOOP::instance().Enqueue(traffCnt, &TraffCounterImpl::SetUserNotifiers, user); } //----------------------------------------------------------------------------- -inline -void DEL_USER_NONIFIER::notify(const UserImplPtr & user) -{ -EVENT_LOOP::instance().Enqueue(traffCnt, &TraffCounterImpl::UnSetUserNotifiers, user); -EVENT_LOOP::instance().Enqueue(traffCnt, &TraffCounterImpl::DelUser, user->GetCurrIP()); -} -//----------------------------------------------------------------------------- -}