Files
pip/utils/cloud_dispatcher/cloudserver.cpp
T
andrey 1ec61a0127 fix(picloud-dispatcher): forward data to sub-client under mutex_clients
The dataReadedServer lambda looked up the sub-client under
mutex_clients but called sendData() after the unlock; the client could
be deleted by the drainer in between (same UAF pattern that was fixed
in PICloudServer::_readed). Keep the send inside the locked section:
sendData only takes mutex_send/prog_s_mutex downstream, so the lock
hold is bounded.
2026-08-23 18:55:00 +03:00

100 lines
2.3 KiB
C++

#include "cloudserver.h"
CloudServer::CloudServer(DispatcherClient * c, const PIByteArray & sname): server(c) {
setName(sname.toHex());
server_uuid = sname;
connects << CONNECTL(c, dataReadedServer, ([this](uint id, PIByteArray & ba) {
// keep the lookup and the send under the lock: the
// sub-client may be deleted by the drainer concurrently
mutex_clients.lock();
last_ping.reset();
DispatcherClient * cl = index_clients.value(id, nullptr);
if (cl) cl->sendData(ba);
mutex_clients.unlock();
}));
connects << CONNECTL(c, pingReceived, [this]() {
mutex_clients.lock();
last_ping.reset();
mutex_clients.unlock();
});
{
PIMutexLocker locker(mutex_clients);
last_ping.reset();
}
}
CloudServer::~CloudServer() {}
PIByteArray CloudServer::serverUUID() const {
return server_uuid;
}
void CloudServer::addClient(DispatcherClient * c) {
mutex_clients.lock();
last_ping.reset();
clients << c;
uint cid = c->clientId();
index_clients.insert(cid, c);
mutex_clients.unlock();
c->sendConnected(1);
server->sendConnected(cid);
CONNECTL(c, dataReaded, ([this, cid](PIByteArray & ba) {
// piCoutObj << c->clientId() << "dataReaded";
server->sendDataToClient(ba, cid);
}));
}
void CloudServer::removeClient(DispatcherClient * c) {
mutex_clients.lock();
last_ping.reset();
clients.removeOne(c);
index_clients.remove(c->clientId());
mutex_clients.unlock();
server->sendDisconnected(c->clientId());
}
PIVector<DispatcherClient *> CloudServer::getClients() {
PIMutexLocker locker(mutex_clients);
PIVector<DispatcherClient *> cl = clients;
return cl;
}
double CloudServer::lastPing() {
PIMutexLocker locker(mutex_clients);
return last_ping.elapsed_s();
}
void CloudServer::close() {
server->close();
}
void CloudServer::stop() {
// copy the list under the lock: addClient()/removeClient() modify it
// concurrently from other threads
PIVector<DispatcherClient *> cl;
{
PIMutexLocker locker(mutex_clients);
cl = clients;
}
for (auto c: cl)
c->close();
server->stop();
}
void CloudServer::printStatus() {
PIMutexLocker locker(mutex_clients);
piCout << " " << "Clients for" << server->address() << server_uuid.toHex() << ":";
for (auto c: clients) {
piCout << " " << c->address() << c->clientId();
}
}