-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathio-mainloop.c
423 lines (341 loc) · 8.64 KB
/
io-mainloop.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
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
421
422
423
/**
* @file io-mainloop.c
* @brief set of function to manage io part of the mainloop feature
* @see mainloop.c
* @author Gilbert Brault
* @copyright Gilbert Brault 2015
* the original work comes from bluez v5.39
* value add: documenting main features
*
*/
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2012-2014 Intel Corporation. All rights reserved.
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include "mainloop.h"
#include "util.h"
#include "io.h"
/**
* @brief data structure to manage io
*/
struct io {
int ref_count; /**< number of references to the data structure */
int fd; /**< file descriptor */
uint32_t events; /**< epoll events (might be ored) */
bool close_on_destroy; /**< do you need to close the underlying socket on destroy? */
io_callback_func_t read_callback; /**< read call back */
io_destroy_func_t read_destroy; /**< data management for read */
void *read_data; /**< user pointer for read data */
io_callback_func_t write_callback; /**< write call back*/
io_destroy_func_t write_destroy; /**< data management for write */
void *write_data; /**< user pointer for write data */
io_callback_func_t disconnect_callback; /**< call back on disconnect */
io_destroy_func_t disconnect_destroy; /**< data management at disconnect */
void *disconnect_data; /**< user pointer for disconnect */
};
/**
* Increment &io->ref_count
* @param io point to io data structure
* @return
*/
static struct io *io_ref(struct io *io)
{
if (!io)
return NULL;
__sync_fetch_and_add(&io->ref_count, 1);
return io;
}
/**
* Decrement &io->ref_count and free io data structure when count reaches 0
* @param io point to io data structure
* @return
*/
static void io_unref(struct io *io)
{
if (!io)
return;
if (__sync_sub_and_fetch(&io->ref_count, 1))
return;
free(io);
}
/**
* io data structure house keeping
* @param user_data pointer to io data structure
*/
static void io_cleanup(void *user_data)
{
struct io *io = user_data;
if (io->write_destroy)
io->write_destroy(io->write_data);
if (io->read_destroy)
io->read_destroy(io->read_data);
if (io->disconnect_destroy)
io->disconnect_destroy(io->disconnect_data);
if (io->close_on_destroy)
close(io->fd);
io->fd = -1;
}
/**
* depending on events epoll event and read, write or disconnect prepare for the appropriate call back calling mainloop_modify_fd
*
* epoll event value (some 'ored' combination allowed) EPOLLRDHUP | EPOLLHUP | EPOLLERR | EPOLLIN | EPOLLOUT )
*
* @param fd file descriptor data structure (can be socket)
* @param events epoll event
* @param user_data pointer to io data structure
*/
static void io_callback(int fd, uint32_t events, void *user_data)
{
struct io *io = user_data;
io_ref(io);
if ((events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR))) {
io->read_callback = NULL;
io->write_callback = NULL;
if (!io->disconnect_callback) {
mainloop_remove_fd(io->fd);
io_unref(io);
return;
}
if (!io->disconnect_callback(io, io->disconnect_data)) {
if (io->disconnect_destroy)
io->disconnect_destroy(io->disconnect_data);
io->disconnect_callback = NULL;
io->disconnect_destroy = NULL;
io->disconnect_data = NULL;
io->events &= ~EPOLLRDHUP;
mainloop_modify_fd(io->fd, io->events);
}
}
if ((events & EPOLLIN) && io->read_callback) {
if (!io->read_callback(io, io->read_data)) {
if (io->read_destroy)
io->read_destroy(io->read_data);
io->read_callback = NULL;
io->read_destroy = NULL;
io->read_data = NULL;
io->events &= ~EPOLLIN;
mainloop_modify_fd(io->fd, io->events);
}
}
if ((events & EPOLLOUT) && io->write_callback) {
if (!io->write_callback(io, io->write_data)) {
if (io->write_destroy)
io->write_destroy(io->write_data);
io->write_callback = NULL;
io->write_destroy = NULL;
io->write_data = NULL;
io->events &= ~EPOLLOUT;
mainloop_modify_fd(io->fd, io->events);
}
}
io_unref(io);
}
/**
* create a new io data structure
*
* @param fd file descriptor (includes socket)
* @return NULL if error or io data structure
*/
struct io *io_new(int fd)
{
struct io *io;
if (fd < 0)
return NULL;
io = new0(struct io, 1);
if (!io)
return NULL;
io->fd = fd;
io->events = 0;
io->close_on_destroy = false;
if (mainloop_add_fd(io->fd, io->events, io_callback,
io, io_cleanup) < 0) {
free(io);
return NULL;
}
return io_ref(io);
}
/**
* remove the fd from the 'mainloop' and reset all io call back, unref the io data structure
*
* @param io pointer to the io data structure
*/
void io_destroy(struct io *io)
{
if (!io)
return;
io->read_callback = NULL;
io->write_callback = NULL;
io->disconnect_callback = NULL;
mainloop_remove_fd(io->fd);
io_unref(io);
}
/**
* return the fd (file descriptor, can be a socket) attached to the io data structure
*
* @param io pointer to io data structure
* @return
*/
int io_get_fd(struct io *io)
{
if (!io)
return -ENOTCONN;
return io->fd;
}
/**
*
* @param io
* @param do_close
* @return
*/
bool io_set_close_on_destroy(struct io *io, bool do_close)
{
if (!io)
return false;
io->close_on_destroy = do_close;
return true;
}
/**
*
* @param io
* @param callback
* @param user_data
* @param destroy
* @return
*/
bool io_set_read_handler(struct io *io, io_callback_func_t callback,
void *user_data, io_destroy_func_t destroy)
{
uint32_t events;
if (!io || io->fd < 0)
return false;
if (io->read_destroy)
io->read_destroy(io->read_data);
if (callback)
events = io->events | EPOLLIN;
else
events = io->events & ~EPOLLIN;
io->read_callback = callback;
io->read_destroy = destroy;
io->read_data = user_data;
if (events == io->events)
return true;
if (mainloop_modify_fd(io->fd, events) < 0)
return false;
io->events = events;
return true;
}
/**
*
* @param io
* @param callback
* @param user_data
* @param destroy
* @return
*/
bool io_set_write_handler(struct io *io, io_callback_func_t callback,
void *user_data, io_destroy_func_t destroy)
{
uint32_t events;
if (!io || io->fd < 0)
return false;
if (io->write_destroy)
io->write_destroy(io->write_data);
if (callback)
events = io->events | EPOLLOUT;
else
events = io->events & ~EPOLLOUT;
io->write_callback = callback;
io->write_destroy = destroy;
io->write_data = user_data;
if (events == io->events)
return true;
if (mainloop_modify_fd(io->fd, events) < 0)
return false;
io->events = events;
return true;
}
/**
*
* @param io
* @param callback
* @param user_data
* @param destroy
* @return
*/
bool io_set_disconnect_handler(struct io *io, io_callback_func_t callback,
void *user_data, io_destroy_func_t destroy)
{
uint32_t events;
if (!io || io->fd < 0)
return false;
if (io->disconnect_destroy)
io->disconnect_destroy(io->disconnect_data);
if (callback)
events = io->events | EPOLLRDHUP;
else
events = io->events & ~EPOLLRDHUP;
io->disconnect_callback = callback;
io->disconnect_destroy = destroy;
io->disconnect_data = user_data;
if (events == io->events)
return true;
if (mainloop_modify_fd(io->fd, events) < 0)
return false;
io->events = events;
return true;
}
/**
* send data to the underlying socket (actual write)
*
* @param io io structure which describe the underlying socket and its context
* @param iov pointer to pdu, pdu length structure
* @param iovcnt number of pdu to send (iov is an array)
* @return The return value is a count of bytes written, or -errno indicating an error.
*/
ssize_t io_send(struct io *io, const struct iovec *iov, int iovcnt)
{
ssize_t ret;
if (!io || io->fd < 0)
return -ENOTCONN;
do {
ret = writev(io->fd, iov, iovcnt);
} while (ret < 0 && errno == EINTR);
if (ret < 0)
return -errno;
return ret;
}
/**
*
* @param io
* @return
*/
bool io_shutdown(struct io *io)
{
if (!io || io->fd < 0)
return false;
return shutdown(io->fd, SHUT_RDWR) == 0;
}