73 lines
1.7 KiB
C++
73 lines
1.7 KiB
C++
extern "C" {
|
|
#include "pipe_io.h"
|
|
}
|
|
#include <string>
|
|
#include <iostream>
|
|
|
|
|
|
class PipeReader {
|
|
public:
|
|
PipeReader(const char* pipe_path)
|
|
:preader(new_pipe_reader(pipe_path)) {}
|
|
|
|
virtual ~PipeReader() {
|
|
close_pipe_reader(&this->preader);
|
|
}
|
|
|
|
PipeReader(PipeReader&) = delete;
|
|
PipeReader(PipeReader&&) = delete;
|
|
PipeReader& operator=(const PipeReader&) = delete;
|
|
PipeReader& operator=(const PipeReader&&) = delete;
|
|
|
|
void run() {
|
|
while (const char* linebuf = this->recv_message()) {
|
|
std::string msg(linebuf);
|
|
this->handle_message(msg);
|
|
}
|
|
}
|
|
|
|
const char* recv_message() {
|
|
return recv_msg_pipe_reader(&this->preader);
|
|
}
|
|
|
|
virtual void handle_message(std::string msg) {}
|
|
|
|
private:
|
|
pipe_reader preader;
|
|
};
|
|
|
|
|
|
class PipeWriter {
|
|
public:
|
|
PipeWriter(const char* pipe_path)
|
|
:pwriter(new_pipe_writer(pipe_path)) {}
|
|
|
|
virtual ~PipeWriter() {
|
|
close_pipe_writer(&this->pwriter);
|
|
}
|
|
|
|
PipeWriter(PipeReader&) = delete;
|
|
PipeWriter(PipeReader&&) = delete;
|
|
PipeWriter& operator=(const PipeReader&) = delete;
|
|
PipeWriter& operator=(const PipeReader&&) = delete;
|
|
|
|
void send_message(std::string msg) {
|
|
send_msg_pipe_writer(&this->pwriter, msg.c_str());
|
|
}
|
|
|
|
private:
|
|
pipe_writer pwriter;
|
|
};
|
|
|
|
|
|
class PipeIO : public PipeReader, public PipeWriter {
|
|
public:
|
|
PipeIO(const char* in_pipe_path, const char* out_pipe_path)
|
|
:PipeReader(in_pipe_path), PipeWriter(out_pipe_path) {}
|
|
|
|
PipeIO(PipeReader&) = delete;
|
|
PipeIO(PipeReader&&) = delete;
|
|
PipeIO& operator=(const PipeReader&) = delete;
|
|
PipeIO& operator=(const PipeReader&&) = delete;
|
|
};
|