#include <errno.h>
#include <sys/epoll.h>
#include "Utils/Exception.hh"
+#include "Utils/MicroTime.hh"
static const int EPollInitialSize = 16;
return instance;
}
+prefix_ void satcom::lib::Scheduler::timeout(unsigned long timeout, TimerCallback const & cb)
+{
+ timerQueue_.push(TimerSpec(now()+1000*timeout,cb));
+}
+
prefix_ satcom::lib::Scheduler::Scheduler()
: epollFd_(epoll_create(EPollInitialSize))
{
throw SystemException(errno);
}
-prefix_ void satcom::lib::Scheduler::do_add(int fd, InternalCallback const & cb, EventId eventMask)
+prefix_ void satcom::lib::Scheduler::do_add(int fd, SimpleCallback const & cb, int eventMask)
{
FdTable::iterator i (fdTable_.find(fd));
int action (EPOLL_CTL_MOD);
throw SystemException(errno);
}
-prefix_ void satcom::lib::Scheduler::do_remove(int fd, EventId eventMask)
+prefix_ void satcom::lib::Scheduler::do_remove(int fd, int eventMask)
{
FdTable::iterator i (fdTable_.find(fd));
if (i == fdTable_.end())
memset(&ev,0,sizeof(ev));
ev.events = i->second.epollMask();
ev.data.fd = fd;
-
+
int action (EPOLL_CTL_MOD);
if (ev.events==0) {
action = EPOLL_CTL_DEL;
throw SystemException(errno);
}
+
prefix_ int satcom::lib::Scheduler::EventSpec::epollMask()
const
{
{
terminate_ = false;
while (! terminate_) {
+
+ MicroTime timeNow = now();
+ while ( ! timerQueue_.empty() && timerQueue_.top().timeout <= timeNow ) {
+ timerQueue_.top().cb();
+ timerQueue_.pop();
+ }
+ if (terminate_)
+ return;
+ int timeout = timerQueue_.empty() ? -1 : int((timerQueue_.top().timeout - timeNow)/1000);
+
struct epoll_event ev;
- int events = epoll_wait(epollFd_, &ev, 1, 1000);
+ int events = epoll_wait(epollFd_, &ev, 1, timeout);
if (events<0)
// Hmm ... man epoll says, it will NOT return with EINTR ??
throw SystemException(errno);
if (events==0)
+ // Timeout .. it will be run when reachiung the top of the loop
continue;
FdTable::iterator i = fdTable_.find(ev.data.fd);
}
else if (ev.events & EPOLLHUP) {
- BOOST_ASSERT(spec.cb_hup);
- spec.cb_hup(EV_HUP);
+ if (spec.cb_hup)
+ spec.cb_hup(EV_HUP);
+ else if (ev.events & EPOLLERR) {
+ if (spec.cb_write) spec.cb_write(EV_HUP);
+ if (spec.cb_read) spec.cb_read(EV_HUP);
+ }
}
- else if (ev.events & EPOLLERR) {
- BOOST_ASSERT(spec.cb_err);
- spec.cb_err(EV_ERR);
+ else if (ev.events & EPOLLERR && ! ev.events & EPOLLHUP) {
+ if (spec.cb_err)
+ spec.cb_err(EV_ERR);
+ else {
+ if (spec.cb_write) spec.cb_write(EV_ERR);
+ if (spec.cb_read) spec.cb_read(EV_ERR);
+ }
}
+
}
}