From 2320470d342db4cb5ae0ae717dfc5ac94bd3f2d2 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Sat, 21 Jun 2025 21:18:28 +0800 Subject: internal(server): remove return of std::any for callbacks use bool for cases for premature return --- src/server/client.cpp | 24 +++++++++++++----------- src/server/clients.cpp | 15 +++++++-------- 2 files changed, 20 insertions(+), 19 deletions(-) (limited to 'src/server') diff --git a/src/server/client.cpp b/src/server/client.cpp index 33cbf9d..c59100e 100644 --- a/src/server/client.cpp +++ b/src/server/client.cpp @@ -40,7 +40,7 @@ Client::~Client() { m_sockfd.reset(); } -void Client::recvLoop(std::optional> cb) { +void Client::recvLoop(std::optional> cb) { while (true) { auto recvData = read(); if (!recvData->good) @@ -52,8 +52,8 @@ void Client::recvLoop(std::optional> pClients->m_vDatas.push_back({recvData->data, self}); - if (cb) - (*cb)(*recvData); + if (cb && !(*cb)(*recvData)) + return; } } @@ -76,7 +76,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); @@ -84,8 +84,10 @@ UP Client::read(std::optionalgood = false; - if (cb) - (*cb)(*recvData); + if (cb && !(*cb)(*recvData)) { + m_szReading.reset(); + return nullptr; + } } else { recvData->data.resize(size); recvData->good = true; @@ -95,13 +97,13 @@ UP Client::read(std::optional 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(cb); } -void Client::runLoop(bool resumeHist, std::optional> cb) { +void Client::runLoop(bool resumeHist, std::optional> cb) { if (resumeHist) resumeHistory(); @@ -124,13 +126,13 @@ bool Client::isValid() { return getsockopt(m_sockfd->get(), SOL_SOCKET, SO_ERROR, &err, &size) >= 0 && err == 0; } -bool Client::write(const std::string &msg, std::optional> cb) { - const auto r = write("{}", msg); +bool Client::write(const std::string &msg, std::optional> cb) { + auto r = write("{}", msg); if (cb) { auto recvData = std::make_unique(); recvData->data = msg; recvData->good = r; - (*cb)(*recvData); + r = (*cb)(*recvData); } return r; } diff --git a/src/server/clients.cpp b/src/server/clients.cpp index 2bf82b4..e1dd161 100644 --- a/src/server/clients.cpp +++ b/src/server/clients.cpp @@ -1,7 +1,6 @@ #include "libsock/server/clients.hpp" #include "libsock/server/client.hpp" #include -#include #include #include #include @@ -12,12 +11,12 @@ using namespace LibSock::Server; namespace LibSock { namespace Server { -void Clients::shutdownClients(std::optional>> &)>> cb) { +void Clients::shutdownClients(std::optional>> &)>> cb) { if (m_vClients.empty()) return; - if (cb) - (*cb)(m_vClients); + if (cb && !(*cb)(m_vClients)) + return; for (auto &[thread, client] : m_vClients) { if (thread.joinable()) @@ -58,7 +57,7 @@ Clients::~Clients() { } } -WP Clients::newClient(std::function &)> loopFunc, SP server) { +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, &loopFunc]() { loopFunc(client); }), client); instance.second->self = std::weak_ptr(instance.second); @@ -93,7 +92,7 @@ std::vector Clients::getDatas() const { return m_vDatas; } -void Clients::kick(WP clientWeak, const bool kill, const std::string &reason, std::optional>>::iterator &)>> cb) { +void Clients::kick(WP clientWeak, const bool kill, const std::string &reason, std::optional>>::iterator &)>> cb) { auto client = clientWeak.lock(); if (!client) return; @@ -103,8 +102,8 @@ void Clients::kick(WP clientWeak, const bool kill, const std::string &re if (client) // this only exist when the client is registered // not sure why the second is null - if (cb) - (*cb)(it); + if (cb && !(*cb)(it)) + return; if (!reason.empty()) client->write(reason); -- cgit v1.2.3