*** empty log message ***
[mmondor.git] / tests / bktr_httpd / client.c
CommitLineData
0ab43060 1/* $Id: client.c,v 1.5 2008/01/21 01:31:38 mmondor Exp $ */
ce436d0b
MM
2
3/*
4 * Copyright (c) 2008, Matthew Mondor
5 * ALL RIGHTS RESERVED.
6 */
7
8
9#include <stdlib.h>
e583c3d8 10#include <unistd.h>
ce436d0b 11
dacf6158 12#include "net.h"
ce436d0b 13#include "client.h"
e583c3d8 14#include "sendq.h"
ce436d0b
MM
15
16
0ab43060
MM
17static void client_destroy(client_t *, client_t **);
18
19
ce436d0b
MM
20static pool_t client_pool;
21
fa6ec601 22list_t client_list;
0ab43060 23static list_t client_destroy_list;
fa6ec601 24
ce436d0b
MM
25
26bool
27client_init(void)
28{
29
30 if (!pool_init(&client_pool, "client_pool", malloc, free, NULL, NULL,
31 sizeof(client_t), 65536 / sizeof(client_t), 1, 0))
32 return false;
33
0ab43060
MM
34 DLIST_INIT(&client_list);
35 DLIST_INIT(&client_destroy_list);
36
ce436d0b
MM
37 return true;
38}
39
40client_t *
e583c3d8 41client_new(netaddr_t *naddr, int fd)
ce436d0b
MM
42{
43 client_t *c;
44
45 if ((c = (client_t *)pool_alloc(&client_pool, false)) != NULL) {
46 DLIST_INIT(&c->sendq);
dacf6158 47 c->netaddr = naddr;
e583c3d8
MM
48 c->fd = fd;
49 c->first = true;
50 c->skip = c->skipcnt = 0;
51 c->received = 0;
0ab43060 52 c->todestroy = false;
ce436d0b 53 }
fa6ec601 54 DLIST_APPEND(&client_list, (node_t *)c);
ce436d0b
MM
55
56 return c;
57}
58
59void
0ab43060
MM
60client_destroy_mark(client_t *c)
61{
62
63 if (c == NULL || c->todestroy)
64 return;
65
66 c->todestroy = true;
67 DLIST_SWAP(&client_destroy_list, &client_list, (node_t *)c, false);
68}
69
70void
71client_destroy_marked(client_t **idx)
72{
73 node_t *n, *next;
74
75 for (n = DLIST_TOP(&client_destroy_list); n != NULL; n = next) {
76 next = DLIST_NEXT(n);
77
78 DLIST_UNLINK(&client_destroy_list, n);
79 client_destroy((client_t *)n, idx);
80 }
81}
82
83static void
84client_destroy(client_t *c, client_t **idx)
ce436d0b
MM
85{
86
0ab43060 87 idx[c->fd] = NULL;
e583c3d8 88 (void) close(c->fd);
e583c3d8 89 sendq_purge(&c->sendq, true);
dacf6158 90 netaddr_destroy(c->netaddr);
ce436d0b
MM
91 (void) pool_free((pnode_t *)c);
92}