150 lines
3.3 KiB
C++
150 lines
3.3 KiB
C++
#include "pip.h"
|
|
#include "piiostream.h"
|
|
#include "pibytearray.h"
|
|
#include "pimathbase.h"
|
|
#include "pijson.h"
|
|
|
|
using namespace PICoutManipulators;
|
|
|
|
typedef PIMap<PIString, PIVariant> PIVariantMap;
|
|
typedef PIVector<PIVariant> PIVariantVector;
|
|
|
|
REGISTER_VARIANT(PIVariantMap);
|
|
REGISTER_VARIANT(PIVariantVector);
|
|
|
|
#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[]) {
|
|
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();
|
|
}
|
|
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;
|
|
}
|