]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/ping/ping.h
Use std::lock_guard instead of STG_LOCKER.
[stg.git] / projects / stargazer / plugins / other / ping / ping.h
1 #pragma once
2
3 #include "stg/plugin.h"
4 #include "stg/module_settings.h"
5 #include "stg/subscriptions.h"
6 #include "stg/user_ips.h"
7 #include "stg/pinger.h"
8 #include "stg/users.h"
9 #include "stg/logger.h"
10
11 #include <string>
12 #include <vector>
13 #include <tuple>
14 #include <list>
15 #include <mutex>
16 #pragma GCC diagnostic push
17 #pragma GCC diagnostic ignored "-Wshadow"
18 #include <jthread.hpp>
19 #pragma GCC diagnostic pop
20 #include <cstdint>
21
22 namespace STG
23 {
24 struct USER;
25 struct SETTINGS;
26
27 using UserPtr = User*;
28 //-----------------------------------------------------------------------------
29 class PING_SETTINGS
30 {
31     public:
32         PING_SETTINGS() : pingDelay(0) {}
33         const std::string & GetStrError() const { return errorStr; }
34         int ParseSettings(const ModuleSettings & s);
35         int GetPingDelay() const { return pingDelay; }
36     private:
37         int pingDelay;
38         std::string errorStr;
39 };
40 //-----------------------------------------------------------------------------
41 class PING : public Plugin
42 {
43     public:
44         PING();
45
46         void SetUsers(Users * u) override { users = u; }
47         void SetSettings(const ModuleSettings & s) override { settings = s; }
48         int ParseSettings() override;
49
50         int Start() override;
51         int Stop() override;
52         int Reload(const ModuleSettings & /*ms*/) override { return 0; }
53         bool IsRunning() override;
54
55         const std::string & GetStrError() const override { return errorStr; }
56         std::string GetVersion() const override { return "Pinger v.1.01"; }
57         uint16_t GetStartPosition() const override { return 10; }
58         uint16_t GetStopPosition() const override { return 10; }
59
60         void AddUser(UserPtr u);
61         void DelUser(UserPtr u);
62
63     private:
64         explicit PING(const PING & rvalue);
65         PING & operator=(const PING & rvalue);
66
67         void GetUsers();
68         void SetUserNotifiers(UserPtr u);
69         void UnSetUserNotifiers(UserPtr u);
70         void Run(std::stop_token token);
71
72         std::string errorStr;
73         PING_SETTINGS pingSettings;
74         ModuleSettings settings;
75         Users * users;
76         std::list<UserPtr> usersList;
77
78         std::jthread m_thread;
79         std::mutex m_mutex;
80         bool isRunning;
81         STG_PINGER m_pinger;
82
83         void updateCurrIP(uint32_t oldVal, uint32_t newVal);
84         void updateIPs(const UserIPs& oldVal, const UserIPs& newVal);
85
86         ScopedConnection m_onAddUserConn;
87         ScopedConnection m_onDelUserConn;
88
89         using ConnHolder = std::tuple<int, ScopedConnection, ScopedConnection>;
90         std::vector<ConnHolder> m_conns;
91
92         PluginLogger logger;
93 };
94
95 }