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

Examples |

# http::server::server Class Reference

Inherits noncopyable.

Collaboration diagram for http::server::server:

List of all members.

## Detailed Description

The top-level class of the HTTP server.

Definition at line 25 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_code &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 17 of file server.cpp.

    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 }

## Member Function Documentation

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

Run the server's io_service loop.

Definition at line 39 of file server.cpp.

Referenced by main().

    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 }

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

Stop the server.

Definition at line 48 of file server.cpp.

Referenced by main().

    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 }

void http::server::server::handle_accept | ( | const asio::error_code & | e | ) | `[private]`

Handle completion of an asynchronous accept operation.

Definition at line 55 of file server.cpp.

Referenced by server().

    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 }

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

Handle a request to stop the server.

Definition at line 68 of file server.cpp.

Referenced by 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 }

## Member Data Documentation

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

The io_service used to perform asynchronous operations.

Definition at line 47 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 50 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 53 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 56 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 59 of file server.hpp.

Referenced by handle_accept().

The documentation for this class was generated from the following files:

- http/server/server.hpp
- http/server/server.cpp

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