comparison listener.c @ 62:20563735e8b5

just checkpointing
author Matt Johnston <matt@ucc.asn.au>
date Tue, 10 Aug 2004 17:09:52 +0000
parents db2c8e6fb284
children dcc43965928f
comparison
equal deleted inserted replaced
61:3a4f0ef1e8c3 62:20563735e8b5
12 12
13 } 13 }
14 14
15 void set_listener_fds(fd_set * readfds) { 15 void set_listener_fds(fd_set * readfds) {
16 16
17 unsigned int i; 17 unsigned int i, j;
18 struct Listener *listener; 18 struct Listener *listener;
19 19
20 /* check each in turn */ 20 /* check each in turn */
21 for (i = 0; i < ses.listensize; i++) { 21 for (i = 0; i < ses.listensize; i++) {
22 listener = ses.listeners[i]; 22 listener = ses.listeners[i];
23 if (listener != NULL) { 23 if (listener != NULL) {
24 TRACE(("set listener fd %d", listener->sock)); 24 for (j = 0; j < listener->nsocks; j++) {
25 FD_SET(listener->sock, readfds); 25 FD_SET(listener->socks[j], readfds);
26 }
26 } 27 }
27 } 28 }
28 } 29 }
29 30
30 31
31 void handle_listeners(fd_set * readfds) { 32 void handle_listeners(fd_set * readfds) {
32 33
33 unsigned int i; 34 unsigned int i, j;
34 struct Listener *listener; 35 struct Listener *listener;
36 int sock;
35 37
36 /* check each in turn */ 38 /* check each in turn */
37 for (i = 0; i < ses.listensize; i++) { 39 for (i = 0; i < ses.listensize; i++) {
38 listener = ses.listeners[i]; 40 listener = ses.listeners[i];
39 if (listener != NULL) { 41 if (listener != NULL) {
40 TRACE(("handle listener num %d fd %d", i, listener->sock)); 42 for (j = 0; j < listener->nsocks; j++) {
41 if (FD_ISSET(listener->sock, readfds)) { 43 sock = listener->socks[j];
42 listener->accepter(listener); 44 if (FD_ISSET(sock, readfds)) {
45 listener->accepter(listener, sock);
46 }
43 } 47 }
44 } 48 }
45 } 49 }
46 } 50 }
47 51
48 52
49 /* accepter(int fd, void* typedata) is a function to accept connections, 53 /* accepter(int fd, void* typedata) is a function to accept connections,
50 * cleanup(void* typedata) happens when cleaning up */ 54 * cleanup(void* typedata) happens when cleaning up */
51 struct Listener* new_listener(int sock, int type, void* typedata, 55 struct Listener* new_listener(int socks[], unsigned int nsocks,
52 void (*accepter)(struct Listener*), 56 int type, void* typedata,
57 void (*accepter)(struct Listener*, int sock),
53 void (*cleanup)(struct Listener*)) { 58 void (*cleanup)(struct Listener*)) {
54 59
55 unsigned int i, j; 60 unsigned int i, j;
56 struct Listener *newlisten = NULL; 61 struct Listener *newlisten = NULL;
57 /* try get a new structure to hold it */ 62 /* try get a new structure to hold it */
63 68
64 /* or create a new one */ 69 /* or create a new one */
65 if (i == ses.listensize) { 70 if (i == ses.listensize) {
66 if (ses.listensize > MAX_LISTENERS) { 71 if (ses.listensize > MAX_LISTENERS) {
67 TRACE(("leave newlistener: too many already")); 72 TRACE(("leave newlistener: too many already"));
68 close(sock); 73 for (j = 0; j < nsocks; j++) {
74 close(socks[i]);
75 }
69 return NULL; 76 return NULL;
70 } 77 }
71 78
72 ses.listeners = (struct Listener**)m_realloc(ses.listeners, 79 ses.listeners = (struct Listener**)m_realloc(ses.listeners,
73 (ses.listensize+LISTENER_EXTEND_SIZE) 80 (ses.listensize+LISTENER_EXTEND_SIZE)
78 for (j = i; j < ses.listensize; j++) { 85 for (j = i; j < ses.listensize; j++) {
79 ses.listeners[j] = NULL; 86 ses.listeners[j] = NULL;
80 } 87 }
81 } 88 }
82 89
83 ses.maxfd = MAX(ses.maxfd, sock); 90 for (j = 0; j < nsocks; j++) {
91 ses.maxfd = MAX(ses.maxfd, socks[j]);
92 }
84 93
85 TRACE(("new listener num %d fd %d", i, sock)); 94 TRACE(("new listener num %d ", i));
86 95
87 newlisten = (struct Listener*)m_malloc(sizeof(struct Listener)); 96 newlisten = (struct Listener*)m_malloc(sizeof(struct Listener));
88 newlisten->index = i; 97 newlisten->index = i;
89 newlisten->type = type; 98 newlisten->type = type;
90 newlisten->typedata = typedata; 99 newlisten->typedata = typedata;
91 newlisten->sock = sock; 100 newlisten->nsocks = nsocks;
101 memcpy(newlisten->socks, socks, nsocks * sizeof(int));
92 newlisten->accepter = accepter; 102 newlisten->accepter = accepter;
93 newlisten->cleanup = cleanup; 103 newlisten->cleanup = cleanup;
94 104
95 ses.listeners[i] = newlisten; 105 ses.listeners[i] = newlisten;
96 return newlisten; 106 return newlisten;
114 return NULL; 124 return NULL;
115 } 125 }
116 126
117 void remove_listener(struct Listener* listener) { 127 void remove_listener(struct Listener* listener) {
118 128
129 unsigned int j;
130
119 if (listener->cleanup) { 131 if (listener->cleanup) {
120 listener->cleanup(listener); 132 listener->cleanup(listener);
121 } 133 }
122 134
123 close(listener->sock); 135 for (j = 0; j < listener->nsocks; j++) {
136 close(listener->socks[j]);
137 }
124 ses.listeners[listener->index] = NULL; 138 ses.listeners[listener->index] = NULL;
125 m_free(listener); 139 m_free(listener);
126 140
127 } 141 }