scclib
Stable Cloud Computing C++ Library
poller.h
Go to the documentation of this file.
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #ifndef _SYS_UTIL_POLLER_H
32 #define _SYS_UTIL_POLLER_H
33 
34 #include <map>
35 #include <chrono>
36 
37 namespace scc::util
38 {
39 
65 class Poller
66 {
67  int m_fd;
68  std::map<int, int> m_polls;
69  std::map<int, int> m_events;
70  void wait(int);
71  int converteflags(int);
72 
73 public:
75  enum PollFlag
76  {
77  input = 0x1,
78  output = 0x2,
79  read_hup = 0x4,
80  priority = 0x8,
81  hup = 0x10,
82  error = 0x20
83  };
84 
85  Poller();
86  virtual ~Poller();
87 
88  Poller(const Poller&) = delete; // no copying
89  void operator=(const Poller&) = delete;
90 
91  Poller(Poller&&) = delete; // no move
92  const Poller& operator=(Poller&&) = delete;
93 
101  void set(int, int);
102 
107  void remove(int);
108 
110  void wait()
111  {
112  wait(-1);
113  }
114 
116  void wait(std::chrono::milliseconds t)
117  {
118  wait(t.count());
119  }
120 
127  int event(int);
128 };
131 }
132 
133 #endif
Poller which allows polling of generic file descriptors for various events.
Definition: poller.h:66
void set(int, int)
Add a file desriptor to poller.
Definition: poller.cc:54
void remove(int)
Remove a file descriptor from poller.
Definition: poller.cc:101
PollFlag
Polling flags.
Definition: poller.h:76
@ priority
There is some exceptional condition on the file descriptor. E.g. There is out-of-band data on a TCP s...
Definition: poller.h:80
@ error
Error condition.
Definition: poller.h:82
@ output
File descriptor is available for write operations.
Definition: poller.h:78
@ hup
Hang up. The local or peer connection has been closed.
Definition: poller.h:81
@ read_hup
For stream sockets, peer connection (or local connection) has been shut down.
Definition: poller.h:79
@ input
File descriptor is available for read operations.
Definition: poller.h:77
void wait()
Wait forever for an event.
Definition: poller.h:110
int event(int)
Return flags which were polled for this file descriptor.
Definition: poller.cc:171
void wait(std::chrono::milliseconds t)
Wait for a number of milliseconds for an event.
Definition: poller.h:116