This commit is contained in:
patrick96 2021-09-13 19:19:39 +02:00 committed by Patrick Ziegler
parent a158f0d7ec
commit 77b9cffaf8
3 changed files with 40 additions and 44 deletions

View File

@ -30,7 +30,7 @@ void close_callback(uv_handle_t*);
*/
template <typename H, typename I, typename... Args>
struct UVHandleGeneric {
UVHandleGeneric(std::function<void(Args...)> fun) {
UVHandleGeneric(function<void(Args...)> fun) {
handle = new H;
handle->data = this;
this->func = fun;
@ -59,56 +59,56 @@ struct UVHandleGeneric {
}
H* handle;
std::function<void(Args...)> func;
function<void(Args...)> func;
};
template <typename H, typename... Args>
struct UVHandle : public UVHandleGeneric<H, H, Args...> {
UVHandle(std::function<void(Args...)> fun) : UVHandleGeneric<H, H, Args...>(fun) {}
UVHandle(function<void(Args...)> fun) : UVHandleGeneric<H, H, Args...>(fun) {}
};
struct SignalHandle : public UVHandle<uv_signal_t, int> {
SignalHandle(uv_loop_t* loop, std::function<void(int)> fun);
SignalHandle(uv_loop_t* loop, function<void(int)> fun);
void start(int signum);
};
struct PollHandle : public UVHandle<uv_poll_t, int, int> {
PollHandle(uv_loop_t* loop, int fd, std::function<void(uv_poll_event)> fun, std::function<void(int)> err_cb);
PollHandle(uv_loop_t* loop, int fd, function<void(uv_poll_event)> fun, function<void(int)> err_cb);
void start(int events);
void poll_cb(int status, int events);
std::function<void(uv_poll_event)> func;
std::function<void(int)> err_cb;
function<void(uv_poll_event)> func;
function<void(int)> err_cb;
};
struct FSEventHandle : public UVHandle<uv_fs_event_t, const char*, int, int> {
FSEventHandle(uv_loop_t* loop, std::function<void(const char*, uv_fs_event)> fun, std::function<void(int)> err_cb);
FSEventHandle(uv_loop_t* loop, function<void(const char*, uv_fs_event)> fun, function<void(int)> err_cb);
void start(const string& path);
void fs_event_cb(const char* path, int events, int status);
std::function<void(const char*, uv_fs_event)> func;
std::function<void(int)> err_cb;
function<void(const char*, uv_fs_event)> func;
function<void(int)> err_cb;
};
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,
std::function<void(int)> err_cb);
PipeHandle(
uv_loop_t* loop, function<void(const string)> fun, function<void(void)> eof_cb, function<void(int)> err_cb);
void start(int fd);
void read_cb(ssize_t nread, const uv_buf_t* buf);
std::function<void(const string)> func;
std::function<void(void)> eof_cb;
std::function<void(int)> err_cb;
function<void(const string)> func;
function<void(void)> eof_cb;
function<void(int)> err_cb;
int fd;
};
struct TimerHandle : public UVHandle<uv_timer_t> {
TimerHandle(uv_loop_t* loop, std::function<void(void)> fun);
TimerHandle(uv_loop_t* loop, function<void(void)> fun);
void start(uint64_t timeout, uint64_t repeat);
};
struct AsyncHandle : public UVHandle<uv_async_t> {
AsyncHandle(uv_loop_t* loop, std::function<void(void)> fun);
AsyncHandle(uv_loop_t* loop, function<void(void)> fun);
void send();
};
@ -126,14 +126,12 @@ class eventloop {
~eventloop();
void run();
void stop();
void signal_handler(int signum, std::function<void(int)> fun);
void poll_handler(int events, int fd, std::function<void(uv_poll_event)> fun, std::function<void(int)> err_cb);
void fs_event_handler(
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, std::function<void(int)> err_cb);
void timer_handle(uint64_t timeout, uint64_t repeat, std::function<void(void)> fun);
AsyncHandle_t async_handle(std::function<void(void)> fun);
void signal_handle(int signum, function<void(int)> fun);
void poll_handle(int events, int fd, function<void(uv_poll_event)> fun, function<void(int)> err_cb);
void fs_event_handle(const string& path, function<void(const char*, uv_fs_event)> fun, function<void(int)> err_cb);
void pipe_handle(int fd, function<void(const string)> fun, function<void(void)> eof_cb, function<void(int)> err_cb);
void timer_handle(uint64_t timeout, uint64_t repeat, function<void(void)> fun);
AsyncHandle_t async_handle(function<void(void)> fun);
protected:
uv_loop_t* get() const;

View File

@ -242,16 +242,16 @@ void controller::read_events(bool confwatch) {
try {
eloop = std::make_unique<eventloop>();
eloop->poll_handler(
eloop->poll_handle(
UV_READABLE, m_connection.get_file_descriptor(), [this](uv_poll_event events) { conn_cb(events); },
[](int status) { throw runtime_error("libuv error while polling X connection: "s + uv_strerror(status)); });
for (auto s : {SIGINT, SIGQUIT, SIGTERM, SIGUSR1, SIGALRM}) {
eloop->signal_handler(s, [this](int signum) { signal_handler(signum); });
eloop->signal_handle(s, [this](int signum) { signal_handler(signum); });
}
if (confwatch) {
eloop->fs_event_handler(
eloop->fs_event_handle(
m_conf.filepath(), [this](const char* path, uv_fs_event events) { confwatch_handler(path, events); },
[this](int status) {
m_log.err("libuv error while watching config file for changes: %s", uv_strerror(status));

View File

@ -41,7 +41,7 @@ static void alloc_cb(uv_handle_t*, size_t, uv_buf_t* buf) {
}
// SignalHandle {{{
SignalHandle::SignalHandle(uv_loop_t* loop, std::function<void(int)> fun) : UVHandle(fun) {
SignalHandle::SignalHandle(uv_loop_t* loop, function<void(int)> fun) : UVHandle(fun) {
UV(uv_signal_init, loop, handle);
}
@ -51,7 +51,7 @@ void SignalHandle::start(int signum) {
// }}}
// PollHandle {{{
PollHandle::PollHandle(uv_loop_t* loop, int fd, std::function<void(uv_poll_event)> fun, std::function<void(int)> err_cb)
PollHandle::PollHandle(uv_loop_t* loop, int fd, function<void(uv_poll_event)> fun, function<void(int)> err_cb)
: UVHandle([this](int status, int events) { poll_cb(status, events); }), func(fun), err_cb(err_cb) {
UV(uv_poll_init, loop, handle, fd);
}
@ -72,8 +72,7 @@ void PollHandle::poll_cb(int status, int events) {
// }}}
// FSEventHandle {{{
FSEventHandle::FSEventHandle(
uv_loop_t* loop, std::function<void(const char*, uv_fs_event)> fun, std::function<void(int)> err_cb)
FSEventHandle::FSEventHandle(uv_loop_t* loop, function<void(const char*, uv_fs_event)> fun, function<void(int)> err_cb)
: UVHandle([this](const char* path, int events, int status) { fs_event_cb(path, events, status); })
, func(fun)
, err_cb(err_cb) {
@ -96,8 +95,8 @@ void FSEventHandle::fs_event_cb(const char* path, int events, int status) {
// }}}
// PipeHandle {{{
PipeHandle::PipeHandle(uv_loop_t* loop, std::function<void(const string)> fun, std::function<void(void)> eof_cb,
std::function<void(int)> err_cb)
PipeHandle::PipeHandle(
uv_loop_t* loop, function<void(const string)> fun, function<void(void)> eof_cb, function<void(int)> err_cb)
: UVHandleGeneric([&](ssize_t nread, const uv_buf_t* buf) { read_cb(nread, buf); })
, func(fun)
, eof_cb(eof_cb)
@ -132,7 +131,7 @@ void PipeHandle::read_cb(ssize_t nread, const uv_buf_t* buf) {
// }}}
// TimerHandle {{{
TimerHandle::TimerHandle(uv_loop_t* loop, std::function<void(void)> fun) : UVHandle(fun) {
TimerHandle::TimerHandle(uv_loop_t* loop, function<void(void)> fun) : UVHandle(fun) {
UV(uv_timer_init, loop, handle);
}
@ -142,7 +141,7 @@ void TimerHandle::start(uint64_t timeout, uint64_t repeat) {
// }}}
// AsyncHandle {{{
AsyncHandle::AsyncHandle(uv_loop_t* loop, std::function<void(void)> fun) : UVHandle(fun) {
AsyncHandle::AsyncHandle(uv_loop_t* loop, function<void(void)> fun) : UVHandle(fun) {
UV(uv_async_init, loop, handle, callback);
}
@ -199,35 +198,34 @@ uv_loop_t* eventloop::get() const {
return m_loop.get();
}
void eventloop::signal_handler(int signum, std::function<void(int)> fun) {
void eventloop::signal_handle(int signum, function<void(int)> fun) {
m_sig_handles.emplace_back(std::make_unique<SignalHandle>(get(), fun));
m_sig_handles.back()->start(signum);
}
void eventloop::poll_handler(
int events, int fd, std::function<void(uv_poll_event)> fun, std::function<void(int)> err_cb) {
void eventloop::poll_handle(int events, int fd, function<void(uv_poll_event)> fun, function<void(int)> err_cb) {
m_poll_handles.emplace_back(std::make_unique<PollHandle>(get(), fd, fun, err_cb));
m_poll_handles.back()->start(events);
}
void eventloop::fs_event_handler(
const string& path, std::function<void(const char*, uv_fs_event)> fun, std::function<void(int)> err_cb) {
void eventloop::fs_event_handle(
const string& path, function<void(const char*, uv_fs_event)> fun, function<void(int)> err_cb) {
m_fs_event_handles.emplace_back(std::make_unique<FSEventHandle>(get(), fun, err_cb));
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, std::function<void(int)> err_cb) {
int fd, function<void(const string)> fun, function<void(void)> eof_cb, 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);
}
void eventloop::timer_handle(uint64_t timeout, uint64_t repeat, std::function<void(void)> fun) {
void eventloop::timer_handle(uint64_t timeout, uint64_t repeat, function<void(void)> fun) {
m_timer_handles.emplace_back(std::make_unique<TimerHandle>(get(), fun));
m_timer_handles.back()->start(timeout, repeat);
}
AsyncHandle_t eventloop::async_handle(std::function<void(void)> fun) {
AsyncHandle_t eventloop::async_handle(function<void(void)> fun) {
m_async_handles.emplace_back(std::make_shared<AsyncHandle>(get(), fun));
return m_async_handles.back();
}