From cb45af8a9eba68a0da2fa9435a4ce82bbdf80f50 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Fri, 13 Nov 2020 06:14:49 +0200 Subject: cam: event_loop: Add support for file descriptor events MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extend the EventLoop class to support watching file descriptors for read and write events. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund Reviewed-by: Paul Elder Reviewed-by: Kieran Bingham Reviewed-by: Umang Jain --- src/cam/event_loop.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'src/cam/event_loop.cpp') diff --git a/src/cam/event_loop.cpp b/src/cam/event_loop.cpp index 6a4c47f2..e25784c0 100644 --- a/src/cam/event_loop.cpp +++ b/src/cam/event_loop.cpp @@ -10,6 +10,7 @@ #include #include #include +#include EventLoop *EventLoop::instance_ = nullptr; @@ -26,6 +27,7 @@ EventLoop::~EventLoop() { instance_ = nullptr; + events_.clear(); event_base_free(base_); libevent_global_shutdown(); } @@ -58,6 +60,30 @@ void EventLoop::callLater(const std::function &func) event_base_once(base_, -1, EV_TIMEOUT, dispatchCallback, this, nullptr); } +void EventLoop::addEvent(int fd, EventType type, + const std::function &callback) +{ + std::unique_ptr event = std::make_unique(callback); + short events = (type & Read ? EV_READ : 0) + | (type & Write ? EV_WRITE : 0) + | EV_PERSIST; + + event->event_ = event_new(base_, fd, events, &EventLoop::Event::dispatch, + event.get()); + if (!event->event_) { + std::cerr << "Failed to create event for fd " << fd << std::endl; + return; + } + + int ret = event_add(event->event_, nullptr); + if (ret < 0) { + std::cerr << "Failed to add event for fd " << fd << std::endl; + return; + } + + events_.push_back(std::move(event)); +} + void EventLoop::dispatchCallback([[maybe_unused]] evutil_socket_t fd, [[maybe_unused]] short flags, void *param) { @@ -80,3 +106,21 @@ void EventLoop::dispatchCall() call(); } + +EventLoop::Event::Event(const std::function &callback) + : callback_(callback), event_(nullptr) +{ +} + +EventLoop::Event::~Event() +{ + event_del(event_); + event_free(event_); +} + +void EventLoop::Event::dispatch([[maybe_unused]] int fd, + [[maybe_unused]] short events, void *arg) +{ + Event *event = static_cast(arg); + event->callback_(); +} -- cgit v1.2.1