diff --git a/network.hpp b/network.hpp index 1c03a81..1a1d873 100644 --- a/network.hpp +++ b/network.hpp @@ -80,6 +80,7 @@ namespace Network { int port( void ); string get_key( void ) { return key.printable_key(); } + bool get_attached( void ) { return attached; } }; } diff --git a/networktransport.cpp b/networktransport.cpp index 04e38dc..03f4ee3 100644 --- a/networktransport.cpp +++ b/networktransport.cpp @@ -77,11 +77,19 @@ void Transport::send_to_receiver( void ) sequence of diffs (this time limited by MTU) that bring us to that state. */ + if ( !connection.get_attached() ) { + return; + } + MyState target_receiver_state( assumed_receiver_state->state ); target_receiver_state.apply_string( current_state.diff_from( target_receiver_state, -1 ) ); if ( assumed_receiver_state->state == target_receiver_state ) { /* send empty ack */ + if ( timestamp() - sent_states.back().timestamp < int64_t( ACK_INTERVAL ) ) { + return; + } + Instruction inst( assumed_receiver_state->num, assumed_receiver_state->num, received_states.back().num, @@ -221,7 +229,7 @@ void Transport::recv( void ) } if ( !found ) { - fprintf( stderr, "Ignoring out-of-order packet. Reference state %d has been discarded.\n", int(inst.old_num) ); + // fprintf( stderr, "Ignoring out-of-order packet. Reference state %d has been discarded or hasn't yet been received.\n", int(inst.old_num) ); return; } diff --git a/networktransport.hpp b/networktransport.hpp index bb7cbe3..0a32d0f 100644 --- a/networktransport.hpp +++ b/networktransport.hpp @@ -50,7 +50,8 @@ namespace Network { private: static const int INITIAL_TIMEOUT = 1000; /* ms, same as TCP */ static const int SEND_INTERVAL = 20; /* ms between frames */ - static const int HEADER_LEN = 80; + static const int ACK_INTERVAL = 1000; /* ms between empty acks */ + static const int HEADER_LEN = 100; /* helper methods for tick() */ void update_assumed_receiver_state( void ); diff --git a/ntester.cpp b/ntester.cpp index abd3a47..a626b4f 100644 --- a/ntester.cpp +++ b/ntester.cpp @@ -52,16 +52,32 @@ 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; + uint64_t last_num = n->get_remote_state_num(); while ( true ) { try { - n->recv(); n->tick(); - fprintf( stderr, "Num: %d. Contents: ", - (int)n->get_remote_state_num() ); - for ( size_t i = 0; i < n->get_remote_state().user_bytes.size(); i++ ) { - fprintf( stderr, "%c", n->get_remote_state().user_bytes[ i ] ); + + if ( poll( &my_pollfd, 1, 10 ) < 0 ) { + perror( "poll" ); + exit( 1 ); + } + + if ( my_pollfd.revents & POLLIN ) { + n->recv(); + + if ( n->get_remote_state_num() != last_num ) { + fprintf( stderr, "Num: %d. Contents: ", + (int)n->get_remote_state_num() ); + for ( size_t i = 0; i < n->get_remote_state().user_bytes.size(); i++ ) { + fprintf( stderr, "%c", n->get_remote_state().user_bytes[ i ] ); + } + fprintf( stderr, "\n" ); + last_num = n->get_remote_state_num(); + } } - fprintf( stderr, "\n" ); } catch ( CryptoException e ) { fprintf( stderr, "Cryptographic error: %s\n", e.text.c_str() ); } @@ -84,16 +100,29 @@ 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; + while( true ) { - char x = getchar(); - - n->get_current_state().key_hit( x ); - try { - if ( readable( n->fd() ) ) { + n->tick(); + + if ( poll( fds, 2, 10 ) < 0 ) { + perror( "poll" ); + } + + if ( fds[ 0 ].revents & POLLIN ) { + char x = getchar(); + n->get_current_state().key_hit( x ); + } + + if ( fds[ 1 ].revents & POLLIN ) { n->recv(); } - n->tick(); } catch ( Network::NetworkException e ) { fprintf( stderr, "%s: %s\r\n", e.function.c_str(), strerror( e.the_errno ) ); break;