5 // Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #include <boost/asio.hpp>
12 #include <boost/lambda/lambda.hpp>
13 #include <boost/lambda/bind.hpp>
14 #include <boost/lambda/if.hpp>
15 #include <boost/shared_ptr.hpp>
21 #include "protocol.hpp"
23 using namespace boost
;
24 using boost::asio::ip::tcp
;
25 using boost::asio::ip::udp
;
27 int main(int argc
, char* argv
[])
33 std::cerr
<< "Usage: client <host> <port>\n";
36 using namespace std
; // For atoi.
37 std::string host_name
= argv
[1];
38 std::string port
= argv
[2];
40 boost::asio::io_service io_service
;
42 // Determine the location of the server.
43 tcp::resolver
resolver(io_service
);
44 tcp::resolver::query
query(host_name
, port
);
45 tcp::endpoint remote_endpoint
= *resolver
.resolve(query
);
47 // Establish the control connection to the server.
48 tcp::socket
control_socket(io_service
);
49 control_socket
.connect(remote_endpoint
);
51 // Create a datagram socket to receive data from the server.
52 boost::shared_ptr
<udp::socket
> data_socket(
53 new udp::socket(io_service
, udp::endpoint(udp::v4(), 0)));
55 // Determine what port we will receive data on.
56 udp::endpoint data_endpoint
= data_socket
->local_endpoint();
58 // Ask the server to start sending us data.
59 control_request start
= control_request::start(data_endpoint
.port());
60 boost::asio::write(control_socket
, start
.to_buffers());
62 unsigned long last_frame_number
= 0;
65 // Receive 50 messages on the current data socket.
66 for (int i
= 0; i
< 50; ++i
)
68 // Receive a frame from the server.
70 data_socket
->receive(f
.to_buffers(), 0);
71 if (f
.number() > last_frame_number
)
73 last_frame_number
= f
.number();
74 std::cout
<< "\n" << f
.payload();
78 // Time to switch to a new socket. To ensure seamless handover we will
79 // continue to receive packets using the old socket until data arrives on
81 std::cout
<< " Starting renegotiation";
83 // Create the new data socket.
84 boost::shared_ptr
<udp::socket
> new_data_socket(
85 new udp::socket(io_service
, udp::endpoint(udp::v4(), 0)));
87 // Determine the new port we will use to receive data.
88 udp::endpoint new_data_endpoint
= new_data_socket
->local_endpoint();
90 // Ask the server to switch over to the new port.
91 control_request change
= control_request::change(
92 data_endpoint
.port(), new_data_endpoint
.port());
93 boost::system::error_code control_result
;
94 boost::asio::async_write(control_socket
, change
.to_buffers(),
96 lambda::var(control_result
) = lambda::_1
99 // Try to receive a frame from the server on the new data socket. If we
100 // successfully receive a frame on this new data socket we can consider
101 // the renegotation complete. In that case we will close the old data
102 // socket, which will cause any outstanding receive operation on it to be
105 boost::system::error_code new_data_socket_result
;
106 new_data_socket
->async_receive(f1
.to_buffers(),
108 // Note: lambda::_1 is the first argument to the callback handler,
109 // which in this case is the error code for the operation.
110 lambda::var(new_data_socket_result
) = lambda::_1
,
111 lambda::if_(!lambda::_1
)
113 // We have successfully received a frame on the new data socket,
114 // so we can close the old data socket. This will cancel any
115 // outstanding receive operation on the old data socket.
116 lambda::var(data_socket
) = boost::shared_ptr
<udp::socket
>()
120 // This loop will continue until we have successfully completed the
121 // renegotiation (i.e. received a frame on the new data socket), or some
122 // unrecoverable error occurs.
126 // Even though we're performing a renegotation, we want to continue
127 // receiving data as smoothly as possible. Therefore we will continue to
128 // try to receive a frame from the server on the old data socket. If we
129 // receive a frame on this socket we will interrupt the io_service,
130 // print the frame, and resume waiting for the other operations to
133 done
= true; // Let's be optimistic.
134 if (data_socket
) // Might have been closed by new_data_socket's handler.
136 data_socket
->async_receive(f2
.to_buffers(), 0,
138 lambda::if_(!lambda::_1
)
140 // We have successfully received a frame on the old data
141 // socket. Stop the io_service so that we can print it.
142 lambda::bind(&boost::asio::io_service::stop
, &io_service
),
143 lambda::var(done
) = false
148 // Run the operations in parallel. This will block until all operations
149 // have finished, or until the io_service is interrupted. (No threads!)
153 // If the io_service.run() was interrupted then we have received a frame
154 // on the old data socket. We need to keep waiting for the renegotation
155 // operations to complete.
158 if (f2
.number() > last_frame_number
)
160 last_frame_number
= f2
.number();
161 std::cout
<< "\n" << f2
.payload();
166 // Since the loop has finished, we have either successfully completed
167 // the renegotation, or an error has occurred. First we'll check for
170 throw boost::system::system_error(control_result
);
171 if (new_data_socket_result
)
172 throw boost::system::system_error(new_data_socket_result
);
174 // If we get here it means we have successfully started receiving data on
175 // the new data socket. This new data socket will be used from now on
176 // (until the next time we renegotiate).
177 std::cout
<< " Renegotiation complete";
178 data_socket
= new_data_socket
;
179 data_endpoint
= new_data_endpoint
;
180 if (f1
.number() > last_frame_number
)
182 last_frame_number
= f1
.number();
183 std::cout
<< "\n" << f1
.payload();
187 catch (std::exception
& e
)
189 std::cerr
<< "Exception: " << e
.what() << std::endl
;