Files
pip/main.cpp

218 lines
4.8 KiB
C++

#include "pip.h"
#include "piiostream.h"
#include "pibytearray.h"
#include "pimathbase.h"
#include "pijson.h"
#include "piwaitevent_p.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;
};
PITimeMeasurer tm;
void phase(const char * msg) {
piCout << "";
piCout << piRound(tm.elapsed_s() * 10) / 10. << "s" << msg;
}
int main(int argc, char * argv[]) {
piCout << "main";
PITimer timer;
timer.setSlot([](){
static int cnt = 0;
piCout << "tick" << ++cnt;
});
timer.start(500);
piSleep(1.12);
piCout << "end";
PITimeMeasurer tm;
timer.stop();
double tm_ms = tm.elapsed_m();
piCout << "stop took" << tm_ms;
return 0;
/*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::UDP), seth(PIEthernet::UDP);
//eth.setReadAddress("127.0.0.1", 50000);
//eth.startThreadedRead();
//piCout << eth.open();
PIByteArray req = PIByteArray::fromHex("205e011000000000ef");
PISerial ser;
ser.setSpeed(PISerial::S9600);
ser.setOption(PIIODevice::BlockingRead, false);
ser.setVTime(200);
ser.open("COM3");
CONNECTL(&ser, threadedReadEvent, ([](const uchar * data, ssize_t size){
piCout << "*ser readed" << size;
}));
PIThread thread;
thread.start([&](void*){
piCout << "[T] start";
PIByteArray data = ((PIIODevice*)&ser)->read(1024);
piCout << "[T] readed" << data.size();// << errorString();
piCout << "[T] end";
}, 200);
//ser.startThreadedRead();
piSleep(1);
ser.write(req);
phase("Send");
piSleep(2);
phase("End");
/*
PIEthernet eth(PIEthernet::TCP_Client), seth(PIEthernet::TCP_Server), * server_client = nullptr;
seth.listen("127.0.0.1", 50000, true);
//seth.startThreadedRead();
CONNECTL(&seth, newConnection, ([&server_client](PIEthernet * e){
server_client = e;
e->setName("TCP SC");
piCout << "newConn" << e;
CONNECTL(e, threadedReadEvent, ([](const uchar * data, ssize_t size){
piCout << "*TCP SC* readed" << size;
}));
CONNECTL(e, disconnected, ([](bool error){
piCout << "*TCP SC* disconnected" << error;
}));
e->startThreadedRead();
}));
eth.setName("TCP CC");
//eth.setParameter(PIEthernet::KeepConnection, false);
CONNECTL(&eth, connected, ([&eth](){
piCout << "*TCP CC* connected";
eth.send("byte", 5);
}));
CONNECTL(&eth, disconnected, ([](bool error){
piCout << "*TCP CC* disconnected" << error;
}));
piMSleep(500);
phase("Connect");
eth.connect("127.0.0.1", 50000);
eth.startThreadedRead();
piMSleep(500);
phase("Send 5");
piCout << "c-ing" << eth.isConnecting();
piCout << "c- ed" << eth.isConnected();
eth.send("byte", 5);
piMSleep(500);
phase("Send 6");
eth.send("bytes", 6);
piMSleep(500);
phase("Disconnect");
if (server_client)
server_client->close();
//eth.close();
piMSleep(500);
phase("END");
*/
return 0;
}