From 80b51b45089d2cf3aa5670454d1c971e4adff18c Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Sat, 21 Jun 2025 20:27:35 +0800 Subject: API(server/clients): runLoop, resumeHistory --- src/server/client.cpp | 22 +++++++++++++++------- src/server/clients.cpp | 2 +- 2 files changed, 16 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/server/client.cpp b/src/server/client.cpp index d89aea0..f3d601a 100644 --- a/src/server/client.cpp +++ b/src/server/client.cpp @@ -40,7 +40,7 @@ Client::~Client() { m_sockfd.reset(); } -void Client::recvLoop() { +void Client::recvLoop(std::optional> cb) { while (true) { auto recvData = read(); if (!recvData->good) @@ -51,6 +51,9 @@ void Client::recvLoop() { continue; pClients->m_vDatas.push_back({recvData->data, self}); + + if (cb) + (*cb)(*recvData); } } @@ -98,13 +101,18 @@ UP Client::read(const std::string &msg, std::optionalgetDatas()) { - write(chat.msg); - recvLoop(); +void Client::runLoop(bool resumeHist, std::optional> cb) { + if (resumeHist) + resumeHistory(); - pClients->kick(self); - } + recvLoop(); + + pClients->kick(self); +} + +void Client::resumeHistory() { + for (const auto &chat : pClients->getDatas()) + write(chat.msg); } bool Client::isValid() { diff --git a/src/server/clients.cpp b/src/server/clients.cpp index ac3546e..6f3f4e4 100644 --- a/src/server/clients.cpp +++ b/src/server/clients.cpp @@ -60,7 +60,7 @@ Clients::~Clients() { WP Clients::newClient(SP server, std::optional)>> cb) { SP client = SP(new Client(server, shared_from_this())); - auto &instance = m_vClients.emplace_back(std::jthread([client]() { client->run(); }), client); + auto &instance = m_vClients.emplace_back(std::jthread([&client]() { client->runLoop(); }), client); instance.second->self = std::weak_ptr(instance.second); if (cb) (*cb)(instance.second); -- cgit v1.2.3