Boost.Asio Documentation

repository·develop·Indexed 23 days ago

https://github.com/boostorg/asio

A cross-platform C++ library for network and low-level I/O programming, providing asynchronous models for handling sockets, timers, and SSL/TLS communication. This documentation includes examples and API references for various HTTP server implementations, including the http::server, http::server2, http::server3, and coroutine-based http::server4 classes.

Tokens
1.3K
Snippets
2
Records
8
Agent score
81%

What's inside Boost.Asio

  1. Use the http::server3::server class

    develop

    The http::server3::server class provides a high-level interface for running an HTTP server. It listens on a specified TCP address and port, serves files from a document root, and manages a thread pool to handle asynchronous operations via boost::asio::io_context.

    To use the server:

    1. Instantiate the server with the desired address, port, doc_root (the directory from which to serve files), and thread_pool_size.
    2. Call the run() method to start the server's io_context loop.
  2. Use the http::server4::server class

    develop

    The http::server4::server class is a top-level coroutine designed to listen on a specified TCP address and port, serving as an HTTP server. It uses a user-supplied callback to handle incoming requests.

    To use it, construct the server with an io_context, an address, a port, and a request_handler function. The handler receives a const request& and a reply&, allowing you to inspect the incoming request and populate the reply to be sent back to the client.

    Because it inherits from boost::asio::coroutine, it is intended to be used within an asynchronous execution flow (e.g., via boost::asio::co_spawn or similar coroutine orchestration).

  3. Use the http::server2::server class

    develop

    The http::server2::server class is the top-level interface for an HTTP server. It manages a pool of io_context objects to handle asynchronous operations, listens for incoming TCP connections on a specified address and port, and serves files from a provided document root.

    To use the server:

    1. Instantiate the server with the target address, port, document root directory, and the size of the io_context pool.
    2. Call .run() to start the server's asynchronous event loop.
    3. The server uses an internal request_handler to process incoming requests and a signal_set to handle process termination signals gracefully.
  4. Use the http::server class to run an HTTP server

    develop

    The http::server class provides a high-level interface for running an HTTP server that listens on a specific TCP address and port, serving files from a designated document root.

    To use it:

    1. Instantiate the server by providing the address, port, and doc_root (the directory from which files will be served).
    2. Call the run() method to start the server's io_context loop.

    The server internally manages connection acceptance, connection lifecycles via a connection_manager, and request processing via a request_handler.

  5. http::server class reference

    develop

    The http::server class is the top-level entry point for the HTTP server implementation. It is non-copyable and non-movable.

    Public Methods

    • explicit server(const std::string& address, const std::string& port, const std::string& doc_root) Constructs the server to listen on the specified TCP address and port, and serve up files from the given directory.
    • void run() Runs the server's io_context loop, starting the asynchronous operations.

    Private Members (Internal Logic)

    • io_context_: The boost::asio::io_context used for asynchronous operations.
    • signals_: A boost::asio::signal_set used to register for process termination notifications (e.g., SIGINT, SIGTERM).
    • acceptor_: A boost::asio::ip::tcp::acceptor used to listen for incoming connections.
    • connection_manager_: Manages the lifecycle of all active connections.
    • request_handler_: Processes incoming HTTP requests.
  6. Run the http::server2::server event loop

    develop

    The run() method starts the server's execution by running the io_context loop across the configured pool of io_context objects. This method is blocking and will continue to process incoming connections and requests until the server is stopped (e.g., via a termination signal).

    void run();
  7. Construct an http::server2::server instance

    develop

    The server constructor initializes the server with the following parameters:

    • address (std::string): The TCP address to listen on (e.g., "0.0.0.0" for all interfaces).
    • port (std::string): The port to listen on (e.g., "8080").
    • doc_root (std::string): The directory from which files will be served.
    • io_context_pool_size (std::size_t): The number of io_context objects to use in the pool for parallelizing asynchronous operations.
    explicit server(const std::string& address, const std::string& port,
                    const std::string& doc_root, std::size_t io_context_pool_size);
  8. http::server3::server API Reference

    develop

    The http::server3::server class is the top-level entry point for the HTTP server implementation.

    Public Methods

    explicit server(const std::string& address, const std::string& port, const std::string& doc_root, std::size_t thread_pool_size)

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

    • address: The TCP address to bind to.
    • port: The port to listen on.
    • doc_root: The directory from which files will be served.
    • thread_pool_size: The number of threads that will call io_context::run().

    void run()

    Starts the server's io_context loop, processing asynchronous operations.