forked from embedded2016/server-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.c
483 lines (441 loc) · 13.2 KB
/
buffer.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include "buffer.h"
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
/* The pre-allocated memory per packet */
/* packet sizes */
#ifndef BUFFER_PACKET_SIZE
#define BUFFER_PACKET_SIZE (1024 * 64)
#endif
#ifndef BUFFER_MAX_PACKET_POOL
#define BUFFER_MAX_PACKET_POOL 127
#endif
/* Buffer packets */
struct Packet {
ssize_t length;
struct Packet *next;
void *data;
char mem[BUFFER_PACKET_SIZE];
struct {
unsigned can_interrupt : 1;
unsigned close_after : 1;
unsigned rsrv : 6;
} metadata;
};
/* The global packet container pool */
static struct {
int ref_count;
int pool_count;
struct Packet *pool;
} ContainerPool = { 0, 0 };
/* the packet pool mutex */
static pthread_mutex_t container_pool_locker = PTHREAD_MUTEX_INITIALIZER;
/* register a buffer in the pool - the pool will self-distruct when the last
* buffer unregisters.
*/
static void register_buffer(void)
{
pthread_mutex_lock(&container_pool_locker);
ContainerPool.ref_count++;
pthread_mutex_unlock(&container_pool_locker);
}
/* unregister a buffer in the pool */
static void unregister_buffer(void)
{
pthread_mutex_lock(&container_pool_locker);
ContainerPool.ref_count--;
if (ContainerPool.ref_count <= 0) {
ContainerPool.ref_count = 0; // never fall from 0
struct Packet *to_free;
while ((to_free = ContainerPool.pool)) {
ContainerPool.pool = to_free->next;
free(to_free);
}
}
pthread_mutex_unlock(&container_pool_locker);
}
/* grab a packet from the pool */
static struct Packet *get_packet(void)
{
struct Packet *packet;
pthread_mutex_lock(&container_pool_locker);
packet = ContainerPool.pool;
if (packet) {
ContainerPool.pool = packet->next;
ContainerPool.pool_count--;
} else {
packet = malloc(sizeof(struct Packet));
}
pthread_mutex_unlock(&container_pool_locker);
if (!packet)
return 0;
packet->data = packet->mem;
packet->next = 0;
packet->length = 0;
*((char *) &packet->metadata) = 0;
return packet;
}
/* return a packet to the pool, or free it (when the pool is full) */
static void free_packet(struct Packet* packet)
{
if (packet->data != packet->mem && packet->data) {
if (packet->length)
free(packet->data);
else
fclose(packet->data);
}
pthread_mutex_lock(&container_pool_locker);
if (ContainerPool.pool_count <= BUFFER_MAX_PACKET_POOL) {
packet->next = ContainerPool.pool;
ContainerPool.pool = packet;
ContainerPool.pool_count++;
} else
free(packet);
pthread_mutex_unlock(&container_pool_locker);
}
// The buffer structure
struct Buffer {
void *id;
struct Packet *packet; /**< pointer to the actual data */
size_t sent; /**< the amount of data sent from the first packet */
pthread_mutex_t lock; /**< a mutex preventing buffer corruption */
/** a writing hook, allowing for SSL sockets or other extensions. */
ssize_t (*writing_hook)(server_pt srv, int fd, void* data, size_t len);
server_pt owner; /**< the buffer's owner */
};
/* validates that this is an actual buffer object. */
static inline
int is_buffer(struct Buffer *object)
{
return object->id == is_buffer;
}
/* create a new buffer object */
static inline void *new_buffer(server_pt owner)
{
struct Buffer *buffer = malloc(sizeof(struct Buffer));
if (!buffer) return 0;
*buffer = (struct Buffer) {
.id = is_buffer,
.sent = 0, .packet = NULL,
.owner = owner,
};
if (pthread_mutex_init(&buffer->lock, NULL)) {
free(buffer);
return 0;
}
register_buffer();
return buffer;
}
/* clears all the buffer data */
static inline
void clear_buffer(void *buf)
{
struct Buffer *buffer = buf;
if (is_buffer(buffer)) {
pthread_mutex_lock(&buffer->lock);
struct Packet* to_free = NULL;
while ((to_free = buffer->packet)) {
buffer->packet = buffer->packet->next;
free_packet(to_free);
}
buffer->writing_hook = NULL;
pthread_mutex_unlock(&buffer->lock);
}
}
void set_whook(void *buf,
ssize_t (*writing_hook)(server_pt srv, int fd,
void *data, size_t len))
{
struct Buffer *buffer = buf;
if (is_buffer(buffer))
buffer->writing_hook = writing_hook;
}
static inline
void destroy_buffer(void *buf)
{
struct Buffer *buffer = buf;
if (is_buffer(buffer)) {
clear_buffer(buffer);
pthread_mutex_destroy(&buffer->lock);
free(buffer);
unregister_buffer();
}
}
/* applys the move logic for either urgent or non urgent packets */
static void
insert_packets_to_buffer(struct Buffer *buffer, struct Packet *packet,
char urgent)
{
pthread_mutex_lock(&buffer->lock);
struct Packet *tail, **pos = &(buffer->packet);
if (urgent) {
while (*pos && (!(*pos)->next ||
!(*pos)->next->metadata.can_interrupt))
pos = &((*pos)->next);
} else {
while (*pos)
pos = &((*pos)->next);
}
tail = (*pos);
*pos = packet;
if (tail) {
pos = &(packet->next);
while (*pos)
pos = &((*pos)->next);
*pos = tail;
}
pthread_mutex_unlock(&buffer->lock);
}
static inline
size_t buffer_move_logic(void *buf, void *data,
size_t length, char urgent)
{
struct Buffer *buffer = buf;
if (!is_buffer(buffer)) return 0;
if (!length || !data) {
/* FIXME: warn the messages:
* "Buffer: Canot move data because either length (%lu) or
* data (%p) are "invalid\n", length, data
*/
return 0;
}
struct Packet *np = get_packet();
if (!np) return 0;
np->data = data;
np->length = length;
np->next = NULL;
*((char *) &np->metadata) = 0;
np->metadata.can_interrupt = 1;
insert_packets_to_buffer(buffer, np, urgent);
return length;
}
static size_t buffer_move(void *buf, void *data, size_t length)
{
struct Buffer *buffer = buf;
return buffer_move_logic(buffer, data, length, 0);
}
static size_t buffer_move_next(void *buf, void *data, size_t length)
{
struct Buffer*buffer = buf;
return buffer_move_logic(buffer, data, length, 1);
}
static size_t
buffer_copy_logic(void *buf, void *data,
size_t length, char urgent)
{
struct Buffer *buffer = buf;
if (!length || !data) {
/* FIXME: warn the message
* "Buffer: Canot copy data because either length (%lu) or
* data (%p) are invalid\n", length, data
*/
return 0;
}
size_t to_copy = length;
struct Packet *np = get_packet();
if (!np) {
/* FIXME: warn the message
* "Couldn't allocate memory for the buffer (on copy)"
*/
return 0;
}
/* set marker for packet interrupt */
np->metadata.can_interrupt = 1;
struct Packet *tmp = np;
while (to_copy) {
if (to_copy > BUFFER_PACKET_SIZE) {
memcpy(tmp->mem, data, BUFFER_PACKET_SIZE);
tmp->data = tmp->mem;
data += BUFFER_PACKET_SIZE;
to_copy -= BUFFER_PACKET_SIZE;
tmp->length = BUFFER_PACKET_SIZE;
tmp->next = get_packet();
if (!(tmp->next)) {
/* FIXME: warn the message
* "Couldn't allocate memory for the buffer (on copy)"
*/
tmp = np;
while (tmp) {
np = tmp;
tmp = np->next;
free_packet(np);
}
return 0;
}
tmp = tmp->next;
} else {
memcpy(tmp->mem, data, to_copy);
tmp->data = tmp->mem;
tmp->length = to_copy;
to_copy = 0;
}
}
insert_packets_to_buffer(buffer, np, urgent);
return length;
}
static size_t buffer_copy(void *buf, void *data, size_t length)
{
struct Buffer* buffer = buf;
return buffer_copy_logic(buffer, data, length, 0);
}
static size_t buffer_copy_next(void *buf, void *data, size_t length)
{
struct Buffer *buffer = buf;
return buffer_copy_logic(buffer, data, length, 1);
}
static ssize_t buffer_flush(void *buf, int fd)
{
struct Buffer *buffer = buf;
if (!is_buffer(buffer)) return -1;
ssize_t sent = 0;
struct Packet *packet;
pthread_mutex_lock(&buffer->lock);
start_flush:
/* no packets to send */
if (!buffer->packet) {
pthread_mutex_unlock(&buffer->lock);
return 0;
}
/* packet is a file */
if (!buffer->packet->length) {
/* make sure file sending isn't interrupted. */
buffer->packet->metadata.can_interrupt = 0;
/* grab a packet from the pool */
packet = get_packet();
/* read the data */
packet->length =
fread(packet->data, 1, BUFFER_PACKET_SIZE, buffer->packet->data);
/* read less? done sending file */
if (packet->length < BUFFER_PACKET_SIZE) {
if (packet->length <= 0) { /* no more data */
free_packet(packet);
/* move the buffer one step forward */
packet = buffer->packet;
buffer->packet = buffer->packet->next;
free_packet(packet);
packet = NULL;
} else { /* this will be the last the file will offer */
/* set the next packet */
packet->next = buffer->packet->next;
free_packet(buffer->packet);
/* set the data packet as the buffer's packet */
buffer->packet = packet;
}
} else {
packet->next = buffer->packet;
/* set the data packet as the buffer's packet,
* the file packet is next.
*/
buffer->packet = packet;
}
/* make sure the sent property is reset */
buffer->sent = 0;
goto start_flush;
}
/* the packet, at this point, is always a data packet. send the data */
/* write using the writing hook if available. */
if (buffer->writing_hook) {
sent = buffer->writing_hook(buffer->owner, fd,
buffer->packet->data + buffer->sent,
buffer->packet->length - buffer->sent);
} else {
sent = write(fd, buffer->packet->data + buffer->sent,
buffer->packet->length - buffer->sent);
if (sent < 0 && (errno & (EWOULDBLOCK | EAGAIN | EINTR)))
sent = 0;
}
if (sent < 0) {
pthread_mutex_unlock(&buffer->lock);
return -1;
} else if (sent > 0) {
buffer->sent += sent;
}
if (buffer->sent >= buffer->packet->length) {
/* review the close connection flag means: "Close the connection" */
if (buffer->packet->metadata.close_after) {
pthread_mutex_unlock(&(buffer->lock));
Server.close(buffer->owner, fd);
return sent;
/* buffer clearing should be performed by the Buffer's owner. */
}
packet = buffer->packet;
buffer->sent = 0;
buffer->packet = buffer->packet->next;
free_packet(packet);
}
pthread_mutex_unlock(&(buffer->lock));
return sent;
}
static int buffer_sendfile(void *buf, FILE *file)
{
struct Buffer *buffer = buf;
if (!is_buffer(buffer)) return -1;
struct Packet *np = get_packet();
if (!np) return -1;
np->data = file;
np->metadata.can_interrupt = 1;
insert_packets_to_buffer(buffer, np, 0);
return 0;
}
static void buffer_close_w_d(void *buf, int fd)
{
struct Buffer *buffer = buf;
if (!is_buffer(buffer)) return;
if (!buffer->packet) {
close(fd);
return;
}
pthread_mutex_lock(&buffer->lock);
struct Packet *packet = buffer->packet;
if (!packet)
goto finish;
while (packet->next)
packet = packet->next;
packet->metadata.close_after = 1;
finish:
pthread_mutex_unlock(&buffer->lock);
}
size_t buffer_pending(struct Buffer *buffer)
{
if (!is_buffer(buffer)) return 0;
size_t len = 0;
struct Packet *p;
pthread_mutex_lock(&buffer->lock);
p = buffer->packet;
while (p) {
if (p->data && p->length)
len += p->length;
else if (p->data)
len += 1; /* if it is a file - can we check it's size? */
else
break; /* no need to move beyond a close connection packet */
p = p->next;
}
len -= buffer->sent;
pthread_mutex_unlock(&buffer->lock);
return len;
}
char buffer_is_empty(void *buf)
{
struct Buffer *buffer = buf;
return (!is_buffer(buffer)) ? 1 : (buffer->packet == NULL);
}
/* API gateway */
const struct BufferClass Buffer = {
.new = new_buffer,
.destroy = destroy_buffer,
.clear = clear_buffer,
.set_whook = set_whook,
.sendfile = buffer_sendfile,
.write = buffer_copy,
.write_move = buffer_move,
.write_next = buffer_copy_next,
.write_move_next = buffer_move_next,
.flush = buffer_flush,
.close_when_done = buffer_close_w_d,
.is_empty = buffer_is_empty,
};