Debian Codegraph @

protolib/src/common/protoEvent.cpp

raw
1
2 #include "protoEvent.h"
3 #include "protoDebug.h"
4
5 #ifdef WIN32
6
7 #else
8 #include <string.h> // for memset()
9 #include <unistd.h>
10 #include <fcntl.h>
11 #ifdef LINUX
12 #include <sys/eventfd.h>
13 #endif // LINUX
14 #endif // if/else WIN32/UNIX
15
16 ProtoEvent::ProtoEvent(bool autoReset)
17 : auto_reset(autoReset), notifier(NULL), listener(NULL)
18 {
19 #ifdef WIN32
20 event_handle = INVALID_HANDLE_VALUE;
21 #else // UNIX
22 #if defined(USE_EVENTFD) || defined(USE_KEVENT)
23 event_fd = -1;
24 #else
25 event_pipe_fd[0] = event_pipe_fd[1] = -1;
26 #endif // if/else (USE_EVENTFD | USE_KEVENT) / pipe
27 #endif // if/else WIN32/UNIX
28 }
29
30 ProtoEvent::~ProtoEvent()
31 {
32 Close();
33 }
34
35 bool ProtoEvent::UpdateNotification()
36 {
37 if (NULL != notifier)
38 {
39 if (IsOpen())
40 {
41 if (!notifier->UpdateEventNotification(*this, NOTIFY_INPUT))
42 {
43 PLOG(PL_FATAL, "ProtoEvent::UpdateNotification() error: unable to update notifier\n");
44 return false;
45 }
46 }
47 else
48 {
49 notifier->UpdateEventNotification(*this, NOTIFY_NONE);
50 }
51 }
52 return true;
53 } // end ProtoEvent::UpdateNotification()
54
55 bool ProtoEvent::SetNotifier(ProtoEvent::Notifier* theNotifier)
56 {
57 if (notifier != theNotifier)
58 {
59 if (IsOpen())
60 {
61 // 1) Detach old notifier, if any
62 if (NULL != notifier)
63 notifier->UpdateEventNotification(*this, NOTIFY_NONE);
64 // 2) Set and update new notifier (if applicable)
65 notifier = theNotifier;
66 if (!UpdateNotification())
67 {
68 notifier = NULL;
69 return false;
70 }
71 }
72 else
73 {
74 notifier = theNotifier;
75 }
76 }
77 return true;
78 } // end ProtoEvent::SetNotifier()
79
80 void ProtoEvent::OnNotify()
81 {
82 if (NULL != listener)
83 listener->on_event(*this);
84 } // end ProtoEvent::OnNotify()
85
86 bool ProtoEvent::Open()
87 {
88 if (IsOpen()) return true;
89 #ifdef WIN32
90 // Create initially non-signalled, manual reset event
91 event_handle = CreateEvent(NULL, TRUE, FALSE, NULL);
92 if (NULL == event_handle)
93 {
94 PLOG(PL_FATAL, "ProtoEvent::Init() CreateEvent() error: %s\n", GetErrorString());
95 event_handle = INVALID_HANDLE_VALUE;
96 return false;
97 }
98 #else // UNIX
99 #if defined(USE_EVENTFD)
100 // Create eventfd() descriptor
101 if (-1 == (event_fd = eventfd(0, 0))) // TBD - should we set the flag "EFD_NONBLOCK"???
102 {
103 PLOG(PL_ERROR, "ProtoEvent::Init() eventfd() error: %s\n", GetErrorString());
104 return false;
105 }
106 #elif defined(USE_KEVENT)
107 // Create a kqueue() (this descriptor may be used by ProtoDispatcher, select(), etc.)
108 if (-1 = (event_fd = kqueue()))
109 {
110 PLOG(PL_ERROR, "ProtoEvent::Init() kqueue() error: %s\n", GetErrorString());
111 return false;
112 }
113 struct kevent kev;
114 struct kevent out_kev;
115 memset(&kev, 0, sizeof(kev));
116 kev.ident = event_fd;
117 kev.filter = EVFILT_USER;
118 // Even though we set the EV_CLEAR flag here, the ProtoEvent
119 // still behaves as manual reset since the ProtoEvent::Reset()
120 // has to be called to clear the kqueue() descriptor. This is
121 // analogous to read() on an eventfd() or event_pipe_fd ....
122 kev.flags = EV_ADD;
123 if (-1 == kevent(event_fd, &kev, 1, NULL, 0, NULL))
124 {
125 PLOG(PL_ERROR, "ProtoEvent::Init() kqueue() error: %s\n", GetErrorString());
126 close(event_fd);
127 event_fd = -1;
128 return false;
129 }
130 #else
131 if (0 != pipe(event_pipe_fd))
132 {
133 PLOG(PL_FATAL, "ProtoEvent::Init() pipe() error: %s\n", GetErrorString());
134 return false;
135 }
136 // make reading non-blocking so we can reset the pipe
137 if(-1 == fcntl(event_pipe_fd[0], F_SETFL, fcntl(event_pipe_fd[0], F_GETFL, 0) | O_NONBLOCK))
138 {
139 PLOG(PL_FATAL, "ProtoEvent::Init() fcntl(F_SETFL(O_NONBLOCK)) error: %s\n", GetErrorString());
140 close(event_pipe_fd[0]);
141 close(event_pipe_fd[1]);
142 event_pipe_fd[0] = event_pipe_fd[1] = -1;
143 return false;
144 }
145 #endif // if/else USE_EVENTFD / USE_KEVENT) / pipe
146 #endif // if/else WIN32/UNIX
147 if (NULL != notifier)
148 notifier->UpdateEventNotification(*this, NOTIFY_INPUT);
149 return true;
150 } // end ProtoEvent::Open()
151
152 bool ProtoEvent::IsOpen() const
153 {
154 #ifdef WIN32
155 return (INVALID_HANDLE_VALUE != event_handle && NULL != event_handle);
156 #else // UNIX
157 #if defined(USE_EVENTFD) || defined(USE_KEVENT)
158 return (-1 != event_fd);
159 #else
160 return (-1 != event_pipe_fd[0]);
161 #endif // if/else USE_EVENTFD / USE_KEVENT) / pipe
162 #endif // if/else WIN32/UNIX
163 } // end ProtoEvent::IsOpen()
164
165 void ProtoEvent::Close()
166 {
167 if (!IsOpen()) return;
168 if (NULL != notifier)
169 notifier->UpdateEventNotification(*this, NOTIFY_NONE);
170 #ifdef WIN32
171 CloseHandle(event_handle);
172 event_handle = NULL;
173 #else // UNIX
174 #if defined(USE_EVENTFD) || defined(USE_KEVENT)
175 close(event_fd);
176 event_fd = -1;
177 #else
178 close(event_pipe_fd[0]);
179 close(event_pipe_fd[1]);
180 event_pipe_fd[0] = event_pipe_fd[1] = -1;
181 #endif // if/else USE_EVENTFD / USE_KEVENT) / pipe
182 #endif // if/else WIN32/UNIX
183 } // end ProtoEvent::Close()
184
185
186 bool ProtoEvent::Set()
187 {
188 #ifdef WIN32
189 if (0 == SetEvent(event_handle))
190 {
191 PLOG(PL_ERROR, "ProtoEvent::Set() SetEvent() error: %s\n", GetErrorString());
192 return false;
193 }
194 #else // UNIX
195 #if defined(USE_EVENTFD)
196 uint64_t value = 1;
197 // TBD - should we make this a non-blocking call?
198 if (write(event_fd, &value, sizeof(uint64_t)) < 0)
199 {
200 PLOG(PL_ERROR, "ProtoEvent::Set() write(eventfd) error: %s\n", GetErrorString());
201 return false;
202 }
203 #elif defined (USE_KEVENT)
204 struct kevent kev;
205 EV_SET(&kev, event_fd, EVFILT_USER, 0, NOTE_TRIGGER, 0, NULL);
206 if (-1 == kevent(event_fd, &kev, 1, NULL, 0, NULL))
207 {
208 PLOG(PL_ERROR, "ProtoEvent::Set() kevent() error: %s\n", GetErrorString());
209 return false;
210 }
211 #else
212 while (true)
213 {
214 char byte = 0;
215 ssize_t result = write(event_pipe_fd[1], &byte, 1);
216 if (1 == result)
217 {
218 break;
219 }
220 else if (0 == result)
221 {
222 PLOG(PL_WARN, "ProtoEvent::Set() warning: write() returned zero\n");
223 continue;
224 }
225 else
226 {
227 if ((EINTR == errno) || (EAGAIN == errno)) continue; // (TBD) also for EAGAIN ???
228 PLOG(PL_ERROR, "ProtoEvent::Set() write(break_pipe) error: %s\n", GetErrorString());
229 return false;
230 }
231 }
232 #endif // if/else USE_EVENTFD / USE_KEVENT) / pipe
233 #endif // if/else WIN32/UNIX
234 return true;
235 } // end ProtoEvent::Set()
236
237 bool ProtoEvent::Reset()
238 {
239 #ifdef WIN32
240 if (0 == ResetEvent(event_handle))
241 {
242 PLOG(PL_ERROR, "ProtoEvent::Reset() ResetEvent() error: %s\n", GetErrorString());
243 return false;
244 }
245 #else // UNIX
246 #if defined(USE_EVENTFD)
247 // Reset "signal" by reading eventfd count
248 uint64_t counter = 0;
249 if (read(event_fd, &counter, sizeof(counter)) < 0)
250 {
251 PLOG(PL_ERROR, "ProtoEvent::Reset() read(event_fd) error: %s\n", GetErrorString());
252 return false;
253 }
254 #elif defined(USE_KEVENT)
255 struct kevent kev;
256 if (-1 == kevent(kq, NULL, 0, &kev, 1, NULL))
257 {
258 PLOG(PL_ERROR, "ProtoEvent::Reset() kevent() error: %s\n", GetErrorString());
259 return false;
260 }
261 ASSERT(event_fd == kev.ident);
262 #else
263 // Reset "signal" by emptying event_pipe
264 char byte[32];
265 while (read(event_pipe_fd[0], byte, 32) > 0);
266 #endif // if/else USE_EVENTFD / USE_KEVENT) / pipe
267 #endif // if/else WIN32/UNIX
268 return true;
269 } // end ProtoEvent::Reset()
270
271 bool ProtoEvent::Wait()
272 {
273 #ifdef WIN32
274 if (WAIT_OBJECT_0 != WaitForSingleObject(event_handle, INFINITE))
275 {
276 PLOG(PL_ERROR, "ProtoEvent::Wait() WaitForSingleObject() error: %s\n", GetErrorString());
277 return false;
278 }
279 #else
280 #if defined(USE_EVENTFD) || defined (USE_KEVENT)
281 int eventFd = event_fd;
282 #else
283 int eventFd = event_pipe_fd[0];
284 #endif
285 // a "select()" call is good enough here to wait on the event_fd
286 fd_set fdset;
287 FD_ZERO(&fdset);
288 FD_SET(eventFd ,&fdset);
289 if (-1 == select(eventFd+1,& fdset, 0, 0, 0))
290 {
291 PLOG(PL_ERROR, "ProtoEvent::Wait() select() error: %s\n", GetErrorString());
292 return false;
293 }
294 #endif // if/else WIN32/UNIX
295 if (auto_reset && !Reset())
296 {
297 PLOG(PL_ERROR, "ProtoEvent::Wait() error: Reset() failed!\n");
298 return false;
299 }
300 return true;
301 } // end ProtoEvent::Wait()