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

dynamic-accesslists
erdgeist 18 years ago
parent ccafcb0ff9
commit 7c4fcdbd97

@ -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 );
int l = io_tryread( clientsocket, static_scratch, sizeof static_scratch );
tai6464 t;
taia_now(&t);
taia_addsec(&t,&t,OT_CLIENT_TIMEOUT);
io_timeout(clientsocket,t);
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 ) {
array_reset(&h->r);
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");
else if ((l=header_complete(h)))
httpresponse(clientsocket,h);
else {
taia_now(&t);
taia_addsec(&t,&t,OT_CLIENT_TIMEOUT);
io_timeout(clientsocket,t);
}
}
void server_mainloop( int64 serversocket ) {
tai6464 t, next_timeout_check;
void handle_accept( int64 serversocket ) {
struct http_data* h;
unsigned char ip[4];
uint16 port;
tai6464 t;
int64 i;
io_wantread( serversocket );
taia_now( &next_timeout_check );
while( ( i = socket_accept4( serversocket, (char*)ip, &port) ) != -1 ) {
for (;;) {
int64 i;
int handled_connections = 1024;
if( !io_fd( i ) ||
!(h = (struct http_data*)malloc(sizeof(struct http_data))) ) {
io_close( i );
continue;
}
taia_now(&t);
taia_addsec(&t,&t,OT_CLIENT_TIMEOUT_CHECKINTERVAL);
io_waituntil(t);
io_wantread( i );
byte_zero(h,sizeof(struct http_data));
memmove(h->ip,ip,sizeof(ip));
io_setcookie(i,h);
++ot_overall_connections;
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 ) {
struct http_data* h=io_getcookie(i);
if( h ) {
@ -444,40 +456,33 @@ void server_mainloop( int64 serversocket ) {
}
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 ) {
handle_read( i, 0 );
continue;
}
io_wantread( serversocket );
taia_now( &next_timeout_check );
// Attention, i changes from what io_canread() returned to
// what socket_accept4 returns as new socket
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 );
for (;;) {
int64 i;
if (h) {
byte_zero(h,sizeof(struct http_data));
memmove(h->ip,ip,sizeof(ip));
io_setcookie(i,h);
++ot_overall_connections;
handle_read(i,1);
} else
io_close(i);
} else
io_close(i);
}
taia_now(&t);
taia_addsec(&t,&t,OT_CLIENT_TIMEOUT_CHECKINTERVAL);
io_waituntil(t);
if( errno==EAGAIN )
io_eagain( serversocket );
while( ( i = io_canread() ) != -1 ) {
if( i == serversocket )
handle_accept( i );
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