RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
soa/service/epoller.h
00001 /* epoller.h                                                       -*- C++ -*-
00002    Jeremy Barnes, 26 September 2012
00003    Copyright (c) 2012 Datacratic.  All rights reserved.
00004 
00005    Structure do allow multiplexing of FDs based upon epoll.
00006 */
00007 
00008 #ifndef __endpoint__epoller_h__
00009 #define __endpoint__epoller_h__
00010 
00011 #include <functional>
00012 #include "soa/service/async_event_source.h"
00013 
00014 struct epoll_event;
00015 
00016 namespace Datacratic {
00017 
00018 /*****************************************************************************/
00019 /* EPOLLER                                                                   */
00020 /*****************************************************************************/
00021 
00026 struct Epoller: public AsyncEventSource {
00027 
00028     Epoller();
00029 
00030     ~Epoller();
00031 
00032     void init(int maxFds);
00033 
00034     void close();
00035 
00039     void addFd(int fd, void * data = 0);
00040     
00044     void addFdOneShot(int fd, void * data = 0);
00045 
00047     void restartFdOneShot(int fd, void * data = 0);
00048 
00050     void removeFd(int fd);
00051     
00052     typedef std::function<bool (epoll_event & event)> HandleEvent;
00053     typedef std::function<void ()> OnEvent;
00054 
00056     HandleEvent handleEvent;
00057 
00058     OnEvent beforeSleep;
00059     OnEvent afterSleep;
00060 
00071     int handleEvents(int usToWait = 0, int nEvents = 1,
00072                      const HandleEvent & handleEvent = HandleEvent(),
00073                      const OnEvent & beforeSleep = OnEvent(),
00074                      const OnEvent & afterSleep = OnEvent());
00075 
00076     virtual int selectFd() const
00077     {
00078         return epoll_fd;
00079     }
00080 
00081     virtual bool poll() const;
00082 
00083     virtual bool processOne()
00084     {
00085         handleEvents();
00086         return poll();
00087     }
00088     
00089 private:
00090     /* Fd for the epoll mechanism. */
00091     int epoll_fd;
00092 };
00093 
00094 } // namespace Datacratic
00095 
00096 #endif /* __endpoint__epoller_h__ */
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator