PIClout data send and receive test
This commit is contained in:
36
main.cpp
36
main.cpp
@@ -3,11 +3,46 @@
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
PICLI cli(argc, argv);
|
||||
PITimer tm;
|
||||
cli.addArgument("connect", true);
|
||||
cli.addArgument("name", true);
|
||||
PICloudClient c("127.0.0.1:10101");
|
||||
// c.setReopenEnabled(true);
|
||||
PICloudServer s("127.0.0.1:10101");
|
||||
PIVector<PICloudServer::Client *> clients;
|
||||
CONNECTL(&tm, tickEvent, ([&](void *, int){
|
||||
if (c.isConnected()) {
|
||||
PIString str = "ping";
|
||||
piCout << "[Client] send:" << str;
|
||||
c.write(str.toByteArray());
|
||||
}
|
||||
if (s.isRunning()) {
|
||||
for (auto cl : clients) {
|
||||
if (cl->isOpened()) {
|
||||
PIString str = "ping_S";
|
||||
piCout << "[Server] send to" << cl << ":" << str;
|
||||
cl->write(str.toByteArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
}));
|
||||
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());
|
||||
}));
|
||||
CONNECTL(&s, newConnection, ([&](PICloudServer::Client * cl){
|
||||
piCout << "[Server] new client:" << cl;
|
||||
clients << cl;
|
||||
CONNECTL(cl, threadedReadEvent, ([&c, &s, cl](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());
|
||||
}));
|
||||
cl->startThreadedRead();
|
||||
}));
|
||||
if (cli.hasArgument("name")) s.setServerName(cli.argumentValue("name"));
|
||||
if (cli.hasArgument("connect")) {
|
||||
c.setServerName(cli.argumentValue("connect"));
|
||||
@@ -15,6 +50,7 @@ int main(int argc, char * argv[]) {
|
||||
} else {
|
||||
s.startThreadedRead();
|
||||
}
|
||||
tm.start(1000);
|
||||
PIKbdListener ls;
|
||||
ls.enableExitCapture(PIKbdListener::F10);
|
||||
ls.start();
|
||||
|
||||
Reference in New Issue
Block a user