TCP server for the vision library.
Change-Id: Id73304cabe3f746d72d51cc7dfe5adbe61c20c16
diff --git a/aos/vision/events/epoll_events.cc b/aos/vision/events/epoll_events.cc
index aaecdbf..f191259 100644
--- a/aos/vision/events/epoll_events.cc
+++ b/aos/vision/events/epoll_events.cc
@@ -1,87 +1,72 @@
#include "aos/vision/events/epoll_events.h"
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
#include <fcntl.h>
+#include <string.h>
#include <sys/epoll.h>
-
+#include <sys/socket.h>
+#include <sys/types.h>
#include <vector>
-#include "aos/common/scoped_fd.h"
#include "aos/common/logging/logging.h"
namespace aos {
namespace events {
-class EpollLoop::Impl {
- public:
- Impl() : epoll_fd_(PCHECK(epoll_create1(0))) {}
+EpollLoop::EpollLoop() : epoll_fd_(PCHECK(epoll_create1(0))) {}
- void Add(EpollEvent *event) {
- struct epoll_event temp_event;
- temp_event.data.ptr = static_cast<void *>(event);
- temp_event.events = EPOLLIN;
- PCHECK(epoll_ctl(epoll_fd(), EPOLL_CTL_ADD, event->fd(), &temp_event));
- }
+void EpollLoop::Add(EpollEvent *event) {
+ event->loop_ = this;
+ struct epoll_event temp_event;
+ temp_event.data.ptr = static_cast<void *>(event);
+ temp_event.events = EPOLLIN;
+ PCHECK(epoll_ctl(epoll_fd(), EPOLL_CTL_ADD, event->fd(), &temp_event));
+}
- void Run() {
- while (true) {
- const int timeout = CalculateTimeout();
- static constexpr size_t kNumberOfEvents = 64;
- epoll_event events[kNumberOfEvents];
- const int number_events = PCHECK(
- epoll_wait(epoll_fd(), events, kNumberOfEvents, timeout));
+void EpollLoop::Delete(EpollEvent *event) {
+ PCHECK(epoll_ctl(epoll_fd(), EPOLL_CTL_DEL, event->fd(), NULL));
+}
- for (int i = 0; i < number_events; i++) {
- EpollEvent *event =
- static_cast<EpollEvent *>(events[i].data.ptr);
- if ((events[i].events & ~(EPOLLIN | EPOLLPRI)) != 0) {
- LOG(FATAL, "unexpected epoll events set in %x on %d\n",
- events[i].events, event->fd());
- }
- event->ReadEvent();
+void EpollLoop::Run() {
+ while (true) {
+ const int timeout = CalculateTimeout();
+ static constexpr size_t kNumberOfEvents = 64;
+ epoll_event events[kNumberOfEvents];
+ const int number_events =
+ PCHECK(epoll_wait(epoll_fd(), events, kNumberOfEvents, timeout));
+
+ for (int i = 0; i < number_events; i++) {
+ EpollEvent *event = static_cast<EpollEvent *>(events[i].data.ptr);
+ if ((events[i].events & ~(EPOLLIN | EPOLLPRI)) != 0) {
+ LOG(FATAL, "unexpected epoll events set in %x on %d\n",
+ events[i].events, event->fd());
}
+ event->ReadEvent();
+ }
- for (EpollWatcher *watcher : watchers_) {
- watcher->Wake();
- }
+ for (EpollWatcher *watcher : watchers_) {
+ watcher->Wake();
}
}
+}
- void AddWait(EpollWait *wait) { waits_.push_back(wait); }
- void AddWatcher(EpollWatcher *watcher) { watchers_.push_back(watcher); }
-
- int epoll_fd() { return epoll_fd_.get(); }
-
- private:
- // Calculates the new timeout value to pass to epoll_wait.
- int CalculateTimeout() {
- const monotonic_clock::time_point monotonic_now = monotonic_clock::now();
- int r = -1;
- for (EpollWait *c : waits_) {
- const int new_timeout = c->Recalculate(monotonic_now);
- if (new_timeout >= 0) {
- if (r < 0 || new_timeout < r) {
- r = new_timeout;
- }
- }
- }
- return r;
- }
-
- private:
- ::aos::ScopedFD epoll_fd_;
- ::std::vector<EpollWait *> waits_;
- ::std::vector<EpollWatcher *> watchers_;
-};
-
-EpollLoop::EpollLoop() : impl_(new Impl()) {}
-void EpollLoop::Run() { impl_->Run(); }
-void EpollLoop::Add(EpollEvent *event) { impl_->Add(event); }
-void EpollLoop::AddWait(EpollWait *wait) { impl_->AddWait(wait); }
+void EpollLoop::AddWait(EpollWait *wait) { waits_.push_back(wait); }
void EpollLoop::AddWatcher(EpollWatcher *watcher) {
- impl_->AddWatcher(watcher);
+ watchers_.push_back(watcher);
+}
+
+// Calculates the new timeout value to pass to epoll_wait.
+int EpollLoop::CalculateTimeout() {
+ const monotonic_clock::time_point monotonic_now = monotonic_clock::now();
+ int r = -1;
+ for (EpollWait *c : waits_) {
+ const int new_timeout = c->Recalculate(monotonic_now);
+ if (new_timeout >= 0) {
+ if (r < 0 || new_timeout < r) {
+ r = new_timeout;
+ }
+ }
+ }
+ return r;
}
} // namespace events