From b46ba43e06aba714abc0afb29566a56d69891691 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Mon, 16 Jun 2025 19:25:57 +0800 Subject: fix!: create new client without SP(this) param API: removed run(), use newClient() to accept a new client, now returning a WP of the client instance API: allow all cb to return std::any --- include/libsock/server/client.hpp | 10 +++++----- src/server/client.cpp | 10 ++++------ src/server/clients.cpp | 15 ++++++--------- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/include/libsock/server/client.hpp b/include/libsock/server/client.hpp index eb528a2..6a5aa88 100644 --- a/include/libsock/server/client.hpp +++ b/include/libsock/server/client.hpp @@ -2,6 +2,7 @@ #include "../types.hpp" #include "server.hpp" +#include #include #include #include @@ -42,9 +43,9 @@ class Client { const std::string &getIp() const; bool isAdmin() const; - UP read(std::optional> cb = std::nullopt); - UP read(const std::string &msg, std::optional> cb = std::nullopt); - bool write(const std::string &msg, std::optional> cb = std::nullopt); + UP read(std::optional> cb = std::nullopt); + UP read(const std::string &msg, std::optional> cb = std::nullopt); + bool write(const std::string &msg, std::optional> cb = std::nullopt); template bool write(std::format_string fmt, Args &&...args); @@ -81,9 +82,8 @@ class Clients { Clients(); ~Clients(); - void newClient(std::optional> cb = std::nullopt); + WP newClient(std::optional)>> cb = std::nullopt); - void run(std::optional> cb = std::nullopt); void broadcast(const std::string &msg, std::optional> self = std::nullopt); // second param only specified when we want to exclude the sender void kick(WP client, const bool kill = false, const std::string &reason = ""); void addClient(const Client &client); diff --git a/src/server/client.cpp b/src/server/client.cpp index 5de8b79..f8dc1ca 100644 --- a/src/server/client.cpp +++ b/src/server/client.cpp @@ -75,8 +75,7 @@ void SRecvData::sanitize() { data.clear(); } -UP -Client::read(std::optional> cb) { +UP Client::read(std::optional> cb) { auto recvData = std::make_unique(); recvData->data.resize(recvData->size); @@ -99,11 +98,10 @@ Client::read(std::optional> cb) { return recvData; } -UP -Client::read(const std::string &msg, std::optional> cb) { +UP Client::read(const std::string &msg, std::optional> cb) { write("{}", msg); m_szReading = msg; - return read(); + return read(cb); } void Client::run() { @@ -126,7 +124,7 @@ bool Client::isValid() { err == 0; } -bool Client::write(const std::string &msg, std::optional> cb) { +bool Client::write(const std::string &msg, std::optional> cb) { const auto r = write("{}", msg); if (cb) { auto recvData = std::make_unique(); diff --git a/src/server/clients.cpp b/src/server/clients.cpp index 5fc63df..210581a 100644 --- a/src/server/clients.cpp +++ b/src/server/clients.cpp @@ -1,5 +1,6 @@ #include "libsock/server/client.hpp" #include +#include #include #include #include @@ -44,17 +45,13 @@ Clients::~Clients() { } } -void Clients::newClient(std::optional> cb) { - if (cb) - (*cb)(); - SP client = std::make_shared(pServer, SP(this)); +WP Clients::newClient(std::optional)>> cb) { + SP client = std::make_shared(); auto &instance = m_vClients.emplace_back(std::jthread([client]() { client->run(); }), client); instance.second->self = std::weak_ptr(instance.second); -} - -void Clients::run(std::optional> cb) { - while (true) - newClient(cb); + if (cb) + (*cb)(instance.second); + return instance.second; } void Clients::broadcast(const std::string &msg, std::optional> self) { -- cgit v1.2.3