PICloud connect/disconnect
This commit is contained in:
@@ -20,11 +20,13 @@ PIString CloudServer::serverName() const {
|
||||
|
||||
void CloudServer::addClient(DispatcherClient * c) {
|
||||
clients << c;
|
||||
index_clients.insert(c->clientId(), c);
|
||||
}
|
||||
|
||||
|
||||
void CloudServer::removeClient(DispatcherClient * c) {
|
||||
clients.removeOne(c);
|
||||
index_clients.removeOne(c->clientId());
|
||||
}
|
||||
|
||||
|
||||
@@ -34,9 +36,11 @@ PIVector<DispatcherClient *> CloudServer::getClients() {
|
||||
|
||||
|
||||
void CloudServer::printStatus() {
|
||||
piCout << " " << "Clients for" << server->address() << ":";
|
||||
piCout << " " << "Clients for" << server->address() << server_name << ":";
|
||||
for (auto c: clients) {
|
||||
piCout << " " << c->address();
|
||||
piCout << " " << c->address() << c->clientId();
|
||||
}
|
||||
for (auto c: clients) c->sendData(PIByteArray::fromHex("000000"));
|
||||
server->sendData(PIByteArray::fromHex("000000"));
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ public:
|
||||
private:
|
||||
DispatcherClient * server;
|
||||
PIVector<DispatcherClient*> clients;
|
||||
PIMap<uint, DispatcherClient*> index_clients;
|
||||
PIString server_name;
|
||||
};
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include "picloudtcp.h"
|
||||
|
||||
|
||||
DispatcherClient::DispatcherClient(PIEthernet * eth_) : eth(eth_), authorised(false) {
|
||||
DispatcherClient::DispatcherClient(PIEthernet * eth_, int id) : eth(eth_), authorised(false), client_id(id) {
|
||||
CONNECTU(&disconnect_tm, tickEvent, eth, close);
|
||||
CONNECTU(eth, threadedReadEvent, this, readed);
|
||||
CONNECTU(eth, disconnected, this, disconnected);
|
||||
@@ -12,7 +12,7 @@ DispatcherClient::DispatcherClient(PIEthernet * eth_) : eth(eth_), authorised(fa
|
||||
|
||||
void DispatcherClient::start() {
|
||||
eth->startThreadedRead();
|
||||
disconnect_tm.start(10000);
|
||||
//disconnect_tm.start(10000);
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,16 @@ void DispatcherClient::close() {
|
||||
}
|
||||
|
||||
|
||||
void DispatcherClient::sendConnected() {
|
||||
tcp.sendConnected(eth, client_id);
|
||||
}
|
||||
|
||||
|
||||
void DispatcherClient::sendData(const PIByteArray & data) {
|
||||
tcp.sendData(eth, data);
|
||||
}
|
||||
|
||||
|
||||
void DispatcherClient::disconnected(bool withError) {
|
||||
piCoutObj << "client disconnected" << eth->sendAddress();
|
||||
disconnectEvent(this);
|
||||
@@ -37,7 +47,7 @@ void DispatcherClient::disconnected(bool withError) {
|
||||
|
||||
|
||||
void DispatcherClient::readed(uchar *data, int size) {
|
||||
piCout << size;
|
||||
// piCout << size;
|
||||
PIByteArray ba(data, size);
|
||||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
|
||||
if (hdr.first == PICloud::TCP::Invalid) {
|
||||
@@ -61,6 +71,7 @@ void DispatcherClient::readed(uchar *data, int size) {
|
||||
} else {
|
||||
switch (hdr.first) {
|
||||
case PICloud::TCP::Connect: {
|
||||
tcp.setRole(hdr.second);
|
||||
PIString sn = tcp.parseConnect(ba);
|
||||
if (hdr.second == PICloud::TCP::Server) registerServer(sn, this);
|
||||
if (hdr.second == PICloud::TCP::Client) registerClient(sn, this);
|
||||
|
||||
@@ -8,14 +8,17 @@
|
||||
class DispatcherClient: public PIObject {
|
||||
PIOBJECT(DispatcherClient)
|
||||
public:
|
||||
DispatcherClient(PIEthernet * eth_);
|
||||
void start();
|
||||
DispatcherClient(PIEthernet * eth_, int id);
|
||||
~DispatcherClient();
|
||||
void start();
|
||||
void close();
|
||||
void sendConnected();
|
||||
void sendData(const PIByteArray & data);
|
||||
PIString address();
|
||||
uint clientId() const {return client_id;}
|
||||
EVENT1(disconnectEvent, DispatcherClient *, client)
|
||||
EVENT2(registerServer, PIString, sname, DispatcherClient *, client)
|
||||
EVENT2(registerClient, PIString, sname, DispatcherClient *, client)
|
||||
PIString address();
|
||||
void close();
|
||||
EVENT1(dataReaded, PIByteArray, ba)
|
||||
|
||||
private:
|
||||
@@ -26,6 +29,7 @@ private:
|
||||
PITimer disconnect_tm;
|
||||
bool authorised;
|
||||
PICloud::TCP tcp;
|
||||
uint client_id;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
@@ -14,14 +14,18 @@ public:
|
||||
private:
|
||||
EVENT_HANDLER1(void, newConnection, PIEthernet * , cl);
|
||||
EVENT_HANDLER1(void, disconnectClient, DispatcherClient *, client);
|
||||
EVENT_HANDLER0(void, cleanClients);
|
||||
|
||||
PIEthernet eth;
|
||||
PIVector<DispatcherClient*> clients;
|
||||
PIMap<PIString, CloudServer *> c_servers;
|
||||
PIMap<const DispatcherClient *, CloudServer *> index_c_servers;
|
||||
PIMap<const DispatcherClient *, CloudServer *> index_c_clients;
|
||||
PIVector<DispatcherClient*> rm_clients;
|
||||
PITimer status_timer;
|
||||
PITimer timeout_timer;
|
||||
PIMutex map_mutex;
|
||||
uint client_gid;
|
||||
};
|
||||
|
||||
#endif // DISPATCHERSERVER_H
|
||||
|
||||
Reference in New Issue
Block a user