PICloud many important fixes

This commit is contained in:
Бычков Андрей
2022-11-11 16:18:05 +03:00
parent ec8fbcb112
commit cb59017ebb
9 changed files with 57 additions and 19 deletions

View File

@@ -5,7 +5,9 @@ CloudServer::CloudServer(DispatcherClient * c, const PIByteArray & sname) : serv
server_uuid = sname;
CONNECTL(c, dataReadedServer, ([this](uint id, PIByteArray & ba){
last_ping.reset();
mutex_clients.lock();
DispatcherClient * cl = index_clients.value(id, nullptr);
mutex_clients.unlock();
if (cl) cl->sendData(ba);
}));
CONNECTL(c, pingReceived, [this]() {last_ping.reset();});
@@ -27,9 +29,11 @@ PIByteArray CloudServer::serverUUID() const {
void CloudServer::addClient(DispatcherClient * c) {
last_ping.reset();
mutex_clients.lock();
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){
@@ -41,14 +45,19 @@ void CloudServer::addClient(DispatcherClient * c) {
void CloudServer::removeClient(DispatcherClient * c) {
last_ping.reset();
mutex_clients.lock();
clients.removeOne(c);
index_clients.remove(c->clientId());
mutex_clients.unlock();
server->sendDisconnected(c->clientId());
}
PIVector<DispatcherClient *> CloudServer::getClients() {
return clients;
mutex_clients.lock();
PIVector<DispatcherClient *> cl = clients;
mutex_clients.unlock();
return cl;
}
@@ -58,14 +67,12 @@ double CloudServer::lastPing() {
void CloudServer::printStatus() {
mutex_clients.lock();
piCout << " " << "Clients for" << server->address() << server_uuid.toHex() << ":";
for (auto c: clients) {
piCout << " " << c->address() << c->clientId();
}
// for (auto c: clients) {
// c->sendData(PIByteArray::fromHex("000000"));
// server->sendDataToClient(PIByteArray::fromHex("000000"), c->clientId());
// }
mutex_clients.unlock();
}

View File

@@ -23,6 +23,7 @@ private:
PIMap<uint, DispatcherClient*> index_clients;
PIByteArray server_uuid;
PITimeMeasurer last_ping;
PIMutex mutex_clients;
};
#endif // CLOUDSERVER_H

View File

@@ -58,7 +58,18 @@ void DispatcherServer::cleanClients() {
for (auto c: ss) {
if (c->lastPing() > 15.0) {
piCout << "remove Server by ping timeout" << c->getConnection()->clientId();
PIVector<DispatcherClient *> cscv = c->getClients();
for(auto csc : cscv) {
clients.removeAll(csc);
index_c_clients.remove(csc);
c->removeClient(csc);
csc->close();
rmrf_clients << csc;
}
c_servers.remove(c->serverUUID());
index_c_servers.remove(c->getConnection());
rmrf_clients << const_cast<DispatcherClient *>(c->getConnection());
delete c;
}
}
for (auto c: rm_clients) {
@@ -250,8 +261,8 @@ void DispatcherServer::newConnection(PIEthernet *cl) {
map_mutex.unlock();
});
//piCoutObj << "add client" << client;
client->start();
map_mutex.lock();
clients.push_back(client);
map_mutex.unlock();
client->start();
}