From 043f9af260f87d5ffbff0dd1e139f4a146b98b23 Mon Sep 17 00:00:00 2001 From: Keegan McAllister Date: Mon, 30 Apr 2012 23:32:13 -0400 Subject: [PATCH] Use Select in examples --- src/examples/benchmark.cc | 1 - src/examples/ntester.cc | 30 +++++++++++++--------------- src/examples/parse.cc | 24 ++++++++++------------- src/examples/termemu.cc | 41 ++++++++++++++++----------------------- 4 files changed, 40 insertions(+), 56 deletions(-) diff --git a/src/examples/benchmark.cc b/src/examples/benchmark.cc index d3f5748..df2b7e4 100644 --- a/src/examples/benchmark.cc +++ b/src/examples/benchmark.cc @@ -24,7 +24,6 @@ #include #include #include -#include #include #include #include diff --git a/src/examples/ntester.cc b/src/examples/ntester.cc index 878d824..734263a 100644 --- a/src/examples/ntester.cc +++ b/src/examples/ntester.cc @@ -18,11 +18,11 @@ #include #include -#include #include "user.h" #include "fatal_assert.h" #include "networktransport.cc" +#include "select.h" using namespace Network; @@ -58,20 +58,19 @@ int main( int argc, char *argv[] ) fprintf( stderr, "Port bound is %d, key is %s\n", n->port(), n->get_key().c_str() ); if ( server ) { - struct pollfd my_pollfd; - my_pollfd.fd = n->fd(); - my_pollfd.events = POLLIN; + Select sel; + sel.add_fd( n->fd() ); uint64_t last_num = n->get_remote_state_num(); while ( true ) { try { - if ( poll( &my_pollfd, 1, n->wait_time() ) < 0 ) { - perror( "poll" ); + if ( sel.select( n->wait_time() ) < 0 ) { + perror( "select" ); exit( 1 ); } n->tick(); - if ( my_pollfd.revents & POLLIN ) { + if ( sel.read( n->fd() ) ) { n->recv(); if ( n->get_remote_state_num() != last_num ) { @@ -101,28 +100,25 @@ int main( int argc, char *argv[] ) exit( 1 ); } - struct pollfd fds[ 2 ]; - fds[ 0 ].fd = STDIN_FILENO; - fds[ 0 ].events = POLLIN; - - fds[ 1 ].fd = n->fd(); - fds[ 1 ].events = POLLIN; + Select sel; + sel.add_fd( STDIN_FILENO ); + sel.add_fd( n->fd() ); while( true ) { try { - if ( poll( fds, 2, n->wait_time() ) < 0 ) { - perror( "poll" ); + if ( sel.select( n->wait_time() ) < 0 ) { + perror( "select" ); } n->tick(); - if ( fds[ 0 ].revents & POLLIN ) { + if ( sel.read( STDIN_FILENO ) ) { char x; fatal_assert( read( STDIN_FILENO, &x, 1 ) == 1 ); n->get_current_state().push_back( Parser::UserByte( x ) ); } - if ( fds[ 1 ].revents & POLLIN ) { + if ( sel.read( n->fd() ) ) { n->recv(); } } catch ( NetworkException e ) { diff --git a/src/examples/parse.cc b/src/examples/parse.cc index f507eec..84bd245 100644 --- a/src/examples/parse.cc +++ b/src/examples/parse.cc @@ -23,7 +23,6 @@ #include #include #include -#include #include #include #include @@ -43,6 +42,7 @@ #include "swrite.h" #include "locale_utils.h" #include "fatal_assert.h" +#include "select.h" const size_t buf_size = 1024; @@ -121,34 +121,30 @@ int main( int argc __attribute__((unused)), void emulate_terminal( int fd ) { Parser::UTF8Parser parser; - struct pollfd pollfds[ 2 ]; - pollfds[ 0 ].fd = STDIN_FILENO; - pollfds[ 0 ].events = POLLIN; - - pollfds[ 1 ].fd = fd; - pollfds[ 1 ].events = POLLIN; + Select sel; + sel.add_fd( STDIN_FILENO ); + sel.add_fd( fd ); while ( 1 ) { - int active_fds = poll( pollfds, 2, -1 ); + int active_fds = sel.select( -1 ); if ( active_fds <= 0 ) { - perror( "poll" ); + perror( "select" ); return; } - if ( pollfds[ 0 ].revents & POLLIN ) { + if ( sel.read( STDIN_FILENO ) ) { if ( copy( STDIN_FILENO, fd ) < 0 ) { return; } - } else if ( pollfds[ 1 ].revents & POLLIN ) { + } else if ( sel.read( fd ) ) { if ( vt_parser( fd, &parser ) < 0 ) { return; } - } else if ( (pollfds[ 0 ].revents | pollfds[ 1 ].revents) - & (POLLERR | POLLHUP | POLLNVAL) ) { + } else if ( sel.error( STDIN_FILENO ) || sel.error( fd ) ) { return; } else { - fprintf( stderr, "poll mysteriously woken up\n" ); + fprintf( stderr, "select mysteriously woken up\n" ); } } } diff --git a/src/examples/termemu.cc b/src/examples/termemu.cc index 8d3296c..55bf883 100644 --- a/src/examples/termemu.cc +++ b/src/examples/termemu.cc @@ -23,7 +23,6 @@ #include #include #include -#include #include #include #include @@ -51,6 +50,7 @@ #include "fatal_assert.h" #include "locale_utils.h" #include "sigfd.h" +#include "select.h" /* For newer skalibs */ extern "C" { @@ -190,7 +190,7 @@ bool tick( Terminal::Framebuffer &state, Terminal::Framebuffer &new_frame, 3. Resize events (from a SIGWINCH signal) get turned into "Resize" actions and applied to the terminal. - At every event from poll(), we run the tick() function to + At every event from select(), we run the tick() function to possibly print a new frame (if we haven't printed one in the last 1/50 second). The new frames are "differential" -- they assume the previous frame was sent to the real terminal. @@ -227,36 +227,30 @@ void emulate_terminal( int fd ) /* open display */ Terminal::Display display( true ); /* use TERM to initialize */ - struct pollfd pollfds[ 3 ]; - - pollfds[ 0 ].fd = STDIN_FILENO; - pollfds[ 0 ].events = POLLIN; - - pollfds[ 1 ].fd = fd; - pollfds[ 1 ].events = POLLIN; - - pollfds[ 2 ].fd = signal_fd; - pollfds[ 2 ].events = POLLIN; + Select sel; + sel.add_fd( STDIN_FILENO ); + sel.add_fd( fd ); + sel.add_fd( signal_fd ); swrite( STDOUT_FILENO, Terminal::Emulator::open().c_str() ); - int poll_timeout = -1; + int timeout = -1; while ( 1 ) { - int active_fds = poll( pollfds, 3, poll_timeout ); + int active_fds = sel.select( timeout ); if ( active_fds < 0 && errno == EINTR ) { continue; } else if ( active_fds < 0 ) { - perror( "poll" ); + perror( "select" ); break; } - if ( pollfds[ 0 ].revents & POLLIN ) { + if ( sel.read( STDIN_FILENO ) ) { /* input from user */ char buf[ buf_size ]; /* fill buffer if possible */ - ssize_t bytes_read = read( pollfds[ 0 ].fd, buf, buf_size ); + ssize_t bytes_read = read( STDIN_FILENO, buf, buf_size ); if ( bytes_read == 0 ) { /* EOF */ return; } else if ( bytes_read < 0 ) { @@ -274,12 +268,12 @@ void emulate_terminal( int fd ) if ( swrite( fd, terminal_to_host.c_str(), terminal_to_host.length() ) < 0 ) { break; } - } else if ( pollfds[ 1 ].revents & POLLIN ) { + } else if ( sel.read( fd ) ) { /* input from host */ char buf[ buf_size ]; /* fill buffer if possible */ - ssize_t bytes_read = read( pollfds[ 1 ].fd, buf, buf_size ); + ssize_t bytes_read = read( fd, buf, buf_size ); if ( bytes_read == 0 ) { /* EOF */ return; } else if ( bytes_read < 0 ) { @@ -291,7 +285,7 @@ void emulate_terminal( int fd ) if ( swrite( fd, terminal_to_host.c_str(), terminal_to_host.length() ) < 0 ) { break; } - } else if ( pollfds[ 2 ].revents & POLLIN ) { + } else if ( sel.read( signal_fd ) ) { /* resize */ fatal_assert( sigfd_read() == SIGWINCH ); @@ -310,17 +304,16 @@ void emulate_terminal( int fd ) perror( "ioctl TIOCSWINSZ" ); return; } - } else if ( (pollfds[ 0 ].revents | pollfds[ 1 ].revents) - & (POLLERR | POLLHUP | POLLNVAL) ) { + } else if ( sel.error( STDIN_FILENO ) || sel.error( fd ) ) { break; } Terminal::Framebuffer new_frame( complete.get_fb() ); if ( tick( state, new_frame, display ) ) { /* there was a frame */ - poll_timeout = -1; + timeout = -1; } else { - poll_timeout = 20; + timeout = 20; } }