Browse Source

Tidied main loop do accept() and read() before timeout()

dynamic-accesslists
erdgeist 18 years ago
parent
commit
7c4fcdbd97
  1. 103
      opentracker.c

103
opentracker.c

@ -385,21 +385,12 @@ void help( char *name ) {
); );
} }
void handle_read( int64 clientsocket, int afteraccept ) { void handle_read( int64 clientsocket ) {
struct http_data* h = io_getcookie( clientsocket ); struct http_data* h = io_getcookie( clientsocket );
int l = io_tryread( clientsocket, static_scratch, sizeof static_scratch ); int l = io_tryread( clientsocket, static_scratch, sizeof static_scratch );
tai6464 t; tai6464 t;
taia_now(&t);
taia_addsec(&t,&t,OT_CLIENT_TIMEOUT);
io_timeout(clientsocket,t);
if( l <= 0 ) { if( l <= 0 ) {
// getting 0 bytes doesn't mean connection closed,
// when we try the shortcut read() after accept()
// and nothing is there yet
if( afteraccept && ( errno == EAGAIN ) )
return;
if( h ) { if( h ) {
array_reset(&h->r); array_reset(&h->r);
free(h); free(h);
@ -416,26 +407,47 @@ void handle_read( int64 clientsocket, int afteraccept ) {
httperror(clientsocket,h,"500 request too long","You sent too much headers"); httperror(clientsocket,h,"500 request too long","You sent too much headers");
else if ((l=header_complete(h))) else if ((l=header_complete(h)))
httpresponse(clientsocket,h); httpresponse(clientsocket,h);
else {
taia_now(&t);
taia_addsec(&t,&t,OT_CLIENT_TIMEOUT);
io_timeout(clientsocket,t);
}
} }
void server_mainloop( int64 serversocket ) { void handle_accept( int64 serversocket ) {
tai6464 t, next_timeout_check; struct http_data* h;
unsigned char ip[4]; unsigned char ip[4];
uint16 port; uint16 port;
tai6464 t;
int64 i;
io_wantread( serversocket ); while( ( i = socket_accept4( serversocket, (char*)ip, &port) ) != -1 ) {
taia_now( &next_timeout_check );
for (;;) { if( !io_fd( i ) ||
int64 i; !(h = (struct http_data*)malloc(sizeof(struct http_data))) ) {
int handled_connections = 1024; io_close( i );
continue;
}
taia_now(&t); io_wantread( i );
taia_addsec(&t,&t,OT_CLIENT_TIMEOUT_CHECKINTERVAL);
io_waituntil(t);
byte_zero(h,sizeof(struct http_data));
memmove(h->ip,ip,sizeof(ip));
io_setcookie(i,h);
++ot_overall_connections;
taia_now(&t); taia_now(&t);
if( taia_less( &next_timeout_check, &t ) ) { taia_addsec(&t,&t,OT_CLIENT_TIMEOUT);
io_timeout(i,t);
}
if( errno==EAGAIN )
io_eagain( serversocket );
else
carp( "socket_accept4" );
}
void handle_timeouted( ) {
int64 i;
while( ( i = io_timeouted() ) != -1 ) { while( ( i = io_timeouted() ) != -1 ) {
struct http_data* h=io_getcookie(i); struct http_data* h=io_getcookie(i);
if( h ) { if( h ) {
@ -444,40 +456,33 @@ void server_mainloop( int64 serversocket ) {
} }
io_close(i); io_close(i);
} }
taia_now(&next_timeout_check); }
taia_addsec(&next_timeout_check,&next_timeout_check,OT_CLIENT_TIMEOUT_CHECKINTERVAL);
}
while( --handled_connections && ( ( i = io_canread() ) != -1 ) ) { void server_mainloop( int64 serversocket ) {
tai6464 t, next_timeout_check;
if( i != serversocket ) { io_wantread( serversocket );
handle_read( i, 0 ); taia_now( &next_timeout_check );
continue;
}
// Attention, i changes from what io_canread() returned to for (;;) {
// what socket_accept4 returns as new socket int64 i;
while( ( i = socket_accept4( serversocket, (char*)ip, &port) ) != -1 ) {
if( io_fd( i ) ) {
struct http_data* h=(struct http_data*)malloc(sizeof(struct http_data));
io_wantread( i );
if (h) { taia_now(&t);
byte_zero(h,sizeof(struct http_data)); taia_addsec(&t,&t,OT_CLIENT_TIMEOUT_CHECKINTERVAL);
memmove(h->ip,ip,sizeof(ip)); io_waituntil(t);
io_setcookie(i,h);
++ot_overall_connections;
handle_read(i,1);
} else
io_close(i);
} else
io_close(i);
}
if( errno==EAGAIN ) while( ( i = io_canread() ) != -1 ) {
io_eagain( serversocket ); if( i == serversocket )
handle_accept( i );
else else
carp( "socket_accept4" ); handle_read( i );
}
taia_now(&t);
if( taia_less( &next_timeout_check, &t ) ) {
handle_timeouted( );
taia_now(&next_timeout_check);
taia_addsec(&next_timeout_check,&next_timeout_check,OT_CLIENT_TIMEOUT_CHECKINTERVAL);
} }
} }
} }

Loading…
Cancel
Save