2019-04-08 13:16:11 +00:00
|
|
|
extern "C" {
|
|
|
|
#include "pipe_io.h"
|
|
|
|
}
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
2019-04-11 16:30:25 +00:00
|
|
|
#include <functional>
|
2019-04-08 13:16:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PipeReader {
|
|
|
|
public:
|
2019-04-11 16:30:25 +00:00
|
|
|
PipeReader(const std::string& pipe_path)
|
|
|
|
:preader(pipeio_new_reader(pipe_path.c_str())) {}
|
2019-04-08 13:16:11 +00:00
|
|
|
|
|
|
|
virtual ~PipeReader() {
|
2019-04-11 16:30:25 +00:00
|
|
|
pipeio_close_reader(&this->preader);
|
2019-04-08 13:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PipeReader(PipeReader&) = delete;
|
|
|
|
PipeReader(PipeReader&&) = delete;
|
|
|
|
PipeReader& operator=(const PipeReader&) = delete;
|
|
|
|
PipeReader& operator=(const PipeReader&&) = delete;
|
|
|
|
|
2019-04-11 16:30:25 +00:00
|
|
|
void set_message_handler(std::function<void(std::string)> message_handler) {
|
|
|
|
this->message_handler = message_handler;
|
|
|
|
}
|
|
|
|
|
2019-04-08 13:16:11 +00:00
|
|
|
void run() {
|
|
|
|
while (const char* linebuf = this->recv_message()) {
|
|
|
|
std::string msg(linebuf);
|
2019-04-11 16:30:25 +00:00
|
|
|
this->message_handler(msg);
|
2019-04-08 13:16:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* recv_message() {
|
2019-04-11 16:30:25 +00:00
|
|
|
return pipeio_recv_msg(&this->preader);
|
2019-04-08 13:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
pipe_reader preader;
|
2019-04-11 16:30:25 +00:00
|
|
|
std::function<void(std::string)> message_handler = [](std::string){};
|
2019-04-08 13:16:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class PipeWriter {
|
|
|
|
public:
|
2019-04-11 16:30:25 +00:00
|
|
|
PipeWriter(const std::string& pipe_path)
|
|
|
|
:pwriter(pipeio_new_writer(pipe_path.c_str())) {}
|
2019-04-08 13:16:11 +00:00
|
|
|
|
|
|
|
virtual ~PipeWriter() {
|
2019-04-11 16:30:25 +00:00
|
|
|
pipeio_close_writer(&this->pwriter);
|
2019-04-08 13:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PipeWriter(PipeReader&) = delete;
|
|
|
|
PipeWriter(PipeReader&&) = delete;
|
|
|
|
PipeWriter& operator=(const PipeReader&) = delete;
|
|
|
|
PipeWriter& operator=(const PipeReader&&) = delete;
|
|
|
|
|
|
|
|
void send_message(std::string msg) {
|
2019-04-11 16:30:25 +00:00
|
|
|
pipeio_send_msg(&this->pwriter, msg.c_str());
|
2019-04-08 13:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
pipe_writer pwriter;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-04-11 16:30:25 +00:00
|
|
|
class PipeIO {
|
2019-04-08 13:16:11 +00:00
|
|
|
public:
|
2019-04-11 16:30:25 +00:00
|
|
|
PipeReader reader;
|
|
|
|
PipeWriter writer;
|
|
|
|
|
|
|
|
PipeIO(const std::string& in_pipe_path, const std::string& out_pipe_path)
|
|
|
|
:reader(in_pipe_path), writer(out_pipe_path) {}
|
2019-04-08 13:16:11 +00:00
|
|
|
|
|
|
|
PipeIO(PipeReader&) = delete;
|
|
|
|
PipeIO(PipeReader&&) = delete;
|
|
|
|
PipeIO& operator=(const PipeReader&) = delete;
|
|
|
|
PipeIO& operator=(const PipeReader&&) = delete;
|
|
|
|
};
|