From 00edfa4ef04448af9cd8610d0e1bb3a2c33f749b Mon Sep 17 00:00:00 2001 From: Andrey Date: Wed, 1 Sep 2021 17:48:58 +0300 Subject: [PATCH] cloud data send optimize --- libs/cloud/picloudtcp.cpp | 11 ++++------- libs/main/cloud/picloudtcp.h | 2 +- main.cpp | 17 ++++++++++++----- utils/cloud_dispatcher/cloudserver.cpp | 11 +++++------ utils/cloud_dispatcher/dispatcherclient.cpp | 3 +-- 5 files changed, 23 insertions(+), 21 deletions(-) diff --git a/libs/cloud/picloudtcp.cpp b/libs/cloud/picloudtcp.cpp index 62dcbd95..035d7129 100644 --- a/libs/cloud/picloudtcp.cpp +++ b/libs/cloud/picloudtcp.cpp @@ -33,7 +33,7 @@ PICloud::TCP::Header::Header() { PICloud::TCP::TCP(PIStreamPacker * s) : streampacker(s) { - + streampacker->setMaxPacketSize(63*1024); } void PICloud::TCP::setRole(PICloud::TCP::Role r) { @@ -141,11 +141,8 @@ PIPair PICloud::TCP::parseHeader(PIByteA } -PIByteArray PICloud::TCP::parseData(PIByteArray & ba) { - if (header.role == Client) { - return ba; - } - return PIByteArray(); +bool PICloud::TCP::canParseData(PIByteArray & ba) { + return header.role == Client; } @@ -154,7 +151,7 @@ PIPair PICloud::TCP::parseDataServer(PIByteArray & ba) { ret.first = 0; if (header.role == Server) { ba >> ret.first; - ret.second = ba; + ret.second.swap(ba); } return ret; } diff --git a/libs/main/cloud/picloudtcp.h b/libs/main/cloud/picloudtcp.h index aada123f..cf9af60b 100644 --- a/libs/main/cloud/picloudtcp.h +++ b/libs/main/cloud/picloudtcp.h @@ -69,7 +69,7 @@ public: int sendData(const PIByteArray & data, uint client_id); void sendPing(); PIPair parseHeader(PIByteArray & ba); - PIByteArray parseData(PIByteArray & ba); + bool canParseData(PIByteArray & ba); PIPair parseDataServer(PIByteArray & ba); PIByteArray parseConnect_d(PIByteArray & ba); uint parseConnect(PIByteArray & ba); diff --git a/main.cpp b/main.cpp index 764f7dd0..78eae1d2 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,8 @@ int main(int argc, char * argv[]) { + PIByteArray rnd; + rnd.resize(1024*1024, 'x'); PICLI cli(argc, argv); PITimer tm; cli.addArgument("connect", true); @@ -28,20 +30,25 @@ int main(int argc, char * argv[]) { })); CONNECTL(&c, threadedReadEvent, ([&](uchar * readed, int size){ PIByteArray ba(readed, size); - PIString str = PIString(ba); - piCout << "[Client] data:" << str; - if (str == "ping_S") c.write(PIString("pong_S").toByteArray()); + if (size < 1024) { + PIString str = PIString(ba); + 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, disconnected, ([](){piCout << "disconnected";})); CONNECTL(&s, newConnection, ([&](PICloudServer::Client * cl){ piCout << "[Server] new client:" << 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); PIString str = PIString(ba); 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](){ cl->stop(); diff --git a/utils/cloud_dispatcher/cloudserver.cpp b/utils/cloud_dispatcher/cloudserver.cpp index 09241e9b..54ddd031 100644 --- a/utils/cloud_dispatcher/cloudserver.cpp +++ b/utils/cloud_dispatcher/cloudserver.cpp @@ -27,14 +27,13 @@ PIByteArray CloudServer::serverUUID() const { void CloudServer::addClient(DispatcherClient * c) { last_ping.reset(); clients << c; - index_clients.insert(c->clientId(), c); + uint cid = c->clientId(); + index_clients.insert(cid, c); c->sendConnected(1); - server->sendConnected(c->clientId()); - CONNECTL(c, dataReaded, ([this, c](PIByteArray & ba){ + server->sendConnected(cid); + CONNECTL(c, dataReaded, ([this, cid](PIByteArray & ba){ // piCoutObj << c->clientId() << "dataReaded"; - if (clients.contains(c)) { - server->sendDataToClient(ba, c->clientId()); - } + server->sendDataToClient(ba, cid); })); } diff --git a/utils/cloud_dispatcher/dispatcherclient.cpp b/utils/cloud_dispatcher/dispatcherclient.cpp index 936bfca7..dcb38bd8 100644 --- a/utils/cloud_dispatcher/dispatcherclient.cpp +++ b/utils/cloud_dispatcher/dispatcherclient.cpp @@ -90,8 +90,7 @@ void DispatcherClient::readed(PIByteArray & ba) { case PICloud::TCP::Data: //piCoutObj << "TCP::Data" << tcp.role(); if (tcp.role() == PICloud::TCP::Client) { - PIByteArray data = tcp.parseData(ba); - if (!data.isEmpty()) dataReaded(data); + if (tcp.canParseData(ba)) dataReaded(ba); else piCoutObj << "invalid data from client"; } if (tcp.role() == PICloud::TCP::Server) {