picloud ping and fix big bugs
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
|
|
||||||
PICloudBase::PICloudBase() : eth(PIEthernet::TCP_Client), streampacker(ð), tcp(&streampacker) {
|
PICloudBase::PICloudBase() : eth(PIEthernet::TCP_Client), streampacker(ð), tcp(&streampacker) {
|
||||||
|
eth.setDebug(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -25,10 +25,10 @@ PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode)
|
|||||||
tcp.setRole(PICloud::TCP::Client);
|
tcp.setRole(PICloud::TCP::Client);
|
||||||
setName("cloud_client");
|
setName("cloud_client");
|
||||||
is_connected = false;
|
is_connected = false;
|
||||||
CONNECTL(ð, connected, [this](){tcp.sendStart();});
|
CONNECTL(ð, connected, [this](){opened_ = true; tcp.sendStart();});
|
||||||
CONNECTU(&streampacker, packetReceiveEvent, this, _readed);
|
CONNECTU(&streampacker, packetReceiveEvent, this, _readed);
|
||||||
CONNECTL(ð, disconnected, [this](bool){
|
CONNECTL(ð, disconnected, [this](bool){
|
||||||
piCoutObj << "disconnected";
|
// piCoutObj << "disconnected";
|
||||||
opened_ = false;
|
opened_ = false;
|
||||||
is_connected = false;
|
is_connected = false;
|
||||||
cond_connect.notifyOne();
|
cond_connect.notifyOne();
|
||||||
|
|||||||
@@ -32,6 +32,9 @@ PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode)
|
|||||||
opened_ = false;
|
opened_ = false;
|
||||||
piMSleep(100);
|
piMSleep(100);
|
||||||
});
|
});
|
||||||
|
CONNECTL(&ping_timer, tickEvent, [this] (void *, int){
|
||||||
|
if (eth.isConnected()) tcp.sendPing();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -58,8 +61,10 @@ bool PICloudServer::openDevice() {
|
|||||||
bool op = eth.connect(PIEthernet::Address::resolve(path()), false);
|
bool op = eth.connect(PIEthernet::Address::resolve(path()), false);
|
||||||
if (op) {
|
if (op) {
|
||||||
eth.startThreadedRead();
|
eth.startThreadedRead();
|
||||||
|
ping_timer.start(5000);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
ping_timer.stop();
|
||||||
eth.close();
|
eth.close();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ int PICloud::TCP::sendData(const PIByteArray & data) {
|
|||||||
PIByteArray ba;
|
PIByteArray ba;
|
||||||
ba << header;
|
ba << header;
|
||||||
ba.append(data);
|
ba.append(data);
|
||||||
// piCout << "sendData" << ba.toHex();
|
// piCout << "[PICloud::TCP] sendData" << ba.toHex();
|
||||||
streampacker->send(ba);
|
streampacker->send(ba);
|
||||||
return data.size_s();
|
return data.size_s();
|
||||||
}
|
}
|
||||||
@@ -103,6 +103,15 @@ int PICloud::TCP::sendData(const PIByteArray & data, uint client_id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PICloud::TCP::sendPing() {
|
||||||
|
header.type = PICloud::TCP::Ping;
|
||||||
|
PIByteArray ba;
|
||||||
|
ba << header;
|
||||||
|
ba.append(suuid);
|
||||||
|
streampacker->send(ba);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> PICloud::TCP::parseHeader(PIByteArray & ba) {
|
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> PICloud::TCP::parseHeader(PIByteArray & ba) {
|
||||||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> ret;
|
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> ret;
|
||||||
ret.first = InvalidType;
|
ret.first = InvalidType;
|
||||||
|
|||||||
@@ -203,6 +203,10 @@ void PIBroadcast::initAll(PIVector<PIEthernet::Address> al) {
|
|||||||
|
|
||||||
|
|
||||||
void PIBroadcast::send(const PIByteArray & data) {
|
void PIBroadcast::send(const PIByteArray & data) {
|
||||||
|
if (!isRunning()) {
|
||||||
|
reinit();
|
||||||
|
PIThread::start(3000);
|
||||||
|
}
|
||||||
PIByteArray cd = cryptData(data);
|
PIByteArray cd = cryptData(data);
|
||||||
if (cd.isEmpty()) return;
|
if (cd.isEmpty()) return;
|
||||||
PIMutexLocker ml(mcast_mutex);
|
PIMutexLocker ml(mcast_mutex);
|
||||||
|
|||||||
@@ -78,6 +78,7 @@ private:
|
|||||||
|
|
||||||
PIVector<Client *> clients_;
|
PIVector<Client *> clients_;
|
||||||
PIMap<uint, Client *> index_clients;
|
PIMap<uint, Client *> index_clients;
|
||||||
|
PITimer ping_timer;
|
||||||
mutable PIMutex clients_mutex;
|
mutable PIMutex clients_mutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ public:
|
|||||||
Connect = 1,
|
Connect = 1,
|
||||||
Disconnect = 2,
|
Disconnect = 2,
|
||||||
Data = 3,
|
Data = 3,
|
||||||
|
Ping = 4,
|
||||||
};
|
};
|
||||||
|
|
||||||
TCP(PIStreamPacker * s);
|
TCP(PIStreamPacker * s);
|
||||||
@@ -65,6 +66,7 @@ public:
|
|||||||
void sendDisconnected(uint client_id);
|
void sendDisconnected(uint client_id);
|
||||||
int sendData(const PIByteArray & data);
|
int sendData(const PIByteArray & data);
|
||||||
int sendData(const PIByteArray & data, uint client_id);
|
int sendData(const PIByteArray & data, uint client_id);
|
||||||
|
void sendPing();
|
||||||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> parseHeader(PIByteArray & ba);
|
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> parseHeader(PIByteArray & ba);
|
||||||
PIByteArray parseData(PIByteArray & ba);
|
PIByteArray parseData(PIByteArray & ba);
|
||||||
PIPair<uint, PIByteArray> parseDataServer(PIByteArray & ba);
|
PIPair<uint, PIByteArray> parseDataServer(PIByteArray & ba);
|
||||||
|
|||||||
22
main.cpp
22
main.cpp
@@ -2,25 +2,7 @@
|
|||||||
|
|
||||||
|
|
||||||
int main(int argc, char * argv[]) {
|
int main(int argc, char * argv[]) {
|
||||||
PIByteArray v0 = PIByteArray::fromHex("01020304");
|
PICLI cli(argc, argv);
|
||||||
PIByteArray v1 = PIByteArray::fromHex("01020304");
|
|
||||||
piCout << "==" << (v0 == v1) << ", !=" << (v0 != v1) << ", <" << (v0 < v1) << ", >" << (v0 > v1);
|
|
||||||
v0 = PIByteArray::fromHex("0102030405");
|
|
||||||
v1 = PIByteArray::fromHex("01020304");
|
|
||||||
piCout << "==" << (v0 == v1) << ", !=" << (v0 != v1) << ", <" << (v0 < v1) << ", >" << (v0 > v1);
|
|
||||||
v0 = PIByteArray::fromHex("01020304");
|
|
||||||
v1 = PIByteArray::fromHex("0102030405");
|
|
||||||
piCout << "==" << (v0 == v1) << ", !=" << (v0 != v1) << ", <" << (v0 < v1) << ", >" << (v0 > v1);
|
|
||||||
v0 = PIByteArray::fromHex("01020404");
|
|
||||||
v1 = PIByteArray::fromHex("01020304");
|
|
||||||
piCout << "==" << (v0 == v1) << ", !=" << (v0 != v1) << ", <" << (v0 < v1) << ", >" << (v0 > v1);
|
|
||||||
v0 = PIByteArray::fromHex("0102040405");
|
|
||||||
v1 = PIByteArray::fromHex("01020304");
|
|
||||||
piCout << "==" << (v0 == v1) << ", !=" << (v0 != v1) << ", <" << (v0 < v1) << ", >" << (v0 > v1);
|
|
||||||
v0 = PIByteArray::fromHex("01020404");
|
|
||||||
v1 = PIByteArray::fromHex("0102030405");
|
|
||||||
piCout << "==" << (v0 == v1) << ", !=" << (v0 != v1) << ", <" << (v0 < v1) << ", >" << (v0 > v1);
|
|
||||||
/*PICLI cli(argc, argv);
|
|
||||||
PITimer tm;
|
PITimer tm;
|
||||||
cli.addArgument("connect", true);
|
cli.addArgument("connect", true);
|
||||||
cli.addArgument("name", true);
|
cli.addArgument("name", true);
|
||||||
@@ -77,6 +59,6 @@ int main(int argc, char * argv[]) {
|
|||||||
PIKbdListener ls;
|
PIKbdListener ls;
|
||||||
ls.enableExitCapture(PIKbdListener::F10);
|
ls.enableExitCapture(PIKbdListener::F10);
|
||||||
ls.start();
|
ls.start();
|
||||||
WAIT_FOR_EXIT*/
|
WAIT_FOR_EXIT
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ CloudServer::CloudServer(DispatcherClient * c, const PIByteArray & sname) : serv
|
|||||||
DispatcherClient * cl = index_clients.value(id, nullptr);
|
DispatcherClient * cl = index_clients.value(id, nullptr);
|
||||||
if (cl) cl->sendData(ba);
|
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) {
|
void CloudServer::addClient(DispatcherClient * c) {
|
||||||
|
last_ping.reset();
|
||||||
clients << c;
|
clients << c;
|
||||||
index_clients.insert(c->clientId(), c);
|
index_clients.insert(c->clientId(), c);
|
||||||
c->sendConnected(1);
|
c->sendConnected(1);
|
||||||
@@ -37,6 +40,7 @@ void CloudServer::addClient(DispatcherClient * c) {
|
|||||||
|
|
||||||
|
|
||||||
void CloudServer::removeClient(DispatcherClient * c) {
|
void CloudServer::removeClient(DispatcherClient * c) {
|
||||||
|
last_ping.reset();
|
||||||
clients.removeOne(c);
|
clients.removeOne(c);
|
||||||
index_clients.removeOne(c->clientId());
|
index_clients.removeOne(c->clientId());
|
||||||
server->sendDisconnected(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() {
|
void CloudServer::printStatus() {
|
||||||
piCout << " " << "Clients for" << server->address() << server_uuid.toHex() << ":";
|
piCout << " " << "Clients for" << server->address() << server_uuid.toHex() << ":";
|
||||||
for (auto c: clients) {
|
for (auto c: clients) {
|
||||||
|
|||||||
@@ -15,12 +15,14 @@ public:
|
|||||||
PIVector<DispatcherClient*> getClients();
|
PIVector<DispatcherClient*> getClients();
|
||||||
EVENT_HANDLER0(void, printStatus);
|
EVENT_HANDLER0(void, printStatus);
|
||||||
const DispatcherClient * getConnection() const {return server;}
|
const DispatcherClient * getConnection() const {return server;}
|
||||||
|
double lastPing();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
DispatcherClient * server;
|
DispatcherClient * server;
|
||||||
PIVector<DispatcherClient*> clients;
|
PIVector<DispatcherClient*> clients;
|
||||||
PIMap<uint, DispatcherClient*> index_clients;
|
PIMap<uint, DispatcherClient*> index_clients;
|
||||||
PIByteArray server_uuid;
|
PIByteArray server_uuid;
|
||||||
|
PITimeMeasurer last_ping;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CLOUDSERVER_H
|
#endif // CLOUDSERVER_H
|
||||||
|
|||||||
@@ -72,10 +72,11 @@ void DispatcherClient::disconnected(bool withError) {
|
|||||||
|
|
||||||
|
|
||||||
void DispatcherClient::readed(PIByteArray & ba) {
|
void DispatcherClient::readed(PIByteArray & ba) {
|
||||||
// piCout << size;
|
|
||||||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
|
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
|
||||||
|
// piCoutObj << "readed" << hdr.first << hdr.second;
|
||||||
if (hdr.first == PICloud::TCP::InvalidType) {
|
if (hdr.first == PICloud::TCP::InvalidType) {
|
||||||
disconnected(true);
|
disconnected(true);
|
||||||
|
piCoutObj << "invalid message";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (authorised) {
|
if (authorised) {
|
||||||
@@ -87,7 +88,7 @@ void DispatcherClient::readed(PIByteArray & ba) {
|
|||||||
disconnected(false);
|
disconnected(false);
|
||||||
return;
|
return;
|
||||||
case PICloud::TCP::Data:
|
case PICloud::TCP::Data:
|
||||||
// piCoutObj << "TCP::Data";
|
//piCoutObj << "TCP::Data" << tcp.role();
|
||||||
if (tcp.role() == PICloud::TCP::Client) {
|
if (tcp.role() == PICloud::TCP::Client) {
|
||||||
PIByteArray data = tcp.parseData(ba);
|
PIByteArray data = tcp.parseData(ba);
|
||||||
if (!data.isEmpty()) dataReaded(data);
|
if (!data.isEmpty()) dataReaded(data);
|
||||||
@@ -99,11 +100,15 @@ void DispatcherClient::readed(PIByteArray & ba) {
|
|||||||
else piCoutObj << "invalid data from server";
|
else piCoutObj << "invalid data from server";
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
case PICloud::TCP::Ping:
|
||||||
|
pingReceived();
|
||||||
|
return;
|
||||||
default:
|
default:
|
||||||
|
piCoutObj << "unknown data";
|
||||||
//disconnected(true);
|
//disconnected(true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
} else piCoutObj << "invalid role";
|
||||||
} else {
|
} else {
|
||||||
switch (hdr.first) {
|
switch (hdr.first) {
|
||||||
case PICloud::TCP::Connect: {
|
case PICloud::TCP::Connect: {
|
||||||
@@ -111,7 +116,8 @@ void DispatcherClient::readed(PIByteArray & ba) {
|
|||||||
PIByteArray sn = tcp.parseConnect_d(ba);
|
PIByteArray sn = tcp.parseConnect_d(ba);
|
||||||
if (hdr.second == PICloud::TCP::Server) registerServer(sn, this);
|
if (hdr.second == PICloud::TCP::Server) registerServer(sn, this);
|
||||||
if (hdr.second == PICloud::TCP::Client) registerClient(sn, this);
|
if (hdr.second == PICloud::TCP::Client) registerClient(sn, this);
|
||||||
return;}
|
return;
|
||||||
|
}
|
||||||
case PICloud::TCP::Disconnect:
|
case PICloud::TCP::Disconnect:
|
||||||
disconnected(false);
|
disconnected(false);
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ public:
|
|||||||
EVENT2(registerClient, const PIByteArray &, sname, DispatcherClient *, client)
|
EVENT2(registerClient, const PIByteArray &, sname, DispatcherClient *, client)
|
||||||
EVENT1(dataReaded, PIByteArray &, ba)
|
EVENT1(dataReaded, PIByteArray &, ba)
|
||||||
EVENT2(dataReadedServer, uint, id, PIByteArray &, ba)
|
EVENT2(dataReadedServer, uint, id, PIByteArray &, ba)
|
||||||
|
EVENT0(pingReceived)
|
||||||
|
|
||||||
private:
|
private:
|
||||||
EVENT_HANDLER1(void, readed, PIByteArray &, data);
|
EVENT_HANDLER1(void, readed, PIByteArray &, data);
|
||||||
|
|||||||
@@ -53,6 +53,10 @@ void DispatcherServer::cleanClients() {
|
|||||||
if (!rm_clients.contains(c)) rm_clients << c;
|
if (!rm_clients.contains(c)) rm_clients << c;
|
||||||
} else rm_clients.removeAll(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) {
|
for (auto c: rm_clients) {
|
||||||
if (clients.contains(c)) {
|
if (clients.contains(c)) {
|
||||||
rmrf_clients << c;
|
rmrf_clients << c;
|
||||||
@@ -60,7 +64,10 @@ void DispatcherServer::cleanClients() {
|
|||||||
}
|
}
|
||||||
for (auto c: rmrf_clients) {
|
for (auto c: rmrf_clients) {
|
||||||
clients.removeAll(c);
|
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);
|
index_c_clients.remove(c);
|
||||||
rm_clients.removeAll(c);
|
rm_clients.removeAll(c);
|
||||||
}
|
}
|
||||||
@@ -226,9 +233,9 @@ void DispatcherServer::newConnection(PIEthernet *cl) {
|
|||||||
CloudServer * cs = c_servers.value(sname, nullptr);
|
CloudServer * cs = c_servers.value(sname, nullptr);
|
||||||
if (cs) {
|
if (cs) {
|
||||||
piCoutObj << "add new Client to Server ->" << sname.toHex();
|
piCoutObj << "add new Client to Server ->" << sname.toHex();
|
||||||
|
c->authorise(true);
|
||||||
cs->addClient(c);
|
cs->addClient(c);
|
||||||
index_c_clients.insert(c, cs);
|
index_c_clients.insert(c, cs);
|
||||||
c->authorise(true);
|
|
||||||
} else {
|
} else {
|
||||||
rm_clients << c;
|
rm_clients << c;
|
||||||
piCoutObj << "Client can't connect to Server ->" << sname.toHex();
|
piCoutObj << "Client can't connect to Server ->" << sname.toHex();
|
||||||
|
|||||||
Reference in New Issue
Block a user