-
Notifications
You must be signed in to change notification settings - Fork 8
/
datagrump-sender.cc
89 lines (75 loc) · 2.37 KB
/
datagrump-sender.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <poll.h>
#include "socket.hh"
#include "controller.hh"
using namespace std;
using namespace Network;
int main( int argc, char *argv[] )
{
/* check arguments */
bool debug = false;
if ( argc == 4 && string( argv[ 3 ] ) == "debug" ) {
debug = true;
} else if ( argc == 3 ) {
/* do nothing */
} else {
fprintf( stderr, "Usage: %s IP PORT [debug]\n", argv[ 0 ] );
exit( 1 );
}
try {
/* Fill in destination address from command line arguments */
Address destination( argv[ 1 ] /* ip */, argv[ 2 ] /* port */ );
fprintf( stderr, "Sending packets to %s:%d.\n",
destination.ip().c_str(), destination.port() );
/* Create UDP socket for outgoing datagrams. */
Network::Socket sock;
/* Initialize packet counters */
uint64_t sequence_number = 0;
uint64_t next_ack_expected = 0;
/* Initialize flow controller */
Controller controller( debug );
/* Loop */
while ( 1 ) {
/* Ask controller: what is the window size? */
unsigned int window_size = controller.window_size();
/* fill up window */
while ( sequence_number - next_ack_expected < window_size ) {
Packet x( destination, sequence_number++ );
sock.send( x );
controller.packet_was_sent( x.sequence_number(),
x.send_timestamp() );
}
/* Wait for acknowledgement or timeout */
struct pollfd fd = { sock.fd(), POLLIN, 0 };
int packet_received = poll( &fd, 1, controller.timeout_ms() );
if ( packet_received < 0 ) { /* error */
perror( "poll" );
throw string( "poll returned error." );
} else if ( packet_received == 0 ) { /* timeout */
/* send a packet */
Packet x( destination, sequence_number++ );
sock.send( x );
controller.packet_was_sent( x.sequence_number(),
x.send_timestamp() );
} else {
/* we got an acknowledgment */
Packet ack = sock.recv();
/* update our counter */
next_ack_expected = max( next_ack_expected,
ack.ack_sequence_number() + 1 );
/* tell the controller */
controller.ack_received( ack.ack_sequence_number(),
ack.ack_send_timestamp(),
ack.ack_recv_timestamp(),
ack.recv_timestamp() );
}
}
} catch ( const string & exception ) {
/* We got an exception, so quit. */
fprintf( stderr, "Exiting on exception: %s\n", exception.c_str() );
exit( 1 );
}
return 0;
}