start move to interruption of blocking calls, PIThread and PIEthernet
This commit is contained in:
163
main.cpp
163
main.cpp
@@ -12,43 +12,138 @@ typedef PIVector<PIVariant> PIVariantVector;
|
||||
REGISTER_VARIANT(PIVariantMap);
|
||||
REGISTER_VARIANT(PIVariantVector);
|
||||
|
||||
const char J[] =
|
||||
"[ \n"
|
||||
"{ \n"
|
||||
" \"idFligth\":\"456123\", \n"
|
||||
" \"FligthPath\": \"d:/orders/nicirt/BSK-52(BBR)/219031.001/EBN-NM-BBR.IMG\",\n"
|
||||
" \"FligthDataPath\": \"\", \n"
|
||||
" \"Passport\": \n"
|
||||
" { \n"
|
||||
" \"id\": \"\", \n"
|
||||
" \"TypePlane\": \"\", \n"
|
||||
" \"FA_PLANEBORT\": \"Ka52-10\" \n"
|
||||
" } \n"
|
||||
" }, [1.1,2,3,4,{\"a\":null},{\"bool\":true,\"bool2\":false}] \n"
|
||||
"] \n"
|
||||
;
|
||||
#ifdef WINDOWS
|
||||
# include <namedpipeapi.h>
|
||||
# include <handleapi.h>
|
||||
# include <fileapi.h>
|
||||
# include <processthreadsapi.h>
|
||||
typedef HANDLE pipe_type;
|
||||
#else
|
||||
# include <unistd.h>
|
||||
typedef int pipe_type;
|
||||
#endif
|
||||
|
||||
struct Pipe {
|
||||
pipe_type fd_read = 0;
|
||||
pipe_type fd_write = 0;
|
||||
void create() {
|
||||
#ifdef WINDOWS
|
||||
CreatePipe(&fd_read, &fd_write, NULL, 0);
|
||||
#else
|
||||
pipe((int*)this);
|
||||
#endif
|
||||
}
|
||||
void destoy() {
|
||||
#ifdef WINDOWS
|
||||
CloseHandle(fd_read);
|
||||
CloseHandle(fd_write);
|
||||
#else
|
||||
close(fd_read);
|
||||
close(fd_write);
|
||||
#endif
|
||||
}
|
||||
int read(void * d, int s) {
|
||||
#ifdef WINDOWS
|
||||
DWORD ret(0);
|
||||
ReadFile(fd_read, d, s, &ret, NULL);
|
||||
return ret;
|
||||
#else
|
||||
return ::read(fd_read, d, s);
|
||||
#endif
|
||||
}
|
||||
int write(void * d, int s) {
|
||||
#ifdef WINDOWS
|
||||
DWORD ret(0);
|
||||
WriteFile(fd_write, d, s, &ret, NULL);
|
||||
return ret;
|
||||
#else
|
||||
return ::write(fd_write, d, s);
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
constexpr int count = 4;
|
||||
Pipe pipes[count];
|
||||
|
||||
class T: public PIThread {
|
||||
public:
|
||||
T(int index): PIThread() {ind = index; pipe = pipes[index];}
|
||||
void run() {
|
||||
PIByteArray data(1024);
|
||||
piCout << "[T"<<ind<<"] start";
|
||||
int readed = pipe.read(data.data(), data.size());
|
||||
piCout << "[T"<<ind<<"] readed" << readed << errorString();
|
||||
piCout << "[T"<<ind<<"] end";
|
||||
}
|
||||
int ind;
|
||||
Pipe pipe;
|
||||
};
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
struct {
|
||||
PIString name;
|
||||
PIString surname;
|
||||
PIString email;
|
||||
} persons[] = {
|
||||
{"Ivan", "Ivanov", "ivan@pip.ru"},
|
||||
{"Igor", "Igorevich", "igor@pip.ru"},
|
||||
{"Andrey", "Andreevich", "andrey@pip.ru"}
|
||||
};
|
||||
PIJSON obj;
|
||||
PIJSON json;
|
||||
int index = 0;
|
||||
for (const auto & p: persons) {
|
||||
obj["index"] = index++;
|
||||
obj["name"] = p.name;
|
||||
obj["surname"] = p.surname;
|
||||
obj["email"] = p.email;
|
||||
json << obj;
|
||||
piCout << "main" << GetCurrentThreadId();
|
||||
|
||||
/*for (int i = 0; i < count; ++i)
|
||||
pipes[i].create();
|
||||
|
||||
PIVector<T*> threads;
|
||||
piCout << "main start";
|
||||
for (int i = 0; i < count; ++i) {
|
||||
T * t = new T(i);
|
||||
threads << t;
|
||||
t->startOnce();
|
||||
}
|
||||
piCout << json;
|
||||
piMSleep(100);
|
||||
for (int i = 0; i < count; ++i) {
|
||||
//pipes[i].write((void*)"string", 7);
|
||||
piMSleep(500);
|
||||
}
|
||||
piCout << "main wait";
|
||||
for (int i = 0; i < count; ++i) {
|
||||
threads[i]->interrupt();
|
||||
threads[i]->waitForFinish();
|
||||
piCout << "main T" << i << "done";
|
||||
}
|
||||
piCout << "main end";
|
||||
for (int i = 0; i < count; ++i) {
|
||||
pipes[i].destoy();
|
||||
delete threads[i];
|
||||
}*/
|
||||
|
||||
PIEthernet eth(PIEthernet::TCP_Server), seth(PIEthernet::TCP_Client);
|
||||
//eth.setReadAddress("127.0.0.1", 50000);
|
||||
//piCout << eth.open();
|
||||
|
||||
//PISerial ser;
|
||||
//ser.setSpeed(PISerial::S9600);
|
||||
//ser.setOption(PIIODevice::BlockingRead);
|
||||
//piCout << ser.open("COM3");
|
||||
|
||||
/*
|
||||
PIThread thread;
|
||||
thread.start([&](void*){
|
||||
piCout << "[T] start" << GetCurrentThreadId();
|
||||
//PIByteArray data = ((PIIODevice*)ð)->read(1024);
|
||||
eth.connect("192.168.1.13", 23, false);
|
||||
piCout << "[T] connected" << eth.isConnected() << errorString();
|
||||
//piCout << "[T] readed" << data.size() << errorString();
|
||||
piCout << "[T] end";
|
||||
});
|
||||
piMSleep(500);
|
||||
eth.close();
|
||||
//piMSleep(500);
|
||||
//thread.stop();
|
||||
//thread.interrupt();
|
||||
//seth.send("127.0.0.1", 50000, "string", 7);
|
||||
//thread.interrupt();
|
||||
thread.waitForFinish();*/
|
||||
eth.listen("127.0.0.1", 50000);
|
||||
piMSleep(500);
|
||||
seth.connect("127.0.0.1", 50001, false);
|
||||
piMSleep(500);
|
||||
piCout << "connected" << seth.isConnected();
|
||||
//eth.close();
|
||||
piCout << "main end";
|
||||
|
||||
eth.close();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user