]> git.stg.codes - stg.git/blobdiff - projects/stargazer/plugins/other/rscript/rscript.h
Start replacing notifiers with subscriptions.
[stg.git] / projects / stargazer / plugins / other / rscript / rscript.h
index e734227c48e67d50b72fd985abe1eed248ef0e3f..d0bf1e7e262f3d82fd23f7dab3b41e441861b4d6 100644 (file)
@@ -23,6 +23,7 @@
 
 #include "stg/plugin.h"
 #include "stg/module_settings.h"
+#include "stg/subscriptions.h"
 #include "stg/notifer.h"
 #include "stg/user.h"
 #include "stg/blowfish.h"
 #include <map>
 #include <functional>
 #include <utility>
+#include <mutex>
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wshadow"
+#include <jthread.hpp>
+#pragma GCC diagnostic pop
 #include <cstdint>
 
-#include <pthread.h>
-
 namespace STG
 {
 struct Settings;
@@ -55,32 +59,6 @@ class DisconnectUser;
 
 using UserPtr = STG::User*;
 
-//-----------------------------------------------------------------------------
-class ADD_USER_NONIFIER: public STG::NotifierBase<UserPtr> {
-public:
-    explicit ADD_USER_NONIFIER(REMOTE_SCRIPT & r)
-        : rs(r) {}
-    void Notify(const UserPtr & user);
-
-private:
-    ADD_USER_NONIFIER(const ADD_USER_NONIFIER & rhs);
-    ADD_USER_NONIFIER & operator=(const ADD_USER_NONIFIER);
-
-    REMOTE_SCRIPT & rs;
-};
-//-----------------------------------------------------------------------------
-class DEL_USER_NONIFIER: public STG::NotifierBase<UserPtr> {
-public:
-    explicit DEL_USER_NONIFIER(REMOTE_SCRIPT & r)
-        : rs(r) {}
-    void Notify(const UserPtr & user);
-
-private:
-    DEL_USER_NONIFIER(const DEL_USER_NONIFIER & rhs);
-    DEL_USER_NONIFIER & operator=(const DEL_USER_NONIFIER);
-
-    REMOTE_SCRIPT & rs;
-};
 //-----------------------------------------------------------------------------
 class IP_NOTIFIER: public STG::PropertyNotifierBase<uint32_t> {
 public:
@@ -98,7 +76,7 @@ public:
         return *this;
     }
 
-    void Notify(const uint32_t & oldValue, const uint32_t & newValue);
+    void notify(const uint32_t & oldValue, const uint32_t & newValue) override;
     UserPtr GetUser() const { return user; }
 
 private:
@@ -123,7 +101,7 @@ public:
         return *this;
     }
 
-    void Notify(const bool & oldValue, const bool & newValue);
+    void notify(const bool & oldValue, const bool & newValue) override;
     UserPtr GetUser() const { return user; }
 
 private:
@@ -174,7 +152,6 @@ private:
 class REMOTE_SCRIPT : public STG::Plugin {
 public:
                         REMOTE_SCRIPT();
-                        ~REMOTE_SCRIPT() override;
 
     void                SetUsers(STG::Users * u) override { users = u; }
     void                SetSettings(const STG::ModuleSettings & s) override { settings = s; }
@@ -200,7 +177,7 @@ private:
     REMOTE_SCRIPT(const REMOTE_SCRIPT & rhs);
     REMOTE_SCRIPT & operator=(const REMOTE_SCRIPT & rhs);
 
-    static void *       Run(void *);
+    void                Run(std::stop_token token);
     bool                PrepareNet();
     bool                FinalizeNet();
 
@@ -215,8 +192,8 @@ private:
     void                SetUserNotifiers(UserPtr u);
     void                UnSetUserNotifiers(UserPtr u);
 
-    void                InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password) const;
-    void                Encrypt(BLOWFISH_CTX * ctx, void * dst, const void * src, size_t len8) const;
+    void                InitEncrypt(const std::string & password) const;
+    void                Encrypt(void * dst, const void * src, size_t len8) const;
 
     mutable BLOWFISH_CTX ctx;
 
@@ -230,20 +207,19 @@ private:
     int                 sendPeriod;
     int                 halfPeriod;
 
-    bool                nonstop;
     bool                isRunning;
 
     STG::Users *             users;
 
     std::vector<NET_ROUTER> netRouters;
 
-    pthread_t           thread;
-    pthread_mutex_t     mutex;
+    std::jthread        m_thread;
+    std::mutex          m_mutex;
 
     int                 sock;
 
-    ADD_USER_NONIFIER onAddUserNotifier;
-    DEL_USER_NONIFIER onDelUserNotifier;
+    STG::ScopedConnection m_onAddUserConn;
+    STG::ScopedConnection m_onDelUserConn;
 
     STG::PluginLogger       logger;
 
@@ -263,15 +239,5 @@ class DisconnectUser : public std::unary_function<std::pair<const uint32_t, USER
         REMOTE_SCRIPT & rscript;
 };
 //-----------------------------------------------------------------------------
-inline void ADD_USER_NONIFIER::Notify(const UserPtr & user)
-{
-rs.AddUser(user);
-}
-//-----------------------------------------------------------------------------
-inline void DEL_USER_NONIFIER::Notify(const UserPtr & user)
-{
-rs.DelUser(user);
-}
-//-----------------------------------------------------------------------------
 
 } // namespace RS