summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/server/clients.cpp13
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) {