comparison src/c/urweb.c @ 863:305bc0a431de

.msgs processing in FastCGI
author Adam Chlipala <adamc@hcoop.net>
date Sat, 27 Jun 2009 17:50:31 -0400
parents a738002d5b4d
children 6304f5e8fbb4
comparison
equal deleted inserted replaced
862:66dbf3953758 863:305bc0a431de
17 uw_unit uw_unit_v = {}; 17 uw_unit uw_unit_v = {};
18 18
19 19
20 // Socket extras 20 // Socket extras
21 21
22 int uw_really_send(int sock, const void *buf, size_t len) { 22 int uw_really_send(int sock, const void *buf, ssize_t len) {
23 while (len > 0) { 23 while (len > 0) {
24 ssize_t n = send(sock, buf, len, 0); 24 ssize_t n = send(sock, buf, len, 0);
25 25
26 if (n < 0) 26 if (n < 0)
27 return n; 27 return n;
110 int pass; 110 int pass;
111 struct client *next; 111 struct client *next;
112 pthread_mutex_t lock, pull_lock; 112 pthread_mutex_t lock, pull_lock;
113 buf msgs; 113 buf msgs;
114 int sock; 114 int sock;
115 int (*send)(int sockfd, const void *buf, ssize_t len);
116 int (*close)(int fd);
115 time_t last_contact; 117 time_t last_contact;
116 unsigned n_channels; 118 unsigned n_channels;
117 unsigned refcount; 119 unsigned refcount;
118 } client; 120 } client;
119 121
200 202
201 void uw_set_on_success(char *s) { 203 void uw_set_on_success(char *s) {
202 on_success = s; 204 on_success = s;
203 } 205 }
204 206
205 void uw_client_connect(unsigned id, int pass, int sock) { 207 void uw_client_connect(unsigned id, int pass, int sock,
208 int (*send)(int sockfd, const void *buf, ssize_t len),
209 int (*close)(int fd)) {
206 client *c = find_client(id); 210 client *c = find_client(id);
207 211
208 if (c == NULL) { 212 if (c == NULL) {
209 close(sock); 213 close(sock);
210 fprintf(stderr, "Out-of-bounds client request (%u)\n", id); 214 fprintf(stderr, "Out-of-bounds client request (%u)\n", id);
226 fprintf(stderr, "Wrong client password (%u, %d)\n", id, pass); 230 fprintf(stderr, "Wrong client password (%u, %d)\n", id, pass);
227 return; 231 return;
228 } 232 }
229 233
230 if (c->sock != -1) { 234 if (c->sock != -1) {
231 close(c->sock); 235 c->close(c->sock);
232 c->sock = -1; 236 c->sock = -1;
233 } 237 }
234 238
235 c->last_contact = time(NULL); 239 c->last_contact = time(NULL);
236 240
237 if (buf_used(&c->msgs) > 0) { 241 if (buf_used(&c->msgs) > 0) {
238 uw_really_send(sock, on_success, strlen(on_success)); 242 send(sock, on_success, strlen(on_success));
239 uw_really_send(sock, begin_msgs, sizeof(begin_msgs) - 1); 243 send(sock, begin_msgs, sizeof(begin_msgs) - 1);
240 uw_really_send(sock, c->msgs.start, buf_used(&c->msgs)); 244 send(sock, c->msgs.start, buf_used(&c->msgs));
241 buf_reset(&c->msgs); 245 buf_reset(&c->msgs);
242 close(sock); 246 close(sock);
243 } 247 }
244 else 248 else {
245 c->sock = sock; 249 c->sock = sock;
250 c->send = send;
251 c->close = close;
252 }
246 253
247 pthread_mutex_unlock(&c->lock); 254 pthread_mutex_unlock(&c->lock);
248 } 255 }
249 256
250 static void free_client(client *c) { 257 static void free_client(client *c) {
262 269
263 static void client_send(client *c, buf *msg) { 270 static void client_send(client *c, buf *msg) {
264 pthread_mutex_lock(&c->lock); 271 pthread_mutex_lock(&c->lock);
265 272
266 if (c->sock != -1) { 273 if (c->sock != -1) {
267 uw_really_send(c->sock, on_success, strlen(on_success)); 274 c->send(c->sock, on_success, strlen(on_success));
268 uw_really_send(c->sock, begin_msgs, sizeof(begin_msgs) - 1); 275 c->send(c->sock, begin_msgs, sizeof(begin_msgs) - 1);
269 uw_really_send(c->sock, msg->start, buf_used(msg)); 276 c->send(c->sock, msg->start, buf_used(msg));
270 close(c->sock); 277 c->close(c->sock);
271 c->sock = -1; 278 c->sock = -1;
272 } else 279 } else
273 buf_append(&c->msgs, msg->start, buf_used(msg)); 280 buf_append(&c->msgs, msg->start, buf_used(msg));
274 281
275 pthread_mutex_unlock(&c->lock); 282 pthread_mutex_unlock(&c->lock);