PICloud connect/disconnect
This commit is contained in:
@@ -2,12 +2,15 @@
|
||||
|
||||
|
||||
DispatcherServer::DispatcherServer(PIEthernet::Address addr) : eth(PIEthernet::TCP_Server) {
|
||||
client_gid = 0;
|
||||
eth.setParameter(PIEthernet::ReuseAddress);
|
||||
CONNECTU(ð, newConnection, this, newConnection);
|
||||
eth.listen(addr, true);
|
||||
piCoutObj << "server started" << addr;
|
||||
CONNECTU(&status_timer, tickEvent, this, printStatus);
|
||||
CONNECTU(&timeout_timer, tickEvent, this, cleanClients);
|
||||
status_timer.start(1000);
|
||||
timeout_timer.start(5000);
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +20,26 @@ DispatcherServer::~DispatcherServer() {
|
||||
}
|
||||
|
||||
|
||||
void DispatcherServer::cleanClients() {
|
||||
PIVector<DispatcherClient*> rm;
|
||||
map_mutex.lock();
|
||||
for (auto c: clients) {
|
||||
if (!index_c_servers.contains(c) && !index_c_clients.contains(c)) {
|
||||
if (rm_clients.contains(c)) rm << c;
|
||||
else rm_clients << c;
|
||||
} else rm_clients.removeAll(c);
|
||||
}
|
||||
for (auto c: rm_clients) {
|
||||
if (clients.contains(c)) rm << c;
|
||||
}
|
||||
map_mutex.unlock();
|
||||
for (auto c: rm) {
|
||||
c->close();
|
||||
// c->deleteLater();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void DispatcherServer::printStatus() {
|
||||
map_mutex.lock();
|
||||
piCout << PICoutManipulators::NewLine;
|
||||
@@ -48,20 +71,25 @@ void DispatcherServer::disconnectClient(DispatcherClient *client) {
|
||||
for(auto csc : cscv) {
|
||||
csc->close();
|
||||
clients.removeOne(csc);
|
||||
delete csc;
|
||||
index_c_clients.removeOne(csc);
|
||||
csc->deleteLater();
|
||||
}
|
||||
c_servers.remove(cs->serverName());
|
||||
index_c_servers.removeOne(client);
|
||||
}
|
||||
CloudServer * cc = index_c_clients.value(client, nullptr);
|
||||
if (cc) cc->removeClient(client);
|
||||
if (cc) {
|
||||
cc->removeClient(client);
|
||||
index_c_clients.removeOne(client);
|
||||
}
|
||||
client->close();
|
||||
map_mutex.unlock();
|
||||
delete client;
|
||||
client->deleteLater();
|
||||
}
|
||||
|
||||
|
||||
void DispatcherServer::newConnection(PIEthernet *cl) {
|
||||
DispatcherClient * client = new DispatcherClient(cl);
|
||||
DispatcherClient * client = new DispatcherClient(cl, client_gid++);
|
||||
CONNECTU(client, disconnectEvent, this, disconnectClient);
|
||||
CONNECTL(client, registerServer, [this](PIString sname, DispatcherClient * c){
|
||||
map_mutex.lock();
|
||||
@@ -78,6 +106,8 @@ void DispatcherServer::newConnection(PIEthernet *cl) {
|
||||
piCoutObj << "add new Client to Server ->" << sname;
|
||||
cs->addClient(c);
|
||||
index_c_clients.insert(c, cs);
|
||||
} else {
|
||||
rm_clients << c;
|
||||
}
|
||||
map_mutex.unlock();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user