Split out functions in STMClient

This commit is contained in:
Keith Winstein
2011-09-30 01:27:40 -04:00
parent bbc78dbd0c
commit 0d85b410fe
2 changed files with 123 additions and 66 deletions
+88 -62
View File
@@ -61,7 +61,7 @@ void STMClient::shutdown( void )
} }
} }
void STMClient::main( void ) void STMClient::main_init( void )
{ {
/* establish WINCH fd and start listening for signal */ /* establish WINCH fd and start listening for signal */
sigset_t signal_mask; sigset_t signal_mask;
@@ -71,7 +71,7 @@ void STMClient::main( void )
/* stop "ignoring" WINCH signal */ /* stop "ignoring" WINCH signal */
assert( sigprocmask( SIG_BLOCK, &signal_mask, NULL ) == 0 ); assert( sigprocmask( SIG_BLOCK, &signal_mask, NULL ) == 0 );
int winch_fd = signalfd( -1, &signal_mask, 0 ); winch_fd = signalfd( -1, &signal_mask, 0 );
if ( winch_fd < 0 ) { if ( winch_fd < 0 ) {
perror( "signalfd" ); perror( "signalfd" );
return; return;
@@ -87,14 +87,13 @@ void STMClient::main( void )
/* don't let signals kill us */ /* don't let signals kill us */
assert( sigprocmask( SIG_BLOCK, &signal_mask, NULL ) == 0 ); assert( sigprocmask( SIG_BLOCK, &signal_mask, NULL ) == 0 );
int shutdown_signal_fd = signalfd( -1, &signal_mask, 0 ); shutdown_signal_fd = signalfd( -1, &signal_mask, 0 );
if ( shutdown_signal_fd < 0 ) { if ( shutdown_signal_fd < 0 ) {
perror( "signalfd" ); perror( "signalfd" );
return; return;
} }
/* get initial window size */ /* get initial window size */
struct winsize window_size;
if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) { if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) {
perror( "ioctl TIOCGWINSZ" ); perror( "ioctl TIOCGWINSZ" );
return; return;
@@ -109,16 +108,87 @@ void STMClient::main( void )
/* open network */ /* open network */
Network::UserStream blank; Network::UserStream blank;
Network::Transport< Network::UserStream, Terminal::Complete > network( blank, terminal, network = new Network::Transport< Network::UserStream, Terminal::Complete >( blank, terminal,
key.c_str(), ip.c_str(), port ); key.c_str(), ip.c_str(), port );
/* tell server the size of the terminal */ /* tell server the size of the terminal */
network.get_current_state().push_back( Parser::Resize( window_size.ws_col, window_size.ws_row ) ); network->get_current_state().push_back( Parser::Resize( window_size.ws_col, window_size.ws_row ) );
}
bool STMClient::process_network_input( void )
{
network->recv();
/* is a new frame available from the terminal? */
if ( network->get_remote_state_num() != last_remote_num ) {
string diff = network->get_remote_diff();
swrite( STDOUT_FILENO, diff.data(), diff.size() );
}
return true;
}
bool STMClient::process_user_input( int fd )
{
const int buf_size = 16384;
char buf[ buf_size ];
/* fill buffer if possible */
ssize_t bytes_read = read( fd, buf, buf_size );
if ( bytes_read == 0 ) { /* EOF */
return false;
} else if ( bytes_read < 0 ) {
perror( "read" );
return false;
}
if ( !network->shutdown_in_progress() ) {
for ( int i = 0; i < bytes_read; i++ ) {
network->get_current_state().push_back( Parser::UserByte( buf[ i ] ) );
}
}
return true;
}
bool STMClient::process_resize( void )
{
struct signalfd_siginfo info;
assert( read( winch_fd, &info, sizeof( info ) ) == sizeof( info ) );
assert( info.ssi_signo == SIGWINCH );
/* get new size */
if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) {
perror( "ioctl TIOCGWINSZ" );
return false;
}
/* tell remote emulator */
Parser::Resize res( window_size.ws_col, window_size.ws_row );
if ( !network->shutdown_in_progress() ) {
network->get_current_state().push_back( res );
}
/* tell local emulator -- there is probably a safer way to do this */
for ( auto i = network->begin();
i != network->end();
i++ ) {
i->state.act( &res );
}
return true;
}
void STMClient::main( void )
{
/* initialize signal handling and structures */
main_init();
/* prepare to poll for events */ /* prepare to poll for events */
struct pollfd pollfds[ 4 ]; struct pollfd pollfds[ 4 ];
pollfds[ 0 ].fd = network.fd(); pollfds[ 0 ].fd = network->fd();
pollfds[ 0 ].events = POLLIN; pollfds[ 0 ].events = POLLIN;
pollfds[ 1 ].fd = STDIN_FILENO; pollfds[ 1 ].fd = STDIN_FILENO;
@@ -130,11 +200,11 @@ void STMClient::main( void )
pollfds[ 3 ].fd = shutdown_signal_fd; pollfds[ 3 ].fd = shutdown_signal_fd;
pollfds[ 3 ].events = POLLIN; pollfds[ 3 ].events = POLLIN;
uint64_t last_remote_num = network.get_remote_state_num(); last_remote_num = network->get_remote_state_num();
while ( 1 ) { while ( 1 ) {
try { try {
int active_fds = poll( pollfds, 4, network.wait_time() ); int active_fds = poll( pollfds, 4, network->wait_time() );
if ( active_fds < 0 ) { if ( active_fds < 0 ) {
perror( "poll" ); perror( "poll" );
break; break;
@@ -142,67 +212,23 @@ void STMClient::main( void )
if ( pollfds[ 0 ].revents & POLLIN ) { if ( pollfds[ 0 ].revents & POLLIN ) {
/* packet received from the network */ /* packet received from the network */
network.recv(); if ( !process_network_input() ) { return; }
/* is a new frame available from the terminal? */
if ( network.get_remote_state_num() != last_remote_num ) {
string diff = network.get_remote_diff();
swrite( STDOUT_FILENO, diff.data(), diff.size() );
}
} }
if ( pollfds[ 1 ].revents & POLLIN ) { if ( pollfds[ 1 ].revents & POLLIN ) {
/* input from the user needs to be fed to the network */ /* input from the user needs to be fed to the network */
const int buf_size = 16384; if ( !process_user_input( pollfds[ 1 ].fd ) ) { return; }
char buf[ buf_size ];
/* fill buffer if possible */
ssize_t bytes_read = read( pollfds[ 1 ].fd, buf, buf_size );
if ( bytes_read == 0 ) { /* EOF */
return;
} else if ( bytes_read < 0 ) {
perror( "read" );
return;
}
if ( !network.shutdown_in_progress() ) {
for ( int i = 0; i < bytes_read; i++ ) {
network.get_current_state().push_back( Parser::UserByte( buf[ i ] ) );
}
}
} }
if ( pollfds[ 2 ].revents & POLLIN ) { if ( pollfds[ 2 ].revents & POLLIN ) {
/* resize */ /* resize */
struct signalfd_siginfo info; if ( !process_resize() ) { return; }
assert( read( winch_fd, &info, sizeof( info ) ) == sizeof( info ) );
assert( info.ssi_signo == SIGWINCH );
/* get new size */
if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) {
perror( "ioctl TIOCGWINSZ" );
return;
}
/* tell remote emulator */
Parser::Resize res( window_size.ws_col, window_size.ws_row );
if ( !network.shutdown_in_progress() ) {
network.get_current_state().push_back( res );
}
/* tell local emulator -- there is probably a safer way to do this */
for ( list< Network::TimestampedState<Terminal::Complete> >::iterator i = network.begin();
i != network.end();
i++ ) {
i->state.act( &res );
}
} }
if ( pollfds[ 3 ].revents & POLLIN ) { if ( pollfds[ 3 ].revents & POLLIN ) {
/* shutdown signal */ /* shutdown signal */
if ( network.attached() ) { if ( network->attached() ) {
network.start_shutdown(); network->start_shutdown();
} else { } else {
break; break;
} }
@@ -217,20 +243,20 @@ void STMClient::main( void )
if ( (pollfds[ 1 ].revents) if ( (pollfds[ 1 ].revents)
& (POLLERR | POLLHUP | POLLNVAL) ) { & (POLLERR | POLLHUP | POLLNVAL) ) {
/* user problem */ /* user problem */
network.start_shutdown(); network->start_shutdown();
} }
/* quit if our shutdown has been acknowledged */ /* quit if our shutdown has been acknowledged */
if ( network.shutdown_in_progress() && network.shutdown_acknowledged() ) { if ( network->shutdown_in_progress() && network->shutdown_acknowledged() ) {
break; break;
} }
/* quit if we received and acknowledged a shutdown request */ /* quit if we received and acknowledged a shutdown request */
if ( network.counterparty_shutdown_ack_sent() ) { if ( network->counterparty_shutdown_ack_sent() ) {
break; break;
} }
network.tick(); network->tick();
} catch ( Network::NetworkException e ) { } catch ( Network::NetworkException e ) {
fprintf( stderr, "%s: %s\r\n", e.function.c_str(), strerror( e.the_errno ) ); fprintf( stderr, "%s: %s\r\n", e.function.c_str(), strerror( e.the_errno ) );
sleep( 1 ); sleep( 1 );
+32 -1
View File
@@ -1,9 +1,14 @@
#ifndef STM_CLIENT_HPP #ifndef STM_CLIENT_HPP
#define STM_CLIENT_HPP #define STM_CLIENT_HPP
#include <sys/ioctl.h>
#include <termios.h> #include <termios.h>
#include <string> #include <string>
#include "completeterminal.hpp"
#include "networktransport.hpp"
#include "user.hpp"
class STMClient { class STMClient {
private: private:
std::string ip; std::string ip;
@@ -12,15 +17,41 @@ private:
struct termios saved_termios, raw_termios; struct termios saved_termios, raw_termios;
int winch_fd, shutdown_signal_fd;
struct winsize window_size;
Network::Transport< Network::UserStream, Terminal::Complete > *network;
uint64_t last_remote_num;
void main_init( void );
bool process_network_input( void );
bool process_user_input( int fd );
bool process_resize( void );
public: public:
STMClient( const char *s_ip, int s_port, const char *s_key ) STMClient( const char *s_ip, int s_port, const char *s_key )
: ip( s_ip ), port( s_port ), key( s_key ), : ip( s_ip ), port( s_port ), key( s_key ),
saved_termios(), raw_termios() saved_termios(), raw_termios(),
winch_fd(), shutdown_signal_fd(),
window_size(),
network( NULL ),
last_remote_num( -1 )
{} {}
void init( void ); void init( void );
void shutdown( void ); void shutdown( void );
void main( void ); void main( void );
~STMClient()
{
if ( network != NULL ) {
delete network;
}
}
/* unused */
STMClient( const STMClient & );
STMClient & operator=( const STMClient & );
}; };
#endif #endif