piclouddispatcher patch
This commit is contained in:
96
main.cpp
96
main.cpp
@@ -1,42 +1,64 @@
|
||||
#include "pip.h"
|
||||
|
||||
class A: public PIObject {
|
||||
PIOBJECT(A)
|
||||
public:
|
||||
EVENT_HANDLER1(void, eh, PIByteArray, i) {piCout << "eh" << i.toHex();}
|
||||
};
|
||||
|
||||
class B: public PIObject {
|
||||
PIOBJECT(B)
|
||||
public:
|
||||
EVENT2(eh, PIByteArray, i, PIString, str);
|
||||
};
|
||||
|
||||
template <typename S, typename T, typename std::enable_if<
|
||||
std::is_same<T, PIVector<S>>::value
|
||||
, int>::type = 0> PIVector<S> rrr(PIVector<T>) {
|
||||
piCout << std::is_same<T, PIVector<S>>::value;
|
||||
return PIVector<S>();
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
A a;
|
||||
B b;
|
||||
CONNECTU_QUEUED(&b, eh, &a, eh, &a);
|
||||
b.eh(PIByteArray::fromHex("102030"), "str");
|
||||
a.maybeCallQueuedEvents();
|
||||
/*PIDeque<int> x;
|
||||
x.resize(16, [](size_t i) {return i+1;});
|
||||
piCout << x;
|
||||
PIDeque<PIDeque<int>> m = x.reshape(2,8);
|
||||
piCout << m;
|
||||
piCout << x.reshape(4,4,PIDeque<int>::byColumn);
|
||||
piCout << x.reshape(2,8);
|
||||
piCout << x.reshape(2,8,PIDeque<int>::byColumn);
|
||||
PIDeque<int> y;
|
||||
y = m.reshape<int>();
|
||||
piCout << y;
|
||||
piCout << m.reshape<int>(PIDeque<int>::byColumn);*/
|
||||
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());
|
||||
}));
|
||||
CONNECTL(cl, closed, ([&clients, cl](){
|
||||
cl->stop();
|
||||
clients.removeAll(cl);
|
||||
cl->deleteLater();
|
||||
}));
|
||||
cl->startThreadedRead();
|
||||
}));
|
||||
if (cli.hasArgument("name")) s.setServerName(cli.argumentValue("name"));
|
||||
if (cli.hasArgument("connect")) {
|
||||
c.setServerName(cli.argumentValue("connect"));
|
||||
c.startThreadedRead();
|
||||
} else {
|
||||
s.startThreadedRead();
|
||||
}
|
||||
tm.start(1000);
|
||||
PIKbdListener ls;
|
||||
ls.enableExitCapture(PIKbdListener::F10);
|
||||
ls.start();
|
||||
WAIT_FOR_EXIT
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user