summaryrefslogtreecommitdiff
path: root/opentracker.c
diff options
context:
space:
mode:
Diffstat (limited to 'opentracker.c')
-rw-r--r--opentracker.c39
1 files changed, 34 insertions, 5 deletions
diff --git a/opentracker.c b/opentracker.c
index 374824f..6ff9da3 100644
--- a/opentracker.c
+++ b/opentracker.c
@@ -5,6 +5,7 @@
5 5
6#include "socket.h" 6#include "socket.h"
7#include "io.h" 7#include "io.h"
8#include "iob.h"
8#include "buffer.h" 9#include "buffer.h"
9#include "array.h" 10#include "array.h"
10#include "byte.h" 11#include "byte.h"
@@ -52,7 +53,10 @@ static void panic(const char* routine) {
52} 53}
53 54
54struct http_data { 55struct http_data {
55 array r; 56 union {
57 array r;
58 io_batch batch;
59 };
56 unsigned char ip[4]; 60 unsigned char ip[4];
57}; 61};
58 62
@@ -81,12 +85,24 @@ void senddata(int64 s, struct http_data* h, char *buffer, size_t size ) {
81 free(h); io_close( s ); 85 free(h); io_close( s );
82 } else { 86 } else {
83 /* here we would take a copy of the buffer and remember it */ 87 /* here we would take a copy of the buffer and remember it */
84 fprintf( stderr, "Should have handled this.\n" ); 88 char * outbuf = malloc( size - written_size );
89 tai6464 t;
90
91 if( !outbuf ) {
85#ifdef _DEBUG_FDS 92#ifdef _DEBUG_FDS
86 if( !fd_debug_space[s] ) fprintf( stderr, "close on non-open fd\n" ); 93 if( !fd_debug_space[s] ) fprintf( stderr, "close on non-open fd\n" );
87 fd_debug_space[s] = 0; 94 fd_debug_space[s] = 0;
88#endif 95#endif
89 free(h); io_close( s ); 96 free(h); io_close( s );
97 return;
98 }
99
100 iob_reset( &h->batch );
101 memmove( outbuf, buffer + written_size, size - written_size );
102 iob_addbuf_free( &h->batch, outbuf, size - written_size );
103
104 // writeable sockets just have a tcp timeout
105 taia_uint(&t,0); io_timeout( s, t );
90 } 106 }
91} 107}
92 108
@@ -437,6 +453,16 @@ void handle_read( int64 clientsocket ) {
437 httpresponse(clientsocket,h); 453 httpresponse(clientsocket,h);
438} 454}
439 455
456void handle_write( int64 clientsocket ) {
457 struct http_data* h=io_getcookie(clientsocket);
458 if( !h ) return;
459 if( iob_send( clientsocket, &h->batch ) <= 0 ) {
460 iob_reset( &h->batch );
461 io_close( clientsocket );
462 free( h );
463 }
464}
465
440void handle_accept( int64 serversocket ) { 466void handle_accept( int64 serversocket ) {
441 struct http_data* h; 467 struct http_data* h;
442 unsigned char ip[4]; 468 unsigned char ip[4];
@@ -510,6 +536,9 @@ void server_mainloop( int64 serversocket ) {
510 handle_read( i ); 536 handle_read( i );
511 } 537 }
512 538
539 while( ( i = io_canwrite() ) != -1 )
540 handle_write( i );
541
513 taia_now(&t); 542 taia_now(&t);
514 if( taia_less( &next_timeout_check, &t ) ) { 543 if( taia_less( &next_timeout_check, &t ) ) {
515 handle_timeouted( ); 544 handle_timeouted( );