Add error callback for ipc handle

This commit is contained in:
patrick96 2021-09-13 18:30:11 +02:00 committed by Patrick Ziegler
parent 52a3961602
commit a158f0d7ec
3 changed files with 19 additions and 15 deletions

View File

@ -91,12 +91,14 @@ struct FSEventHandle : public UVHandle<uv_fs_event_t, const char*, int, int> {
}; };
struct PipeHandle : public UVHandleGeneric<uv_pipe_t, uv_stream_t, ssize_t, const uv_buf_t*> { struct PipeHandle : public UVHandleGeneric<uv_pipe_t, uv_stream_t, ssize_t, const uv_buf_t*> {
PipeHandle(uv_loop_t* loop, std::function<void(const string)> fun, std::function<void(void)> eof_cb); PipeHandle(uv_loop_t* loop, std::function<void(const string)> fun, std::function<void(void)> eof_cb,
std::function<void(int)> err_cb);
void start(int fd); void start(int fd);
void read_cb(ssize_t nread, const uv_buf_t* buf); void read_cb(ssize_t nread, const uv_buf_t* buf);
std::function<void(const string)> func; std::function<void(const string)> func;
std::function<void(void)> eof_cb; std::function<void(void)> eof_cb;
std::function<void(int)> err_cb;
int fd; int fd;
}; };
@ -128,7 +130,8 @@ class eventloop {
void poll_handler(int events, int fd, std::function<void(uv_poll_event)> fun, std::function<void(int)> err_cb); void poll_handler(int events, int fd, std::function<void(uv_poll_event)> fun, std::function<void(int)> err_cb);
void fs_event_handler( void fs_event_handler(
const string& path, std::function<void(const char*, uv_fs_event)> fun, std::function<void(int)> err_cb); const string& path, std::function<void(const char*, uv_fs_event)> fun, std::function<void(int)> err_cb);
void pipe_handle(int fd, std::function<void(const string)> fun, std::function<void(void)> eof_cb); void pipe_handle(
int fd, std::function<void(const string)> fun, std::function<void(void)> eof_cb, std::function<void(int)> err_cb);
void timer_handle(uint64_t timeout, uint64_t repeat, std::function<void(void)> fun); void timer_handle(uint64_t timeout, uint64_t repeat, std::function<void(void)> fun);
AsyncHandle_t async_handle(std::function<void(void)> fun); AsyncHandle_t async_handle(std::function<void(void)> fun);

View File

@ -261,7 +261,8 @@ void controller::read_events(bool confwatch) {
if (m_ipc) { if (m_ipc) {
eloop->pipe_handle( eloop->pipe_handle(
m_ipc->get_file_descriptor(), [this](const string payload) { m_ipc->receive_data(payload); }, m_ipc->get_file_descriptor(), [this](const string payload) { m_ipc->receive_data(payload); },
[this]() { m_ipc->receive_eof(); }); [this]() { m_ipc->receive_eof(); },
[this](int err) { m_log.err("libuv error while listening to IPC channel: %s", uv_strerror(err)); });
} }
if (!m_snapshot_dst.empty()) { if (!m_snapshot_dst.empty()) {

View File

@ -96,8 +96,12 @@ void FSEventHandle::fs_event_cb(const char* path, int events, int status) {
// }}} // }}}
// PipeHandle {{{ // PipeHandle {{{
PipeHandle::PipeHandle(uv_loop_t* loop, std::function<void(const string)> fun, std::function<void(void)> eof_cb) PipeHandle::PipeHandle(uv_loop_t* loop, std::function<void(const string)> fun, std::function<void(void)> eof_cb,
: UVHandleGeneric([&](ssize_t nread, const uv_buf_t* buf) { read_cb(nread, buf); }), func(fun), eof_cb(eof_cb) { std::function<void(int)> err_cb)
: UVHandleGeneric([&](ssize_t nread, const uv_buf_t* buf) { read_cb(nread, buf); })
, func(fun)
, eof_cb(eof_cb)
, err_cb(err_cb) {
UV(uv_pipe_init, loop, handle, false); UV(uv_pipe_init, loop, handle, false);
} }
@ -108,17 +112,12 @@ void PipeHandle::start(int fd) {
} }
void PipeHandle::read_cb(ssize_t nread, const uv_buf_t* buf) { void PipeHandle::read_cb(ssize_t nread, const uv_buf_t* buf) {
auto log = logger::make();
if (nread > 0) { if (nread > 0) {
string payload = string(buf->base, nread); func(string(buf->base, nread));
// TODO lower logging level
log.notice("Bytes read: %d: '%s'", nread, payload);
func(payload);
} else if (nread < 0) { } else if (nread < 0) {
if (nread != UV_EOF) { if (nread != UV_EOF) {
// TODO maybe handle this differently. exception? close();
log.err("Read error: %s", uv_err_name(nread)); err_cb(nread);
uv_close((uv_handle_t*)handle, nullptr);
} else { } else {
eof_cb(); eof_cb();
// TODO this causes constant EOFs // TODO this causes constant EOFs
@ -217,8 +216,9 @@ void eventloop::fs_event_handler(
m_fs_event_handles.back()->start(path); m_fs_event_handles.back()->start(path);
} }
void eventloop::pipe_handle(int fd, std::function<void(const string)> fun, std::function<void(void)> eof_cb) { void eventloop::pipe_handle(
m_pipe_handles.emplace_back(std::make_unique<PipeHandle>(get(), fun, eof_cb)); int fd, std::function<void(const string)> fun, std::function<void(void)> eof_cb, std::function<void(int)> err_cb) {
m_pipe_handles.emplace_back(std::make_unique<PipeHandle>(get(), fun, eof_cb, err_cb));
m_pipe_handles.back()->start(fd); m_pipe_handles.back()->start(fd);
} }