Files
sdrpi-fpv-control/lib/port/unix/poller.cpp

52 lines
1.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "port/poller.h"
#include <sys/poll.h>
#include <algorithm>
bool poller::PollObject::isPollIn() const { return revents & POLLIN; }
bool poller::PollObject::isPollOut() const { return revents & POLLOUT; }
bool poller::PollObject::isPollHup() const { return revents & POLLHUP; }
poller::PollObject::~PollObject() = default;
poller::PollWrapper::PollWrapper() = default;
void poller::PollWrapper::loop(int timeoutMs) {
if (this->objects.empty()) {
return;
}
// проверяем, что нет объектов с fd < 0, удаляем такие объекты если они есть
for (size_t index = 0; index < this->objects.size();) {
if (this->objects[index]->fd < 0) {
this->objects.erase(this->objects.begin() + static_cast<ssize_t>(index));
} else {
index++;
}
}
const auto qsize = this->objects.size();
// массив для вызова poll
pollfd pollFds[qsize];
// заполняем данные для poll
for (size_t i = 0; i < qsize; i++) {
pollFds[i].revents = 0;
pollFds[i].fd = this->objects[i]->fd;
pollFds[i].events = this->objects[i]->events;
}
// выполняем poll
poll(pollFds, qsize, timeoutMs);
// проверяем события
for (size_t i = 0; i < qsize; i++) {
objects[i]->revents = pollFds[i].revents;
// if (pollFds[i].revents != 0) {
// objects[i]->callback();
// }
}
}
poller::PollWrapper::~PollWrapper() = default;