From 3e11125a1694d61fb0bd578783437acfca8eacc3 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Sat, 21 Jun 2025 21:05:58 +0800 Subject: API(server/clients:newClient): actually use the cb func user provided for client thread --- include/libsock/server/clients.hpp | 2 +- src/server/clients.cpp | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/include/libsock/server/clients.hpp b/include/libsock/server/clients.hpp index a80fb4a..9e08797 100644 --- a/include/libsock/server/clients.hpp +++ b/include/libsock/server/clients.hpp @@ -25,7 +25,7 @@ class Clients : public std::enable_shared_from_this { static SP create(); ~Clients(); - WP newClient(SP server = pServer, std::optional)>> cb = std::nullopt); + WP newClient(std::function &)> loopFunc = [](const WP &) { return std::any(); }, SP server = pServer); void broadcast(const std::string &msg, std::optional> self = std::nullopt); // second param only specified when we // want to exclude the sender diff --git a/src/server/clients.cpp b/src/server/clients.cpp index 6f3f4e4..2bf82b4 100644 --- a/src/server/clients.cpp +++ b/src/server/clients.cpp @@ -58,12 +58,10 @@ Clients::~Clients() { } } -WP Clients::newClient(SP server, std::optional)>> cb) { +WP Clients::newClient(std::function &)> loopFunc, SP server) { SP client = SP(new Client(server, shared_from_this())); - auto &instance = m_vClients.emplace_back(std::jthread([&client]() { client->runLoop(); }), client); + auto &instance = m_vClients.emplace_back(std::jthread([&client, &loopFunc]() { loopFunc(client); }), client); instance.second->self = std::weak_ptr(instance.second); - if (cb) - (*cb)(instance.second); return instance.second; } -- cgit v1.2.3