5 // Copyright (c) 2003-2017 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/bind.hpp>
13 #include <boost/shared_ptr.hpp>
19 #include "protocol.hpp"
21 using boost::asio::ip::tcp;
22 using boost::asio::ip::udp;
24 typedef boost::shared_ptr<tcp::socket> tcp_socket_ptr;
25 typedef boost::shared_ptr<boost::asio::deadline_timer> timer_ptr;
26 typedef boost::shared_ptr<control_request> control_request_ptr;
31 // Construct the server to wait for incoming control connections.
32 server(boost::asio::io_service& io_service, unsigned short port)
33 : acceptor_(io_service, tcp::endpoint(tcp::v4(), port)),
35 udp_socket_(io_service, udp::endpoint(udp::v4(), 0)),
38 // Start waiting for a new control connection.
39 tcp_socket_ptr new_socket(new tcp::socket(acceptor_.get_io_service()));
40 acceptor_.async_accept(*new_socket,
41 boost::bind(&server::handle_accept, this,
42 boost::asio::placeholders::error, new_socket));
44 // Start the timer used to generate outgoing frames.
45 timer_.expires_from_now(boost::posix_time::milliseconds(100));
46 timer_.async_wait(boost::bind(&server::handle_timer, this));
49 // Handle a new control connection.
50 void handle_accept(const boost::system::error_code& ec, tcp_socket_ptr socket)
54 // Start receiving control requests on the connection.
55 control_request_ptr request(new control_request);
56 boost::asio::async_read(*socket, request->to_buffers(),
57 boost::bind(&server::handle_control_request, this,
58 boost::asio::placeholders::error, socket, request));
61 // Start waiting for a new control connection.
62 tcp_socket_ptr new_socket(new tcp::socket(acceptor_.get_io_service()));
63 acceptor_.async_accept(*new_socket,
64 boost::bind(&server::handle_accept, this,
65 boost::asio::placeholders::error, new_socket));
68 // Handle a new control request.
69 void handle_control_request(const boost::system::error_code& ec,
70 tcp_socket_ptr socket, control_request_ptr request)
74 // Delay handling of the control request to simulate network latency.
75 timer_ptr delay_timer(
76 new boost::asio::deadline_timer(acceptor_.get_io_service()));
77 delay_timer->expires_from_now(boost::posix_time::seconds(2));
78 delay_timer->async_wait(
79 boost::bind(&server::handle_control_request_timer, this,
80 socket, request, delay_timer));
84 void handle_control_request_timer(tcp_socket_ptr socket,
85 control_request_ptr request, timer_ptr /*delay_timer*/)
87 // Determine what address this client is connected from, since
88 // subscriptions must be stored on the server as a complete endpoint, not
89 // just a port. We use the non-throwing overload of remote_endpoint() since
90 // it may fail if the socket is no longer connected.
91 boost::system::error_code ec;
92 tcp::endpoint remote_endpoint = socket->remote_endpoint(ec);
95 // Remove old port subscription, if any.
96 if (unsigned short old_port = request->old_port())
98 udp::endpoint old_endpoint(remote_endpoint.address(), old_port);
99 subscribers_.erase(old_endpoint);
100 std::cout << "Removing subscription " << old_endpoint << std::endl;
103 // Add new port subscription, if any.
104 if (unsigned short new_port = request->new_port())
106 udp::endpoint new_endpoint(remote_endpoint.address(), new_port);
107 subscribers_.insert(new_endpoint);
108 std::cout << "Adding subscription " << new_endpoint << std::endl;
112 // Wait for next control request on this connection.
113 boost::asio::async_read(*socket, request->to_buffers(),
114 boost::bind(&server::handle_control_request, this,
115 boost::asio::placeholders::error, socket, request));
118 // Every time the timer fires we will generate a new frame and send it to all
123 double x = next_frame_number_ * 0.2;
124 double y = std::sin(x);
125 int char_index = static_cast<int>((y + 1.0) * (frame::payload_size / 2));
127 for (int i = 0; i < frame::payload_size; ++i)
128 payload += (i == char_index ? '*' : '.');
130 // Create the frame to be sent to all subscribers.
131 frame f(next_frame_number_++, payload);
133 // Send frame to all subscribers. We can use synchronous calls here since
134 // UDP send operations typically do not block.
135 std::set<udp::endpoint>::iterator j;
136 for (j = subscribers_.begin(); j != subscribers_.end(); ++j)
138 boost::system::error_code ec;
139 udp_socket_.send_to(f.to_buffers(), *j, 0, ec);
142 // Wait for next timeout.
143 timer_.expires_from_now(boost::posix_time::milliseconds(100));
144 timer_.async_wait(boost::bind(&server::handle_timer, this));
148 // The acceptor used to accept incoming control connections.
149 tcp::acceptor acceptor_;
151 // The timer used for generating data.
152 boost::asio::deadline_timer timer_;
154 // The socket used to send data to subscribers.
155 udp::socket udp_socket_;
157 // The next frame number.
158 unsigned long next_frame_number_;
160 // The set of endpoints that are subscribed.
161 std::set<udp::endpoint> subscribers_;
164 int main(int argc, char* argv[])
170 std::cerr << "Usage: server <port>\n";
174 boost::asio::io_service io_service;
176 using namespace std; // For atoi.
177 server s(io_service, atoi(argv[1]));
181 catch (std::exception& e)
183 std::cerr << "Exception: " << e.what() << std::endl;