-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventManager.cpp
387 lines (317 loc) · 9.11 KB
/
EventManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x500
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <Windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <process.h>
#include <stdio.h>
#include <stdint.h>
#define __PORT_H__0396846
# define BVTUTIL_EXPORT
#ifndef __GNUC__
#define EPOCHFILETIME (116444736000000000i64)
#else
#define EPOCHFILETIME (116444736000000000LL)
#endif
static int gettimeofday(struct timeval *tv, struct timezone* unused)
{
FILETIME ft;
LARGE_INTEGER li;
__int64 t;
(void) unused;
if (tv)
{
/* WARNING: This function's granularity is quite coarse, especially on Windows XP,
where the time may update only every 10-15 milliseconds.
TODO: Use a timer with finer granularity */
GetSystemTimeAsFileTime(&ft);
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
t = li.QuadPart; /* In 100-nanosecond intervals */
t -= EPOCHFILETIME; /* Offset to the Epoch time */
t /= 10; /* In microseconds */
tv->tv_sec = (long)(t / 1000000);
tv->tv_usec = (long)(t % 1000000);
}
return 0;
}
#ifdef _WIN32
#include <string>
static std::wstring WSAGetErrorString(DWORD errorCode)
{
LPWSTR *wszSockError = NULL;
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
errorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&wszSockError,
0,
NULL);
std::wstring error((LPWSTR)wszSockError);
LocalFree(wszSockError);
return error;
}
#endif
#include <errno.h>
#include "EventManager.hpp"
#include "Lockable.hpp"
#include "ScopedLock.hpp"
#include <algorithm>
#include <string.h>
#define BVT_LOG printf
///////////////////////////////////////////////////////////////////////////////
// input is in relative time
TimerEventBase::TimerEventBase(long seconds, bool reschedule)
{
m_seconds = seconds;
m_reschedule = reschedule;
gettimeofday(&m_tv, NULL);
// switch from relative to absolute
m_tv.tv_sec += m_seconds;
m_tv.tv_usec = 0;
}
TimerEventBase::~TimerEventBase()
{
}
struct timeval& TimerEventBase::time()
{
return m_tv;
}
long TimerEventBase::seconds() const
{
return m_tv.tv_sec;
}
bool TimerEventBase::reschedule()
{
if (m_reschedule)
{
::gettimeofday(&m_tv, NULL);
// switch from relative to absolute
m_tv.tv_sec += m_seconds;
m_tv.tv_usec = 0;
}
return m_reschedule;
}
bool operator<(const TimerEventBase& x, const TimerEventBase& y)
{
return (x.seconds() < y.seconds()) ? true : false;
}
///////////////////////////////////////////////////////////////////////////////
void FDEventBase::ReadableEvent(EventManager*)
{
}
///////////////////////////////////////////////////////////////////////////////
static inline void pair(SOCKET fds[2])
{
// TODO: consider socketpair on Linux
struct sockaddr_in inaddr;
struct sockaddr addr;
#ifdef _WIN32
const int domain = AF_INET;
#else
const int domain = AF_LOCAL;
#endif
const SOCKET listener = socket(domain, SOCK_STREAM, 0);
memset(&inaddr, 0, sizeof(inaddr));
memset(&addr, 0, sizeof(addr));
inaddr.sin_family = domain;
inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
inaddr.sin_port = 0;
const int yes = 1;
setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, sizeof(yes));
bind(listener, (struct sockaddr *)&inaddr, sizeof(inaddr));
listen(listener, 1);
socklen_t len = sizeof(inaddr);
getsockname(listener, &addr, &len);
fds[0] = socket(domain, SOCK_STREAM, 0);
connect(fds[0], &addr, len);
fds[1] = accept(listener, 0, 0);
closesocket(listener);
struct linger dontlinger;
dontlinger.l_onoff = 1;
dontlinger.l_linger = 0;
setsockopt(fds[0], SOL_SOCKET, SO_LINGER, (const char*)&dontlinger, sizeof(dontlinger));
setsockopt(fds[1], SOL_SOCKET, SO_LINGER, (const char*)&dontlinger, sizeof(dontlinger));
}
EventManager::EventManager()
{
}
EventManager::~EventManager()
{
// TODO: need stop(); ?
}
void EventManager::add(FDEventBase* val)
{
if( val == NULL )
return;
ScopedLock _(&m_lock);
m_events.push_back(val);
}
void EventManager::remove(FDEventBase* val)
{
if( val == NULL )
return;
ScopedLock _(&m_lock);
EventList_t::iterator i = m_events.begin();
while (i != m_events.end())
{
EventList_t::iterator j = i;
i++;
if ((*j) == val)
{
m_events.erase(j);
break;
}
}
}
#ifdef _WIN32
static void __stdcall UserAPC_CloseSocket(ULONG_PTR userData)
{
SOCKET *sock = reinterpret_cast<SOCKET*>(userData);
SOCKET s = *sock;
closesocket(s);
*sock = INVALID_SOCKET;
}
#endif
static const char kCancelChar[] = "??????????";
static const int kCancelLength = 1;
RetVal EventManager::start()
{
m_pipelock.Lock();
pair(m_pipe);
if (m_pipe[0] == INVALID_SOCKET || m_pipe[1] == INVALID_SOCKET)
{
m_pipelock.Unlock();
return ReturnCode::FAILED;
}
m_pipelock.Unlock();
return Thread::start();
}
void EventManager::stop()
{
m_start_mutex.Lock();
const bool local_running = m_running;
m_start_mutex.Unlock();
#ifdef _WIN32
// HANDLE threadHandle = Thread::m_tid;
// ULONG_PTR userData = reinterpret_cast<ULONG_PTR>(&m_cancelSocket);
// QueueUserAPC(UserAPC_CloseSocket, threadHandle, userData);
#endif
// NB: if you called closesocket on m_cancelSocket's FD it could be immediately reused
// Furthermore, assigning INVALID_SOCKET to m_cancelSocket is a race
if (local_running)
{
printf("Writing to cancel\n");
int sent = send(m_pipe[1], kCancelChar, 1, 0);
printf("Wrote %d bytes to cancel\n", sent);
}
// NB: Thread::stop blocks, so we have to issue the socket-based cancellation beforehand.
// This means that the event loop can busy spin while Thread::running() keeps returning true!
// TODO: Add Thread::beginStop() and move before cancellation.
Thread::stop();
}
#ifdef _WIN32
# define SOCKETMAX(a, b) 1
#else
# define SOCKETMAX(a, b) MAX((a), (b))
#endif
void EventManager::Run()
{
printf("RUN START\n");
SOCKET local_pipe[2];
m_pipelock.Lock();
memcpy(local_pipe, m_pipe, sizeof(SOCKET) * 2);
m_pipelock.Unlock();
LARGE_INTEGER run_start;
QueryPerformanceCounter(&run_start);
RetVal status = ReturnCode::SUCCESS;
//while( running() && (status == ReturnCode::SUCCESS) )
while( (status == ReturnCode::SUCCESS) )
{
//m_lock.Lock();
// set fds bits and determine highest file descriptor
fd_set fds;
FD_ZERO(&fds);
int fd_max = 0;
for (EventList_t::iterator i = m_events.begin(); i != m_events.end(); i++)
{
const FDEventBase* ev = *i;
if (ev == NULL) // TODO: How can ev be NULL?
continue;
const SOCKET fd = ev->fd();
if (fd == INVALID_SOCKET)
continue;
fd_max = SOCKETMAX(fd_max, fd);
// HACK:
// HACK: FD_SET(fd, &fds);
}
struct timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 500 * 1000;
//m_lock.Unlock();
FD_SET(local_pipe[0], &fds);
fd_max = SOCKETMAX(fd_max, local_pipe[0]);
// Windows' select MUST have a valid fd, but the first (fd_max) parameter is ignored
const int ret = select(fd_max + 1, &fds, NULL, &fds, &tv);
//m_lock.Lock();
switch(ret)
{
case 0: // timedout
//BVT_LOG(LOG_TRACE, "EventManager - select timeout");
printf("EventManager - select timeout\n");
{
std::wstring errorstring = WSAGetErrorString(WSAGetLastError());
wprintf(L"Error: %s\n", errorstring.c_str());
}
status = ReturnCode::SUCCESS;
break;
case -1: // error
//BVT_LOG(LOG_DEBUG, "EventManager - select returned -1 %d %s", errno, strerror(errno));
printf("EventManager - select failed\n");
if (errno != EINTR)
status = ReturnCode::SOCKET_READ;
break;
default: // data
// printf("EventManager - select returned at least one FD ready\n");
for (EventList_t::iterator i = m_events.begin(); i != m_events.end(); i++)
{
SOCKET fd = (*i)->fd();
if (fd != INVALID_SOCKET && FD_ISSET(fd, &fds)) // NB: fd could be zero and valid if stdin is closed before a socket is created!
{
//BVT_LOG(LOG_DEBUG, "EventManager - got data on fd %d", (*i)->fd() );
(*i)->ReadableEvent(this);
}
}
if (FD_ISSET(local_pipe[0], &fds))
{
char discard;
LARGE_INTEGER start, stop;
QueryPerformanceCounter(&start);
if (kCancelLength != recv(local_pipe[0], &discard, kCancelLength, 0))
{
BVT_LOG("EventManager cancel socket error\n");
}
else
{
Sleep(1);
}
// Sleep(1);
QueryPerformanceCounter(&stop);
goto LABEL_DONE; // HACK: skips unlock
}
}
// Sleep(1);
//m_lock.Unlock();
}
LABEL_DONE:
LARGE_INTEGER run_stop;
QueryPerformanceCounter(&run_stop);
LONGLONG total = run_stop.QuadPart - run_start.QuadPart;
printf("run total: %ld\n", total);
closesocket(local_pipe[0]);
closesocket(local_pipe[1]);
// if( status != BVT::ReturnCode::SUCCESS )
BVT_LOG("EventManager::Run() exiting: retval:%d errno:%d\n", status, errno);
}