DUDS
Distributed Update of Data from Something
Poller.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the DUDS project. It is subject to the BSD-style
3  * license terms in the LICENSE file found in the top-level directory of this
4  * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
5  * No part of DUDS, including this file, may be copied, modified, propagated,
6  * or distributed except according to the terms contained in the LICENSE file.
7  *
8  * Copyright (C) 2020 Jeff Jackowski
9  */
10 #ifndef POLLER_HPP
11 #define POLLER_HPP
12 
13 #include <sys/epoll.h>
14 #include <boost/exception/info.hpp>
15 #include <boost/noncopyable.hpp>
16 #include <mutex>
17 #include <vector>
18 #include <memory>
19 
20 #ifdef linux
21 // !@?!#?!#?
22 #undef linux
23 #endif
24 
25 namespace duds {
26 
31 namespace os {
32 
36 namespace linux {
37 
41 struct PollerError : virtual std::exception, virtual boost::exception { };
42 
48 
55 
60 
66 typedef boost::error_info<struct Info_PollerFileDescriptor, int>
68 
69 class Poller;
70 
79 public:
93  virtual void respond(Poller *poller, int fd) = 0;
94 };
95 
96 typedef std::shared_ptr<PollResponder> PollResponderSptr;
97 
117 class Poller : boost::noncopyable {
125  std::weak_ptr<PollResponder> responder;
129  int fd;
130  ResponderRecord() = default;
131  ResponderRecord(const PollResponderSptr &prs, int f) :
132  responder(prs), fd(f) { }
133  };
138  typedef std::vector<ResponderRecord> ResponderVec;
143  ResponderVec responders;
147  std::vector<int> flist;
151  std::mutex block;
155  int epfd;
156 public:
161  static constexpr int maxEvents = 32;
169  Poller(int reserveSize = 0);
173  Poller(Poller &&p);
182  ~Poller();
187  Poller &operator=(Poller &&p) noexcept;
211  void add(const PollResponderSptr &prs, int fd, int events = EPOLLIN);
227  void remove(int fd);
262  int wait(std::chrono::milliseconds timeout);
270  int wait() { // indefinite
271  return wait(std::chrono::milliseconds(-1));
272  }
278  int respond() { // no block
279  return wait(std::chrono::milliseconds(0));
280  }
281 };
282 
283 } } }
284 
285 #endif // #ifndef POLLER_HPP
Responds to a poll event.
Definition: Poller.hpp:78
std::vector< ResponderRecord > ResponderVec
Type that holds PollResponder objects and their associated file descriptors.
Definition: Poller.hpp:138
ResponderVec responders
The responders and their file descriptors.
Definition: Poller.hpp:143
int fd
The file descriptor.
Definition: Poller.hpp:129
A simple C++ interface to using Linux&#39;s epoll functions.
Definition: Poller.hpp:117
The call to epoll_create() failed.
Definition: Poller.hpp:47
std::weak_ptr< PollResponder > responder
The PollResponder held with a weak pointer.
Definition: Poller.hpp:125
int epfd
The file descriptor provided by epoll_create().
Definition: Poller.hpp:155
boost::error_info< struct Info_PollerFileDescriptor, int > PollerFileDescriptor
Poller error attribute that includes the value of the file descriptor.
Definition: Poller.hpp:67
Attempted to use a non-existent PollResponder object.
Definition: Poller.hpp:59
std::shared_ptr< PollResponder > PollResponderSptr
Definition: Poller.hpp:96
ResponderRecord(const PollResponderSptr &prs, int f)
Definition: Poller.hpp:131
int wait()
Waits indefinitely for events, only returning after an event is received.
Definition: Poller.hpp:270
std::vector< int > flist
Free spot list.
Definition: Poller.hpp:147
int respond()
Responds to events that are already waiting.
Definition: Poller.hpp:278
An operation (remove) resulted in an error from an epoll function indicating that the file descriptor...
Definition: Poller.hpp:54
Holds a PollResponder object and its associated file descriptor.
Definition: Poller.hpp:121
std::mutex block
Used to allow for thread-safe operation.
Definition: Poller.hpp:151
The base class for all Poller errors; used for general errors.
Definition: Poller.hpp:41