asio 0.3.8rc3 | Home | Reference | Tutorial | Examples | Design

Examples |

# http/server/server.cpp

Go to the documentation of this file.

    00001 //
    00002 // server.cpp
    00003 // ~~~~~~~~~~
    00004 //
    00005 // Copyright (c) 2003-2007 Christopher M. Kohlhoff (chris at kohlhoff dot com)
    00006 //
    00007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
    00008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
    00009 //
    00010
    00011 #include "server.hpp"
    00012 #include <boost/bind.hpp>
    00013
    00014 namespace http {
    00015 namespace server {
    00016
    00017 server::server(const std::string& address, const std::string& port,
    00018     const std::string& doc_root)
    00019   : io_service_(),
    00020     acceptor_(io_service_),
    00021     connection_manager_(),
    00022     new_connection_(new connection(io_service_,
    00023           connection_manager_, request_handler_)),
    00024     request_handler_(doc_root)
    00025 {
    00026   // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
    00027   asio::ip::tcp::resolver resolver(io_service_);
    00028   asio::ip::tcp::resolver::query query(address, port);
    00029   asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
    00030   acceptor_.open(endpoint.protocol());
    00031   acceptor_.set_option(asio::ip::tcp::acceptor::reuse_address(true));
    00032   acceptor_.bind(endpoint);
    00033   acceptor_.listen();
    00034   acceptor_.async_accept(new_connection_->socket(),
    00035       boost::bind(&server::handle_accept, this,
    00036         asio::placeholders::error));
    00037 }
    00038
    00039 void server::run()
    00040 {
    00041   // The io_service::run() call will block until all asynchronous operations
    00042   // have finished. While the server is running, there is always at least one
    00043   // asynchronous operation outstanding: the asynchronous accept call waiting
    00044   // for new incoming connections.
    00045   io_service_.run();
    00046 }
    00047
    00048 void server::stop()
    00049 {
    00050   // Post a call to the stop function so that server::stop() is safe to call
    00051   // from any thread.
    00052   io_service_.post(boost::bind(&server::handle_stop, this));
    00053 }
    00054
    00055 void server::handle_accept(const asio::error_code& e)
    00056 {
    00057   if (!e)
    00058   {
    00059     connection_manager_.start(new_connection_);
    00060     new_connection_.reset(new connection(io_service_,
    00061           connection_manager_, request_handler_));
    00062     acceptor_.async_accept(new_connection_->socket(),
    00063         boost::bind(&server::handle_accept, this,
    00064           asio::placeholders::error));
    00065   }
    00066 }
    00067
    00068 void server::handle_stop()
    00069 {
    00070   // The server is stopped by cancelling all outstanding asynchronous
    00071   // operations. Once all operations have finished the io_service::run() call
    00072   // will exit.
    00073   acceptor_.close();
    00074   connection_manager_.stop_all();
    00075 }
    00076
    00077 } // namespace server
    00078 } // namespace http

asio 0.3.8rc3 | Home | Reference | Tutorial | Examples | Design