-
Notifications
You must be signed in to change notification settings - Fork 7
/
poll.c
329 lines (266 loc) · 9.18 KB
/
poll.c
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
// Copyright (C) 2018, Jaguar Land Rover
// This program is licensed under the terms and conditions of the
// Mozilla Public License, version 2.0. The full text of the
// Mozilla Public License is at https://www.mozilla.org/MPL/2.0/
//
// Author: Magnus Feuer ([email protected])
#if (!defined(__linux__) && !defined(__ANDROID__)) ||defined(USE_POLL)
#include "dstc.h"
#include "dstc_internal.h"
#include <stdlib.h>
#include <rmc_log.h> // From reliable multicast packet
#include <errno.h>
#include "uthash.h"
static poll_elem_t* find_free_poll_elem_index(dstc_context_t* ctx)
{
int ind = 0;
while(ind < sizeof(ctx->poll_elem_array) / sizeof(ctx->poll_elem_array[0])) {
// Check if file descriptor is unused
if (ctx->poll_elem_array[ind].pfd.fd == -1)
return &ctx->poll_elem_array[ind];
RMC_LOG_DEBUG("Poll element %d = %d", ind, ctx->poll_elem_array[ind].pfd.fd);
ind++;
}
// Out of mem
return 0;
}
static void poll_add(user_data_t user_data,
int descriptor,
uint32_t event_user_data,
rmc_poll_action_t action)
{
dstc_context_t* ctx = (dstc_context_t*) user_data.ptr;
poll_elem_t* pelem = 0;
_dstc_lock_context(ctx);
// Do we already have it in our poll set?
HASH_FIND_INT(ctx->poll_hash, &descriptor, pelem);
if (pelem) {
RMC_LOG_INDEX_FATAL(FROM_POLL_EVENT_USER_DATA(event_user_data), "File descriptor %d already in poll set\n", descriptor);
exit(255);
}
pelem = find_free_poll_elem_index(ctx);
if (!pelem) {
RMC_LOG_INDEX_FATAL(FROM_POLL_EVENT_USER_DATA(event_user_data), "Out of poll_elem_t elements. Rebuild with larger DSTC_MAX_CONNECTIONS");
exit(255);
}
pelem->user_data = event_user_data;
pelem->pfd.events = 0;
pelem->pfd.revents = 0;
pelem->pfd.fd = descriptor;
if (action & RMC_POLLREAD)
pelem->pfd.events |= POLLIN;
if (action & RMC_POLLWRITE)
pelem->pfd.events |= POLLOUT;
HASH_ADD_INT(ctx->poll_hash, pfd.fd, pelem);
RMC_LOG_COMMENT("poll_add(%d) %s read[%c] write[%c] user_data[%X]\n",
descriptor,
IS_PUB(event_user_data)?"pub":"sub",
((action & RMC_POLLREAD)?'y':'n'),
((action & RMC_POLLWRITE)?'y':'n'),
FROM_POLL_EVENT_USER_DATA(event_user_data));
_dstc_unlock_context(ctx);
}
void poll_add_sub(user_data_t user_data,
int descriptor,
rmc_index_t index,
rmc_poll_action_t action)
{
poll_add(user_data, descriptor, TO_POLL_EVENT_USER_DATA(index, 0), action);
}
void poll_add_pub(user_data_t user_data,
int descriptor,
rmc_index_t index,
rmc_poll_action_t action)
{
poll_add(user_data, descriptor, TO_POLL_EVENT_USER_DATA(index, 1), action);
}
static void poll_modify(user_data_t user_data,
int descriptor,
uint32_t event_user_data,
rmc_poll_action_t old_action,
rmc_poll_action_t new_action)
{
dstc_context_t* ctx = (dstc_context_t*) user_data.ptr;
poll_elem_t* pelem = 0;
if (old_action == new_action)
return ;
_dstc_lock_context(ctx);
// Does it even exist in our poll set.
HASH_FIND_INT(ctx->poll_hash, &descriptor, pelem);
if (!pelem) {
RMC_LOG_INDEX_FATAL(event_user_data, "File descriptor %d not found in poll set\n", descriptor);
exit(255);
}
pelem->pfd.events = 0;
pelem->pfd.revents = 0;
if (new_action & RMC_POLLREAD)
pelem->pfd.events |= POLLIN;
if (new_action & RMC_POLLWRITE)
pelem->pfd.events |= POLLOUT;
RMC_LOG_COMMENT("poll_modify(%d) read[%c] write[%c]\n",
descriptor,
((new_action & RMC_POLLREAD)?'y':'n'),
((new_action & RMC_POLLWRITE)?'y':'n'));
_dstc_unlock_context(ctx);
}
void poll_modify_pub(user_data_t user_data,
int descriptor,
rmc_index_t index,
rmc_poll_action_t old_action,
rmc_poll_action_t new_action)
{
poll_modify(user_data,
descriptor,
TO_POLL_EVENT_USER_DATA(index, 1),
old_action,
new_action);
}
void poll_modify_sub(user_data_t user_data,
int descriptor,
rmc_index_t index,
rmc_poll_action_t old_action,
rmc_poll_action_t new_action)
{
poll_modify(user_data,
descriptor,
TO_POLL_EVENT_USER_DATA(index, 0),
old_action,
new_action);
}
void poll_remove(user_data_t user_data,
int descriptor,
rmc_index_t index)
{
dstc_context_t* ctx = (dstc_context_t*) user_data.ptr;
poll_elem_t* pelem = 0;
_dstc_lock_context(ctx);
// Does it even exist in our poll set.
HASH_FIND_INT(ctx->poll_hash, &descriptor, pelem);
if (!pelem) {
RMC_LOG_FATAL("File descriptor %d not found in poll set\n", descriptor);
exit(255);
}
RMC_LOG_COMMENT("poll_remove(%d)\n",
descriptor);
HASH_DEL(ctx->poll_hash, pelem);
pelem->pfd.events = 0;
pelem->pfd.revents = 0;
pelem->pfd.fd = -1;
_dstc_unlock_context(ctx);
}
static void _dstc_process_poll_result(dstc_context_t* ctx,
struct pollfd* event)
{
uint8_t op_res = 0;
poll_elem_t* pelem = 0;
// Does it even exist in our poll set.
HASH_FIND_INT(ctx->poll_hash, &event->fd, pelem);
if (!pelem) {
RMC_LOG_INFO("File descriptor %d not found in poll set. Probably deleted by other thread\n", event->fd);
return;
}
rmc_index_t c_ind = (rmc_index_t) FROM_POLL_EVENT_USER_DATA(pelem->user_data);
int is_pub = IS_PUB(pelem->user_data);
RMC_LOG_INDEX_DEBUG(c_ind, "desc[%d/%d] ind[%d] user_data[%u] %s:%s%s%s",
event->fd,
pelem->pfd.fd,
c_ind,
FROM_POLL_EVENT_USER_DATA(pelem->user_data),
(is_pub?"pub":"sub"),
((event->revents & POLLIN)?" read":""),
((event->revents & POLLOUT)?" write":""),
((event->revents & POLLHUP)?" disconnect":""));
if (event->revents & POLLIN) {
if (is_pub)
rmc_pub_read(ctx->pub_ctx, c_ind, &op_res);
else
rmc_sub_read(ctx->sub_ctx, c_ind, &op_res);
}
if (event->revents & POLLOUT) {
if (is_pub) {
op_res = rmc_pub_write(ctx->pub_ctx, c_ind, &op_res);
if (op_res != 0 && op_res != ENODATA)
rmc_pub_close_connection(ctx->pub_ctx, c_ind);
} else {
op_res = rmc_sub_write(ctx->sub_ctx, c_ind, &op_res);
if (op_res != 0 && op_res != ENODATA)
rmc_sub_close_connection(ctx->sub_ctx, c_ind);
}
}
}
int _dstc_process_single_event(dstc_context_t* ctx, int timeout_msec)
{
struct pollfd pfd[DSTC_MAX_CONNECTIONS];
int n_hits = 0;
int n_events = 0;
int ind = 0;
poll_elem_t* iter;
iter = ctx->poll_hash;
// Fill out pfd.
// SLOW!
while(iter) {
pfd[n_events] = iter->pfd;
pfd[n_events].revents = 0;
iter = iter->hh.next;
n_events++;
}
_dstc_unlock_context(ctx);
do {
errno = 0;
n_hits = poll(pfd, n_events, timeout_msec);
} while(n_hits == -1 && errno == EINTR);
if (n_hits == -1) {
RMC_LOG_FATAL("poll(): %s", strerror(errno));
exit(255);
}
_dstc_lock_context(ctx);
// Timeout
if (n_hits == 0)
return ETIME;
// Process all pending event.s
while(ind < n_events && n_hits) {
if (pfd[ind].revents) {
_dstc_process_poll_result(ctx, &pfd[ind]);
n_hits--;
}
++ind;
}
return 0;
}
int dstc_retrieve_pollfd_vector(struct pollfd* result,
int max_result,
int* stored_result)
{
int n_events = 0;
poll_elem_t* iter;
extern dstc_context_t _dstc_default_context;
// Prep for future, caller-provided contexct.
dstc_context_t* ctx = &_dstc_default_context;
_dstc_lock_and_init_context(ctx);
iter = ctx->poll_hash;
// Fill out pfd.
// SLOW!
while(iter && n_events < max_result) {
result[n_events] = iter->pfd;
result[n_events].revents = 0;
iter = iter->hh.next;
n_events++;
}
if (iter && n_events == max_result) {
_dstc_unlock_context(ctx);
return ENOMEM;
}
*stored_result = n_events;
_dstc_unlock_context(ctx);
return 0;
}
void dstc_process_poll_result(struct pollfd* event)
{
extern dstc_context_t _dstc_default_context;
// Prep for future, caller-provided contexct.
dstc_context_t* ctx = &_dstc_default_context;
_dstc_lock_and_init_context(ctx);
_dstc_process_poll_result(ctx, event);
_dstc_unlock_context(ctx);
}
#endif // Not linux or android