summaryrefslogtreecommitdiff
path: root/src/server
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-06-21 20:27:35 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-06-22 11:13:20 +0800
commit80b51b45089d2cf3aa5670454d1c971e4adff18c (patch)
treea75b62e7e217f9d40daa80bd196325ca4cab818c /src/server
parent2c466d30464b566c471a89c5528d768be7729445 (diff)
API(server/clients): runLoop, resumeHistory
Diffstat (limited to 'src/server')
-rw-r--r--src/server/client.cpp22
-rw-r--r--src/server/clients.cpp2
2 files changed, 16 insertions, 8 deletions
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<std::function<std::any(const SRecvData &)>> 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<SRecvData> Client::read(const std::string &msg, std::optional<std::function<s
return read(cb);
}
-void Client::run() {
- for (const auto &chat : pClients->getDatas()) {
- write(chat.msg);
- recvLoop();
+void Client::runLoop(bool resumeHist, std::optional<std::function<std::any(const SRecvData &)>> 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<Client> Clients::newClient(SP<Server> server, std::optional<std::function<std::any(WP<Client>)>> cb) {
SP client = SP<Client>(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<Client>(instance.second);
if (cb)
(*cb)(instance.second);