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
|
#include "includes.h"
#include "listener.h"
#include "session.h"
#include "dbutil.h"
void listeners_initialise() {
/* just one slot to start with */
ses.listeners = (struct Listener**)m_malloc(sizeof(struct Listener*));
ses.listensize = 1;
ses.listeners[0] = NULL;
}
void set_listener_fds(fd_set * readfds) {
unsigned int i;
struct Listener *listener;
/* check each in turn */
for (i = 0; i < ses.listensize; i++) {
listener = ses.listeners[i];
if (listener != NULL) {
TRACE(("set listener fd %d", listener->sock));
FD_SET(listener->sock, readfds);
}
}
}
void handle_listeners(fd_set * readfds) {
unsigned int i;
struct Listener *listener;
/* check each in turn */
for (i = 0; i < ses.listensize; i++) {
listener = ses.listeners[i];
if (listener != NULL) {
TRACE(("handle listener num %d fd %d", i, listener->sock));
if (FD_ISSET(listener->sock, readfds)) {
listener->accepter(listener);
}
}
}
}
/* accepter(int fd, void* typedata) is a function to accept connections,
* cleanup(void* typedata) happens when cleaning up */
struct Listener* new_listener(int sock, int type, void* typedata,
void (*accepter)(struct Listener*),
void (*cleanup)(struct Listener*)) {
unsigned int i, j;
struct Listener *newlisten = NULL;
/* try get a new structure to hold it */
for (i = 0; i < ses.listensize; i++) {
if (ses.listeners[i] == NULL) {
break;
}
}
/* or create a new one */
if (i == ses.listensize) {
if (ses.listensize > MAX_LISTENERS) {
TRACE(("leave newlistener: too many already"));
close(sock);
return NULL;
}
ses.listeners = (struct Listener**)m_realloc(ses.listeners,
(ses.listensize+LISTENER_EXTEND_SIZE)
*sizeof(struct Listener*));
ses.listensize += LISTENER_EXTEND_SIZE;
for (j = i; j < ses.listensize; j++) {
ses.listeners[j] = NULL;
}
}
ses.maxfd = MAX(ses.maxfd, sock);
TRACE(("new listener num %d fd %d", i, sock));
newlisten = (struct Listener*)m_malloc(sizeof(struct Listener));
newlisten->index = i;
newlisten->type = type;
newlisten->typedata = typedata;
newlisten->sock = sock;
newlisten->accepter = accepter;
newlisten->cleanup = cleanup;
ses.listeners[i] = newlisten;
return newlisten;
}
/* Return the first listener which matches the type-specific comparison
* function. Particularly needed for global requests, like tcp */
struct Listener * get_listener(int type, void* typedata,
int (*match)(void*, void*)) {
unsigned int i;
struct Listener* listener;
for (i = 0, listener = ses.listeners[i]; i < ses.listensize; i++) {
if (listener->type == type
&& match(typedata, listener->typedata)) {
return listener;
}
}
return NULL;
}
void remove_listener(struct Listener* listener) {
if (listener->cleanup) {
listener->cleanup(listener);
}
close(listener->sock);
ses.listeners[listener->index] = NULL;
m_free(listener);
}
|