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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
#include "usuals.h"
#include <errno.h>
#include <sys/time.h>
#include "server-mainloop.h"
typedef struct _socket_callback
{
int fd;
server_socket_callback callback;
void* arg;
struct _socket_callback* next;
}
socket_callback;
typedef struct _timer_callback
{
struct timeval at;
struct timeval interval;
server_timer_callback callback;
void* arg;
struct _timer_callback* next;
}
timer_callback;
typedef struct _server_context
{
int stopped;
fd_set read_fds;
fd_set write_fds;
int max_fd;
socket_callback* callbacks;
timer_callback* timers;
}
server_context;
/* Global context */
static server_context ctx;
static void
timeval_add(struct timeval* t1, struct timeval* t2)
{
ASSERT(t1->tv_usec < 1000000);
ASSERT(t2->tv_usec < 1000000);
t1->tv_sec += t2->tv_sec;
t1->tv_usec += t2->tv_usec;
if(t1->tv_usec >= 1000000)
{
t1->tv_usec -= 1000000;
t1->tv_sec += 1;
}
}
static void
timeval_subtract(struct timeval* t1, struct timeval* t2)
{
ASSERT(t1->tv_usec < 1000000);
ASSERT(t2->tv_usec < 1000000);
t1->tv_sec -= t2->tv_sec;
if(t1->tv_usec < t2->tv_usec)
{
t1->tv_usec += 1000000;
t1->tv_sec -= 1;
}
t1->tv_usec -= t2->tv_usec;
}
static int
timeval_compare(struct timeval* t1, struct timeval* t2)
{
ASSERT(t1->tv_usec < 1000000);
ASSERT(t2->tv_usec < 1000000);
if(t1->tv_sec > t2->tv_sec)
return 1;
else if(t1->tv_sec < t2->tv_sec)
return -1;
else
{
if(t1->tv_usec > t2->tv_usec)
return 1;
else if(t1->tv_usec < t2->tv_usec)
return -1;
else
return 0;
}
}
#define timeval_empty(tv) \
((tv)->tv_sec == 0 && (tv)->tv_usec == 0)
#define timeval_to_ms(tv) \
((((uint64_t)(tv).tv_sec) * 1000L) + (((uint64_t)(tv).tv_usec) / 1000L))
#define timeval_dump(tv) \
(fprintf(stderr, "{ %d:%d }", (uint)((tv)->tv_sec), (uint)((tv)->tv_usec / 1000))
static int
add_timer(int ms, int oneshot, server_timer_callback callback, void* arg)
{
struct timeval interval;
timer_callback* cb;
ASSERT(ms > 0);
ASSERT(callback != NULL);
interval.tv_sec = ms / 1000;
interval.tv_usec = (ms % 1000) * 1000; /* into micro seconds */
cb = (timer_callback*)calloc(1, sizeof(*cb));
if(!cb)
{
errno = ENOMEM;
return -1;
}
if(gettimeofday(&(cb->at), NULL) == -1)
{
free(cb);
return -1;
}
timeval_add(&(cb->at), &interval);
if (oneshot)
memset(&(cb->interval), 0, sizeof(cb->interval));
else
memcpy(&(cb->interval), &interval, sizeof(cb->interval));
cb->callback = callback;
cb->arg = arg;
cb->next = ctx.timers;
ctx.timers = cb;
return 0;
}
static timer_callback*
remove_timer(timer_callback* timcb)
{
timer_callback* cb;
timer_callback* next;
if(!ctx.timers)
return NULL;
/* First in list */;
if(ctx.timers == timcb)
{
cb = ctx.timers;
ctx.timers = ctx.timers->next;
free(cb);
return ctx.timers;
}
/* One ahead processing of rest */
for(cb = ctx.timers; cb->next; cb = cb->next)
{
if(cb->next == timcb)
{
next = cb->next->next;
free(cb->next);
cb->next = next;
return cb->next;
}
}
/* Couldn't remove, return self */
return timcb;
}
void
server_init()
{
memset(&ctx, 0, sizeof (ctx));
FD_ZERO(&ctx.read_fds);
FD_ZERO(&ctx.write_fds);
ctx.max_fd = -1;
ctx.stopped = 1;
ctx.callbacks = NULL;
ctx.timers = NULL;
}
void
server_uninit()
{
timer_callback* timcb;
timer_callback* timn;
socket_callback* sockcb;
socket_callback* sockn;
for(timcb = ctx.timers; timcb; timcb = timn)
{
timn = timcb->next;
free(timcb);
}
ctx.timers = NULL;
for(sockcb = ctx.callbacks; sockcb; sockcb = sockn)
{
sockn = sockcb->next;
free(sockcb);
}
ctx.timers = NULL;
}
uint64_t
server_get_time()
{
struct timeval tv;
if(gettimeofday(&tv, NULL) == -1)
return 0L;
return timeval_to_ms(tv);
}
int
server_run()
{
struct timeval* timeout;
struct timeval tv, current;
timer_callback* timcb;
socket_callback* sockcb;
fd_set rfds, wfds;
int r;
/* No watches have been set */
ASSERT(ctx.max_fd > -1);
ctx.stopped = 0;
while(!ctx.stopped)
{
/* Watch for the various fds */
memcpy(&rfds, &ctx.read_fds, sizeof(rfds));
memcpy(&wfds, &ctx.write_fds, sizeof(wfds));
/* Prepare for timers */
timeout = NULL;
if(gettimeofday(¤t, NULL) == -1)
return -1;
/* Cycle through timers */
for(timcb = ctx.timers; timcb; )
{
ASSERT(timcb->callback);
/* Call any timers that have already passed */
if(timeval_compare(¤t, &timcb->at) >= 0)
{
/* Convert to milliseconds, and make the call */
r = (timcb->callback)(timeval_to_ms(current), timcb->arg);
/* Reset timer if so desired */
if (r == 1 && !timeval_empty(&timcb->interval))
{
timeval_add(&timcb->at, &timcb->interval);
/* If the time has already passed, just use current time */
if(timeval_compare(&(timcb->at), ¤t) <= 0)
memcpy(&(timcb->at), ¤t, sizeof(timcb->at));
}
/* Otherwise remove it. Either one shot, or returned 0 */
else
{
timcb = remove_timer(timcb);
continue;
}
}
/* Get soonest timer */
if (!timeout || timeval_compare(timeout, &timcb->at) < 0)
timeout = &timcb->at;
timcb = timcb->next;
}
/* Convert to an offset */
if(timeout)
{
memcpy(&tv, timeout, sizeof(tv));
timeout = &tv;
timeval_subtract(timeout, ¤t);
}
/* fprintf(stderr, "selecting with timeout: ");
timeval_dump(timeout);
fprintf(stderr, "\n"); */
r = select(ctx.max_fd, &rfds, &wfds, NULL, timeout);
if (r < 0)
{
/* Interrupted so try again, and possibly exit */
if (errno == EINTR)
continue;
/* Programmer errors */
ASSERT (errno != EBADF);
ASSERT (errno != EINVAL);
return r;
}
/* Timeout, just jump to timeout processing */
if(r == 0)
continue;
for(sockcb = ctx.callbacks; sockcb; sockcb = sockcb->next)
{
ASSERT(sockcb->fd != -1);
/* Call any that are set */
if (FD_ISSET(sockcb->fd, &rfds))
(sockcb->callback)(sockcb->fd, SERVER_READ, sockcb->arg);
if (FD_ISSET(sockcb->fd, &wfds))
(sockcb->callback)(sockcb->fd, SERVER_WRITE, sockcb->arg);
}
}
return 0;
}
void
server_stop()
{
ctx.stopped = 1;
}
int
server_stopped()
{
return ctx.stopped;
}
int
server_watch(int fd, int type, server_socket_callback callback, void* arg)
{
socket_callback* cb;
ASSERT(type != 0);
ASSERT(fd != -1);
ASSERT(callback != NULL);
cb = (socket_callback*)calloc(sizeof(*cb), 1);
if(!cb)
{
errno = ENOMEM;
return -1;
}
cb->fd = fd;
cb->callback = callback;
cb->arg = arg;
cb->next = ctx.callbacks;
ctx.callbacks = cb;
if (type & SERVER_READ)
FD_SET(fd, &ctx.read_fds);
if (type & SERVER_WRITE)
FD_SET(fd, &ctx.write_fds);
if(fd >= ctx.max_fd)
ctx.max_fd = fd + 1;
return 0;
}
void
server_unwatch(int fd)
{
socket_callback* cb;
ASSERT(fd != -1);
FD_CLR(fd, &ctx.read_fds);
FD_CLR(fd, &ctx.write_fds);
if(!ctx.callbacks)
return;
/* First in list */;
if(ctx.callbacks->fd == fd)
{
cb = ctx.callbacks;
ctx.callbacks = cb->next;
free(cb);
return;
}
/* One ahead processing of rest */
for(cb = ctx.callbacks; cb->next; cb = cb->next)
{
if(cb->next->fd == fd)
{
cb->next = cb->next->next;
free(cb->next);
return;
}
}
}
int
server_timer(int ms, server_timer_callback callback, void* arg)
{
return add_timer(ms, 0, callback, arg);
}
int
server_oneshot(int ms, server_timer_callback callback, void* arg)
{
return add_timer(ms, 1, callback, arg);
}
|