From cad2569f10e6c0131e5e645ee76330c776b91547 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Thu, 19 Jun 2025 18:45:34 +0800 Subject: fix!: share pointer for details: https://stackoverflow.com/a/5548314 --- src/server/clients.cpp | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'src/server/clients.cpp') diff --git a/src/server/clients.cpp b/src/server/clients.cpp index c957d92..201ccc6 100644 --- a/src/server/clients.cpp +++ b/src/server/clients.cpp @@ -9,6 +9,9 @@ using namespace LibSock::Server; +namespace LibSock { +namespace Server { + void Clients::shutdownClients(std::optional>> &)>> cb) { if (m_vClients.empty()) return; @@ -27,14 +30,25 @@ void Clients::shutdownClients(std::optionalshutdownClients(); + if (pClients) + pClients->shutdownClients(); })) void(); // failed to register exit handler... +} + +SP Clients::create() { if (pClients) - throw std::runtime_error("server:client: ClientManager already exist"); + throw std::runtime_error("server:clients ClientManager already exist"); - pClients = SP(this); -}; + pClients = SP(new Clients()); + return pClients; +} + +SP Clients::get() { + if (!pClients) + pClients = shared_from_this(); + return pClients; +} Clients::~Clients() { for (auto &[thread, client] : m_vClients) { @@ -45,7 +59,7 @@ Clients::~Clients() { } WP Clients::newClient(std::optional)>> cb) { - SP client = SP(new Client()); + SP client = SP(new Client(pServer, shared_from_this())); auto &instance = m_vClients.emplace_back(std::jthread([client]() { client->run(); }), client); instance.second->self = std::weak_ptr(instance.second); if (cb) @@ -111,3 +125,6 @@ void Clients::kick(WP clientWeak, const bool kill, const std::string &re } } } + +} // namespace Server +} // namespace LibSock -- cgit v1.2.3