asio 0.3.7 Home | Reference | Tutorial | Examples | Design
Examples

http::server::server Class Reference

Inherits noncopyable.

Collaboration diagram for http::server::server:

Collaboration graph
List of all members.

Detailed Description

The top-level class of the HTTP server.

Definition at line 15 of file server.hpp.

Public Member Functions

 server (const std::string &address, const std::string &port, const std::string &doc_root)
 Construct the server to listen on the specified TCP address and port, and serve up files from the given directory.
void run ()
 Run the server's io_service loop.
void stop ()
 Stop the server.

Private Member Functions

void handle_accept (const asio::error &e)
 Handle completion of an asynchronous accept operation.
void handle_stop ()
 Handle a request to stop the server.

Private Attributes

asio::io_service io_service_
 The io_service used to perform asynchronous operations.
asio::ip::tcp::acceptor acceptor_
 Acceptor used to listen for incoming connections.
connection_manager connection_manager_
 The connection manager which owns all live connections.
connection_ptr new_connection_
 The next connection to be accepted.
request_handler request_handler_
 The handler for all incoming requests.


Constructor & Destructor Documentation

http::server::server::server ( const std::string &  address,
const std::string &  port,
const std::string &  doc_root 
) [explicit]

Construct the server to listen on the specified TCP address and port, and serve up files from the given directory.

Definition at line 7 of file server.cpp.

00009   : io_service_(),
00010     acceptor_(io_service_),
00011     connection_manager_(),
00012     new_connection_(new connection(io_service_,
00013           connection_manager_, request_handler_)),
00014     request_handler_(doc_root)
00015 {
00016   // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
00017   asio::ip::tcp::resolver resolver(io_service_);
00018   asio::ip::tcp::resolver::query query(address, port);
00019   asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
00020   acceptor_.open(endpoint.protocol());
00021   acceptor_.set_option(asio::ip::tcp::acceptor::reuse_address(true));
00022   acceptor_.bind(endpoint);
00023   acceptor_.listen();
00024   acceptor_.async_accept(new_connection_->socket(),
00025       boost::bind(&server::handle_accept, this,
00026         asio::placeholders::error));
00027 }


Member Function Documentation

void http::server::server::run (  ) 

Run the server's io_service loop.

Definition at line 29 of file server.cpp.

Referenced by main().

00030 {
00031   // The io_service::run() call will block until all asynchronous operations
00032   // have finished. While the server is running, there is always at least one
00033   // asynchronous operation outstanding: the asynchronous accept call waiting
00034   // for new incoming connections.
00035   io_service_.run();
00036 }

void http::server::server::stop (  ) 

Stop the server.

Definition at line 38 of file server.cpp.

Referenced by main().

00039 {
00040   // Post a call to the stop function so that server::stop() is safe to call
00041   // from any thread.
00042   io_service_.post(boost::bind(&server::handle_stop, this));
00043 }

void http::server::server::handle_accept ( const asio::error e  )  [private]

Handle completion of an asynchronous accept operation.

Definition at line 45 of file server.cpp.

Referenced by server().

00046 {
00047   if (!e)
00048   {
00049     connection_manager_.start(new_connection_);
00050     new_connection_.reset(new connection(io_service_,
00051           connection_manager_, request_handler_));
00052     acceptor_.async_accept(new_connection_->socket(),
00053         boost::bind(&server::handle_accept, this,
00054           asio::placeholders::error));
00055   }
00056 }

void http::server::server::handle_stop (  )  [private]

Handle a request to stop the server.

Definition at line 58 of file server.cpp.

Referenced by stop().

00059 {
00060   // The server is stopped by cancelling all outstanding asynchronous
00061   // operations. Once all operations have finished the io_service::run() call
00062   // will exit.
00063   acceptor_.close();
00064   connection_manager_.stop_all();
00065 }


Member Data Documentation

asio::io_service http::server::server::io_service_ [private]

The io_service used to perform asynchronous operations.

Definition at line 37 of file server.hpp.

Referenced by handle_accept(), run(), server(), and stop().

asio::ip::tcp::acceptor http::server::server::acceptor_ [private]

Acceptor used to listen for incoming connections.

Definition at line 40 of file server.hpp.

Referenced by handle_accept(), handle_stop(), and server().

connection_manager http::server::server::connection_manager_ [private]

The connection manager which owns all live connections.

Definition at line 43 of file server.hpp.

Referenced by handle_accept(), and handle_stop().

connection_ptr http::server::server::new_connection_ [private]

The next connection to be accepted.

Definition at line 46 of file server.hpp.

Referenced by handle_accept(), and server().

request_handler http::server::server::request_handler_ [private]

The handler for all incoming requests.

Definition at line 49 of file server.hpp.

Referenced by handle_accept().


The documentation for this class was generated from the following files:
asio 0.3.7 Home | Reference | Tutorial | Examples | Design