diff options
author | Andreas Baumann <abaumann@yahoo.com> | 2009-04-04 11:21:02 +0200 |
---|---|---|
committer | Andreas Baumann <abaumann@yahoo.com> | 2009-04-04 11:21:02 +0200 |
commit | 508c96cba2aac136deaa073eda989a13e8e948d0 (patch) | |
tree | 6e98a15e7079c876cbf3bf656524e374ce80fc7c /tests | |
parent | 4eeabfd1ede65dc89299e7b7ddb6d8a69e0450ff (diff) | |
download | wolfbones-508c96cba2aac136deaa073eda989a13e8e948d0.tar.gz wolfbones-508c96cba2aac136deaa073eda989a13e8e948d0.tar.bz2 |
simple network test client seems to work fine
Diffstat (limited to 'tests')
-rw-r--r-- | tests/network/test1.c | 105 | ||||
-rw-r--r-- | tests/network/test1.req | 3 |
2 files changed, 80 insertions, 28 deletions
diff --git a/tests/network/test1.c b/tests/network/test1.c index b5b2aa8..4a4bf3d 100644 --- a/tests/network/test1.c +++ b/tests/network/test1.c @@ -12,6 +12,8 @@ #include <assert.h> /* for assertions */ #include <signal.h> /* for signal */ +#define DEBUG 0 + static bool wolf_network_sock_nonblocking( int fd ) { int flags; @@ -23,9 +25,30 @@ static bool wolf_network_sock_nonblocking( int fd ) { return true; } +#if DEBUG +static char *fd_set_to_string( fd_set *fd, int min_fd, int max_fd, char *buf, size_t buflen ) { + int i; + size_t j; + + assert( min_fd <= max_fd ); + + for( i = min_fd, j = 0; i <= max_fd && j < buflen; i++, j++ ) { + if( FD_ISSET( i, fd ) ) { + buf[j] = '1'; + } else { + buf[j] = '0'; + } + } + + buf[j] = '\0'; + + return buf; +} +#endif + #define MAX_IDLE_TIMEOUT 10 -#define WRITE_BUFFER_SIZE 1024 -#define READ_BUFFER_SIZE 1024 +#define WRITE_BUFFER_SIZE 4096 +#define READ_BUFFER_SIZE 4096 #define max(a,b) ((a) < (b) ? (b) : (a)) @@ -56,6 +79,9 @@ int main( int argc, char *argv[] ) { size_t total_fd_written; size_t total_stdout_written; int count; + bool stdin_eof; + bool fd_write_closed; + bool fd_eof; if( argc != 3 ) { fprintf( stderr, "usage: test1 <host> <port>\n" ); @@ -124,6 +150,9 @@ int main( int argc, char *argv[] ) { total_fd_read = 0; total_fd_written = 0; total_stdout_written = 0; + stdin_eof = false; + fd_write_closed = false; + fd_eof = false; signal( SIGTERM, term_handler ); signal( SIGINT, term_handler ); @@ -133,13 +162,15 @@ int main( int argc, char *argv[] ) { int max_fd; fd_set read_set; fd_set write_set; - fd_set error_set; struct timeval timeout; ssize_t rres; +#if DEBUG + char buf[10]; + char buf2[10]; +#endif FD_ZERO( &read_set ); FD_ZERO( &write_set ); - FD_ZERO( &error_set ); timeout.tv_sec = 1; timeout.tv_usec = 0; @@ -149,24 +180,36 @@ int main( int argc, char *argv[] ) { max_fd = max( max_fd, STDOUT_FILENO ); max_fd = max( max_fd, fd ); - if( fd_to_write == 0 ) { + if( !stdin_eof && fd_to_write == 0 ) { FD_SET( STDIN_FILENO, &read_set ); } - FD_SET( STDIN_FILENO, &error_set ); if( stdout_to_write > 0 ) { FD_SET( STDOUT_FILENO, &write_set ); } - FD_SET( STDOUT_FILENO, &error_set ); if( stdout_to_write == 0 ) { FD_SET( fd, &read_set ); } if( fd_to_write > 0 ) { FD_SET( fd, &write_set ); } - FD_SET( fd, &error_set ); /* wait for events or timeout */ - res = select( max_fd + 1, &read_set, &write_set, &error_set, &timeout ); +#if DEBUG +fprintf( stderr, "select call read_fd: %s write_fd: %s wbuf: %d rbuf: %d stdin_eof: %d fdwcl: %d\n", + fd_set_to_string( &read_set, 0, max_fd, buf, 10 ), + fd_set_to_string( &write_set, 0, max_fd, buf2, 10 ), + fd_to_write, stdout_to_write, stdin_eof, fd_write_closed ); +#endif + + res = select( max_fd + 1, &read_set, &write_set, NULL, &timeout ); + +#if DEBUG +fprintf( stderr, "select %04d read_fd: %s write_fd: %s wbuf: %d rbuf: %d stdin_eof: %d fdwcl: %d\n", + res, fd_set_to_string( &read_set, 0, max_fd, buf, 10 ), + fd_set_to_string( &write_set, 0, max_fd, buf2, 10 ), + fd_to_write, stdout_to_write, stdin_eof, fd_write_closed ); +#endif + if( res < 0 ) { if( errno == EINTR || errno == EAGAIN ) { /* skip */ @@ -189,17 +232,6 @@ int main( int argc, char *argv[] ) { /* something happened */ idle_secs = 0; - /* handle error conditions */ - if( FD_ISSET( STDOUT_FILENO, &error_set ) ) { - fprintf( stderr, "Error flag on stdout!\n" ); - } - if( FD_ISSET( STDIN_FILENO, &error_set ) ) { - fprintf( stderr, "Error flag on stdin!\n" ); - } - if( FD_ISSET( fd, &error_set ) ) { - fprintf( stderr, "Error flag on socket!\n" ); - } - /* empty the socket read buffer to stdout */ if( FD_ISSET( STDOUT_FILENO, &write_set ) ) { assert( stdout_to_write > 0 ); @@ -259,6 +291,11 @@ int main( int argc, char *argv[] ) { } } else if( rres == 0 ) { /* EOF on socket */ + shutdown( fd, SHUT_RD ); + fd_eof = true; +#if DEBUG + fprintf( stderr, "EOF on socket\n" ); +#endif } else { assert( rres > 0 ); stdout_written = 0; @@ -282,6 +319,12 @@ int main( int argc, char *argv[] ) { } } else if( rres == 0 ) { /* EOF on STDIN */ + stdin_eof = true; + fclose( stdin ); + +#if DEBUG + fprintf( stderr, "EOF on stdin\n" ); +#endif } else { assert( rres > 0 ); fd_written = 0; @@ -289,19 +332,31 @@ int main( int argc, char *argv[] ) { total_stdin_read += (size_t)rres; } } + + /* close writing side of the socket if we have emptied + * the socket write buffer and there is nothing more on + * the input (stdin) + */ + if( fd_to_write == 0 && stdin_eof && !fd_write_closed ) { +#if DEBUG + fprintf( stderr, "socket SHUT_WR\n" ); +#endif + shutdown( fd, SHUT_WR ); + fd_write_closed = true; + } } count++; - if( count % 1000 == 0 ) { - fprintf( stderr, "Transfered stdin: %d, stdout: %d, fd-in: %d, fd-out: %d\r", + if( count % 10000 == 0 ) { + fprintf( stderr, "Transfered stdin: %d, stdout: %d, fd-in: %d, fd-out: %d\n", total_stdin_read, total_stdout_written, total_fd_read, total_fd_written ); } - } while( !terminate ); + } while( !terminate && !( stdin_eof && fd_eof ) ); - fprintf( stderr, "Terminated with stdin: %d, stdout: %d, fd-in: %d, fd-out: %d\n", +END: + fprintf( stderr, "Terminated stdin: %d, stdout: %d, fd-in: %d, fd-out: %d\n", total_stdin_read, total_stdout_written, total_fd_read, total_fd_written ); -END: res = close( fd ); if( res < 0 ) { fprintf( stderr, "close failed: %s (%d)\n", strerror( errno ), errno ); diff --git a/tests/network/test1.req b/tests/network/test1.req deleted file mode 100644 index 708e8b1..0000000 --- a/tests/network/test1.req +++ /dev/null @@ -1,3 +0,0 @@ -GET / HTTP/1.1 -Host: europa5 - |