cloud data send optimize

This commit is contained in:
2021-09-01 17:48:58 +03:00
parent 35a3ce6402
commit 00edfa4ef0
5 changed files with 23 additions and 21 deletions

View File

@@ -33,7 +33,7 @@ PICloud::TCP::Header::Header() {
PICloud::TCP::TCP(PIStreamPacker * s) : streampacker(s) { PICloud::TCP::TCP(PIStreamPacker * s) : streampacker(s) {
streampacker->setMaxPacketSize(63*1024);
} }
void PICloud::TCP::setRole(PICloud::TCP::Role r) { void PICloud::TCP::setRole(PICloud::TCP::Role r) {
@@ -141,11 +141,8 @@ PIPair<PICloud::TCP::Type, PICloud::TCP::Role> PICloud::TCP::parseHeader(PIByteA
} }
PIByteArray PICloud::TCP::parseData(PIByteArray & ba) { bool PICloud::TCP::canParseData(PIByteArray & ba) {
if (header.role == Client) { return header.role == Client;
return ba;
}
return PIByteArray();
} }
@@ -154,7 +151,7 @@ PIPair<uint, PIByteArray> PICloud::TCP::parseDataServer(PIByteArray & ba) {
ret.first = 0; ret.first = 0;
if (header.role == Server) { if (header.role == Server) {
ba >> ret.first; ba >> ret.first;
ret.second = ba; ret.second.swap(ba);
} }
return ret; return ret;
} }

View File

@@ -69,7 +69,7 @@ public:
int sendData(const PIByteArray & data, uint client_id); int sendData(const PIByteArray & data, uint client_id);
void sendPing(); 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); bool canParseData(PIByteArray & ba);
PIPair<uint, PIByteArray> parseDataServer(PIByteArray & ba); PIPair<uint, PIByteArray> parseDataServer(PIByteArray & ba);
PIByteArray parseConnect_d(PIByteArray & ba); PIByteArray parseConnect_d(PIByteArray & ba);
uint parseConnect(PIByteArray & ba); uint parseConnect(PIByteArray & ba);

View File

@@ -2,6 +2,8 @@
int main(int argc, char * argv[]) { int main(int argc, char * argv[]) {
PIByteArray rnd;
rnd.resize(1024*1024, 'x');
PICLI cli(argc, argv); PICLI cli(argc, argv);
PITimer tm; PITimer tm;
cli.addArgument("connect", true); cli.addArgument("connect", true);
@@ -28,20 +30,25 @@ int main(int argc, char * argv[]) {
})); }));
CONNECTL(&c, threadedReadEvent, ([&](uchar * readed, int size){ CONNECTL(&c, threadedReadEvent, ([&](uchar * readed, int size){
PIByteArray ba(readed, size); PIByteArray ba(readed, size);
PIString str = PIString(ba); if (size < 1024) {
piCout << "[Client] data:" << str; PIString str = PIString(ba);
if (str == "ping_S") c.write(PIString("pong_S").toByteArray()); piCout << "[Client] data:" << str;
if (str == "ping_S") c.write(PIString("pong_S").toByteArray());
} else piCout << "[Client] blob:" << size;
})); }));
CONNECTL(&c, connected, ([](){piCout << "connected";})); CONNECTL(&c, connected, ([](){piCout << "connected";}));
CONNECTL(&c, disconnected, ([](){piCout << "disconnected";})); CONNECTL(&c, disconnected, ([](){piCout << "disconnected";}));
CONNECTL(&s, newConnection, ([&](PICloudServer::Client * cl){ CONNECTL(&s, newConnection, ([&](PICloudServer::Client * cl){
piCout << "[Server] new client:" << cl; piCout << "[Server] new client:" << cl;
clients << cl; clients << cl;
CONNECTL(cl, threadedReadEvent, ([&c, &s, cl](uchar * readed, int size){ CONNECTL(cl, threadedReadEvent, ([&c, &s, cl, &rnd](uchar * readed, int size){
PIByteArray ba(readed, size); PIByteArray ba(readed, size);
PIString str = PIString(ba); PIString str = PIString(ba);
piCout << "[Server] data from" << cl << ":" << str; piCout << "[Server] data from" << cl << ":" << str;
if (str == "ping") cl->write(PIString("pong").toByteArray()); if (str == "ping") {
cl->write(PIString("pong").toByteArray());
cl->write(rnd);
}
})); }));
CONNECTL(cl, closed, ([&clients, cl](){ CONNECTL(cl, closed, ([&clients, cl](){
cl->stop(); cl->stop();

View File

@@ -27,14 +27,13 @@ PIByteArray CloudServer::serverUUID() const {
void CloudServer::addClient(DispatcherClient * c) { void CloudServer::addClient(DispatcherClient * c) {
last_ping.reset(); last_ping.reset();
clients << c; clients << c;
index_clients.insert(c->clientId(), c); uint cid = c->clientId();
index_clients.insert(cid, c);
c->sendConnected(1); c->sendConnected(1);
server->sendConnected(c->clientId()); server->sendConnected(cid);
CONNECTL(c, dataReaded, ([this, c](PIByteArray & ba){ CONNECTL(c, dataReaded, ([this, cid](PIByteArray & ba){
// piCoutObj << c->clientId() << "dataReaded"; // piCoutObj << c->clientId() << "dataReaded";
if (clients.contains(c)) { server->sendDataToClient(ba, cid);
server->sendDataToClient(ba, c->clientId());
}
})); }));
} }

View File

@@ -90,8 +90,7 @@ void DispatcherClient::readed(PIByteArray & ba) {
case PICloud::TCP::Data: case PICloud::TCP::Data:
//piCoutObj << "TCP::Data" << tcp.role(); //piCoutObj << "TCP::Data" << tcp.role();
if (tcp.role() == PICloud::TCP::Client) { if (tcp.role() == PICloud::TCP::Client) {
PIByteArray data = tcp.parseData(ba); if (tcp.canParseData(ba)) dataReaded(ba);
if (!data.isEmpty()) dataReaded(data);
else piCoutObj << "invalid data from client"; else piCoutObj << "invalid data from client";
} }
if (tcp.role() == PICloud::TCP::Server) { if (tcp.role() == PICloud::TCP::Server) {