2
0
mirror of https://github.com/boostorg/asio.git synced 2026-01-28 18:52:09 +00:00
Files
asio/example/porthopper/server.cpp
Christopher Kohlhoff ffd222435e Merge bug and doc fixes from trunk.
........
  r46535 | chris_kohlhoff | 2008-06-20 10:25:50 +1000 (Fri, 20 Jun 2008) | 2 lines
  
  One more check needed for BSD serial port extensions.
........
  r46766 | chris_kohlhoff | 2008-06-27 15:38:16 +1000 (Fri, 27 Jun 2008) | 2 lines
  
  Add missing copyright notices.
........
  r46876 | chris_kohlhoff | 2008-06-30 10:24:44 +1000 (Mon, 30 Jun 2008) | 2 lines
  
  Some linux configurations do not automatically define _XOPEN_SOURCE.
........
  r46944 | chris_kohlhoff | 2008-07-01 21:52:54 +1000 (Tue, 01 Jul 2008) | 2 lines
  
  Fix documentation on behaviour when a connect operation fails.
........
  r46945 | chris_kohlhoff | 2008-07-01 21:53:54 +1000 (Tue, 01 Jul 2008) | 2 lines
  
  Fix name of parameter as referred to in documentation.
........
  r46946 | chris_kohlhoff | 2008-07-01 21:58:39 +1000 (Tue, 01 Jul 2008) | 2 lines
  
  Fix typos.
........
  r46947 | chris_kohlhoff | 2008-07-01 21:59:25 +1000 (Tue, 01 Jul 2008) | 2 lines
  
  Merge in new reference index page.
........
  r46948 | chris_kohlhoff | 2008-07-01 22:06:49 +1000 (Tue, 01 Jul 2008) | 2 lines
  
  Documentation generation enhancements.
........
  r46949 | chris_kohlhoff | 2008-07-01 22:13:51 +1000 (Tue, 01 Jul 2008) | 2 lines
  
  Improve buffer() documentation. Fix vector overloads to match TR2 proposal.
........
  r46950 | chris_kohlhoff | 2008-07-01 22:14:57 +1000 (Tue, 01 Jul 2008) | 4 lines
  
  Clarify behaviour of read_until/async_read_until with respect to leftover
  data in the streambuf. Use separate brief descriptions for function groups.
  Add some extra notes to the io_service documentation.
........
  r47013 | chris_kohlhoff | 2008-07-03 08:50:50 +1000 (Thu, 03 Jul 2008) | 2 lines
  
  Regenerate documentation.
........


[SVN r47038]
2008-07-03 13:05:33 +00:00

188 lines
5.9 KiB
C++

//
// server.cpp
// ~~~~~~~~~~
//
// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <cmath>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <set>
#include "protocol.hpp"
using boost::asio::ip::tcp;
using boost::asio::ip::udp;
typedef boost::shared_ptr<tcp::socket> tcp_socket_ptr;
typedef boost::shared_ptr<boost::asio::deadline_timer> timer_ptr;
typedef boost::shared_ptr<control_request> control_request_ptr;
class server
{
public:
// Construct the server to wait for incoming control connections.
server(boost::asio::io_service& io_service, unsigned short port)
: acceptor_(io_service, tcp::endpoint(tcp::v4(), port)),
timer_(io_service),
udp_socket_(io_service, udp::endpoint(udp::v4(), 0)),
next_frame_number_(1)
{
// Start waiting for a new control connection.
tcp_socket_ptr new_socket(new tcp::socket(acceptor_.get_io_service()));
acceptor_.async_accept(*new_socket,
boost::bind(&server::handle_accept, this,
boost::asio::placeholders::error, new_socket));
// Start the timer used to generate outgoing frames.
timer_.expires_from_now(boost::posix_time::milliseconds(100));
timer_.async_wait(boost::bind(&server::handle_timer, this));
}
// Handle a new control connection.
void handle_accept(const boost::system::error_code& ec, tcp_socket_ptr socket)
{
if (!ec)
{
// Start receiving control requests on the connection.
control_request_ptr request(new control_request);
boost::asio::async_read(*socket, request->to_buffers(),
boost::bind(&server::handle_control_request, this,
boost::asio::placeholders::error, socket, request));
// Start waiting for a new control connection.
tcp_socket_ptr new_socket(new tcp::socket(acceptor_.get_io_service()));
acceptor_.async_accept(*new_socket,
boost::bind(&server::handle_accept, this,
boost::asio::placeholders::error, new_socket));
}
}
// Handle a new control request.
void handle_control_request(const boost::system::error_code& ec,
tcp_socket_ptr socket, control_request_ptr request)
{
if (!ec)
{
// Delay handling of the control request to simulate network latency.
timer_ptr delay_timer(
new boost::asio::deadline_timer(acceptor_.get_io_service()));
delay_timer->expires_from_now(boost::posix_time::seconds(2));
delay_timer->async_wait(
boost::bind(&server::handle_control_request_timer, this,
socket, request, delay_timer));
}
}
void handle_control_request_timer(tcp_socket_ptr socket,
control_request_ptr request, timer_ptr delay_timer)
{
// Determine what address this client is connected from, since
// subscriptions must be stored on the server as a complete endpoint, not
// just a port. We use the non-throwing overload of remote_endpoint() since
// it may fail if the socket is no longer connected.
boost::system::error_code ec;
tcp::endpoint remote_endpoint = socket->remote_endpoint(ec);
if (!ec)
{
// Remove old port subscription, if any.
if (unsigned short old_port = request->old_port())
{
udp::endpoint old_endpoint(remote_endpoint.address(), old_port);
subscribers_.erase(old_endpoint);
std::cout << "Removing subscription " << old_endpoint << std::endl;
}
// Add new port subscription, if any.
if (unsigned short new_port = request->new_port())
{
udp::endpoint new_endpoint(remote_endpoint.address(), new_port);
subscribers_.insert(new_endpoint);
std::cout << "Adding subscription " << new_endpoint << std::endl;
}
}
// Wait for next control request on this connection.
boost::asio::async_read(*socket, request->to_buffers(),
boost::bind(&server::handle_control_request, this,
boost::asio::placeholders::error, socket, request));
}
// Every time the timer fires we will generate a new frame and send it to all
// subscribers.
void handle_timer()
{
// Generate payload.
double x = next_frame_number_ * 0.2;
double y = std::sin(x);
int char_index = static_cast<int>((y + 1.0) * (frame::payload_size / 2));
std::string payload;
for (int i = 0; i < frame::payload_size; ++i)
payload += (i == char_index ? '*' : '.');
// Create the frame to be sent to all subscribers.
frame f(next_frame_number_++, payload);
// Send frame to all subscribers. We can use synchronous calls here since
// UDP send operations typically do not block.
std::set<udp::endpoint>::iterator j;
for (j = subscribers_.begin(); j != subscribers_.end(); ++j)
{
boost::system::error_code ec;
udp_socket_.send_to(f.to_buffers(), *j, 0, ec);
}
// Wait for next timeout.
timer_.expires_from_now(boost::posix_time::milliseconds(100));
timer_.async_wait(boost::bind(&server::handle_timer, this));
}
private:
// The acceptor used to accept incoming control connections.
tcp::acceptor acceptor_;
// The timer used for generating data.
boost::asio::deadline_timer timer_;
// The socket used to send data to subscribers.
udp::socket udp_socket_;
// The next frame number.
unsigned long next_frame_number_;
// The set of endpoints that are subscribed.
std::set<udp::endpoint> subscribers_;
};
int main(int argc, char* argv[])
{
try
{
if (argc != 2)
{
std::cerr << "Usage: server <port>\n";
return 1;
}
boost::asio::io_service io_service;
using namespace std; // For atoi.
server s(io_service, atoi(argv[1]));
io_service.run();
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << std::endl;
}
return 0;
}