cloud data send optimize
This commit is contained in:
@@ -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::Type, PICloud::TCP::Role> 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<uint, PIByteArray> PICloud::TCP::parseDataServer(PIByteArray & ba) {
|
||||
ret.first = 0;
|
||||
if (header.role == Server) {
|
||||
ba >> ret.first;
|
||||
ret.second = ba;
|
||||
ret.second.swap(ba);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ public:
|
||||
int sendData(const PIByteArray & data, uint client_id);
|
||||
void sendPing();
|
||||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> parseHeader(PIByteArray & ba);
|
||||
PIByteArray parseData(PIByteArray & ba);
|
||||
bool canParseData(PIByteArray & ba);
|
||||
PIPair<uint, PIByteArray> parseDataServer(PIByteArray & ba);
|
||||
PIByteArray parseConnect_d(PIByteArray & ba);
|
||||
uint parseConnect(PIByteArray & ba);
|
||||
|
||||
11
main.cpp
11
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);
|
||||
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();
|
||||
|
||||
@@ -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);
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user