Cleanup
This commit is contained in:
parent
a158f0d7ec
commit
77b9cffaf8
@ -30,7 +30,7 @@ void close_callback(uv_handle_t*);
|
|||||||
*/
|
*/
|
||||||
template <typename H, typename I, typename... Args>
|
template <typename H, typename I, typename... Args>
|
||||||
struct UVHandleGeneric {
|
struct UVHandleGeneric {
|
||||||
UVHandleGeneric(std::function<void(Args...)> fun) {
|
UVHandleGeneric(function<void(Args...)> fun) {
|
||||||
handle = new H;
|
handle = new H;
|
||||||
handle->data = this;
|
handle->data = this;
|
||||||
this->func = fun;
|
this->func = fun;
|
||||||
@ -59,56 +59,56 @@ struct UVHandleGeneric {
|
|||||||
}
|
}
|
||||||
|
|
||||||
H* handle;
|
H* handle;
|
||||||
std::function<void(Args...)> func;
|
function<void(Args...)> func;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename H, typename... Args>
|
template <typename H, typename... Args>
|
||||||
struct UVHandle : public UVHandleGeneric<H, H, 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> {
|
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);
|
void start(int signum);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct PollHandle : public UVHandle<uv_poll_t, int, int> {
|
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 start(int events);
|
||||||
void poll_cb(int status, int events);
|
void poll_cb(int status, int events);
|
||||||
|
|
||||||
std::function<void(uv_poll_event)> func;
|
function<void(uv_poll_event)> func;
|
||||||
std::function<void(int)> err_cb;
|
function<void(int)> err_cb;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct FSEventHandle : public UVHandle<uv_fs_event_t, const char*, int, int> {
|
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 start(const string& path);
|
||||||
void fs_event_cb(const char* path, int events, int status);
|
void fs_event_cb(const char* path, int events, int status);
|
||||||
|
|
||||||
std::function<void(const char*, uv_fs_event)> func;
|
function<void(const char*, uv_fs_event)> func;
|
||||||
std::function<void(int)> err_cb;
|
function<void(int)> err_cb;
|
||||||
};
|
};
|
||||||
|
|
||||||
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(
|
||||||
std::function<void(int)> err_cb);
|
uv_loop_t* loop, function<void(const string)> fun, function<void(void)> eof_cb, 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;
|
function<void(const string)> func;
|
||||||
std::function<void(void)> eof_cb;
|
function<void(void)> eof_cb;
|
||||||
std::function<void(int)> err_cb;
|
function<void(int)> err_cb;
|
||||||
int fd;
|
int fd;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct TimerHandle : public UVHandle<uv_timer_t> {
|
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);
|
void start(uint64_t timeout, uint64_t repeat);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct AsyncHandle : public UVHandle<uv_async_t> {
|
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();
|
void send();
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -126,14 +126,12 @@ class eventloop {
|
|||||||
~eventloop();
|
~eventloop();
|
||||||
void run();
|
void run();
|
||||||
void stop();
|
void stop();
|
||||||
void signal_handler(int signum, std::function<void(int)> fun);
|
void signal_handle(int signum, 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 poll_handle(int events, int fd, function<void(uv_poll_event)> fun, function<void(int)> err_cb);
|
||||||
void fs_event_handler(
|
void fs_event_handle(const string& path, function<void(const char*, uv_fs_event)> fun, 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, function<void(const string)> fun, function<void(void)> eof_cb, function<void(int)> err_cb);
|
||||||
void pipe_handle(
|
void timer_handle(uint64_t timeout, uint64_t repeat, function<void(void)> fun);
|
||||||
int fd, std::function<void(const string)> fun, std::function<void(void)> eof_cb, std::function<void(int)> err_cb);
|
AsyncHandle_t async_handle(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);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
uv_loop_t* get() const;
|
uv_loop_t* get() const;
|
||||||
|
@ -242,16 +242,16 @@ void controller::read_events(bool confwatch) {
|
|||||||
try {
|
try {
|
||||||
eloop = std::make_unique<eventloop>();
|
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); },
|
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)); });
|
[](int status) { throw runtime_error("libuv error while polling X connection: "s + uv_strerror(status)); });
|
||||||
|
|
||||||
for (auto s : {SIGINT, SIGQUIT, SIGTERM, SIGUSR1, SIGALRM}) {
|
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) {
|
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); },
|
m_conf.filepath(), [this](const char* path, uv_fs_event events) { confwatch_handler(path, events); },
|
||||||
[this](int status) {
|
[this](int status) {
|
||||||
m_log.err("libuv error while watching config file for changes: %s", uv_strerror(status));
|
m_log.err("libuv error while watching config file for changes: %s", uv_strerror(status));
|
||||||
|
@ -41,7 +41,7 @@ static void alloc_cb(uv_handle_t*, size_t, uv_buf_t* buf) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SignalHandle {{{
|
// 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);
|
UV(uv_signal_init, loop, handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,7 +51,7 @@ void SignalHandle::start(int signum) {
|
|||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
// PollHandle {{{
|
// 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) {
|
: UVHandle([this](int status, int events) { poll_cb(status, events); }), func(fun), err_cb(err_cb) {
|
||||||
UV(uv_poll_init, loop, handle, fd);
|
UV(uv_poll_init, loop, handle, fd);
|
||||||
}
|
}
|
||||||
@ -72,8 +72,7 @@ void PollHandle::poll_cb(int status, int events) {
|
|||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
// FSEventHandle {{{
|
// FSEventHandle {{{
|
||||||
FSEventHandle::FSEventHandle(
|
FSEventHandle::FSEventHandle(uv_loop_t* loop, function<void(const char*, uv_fs_event)> fun, function<void(int)> err_cb)
|
||||||
uv_loop_t* loop, std::function<void(const char*, uv_fs_event)> fun, std::function<void(int)> err_cb)
|
|
||||||
: UVHandle([this](const char* path, int events, int status) { fs_event_cb(path, events, status); })
|
: UVHandle([this](const char* path, int events, int status) { fs_event_cb(path, events, status); })
|
||||||
, func(fun)
|
, func(fun)
|
||||||
, err_cb(err_cb) {
|
, err_cb(err_cb) {
|
||||||
@ -96,8 +95,8 @@ 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(
|
||||||
std::function<void(int)> err_cb)
|
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); })
|
: UVHandleGeneric([&](ssize_t nread, const uv_buf_t* buf) { read_cb(nread, buf); })
|
||||||
, func(fun)
|
, func(fun)
|
||||||
, eof_cb(eof_cb)
|
, eof_cb(eof_cb)
|
||||||
@ -132,7 +131,7 @@ void PipeHandle::read_cb(ssize_t nread, const uv_buf_t* buf) {
|
|||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
// TimerHandle {{{
|
// 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);
|
UV(uv_timer_init, loop, handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,7 +141,7 @@ void TimerHandle::start(uint64_t timeout, uint64_t repeat) {
|
|||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
// AsyncHandle {{{
|
// 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);
|
UV(uv_async_init, loop, handle, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -199,35 +198,34 @@ uv_loop_t* eventloop::get() const {
|
|||||||
return m_loop.get();
|
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.emplace_back(std::make_unique<SignalHandle>(get(), fun));
|
||||||
m_sig_handles.back()->start(signum);
|
m_sig_handles.back()->start(signum);
|
||||||
}
|
}
|
||||||
|
|
||||||
void eventloop::poll_handler(
|
void eventloop::poll_handle(int events, int fd, function<void(uv_poll_event)> fun, function<void(int)> err_cb) {
|
||||||
int events, int fd, std::function<void(uv_poll_event)> fun, std::function<void(int)> err_cb) {
|
|
||||||
m_poll_handles.emplace_back(std::make_unique<PollHandle>(get(), fd, fun, err_cb));
|
m_poll_handles.emplace_back(std::make_unique<PollHandle>(get(), fd, fun, err_cb));
|
||||||
m_poll_handles.back()->start(events);
|
m_poll_handles.back()->start(events);
|
||||||
}
|
}
|
||||||
|
|
||||||
void eventloop::fs_event_handler(
|
void eventloop::fs_event_handle(
|
||||||
const string& path, std::function<void(const char*, uv_fs_event)> fun, std::function<void(int)> err_cb) {
|
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.emplace_back(std::make_unique<FSEventHandle>(get(), fun, err_cb));
|
||||||
m_fs_event_handles.back()->start(path);
|
m_fs_event_handles.back()->start(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
void eventloop::pipe_handle(
|
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.emplace_back(std::make_unique<PipeHandle>(get(), fun, eof_cb, err_cb));
|
||||||
m_pipe_handles.back()->start(fd);
|
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.emplace_back(std::make_unique<TimerHandle>(get(), fun));
|
||||||
m_timer_handles.back()->start(timeout, repeat);
|
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));
|
m_async_handles.emplace_back(std::make_shared<AsyncHandle>(get(), fun));
|
||||||
return m_async_handles.back();
|
return m_async_handles.back();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user