diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2025-06-22 17:59:08 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2025-06-22 18:04:51 +0800 |
| commit | 89a41052c9195c684e1eb035b4a556f986c321f3 (patch) | |
| tree | e07c08abcb0a4c351ce30bc7ef59a624bc7684e6 /src/server/clients.cpp | |
| parent | c76c01d1fd2ccbf91a6c3e0a120ed68c37ff106b (diff) | |
API!(server/clients:newClient): now returns a futuremain
Diffstat (limited to 'src/server/clients.cpp')
| -rw-r--r-- | src/server/clients.cpp | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/src/server/clients.cpp b/src/server/clients.cpp index 5337769..8bb85a3 100644 --- a/src/server/clients.cpp +++ b/src/server/clients.cpp @@ -2,6 +2,7 @@ #include "libsock/server/client.hpp" #include <algorithm> #include <functional> +#include <future> #include <ranges> #include <unistd.h> #include <utility> @@ -59,11 +60,17 @@ Clients::~Clients() { vpClients.erase(std::remove_if(vpClients.begin(), vpClients.end(), [this](const WP<Clients> &wptr) { return !wptr.owner_before(m_self) && !m_self.owner_before(wptr); }), vpClients.end()); } -SP<Client> Clients::newClient(std::function<void(SP<Client> &)> cb, bool track, bool wait) { +std::future<SP<Client>> Clients::newClient(std::function<void(SP<Client> &)> cb, bool track, bool wait) { + auto promise = std::make_shared<std::promise<SP<Client>>>(); SP client = SP<Client>(new Client(m_wpServer.lock(), shared_from_this(), track, wait)); - auto &instance = m_vClients.emplace_back(std::jthread([&client, &cb]() { client->init(); cb(client); }), client); + auto &instance = m_vClients.emplace_back(std::jthread([client, cb, promise]() mutable { + client->init(); + promise->set_value(client); + cb(client); + }), + client); instance.second->m_self = std::weak_ptr<Client>(instance.second); - return client; + return promise->get_future(); } void Clients::broadcast(const std::string &msg, std::optional<std::weak_ptr<Client>> self) { |
