From a158f0d7ec0a08366ee1fa5c9f6e7a93095556f4 Mon Sep 17 00:00:00 2001
From: patrick96
Date: Mon, 13 Sep 2021 18:30:11 +0200
Subject: [PATCH] Add error callback for ipc handle
---
include/components/eventloop.hpp | 7 +++++--
src/components/controller.cpp | 3 ++-
src/components/eventloop.cpp | 24 ++++++++++++------------
3 files changed, 19 insertions(+), 15 deletions(-)
diff --git a/include/components/eventloop.hpp b/include/components/eventloop.hpp
index eeec5120..c74bd445 100644
--- a/include/components/eventloop.hpp
+++ b/include/components/eventloop.hpp
@@ -91,12 +91,14 @@ struct FSEventHandle : public UVHandle {
};
struct PipeHandle : public UVHandleGeneric {
- PipeHandle(uv_loop_t* loop, std::function fun, std::function eof_cb);
+ PipeHandle(uv_loop_t* loop, std::function fun, std::function eof_cb,
+ std::function err_cb);
void start(int fd);
void read_cb(ssize_t nread, const uv_buf_t* buf);
std::function func;
std::function eof_cb;
+ std::function err_cb;
int fd;
};
@@ -128,7 +130,8 @@ class eventloop {
void poll_handler(int events, int fd, std::function fun, std::function err_cb);
void fs_event_handler(
const string& path, std::function fun, std::function err_cb);
- void pipe_handle(int fd, std::function fun, std::function eof_cb);
+ void pipe_handle(
+ int fd, std::function fun, std::function eof_cb, std::function err_cb);
void timer_handle(uint64_t timeout, uint64_t repeat, std::function fun);
AsyncHandle_t async_handle(std::function fun);
diff --git a/src/components/controller.cpp b/src/components/controller.cpp
index e04a2843..73ade056 100644
--- a/src/components/controller.cpp
+++ b/src/components/controller.cpp
@@ -261,7 +261,8 @@ void controller::read_events(bool confwatch) {
if (m_ipc) {
eloop->pipe_handle(
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()) {
diff --git a/src/components/eventloop.cpp b/src/components/eventloop.cpp
index 09a302da..e5738f7e 100644
--- a/src/components/eventloop.cpp
+++ b/src/components/eventloop.cpp
@@ -96,8 +96,12 @@ void FSEventHandle::fs_event_cb(const char* path, int events, int status) {
// }}}
// PipeHandle {{{
-PipeHandle::PipeHandle(uv_loop_t* loop, std::function fun, std::function eof_cb)
- : UVHandleGeneric([&](ssize_t nread, const uv_buf_t* buf) { read_cb(nread, buf); }), func(fun), eof_cb(eof_cb) {
+PipeHandle::PipeHandle(uv_loop_t* loop, std::function fun, std::function eof_cb,
+ std::function 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);
}
@@ -108,17 +112,12 @@ void PipeHandle::start(int fd) {
}
void PipeHandle::read_cb(ssize_t nread, const uv_buf_t* buf) {
- auto log = logger::make();
if (nread > 0) {
- string payload = string(buf->base, nread);
- // TODO lower logging level
- log.notice("Bytes read: %d: '%s'", nread, payload);
- func(payload);
+ func(string(buf->base, nread));
} else if (nread < 0) {
if (nread != UV_EOF) {
- // TODO maybe handle this differently. exception?
- log.err("Read error: %s", uv_err_name(nread));
- uv_close((uv_handle_t*)handle, nullptr);
+ close();
+ err_cb(nread);
} else {
eof_cb();
// TODO this causes constant EOFs
@@ -217,8 +216,9 @@ void eventloop::fs_event_handler(
m_fs_event_handles.back()->start(path);
}
-void eventloop::pipe_handle(int fd, std::function fun, std::function eof_cb) {
- m_pipe_handles.emplace_back(std::make_unique(get(), fun, eof_cb));
+void eventloop::pipe_handle(
+ int fd, std::function fun, std::function eof_cb, std::function err_cb) {
+ m_pipe_handles.emplace_back(std::make_unique(get(), fun, eof_cb, err_cb));
m_pipe_handles.back()->start(fd);
}