ry / ebb fork watch download tarball
public this repo is viewable by everyone
Description: web server
Homepage: http://ebb.rubyforge.org
Clone URL: git://github.com/ry/ebb.git
add in_use flag to ebb.c
Ryan Dahl (author)
2 months ago
commit  b33f32ed9e6d70a4684ebf9b86a687383c6121b0
tree    a7ff7f930989417f31fbf59f3a5a46e29573f61f
parent  c8641291900ff5ab9e2a027ffb73213f5440b58b
...
57
58
59
60
 
61
62
63
...
79
80
81
 
 
82
83
84
...
93
94
95
96
97
98
99
100
101
102
...
57
58
59
 
60
61
62
63
...
79
80
81
82
83
84
85
86
...
95
96
97
 
 
 
 
98
99
100
0
@@ -57,7 +57,7 @@ module Ebb
0
         body = "Internal Server Error\n"
0
       end
0
       
0
- write_status(status)
0
+ FFI::client_write_status(self, status.to_i, HTTP_STATUS_CODES[status.to_i])
0
       
0
       if body.respond_to? :length and status != 304
0
         headers['Connection'] = 'close'
0
@@ -79,6 +79,8 @@ module Ebb
0
         body.each { |p| write(p) }
0
         FFI::client_set_body_written(self, true)
0
       end
0
+ ensure
0
+ FFI::client_release(self)
0
     end
0
     
0
     private
0
@@ -93,10 +95,6 @@ module Ebb
0
       FFI::client_write(self, data)
0
     end
0
     
0
- def write_status(status)
0
- FFI::client_write_status(self, status.to_i, HTTP_STATUS_CODES[status])
0
- end
0
-
0
     def write_header(field, value)
0
       FFI::client_write_header(self, field.to_s, value.to_s)
0
     end
...
138
139
140
 
141
142
143
...
232
233
234
 
235
236
237
...
282
283
284
 
285
286
287
...
353
354
355
356
 
357
358
359
...
387
388
389
390
 
391
 
 
 
392
393
394
...
509
510
511
 
 
 
 
 
 
 
512
513
514
...
580
581
582
583
584
 
 
585
586
587
...
593
594
595
596
597
 
 
598
599
600
...
606
607
608
609
610
 
 
611
612
613
...
618
619
620
621
622
 
 
623
624
625
...
648
649
650
651
652
653
 
 
654
655
656
...
138
139
140
141
142
143
144
...
233
234
235
236
237
238
239
...
284
285
286
287
288
289
290
...
356
357
358
 
359
360
361
362
...
390
391
392
 
393
394
395
396
397
398
399
400
...
515
516
517
518
519
520
521
522
523
524
525
526
527
...
593
594
595
 
 
596
597
598
599
600
...
606
607
608
 
 
609
610
611
612
613
...
619
620
621
 
 
622
623
624
625
626
...
631
632
633
 
 
634
635
636
637
638
...
661
662
663
 
 
 
664
665
666
667
668
0
@@ -138,6 +138,7 @@ static void dispatch(ebb_client *client)
0
                         , strlen(server->port)
0
                         );
0
   }
0
+ client->in_use = TRUE;
0
   server->request_cb(client, server->request_cb_data);
0
 }
0
 
0
@@ -232,6 +233,7 @@ static void on_readable(struct ev_loop *loop, ev_io *watcher, int revents)
0
 {
0
   ebb_client *client = (ebb_client*)(watcher->data);
0
   
0
+ assert(client->in_use == FALSE);
0
   assert(client->open);
0
   assert(client->server->open);
0
   assert(client->server->loop == loop);
0
@@ -282,6 +284,7 @@ error:
0
 
0
 static client_init(ebb_server *server, ebb_client *client)
0
 {
0
+ assert(client->in_use == FALSE);
0
 #ifdef DEBUG
0
   /* does ragel fuck up if request buffer isn't null? */
0
   for(i=0; i< EBB_BUFFERSIZE; i++)
0
@@ -353,7 +356,7 @@ static void on_request(struct ev_loop *loop, ev_io *watcher, int revents)
0
   ebb_client *client;
0
   /* Get next availible peer */
0
   for(i=0; i < EBB_MAX_CLIENTS; i++)
0
- if(!server->clients[i].open) {
0
+ if(!server->clients[i].in_use && !server->clients[i].open) {
0
       client = &(server->clients[i]);
0
       break;
0
     }
0
@@ -387,8 +390,11 @@ void ebb_server_init( ebb_server *server
0
                     )
0
 {
0
   int i;
0
- for(i=0; i < EBB_MAX_CLIENTS; i++)
0
+ for(i=0; i < EBB_MAX_CLIENTS; i++) {
0
     server->clients[i].response_buffer = g_string_new("");
0
+ server->clients[i].open = FALSE;
0
+ server->clients[i].in_use = FALSE;
0
+ }
0
   
0
   server->request_cb = request_cb;
0
   server->request_cb_data = request_cb_data;
0
@@ -509,6 +515,13 @@ int ebb_server_listen_on_socket(ebb_server *server, const char *socketpath)
0
 }
0
 
0
 
0
+void ebb_client_release(ebb_client *client)
0
+{
0
+ assert(client->in_use);
0
+ client->in_use = FALSE;
0
+}
0
+
0
+
0
 void ebb_client_close(ebb_client *client)
0
 {
0
   if(client->open) {
0
@@ -580,8 +593,8 @@ static void on_client_writable(struct ev_loop *loop, ev_io *watcher, int revents
0
 
0
 void ebb_client_write_status(ebb_client *client, int status, const char *human_status)
0
 {
0
- if(!client->open)
0
- return;
0
+ assert(client->in_use);
0
+ if(!client->open) return;
0
   assert(client->status_written == FALSE);
0
   g_string_append_printf( client->response_buffer
0
                         , "HTTP/1.1 %d %s\r\n"
0
@@ -593,8 +606,8 @@ void ebb_client_write_status(ebb_client *client, int status, const char *human_s
0
 
0
 void ebb_client_write_header(ebb_client *client, const char *field, const char *value)
0
 {
0
- if(!client->open)
0
- return;
0
+ assert(client->in_use);
0
+ if(!client->open) return;
0
   assert(client->status_written == TRUE);
0
   assert(client->headers_written == FALSE);
0
   g_string_append_printf( client->response_buffer
0
@@ -606,8 +619,8 @@ void ebb_client_write_header(ebb_client *client, const char *field, const char *
0
 
0
 void ebb_client_write(ebb_client *client, const char *data, int length)
0
 {
0
- if(!client->open)
0
- return;
0
+ assert(client->in_use);
0
+ if(!client->open) return;
0
   g_string_append_len(client->response_buffer, data, length);
0
   if(client->began_transmission) {
0
     /* restart the watcher if we're streaming */
0
@@ -618,8 +631,8 @@ void ebb_client_write(ebb_client *client, const char *data, int length)
0
 
0
 void ebb_client_begin_transmission(ebb_client *client)
0
 {
0
- if(!client->open)
0
- return;
0
+ assert(client->in_use);
0
+ if(!client->open) return;
0
   assert(FALSE == ev_is_active(&client->write_watcher));
0
   
0
   /* assure the socket is still in non-blocking mode */
0
@@ -648,9 +661,8 @@ int ebb_client_read(ebb_client *client, char *buffer, int length)
0
 {
0
   size_t read;
0
   
0
- if(!client->open) {
0
- return -1;
0
- }
0
+ assert(client->in_use);
0
+ if(!client->open) return -1;
0
   assert(client_finished_parsing);
0
   
0
   if(client->upload_file) {
...
25
26
27
 
 
28
29
30
...
51
52
53
 
 
54
55
56
...
25
26
27
28
29
30
31
32
...
53
54
55
56
57
58
59
60
0
@@ -25,6 +25,8 @@ typedef struct ebb_client ebb_client;
0
 
0
 /*** Ebb Client ***/
0
 void ebb_client_close(ebb_client*);
0
+/* user MUST call this function on each client passed by the request_cb */
0
+void ebb_client_release(ebb_client*);
0
 int ebb_client_read(ebb_client *client, char *buffer, int length);
0
 void ebb_client_write_status(ebb_client*, int status, const char *human_status);
0
 void ebb_client_write_header(ebb_client*, const char *field, const char *value);
0
@@ -51,6 +53,8 @@ struct ebb_env_item {
0
 struct ebb_client {
0
   EBB_TCP_COMMON
0
   
0
+ unsigned int in_use : 1;
0
+
0
   ebb_server *server;
0
   http_parser parser;
0
   
...
194
195
196
 
 
 
 
 
 
 
 
 
197
198
199
...
234
235
236
 
237
238
239
...
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
...
243
244
245
246
247
248
249
0
@@ -194,6 +194,15 @@ VALUE client_begin_transmission(VALUE _, VALUE rb_client)
0
   return Qnil;
0
 }
0
 
0
+VALUE client_release(VALUE _, VALUE rb_client)
0
+{
0
+ ebb_client *client;
0
+ Data_Get_Struct(rb_client, ebb_client, client);
0
+ ebb_client_release(client);
0
+ return Qnil;
0
+}
0
+
0
+
0
 VALUE client_set_body_written(VALUE _, VALUE rb_client, VALUE v)
0
 {
0
   ebb_client *client;
0
@@ -234,6 +243,7 @@ void Init_ebb_ext()
0
   rb_define_singleton_method(mFFI, "client_begin_transmission", client_begin_transmission, 1);
0
   rb_define_singleton_method(mFFI, "client_set_body_written", client_set_body_written, 2);
0
   rb_define_singleton_method(mFFI, "client_env", client_env, 1);
0
+ rb_define_singleton_method(mFFI, "client_release", client_release, 1);
0
   
0
   /* initialize ebb_server */
0
   loop = ev_default_loop (0);

Comments

    No one has commented yet.