Successfully synchronizes state. Here testing with 50% bidi packet loss.

This commit is contained in:
Keith Winstein
2011-08-09 22:36:22 -04:00
parent 188c44f5be
commit 66a64f0b22
6 changed files with 193 additions and 37 deletions
+32 -9
View File
@@ -1,9 +1,25 @@
#include <termios.h>
#include <unistd.h>
#include <poll.h>
#include "keystroke.hpp"
#include "networktransport.hpp"
bool readable( int fd )
{
struct pollfd my_pollfd;
my_pollfd.fd = fd;
my_pollfd.events = POLLIN;
int num = poll( &my_pollfd, 1, 0 );
if ( num < 0 ) {
perror( "poll" );
exit( 1 );
}
return my_pollfd.revents & POLLIN;
}
int main( int argc, char *argv[] )
{
bool server = true;
@@ -11,7 +27,7 @@ int main( int argc, char *argv[] )
char *ip;
int port;
KeyStroke user;
KeyStroke me, remote;
Network::Transport<KeyStroke, KeyStroke> *n;
@@ -24,9 +40,9 @@ int main( int argc, char *argv[] )
ip = argv[ 2 ];
port = atoi( argv[ 3 ] );
n = new Network::Transport<KeyStroke, KeyStroke>( user, key, ip, port );
n = new Network::Transport<KeyStroke, KeyStroke>( me, remote, key, ip, port );
} else {
n = new Network::Transport<KeyStroke, KeyStroke>( user );
n = new Network::Transport<KeyStroke, KeyStroke>( me, remote );
}
} catch ( CryptoException e ) {
fprintf( stderr, "Fatal error: %s\n", e.text.c_str() );
@@ -36,18 +52,20 @@ 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 ) {
/*
while ( true ) {
try {
string s = n->recv();
printf( "%s", s.c_str() );
fflush( NULL );
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 ] );
}
fprintf( stderr, "\n" );
} catch ( CryptoException e ) {
fprintf( stderr, "Cryptographic error: %s\n", e.text.c_str() );
}
}
*/
sleep( 6000 );
} else {
struct termios saved_termios;
struct termios the_termios;
@@ -72,10 +90,15 @@ int main( int argc, char *argv[] )
n->get_current_state().key_hit( x );
try {
if ( readable( n->fd() ) ) {
n->recv();
}
n->tick();
} catch ( Network::NetworkException e ) {
fprintf( stderr, "%s: %s\r\n", e.function.c_str(), strerror( e.the_errno ) );
break;
} catch ( CryptoException e ) {
fprintf( stderr, "Cryptographic error: %s\n", e.text.c_str() );
}
}