picloud ping and fix big bugs

This commit is contained in:
2021-08-19 18:13:05 +03:00
parent f5af8a1da9
commit 4c0530d89a
13 changed files with 58 additions and 30 deletions

View File

@@ -7,6 +7,8 @@ CloudServer::CloudServer(DispatcherClient * c, const PIByteArray & sname) : serv
DispatcherClient * cl = index_clients.value(id, nullptr);
if (cl) cl->sendData(ba);
}));
CONNECTL(c, pingReceived, [this]() {last_ping.reset();});
last_ping.reset();
}
@@ -23,6 +25,7 @@ PIByteArray CloudServer::serverUUID() const {
void CloudServer::addClient(DispatcherClient * c) {
last_ping.reset();
clients << c;
index_clients.insert(c->clientId(), c);
c->sendConnected(1);
@@ -37,6 +40,7 @@ void CloudServer::addClient(DispatcherClient * c) {
void CloudServer::removeClient(DispatcherClient * c) {
last_ping.reset();
clients.removeOne(c);
index_clients.removeOne(c->clientId());
server->sendDisconnected(c->clientId());
@@ -48,6 +52,11 @@ PIVector<DispatcherClient *> CloudServer::getClients() {
}
double CloudServer::lastPing() {
return last_ping.elapsed_s();
}
void CloudServer::printStatus() {
piCout << " " << "Clients for" << server->address() << server_uuid.toHex() << ":";
for (auto c: clients) {

View File

@@ -15,12 +15,14 @@ public:
PIVector<DispatcherClient*> getClients();
EVENT_HANDLER0(void, printStatus);
const DispatcherClient * getConnection() const {return server;}
double lastPing();
private:
DispatcherClient * server;
PIVector<DispatcherClient*> clients;
PIMap<uint, DispatcherClient*> index_clients;
PIByteArray server_uuid;
PITimeMeasurer last_ping;
};
#endif // CLOUDSERVER_H

View File

@@ -72,10 +72,11 @@ void DispatcherClient::disconnected(bool withError) {
void DispatcherClient::readed(PIByteArray & ba) {
// piCout << size;
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
// piCoutObj << "readed" << hdr.first << hdr.second;
if (hdr.first == PICloud::TCP::InvalidType) {
disconnected(true);
piCoutObj << "invalid message";
return;
}
if (authorised) {
@@ -87,7 +88,7 @@ void DispatcherClient::readed(PIByteArray & ba) {
disconnected(false);
return;
case PICloud::TCP::Data:
// piCoutObj << "TCP::Data";
//piCoutObj << "TCP::Data" << tcp.role();
if (tcp.role() == PICloud::TCP::Client) {
PIByteArray data = tcp.parseData(ba);
if (!data.isEmpty()) dataReaded(data);
@@ -99,11 +100,15 @@ void DispatcherClient::readed(PIByteArray & ba) {
else piCoutObj << "invalid data from server";
}
return;
case PICloud::TCP::Ping:
pingReceived();
return;
default:
piCoutObj << "unknown data";
//disconnected(true);
return;
}
}
} else piCoutObj << "invalid role";
} else {
switch (hdr.first) {
case PICloud::TCP::Connect: {
@@ -111,7 +116,8 @@ void DispatcherClient::readed(PIByteArray & ba) {
PIByteArray sn = tcp.parseConnect_d(ba);
if (hdr.second == PICloud::TCP::Server) registerServer(sn, this);
if (hdr.second == PICloud::TCP::Client) registerClient(sn, this);
return;}
return;
}
case PICloud::TCP::Disconnect:
disconnected(false);
return;

View File

@@ -28,6 +28,7 @@ public:
EVENT2(registerClient, const PIByteArray &, sname, DispatcherClient *, client)
EVENT1(dataReaded, PIByteArray &, ba)
EVENT2(dataReadedServer, uint, id, PIByteArray &, ba)
EVENT0(pingReceived)
private:
EVENT_HANDLER1(void, readed, PIByteArray &, data);

View File

@@ -53,6 +53,10 @@ void DispatcherServer::cleanClients() {
if (!rm_clients.contains(c)) rm_clients << c;
} else rm_clients.removeAll(c);
}
auto ss = c_servers.values();
for (auto c: ss) {
if (c->lastPing() > 15.0) rmrf_clients << const_cast<DispatcherClient *>(c->getConnection());
}
for (auto c: rm_clients) {
if (clients.contains(c)) {
rmrf_clients << c;
@@ -60,7 +64,10 @@ void DispatcherServer::cleanClients() {
}
for (auto c: rmrf_clients) {
clients.removeAll(c);
index_c_servers.remove(c);
if(index_c_servers.contains(c)) {
c_servers.remove(c_servers.key(index_c_servers[c]));
index_c_servers.remove(c);
}
index_c_clients.remove(c);
rm_clients.removeAll(c);
}
@@ -226,9 +233,9 @@ void DispatcherServer::newConnection(PIEthernet *cl) {
CloudServer * cs = c_servers.value(sname, nullptr);
if (cs) {
piCoutObj << "add new Client to Server ->" << sname.toHex();
c->authorise(true);
cs->addClient(c);
index_c_clients.insert(c, cs);
c->authorise(true);
} else {
rm_clients << c;
piCoutObj << "Client can't connect to Server ->" << sname.toHex();