| 22 | using boost::asio::ip::udp; |
| 23 | |
| 24 | int main(int argc, char* argv[]) |
| 25 | { |
| 26 | try |
| 27 | { |
| 28 | if (argc != 3) |
| 29 | { |
| 30 | std::cerr << "Usage: client <host> <port>\n"; |
| 31 | return 1; |
| 32 | } |
| 33 | using namespace std; // For atoi. |
| 34 | std::string host_name = argv[1]; |
| 35 | std::string port = argv[2]; |
| 36 | |
| 37 | boost::asio::io_context io_context; |
| 38 | |
| 39 | // Determine the location of the server. |
| 40 | tcp::resolver resolver(io_context); |
| 41 | tcp::endpoint remote_endpoint = *resolver.resolve(host_name, port).begin(); |
| 42 | |
| 43 | // Establish the control connection to the server. |
| 44 | tcp::socket control_socket(io_context); |
| 45 | control_socket.connect(remote_endpoint); |
| 46 | |
| 47 | // Create a datagram socket to receive data from the server. |
| 48 | udp::socket data_socket(io_context, udp::endpoint(udp::v4(), 0)); |
| 49 | |
| 50 | // Determine what port we will receive data on. |
| 51 | udp::endpoint data_endpoint = data_socket.local_endpoint(); |
| 52 | |
| 53 | // Ask the server to start sending us data. |
| 54 | control_request start = control_request::start(data_endpoint.port()); |
| 55 | boost::asio::write(control_socket, start.to_buffers()); |
| 56 | |
| 57 | unsigned long last_frame_number = 0; |
| 58 | for (;;) |
| 59 | { |
| 60 | // Receive 50 messages on the current data socket. |
| 61 | for (int i = 0; i < 50; ++i) |
| 62 | { |
| 63 | // Receive a frame from the server. |
| 64 | frame f; |
| 65 | data_socket.receive(f.to_buffers(), 0); |
| 66 | if (f.number() > last_frame_number) |
| 67 | { |
| 68 | last_frame_number = f.number(); |
| 69 | std::cout << "\n" << f.payload(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // Time to switch to a new socket. To ensure seamless handover we will |
| 74 | // continue to receive packets using the old socket until data arrives on |
| 75 | // the new one. |
| 76 | std::cout << " Starting renegotiation"; |
| 77 | |
| 78 | // Create the new data socket. |
| 79 | udp::socket new_data_socket(io_context, udp::endpoint(udp::v4(), 0)); |
| 80 | |
| 81 | // Determine the new port we will use to receive data. |
nothing calls this directly
no test coverage detected