]> git.stg.codes - stg.git/blobdiff - projects/rscriptd/listener.h
Add subscriptions (to replace notifiers).
[stg.git] / projects / rscriptd / listener.h
index f4d2856e7c7b0cdd35d3821d3278b396fd49b5c0..4d36b87a1ac7534b339cb609a1e3d1b3b708e8a3 100644 (file)
  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
  */
 
  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
  */
 
-#include <pthread.h>
+#include "stg/blowfish.h"
+#include "stg/rs_packets.h"
+#include "stg/logger.h"
 
 #include <string>
 #include <vector>
 #include <list>
 #include <functional>
 
 #include <string>
 #include <vector>
 #include <list>
 #include <functional>
-
-#include "stg/os_int.h"
-#include "stg/blowfish.h"
-#include "stg/rs_packets.h"
-#include "stg/logger.h"
+#include <mutex>
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wshadow"
+#include <jthread.hpp>
+#pragma GCC diagnostic pop
+#include <cstdint>
 
 struct UserData
 {
 
 struct UserData
 {
@@ -39,31 +42,21 @@ struct UserData
     uint32_t    id;
 };
 
     uint32_t    id;
 };
 
-struct PendingData : public UserData
+struct PendingData
 {
 {
+    UserData data;
     enum {CONNECT, ALIVE, DISCONNECT} type;
 };
 
     enum {CONNECT, ALIVE, DISCONNECT} type;
 };
 
-struct AliveData : public UserData
+struct AliveData
 {
 {
-    explicit AliveData(const UserData & data)
-        : UserData(data),
+    explicit AliveData(const UserData& ud)
+        : data(ud),
           lastAlive(time(NULL))
     {};
           lastAlive(time(NULL))
     {};
-    bool operator<(const std::string & rvalue) const { return login < rvalue; };
-    time_t      lastAlive;
-};
-
-class IsNotTimedOut : public std::unary_function<const AliveData &, bool> {
-    public:
-        IsNotTimedOut(double to) : timeout(to), now(time(NULL)) {}
-        bool operator()(const AliveData & data) const
-        {
-            return difftime(now, data.lastAlive) < timeout;
-        }
-    private:
-        double timeout;
-        time_t now;
+    bool operator<(const std::string& rhs) const { return data.login < rhs; };
+    UserData data;
+    time_t lastAlive;
 };
 
 class LISTENER
 };
 
 class LISTENER
@@ -87,25 +80,23 @@ public:
 
 private:
     // Threading stuff
 
 private:
     // Threading stuff
-    static void *       Run(void * self);
-    static void *       RunProcessor(void * self);
-    void                Runner();
-    void                ProcessorRunner();
+    void                Run(std::stop_token token);
+    void                RunProcessor(std::stop_token token);
     // Networking stuff
     bool                PrepareNet();
     bool                FinalizeNet();
     // Networking stuff
     bool                PrepareNet();
     bool                FinalizeNet();
-    bool                RecvPacket();
+    bool                RecvPacket(const std::stop_token& token);
     // Parsing stuff
     // Parsing stuff
-    bool                CheckHeader(const RS_PACKET_HEADER & header) const;
+    bool                CheckHeader(const RS::PACKET_HEADER & header) const;
     bool                GetParams(char * buffer, UserData & data);
     // Processing stuff
     void                ProcessPending();
     void                ProcessTimeouts();
     bool                GetParams(char * buffer, UserData & data);
     // Processing stuff
     void                ProcessPending();
     void                ProcessTimeouts();
-    bool                Disconnect(const UserData & data) const;
-    bool                Connect(const UserData & data) const;
+    bool                Disconnect(const AliveData& data) const;
+    bool                Connect(const PendingData& data) const;
 
     BLOWFISH_CTX        ctxS;
 
     BLOWFISH_CTX        ctxS;
-    STG_LOGGER &        WriteServLog;
+    STG::Logger&        WriteServLog;
 
     mutable std::string errorStr;
     std::string         scriptOnConnect;
 
     mutable std::string errorStr;
     std::string         scriptOnConnect;
@@ -113,32 +104,17 @@ private:
     std::string         password;
     uint16_t            port;
 
     std::string         password;
     uint16_t            port;
 
-    bool                running;
     bool                receiverStopped;
     bool                processorStopped;
     std::vector<AliveData> users;
     bool                receiverStopped;
     bool                processorStopped;
     std::vector<AliveData> users;
-    std::list<PendingData> pending;
+    std::vector<PendingData> pending;
     int                 userTimeout;
 
     int                 userTimeout;
 
-    pthread_t           receiverThread;
-    pthread_t           processorThread;
-    pthread_mutex_t     mutex;
+    std::jthread        m_receiverThread;
+    std::jthread        m_processorThread;
+    std::mutex          m_mutex;
 
     int                 listenSocket;
 
     std::string         version;
 
     int                 listenSocket;
 
     std::string         version;
-
-    friend class DisconnectUser;
-};
-
-class DisconnectUser : public std::unary_function<const UserData &, void> {
-    public:
-        DisconnectUser(LISTENER & l) : listener(l) {};
-        void operator()(const UserData & data)
-        {
-            listener.Disconnect(data);
-        };
-    private:
-        LISTENER & listener;
 };
 };
-//-----------------------------------------------------------------------------