diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2025-06-19 18:45:34 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2025-06-19 18:45:34 +0800 |
| commit | cad2569f10e6c0131e5e645ee76330c776b91547 (patch) | |
| tree | f246b6a9799644c735228c6c5df2ece65e0d028d /include/libsock/server/clients.hpp | |
| parent | b3a3e73aaefa39e052e53f9f42c6d0e2f7e1bb9b (diff) | |
fix!: share pointer
for details: https://stackoverflow.com/a/5548314
Diffstat (limited to 'include/libsock/server/clients.hpp')
| -rw-r--r-- | include/libsock/server/clients.hpp | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/include/libsock/server/clients.hpp b/include/libsock/server/clients.hpp index ba0b40d..8eccbb7 100644 --- a/include/libsock/server/clients.hpp +++ b/include/libsock/server/clients.hpp @@ -3,6 +3,7 @@ #include "../types.hpp" #include <any> #include <functional> +#include <memory> #include <optional> #include <string> #include <thread> @@ -17,9 +18,10 @@ struct SData { std::string msg; std::optional<WP<Client>> sender = std::nullopt; }; -class Clients { + +class Clients : public std::enable_shared_from_this<Clients> { public: - Clients(); + static SP<Clients> create(); ~Clients(); WP<Client> newClient(std::optional<std::function<std::any(WP<Client>)>> cb = std::nullopt); @@ -36,13 +38,16 @@ class Clients { std::vector<SData> getDatas() const; private: + Clients(); void broadcast(const SData &msg); std::vector<std::pair<std::jthread, SP<Client>>> m_vClients; std::vector<SData> m_vDatas; + SP<Clients> get(); + friend class Client; }; - +inline SP<Clients> pClients; } // namespace Server } // namespace LibSock |
