A class representing a server for handling HTTP requests. More...
#include <server.hpp>
Public Member Functions | |
Server (asio::io_context &io_context, asio::ip::tcp::endpoint endpoint) | |
~Server ()=default | |
asio::awaitable< void > | listen () |
template<class F > | |
void | addRoute (RouteKey route, F &&handler) |
Adds a route to the server with a specified HTTP method and path. | |
template<class F , class ... Args> | |
void | addRoute (RouteKey route, F &&handler, Args &&... binded_args) |
Adds a route to the server with a specified HTTP method and path. | |
void | setIdleInterval (std::chrono::milliseconds idle_interval) |
Set the idle interval after which the server will close the connection. | |
asio::awaitable< void > | close () |
Closes the server gracefully. | |
Protected Member Functions | |
asio::awaitable< void > | handleConnection (asio::ip::tcp::socket sock) |
asio::awaitable< void > | readData (asio::ip::tcp::socket &sock, std::chrono::steady_clock::time_point &deadline) |
Reads data from the given socket and processes incoming HTTP requests. | |
asio::awaitable< void > | writeData (asio::ip::tcp::socket &sock, std::string message) |
Asynchronously writes data to a TCP socket. | |
A class representing a server for handling HTTP requests.
This class is responsible for listening for incoming TCP connections on a specified port, handling incoming HTTP requests, and executing route handlers based on the request method and path.
dcn::Server::Server | ( | asio::io_context & | io_context, |
asio::ip::tcp::endpoint | endpoint | ||
) |
|
default |
Adds a route to the server with a specified HTTP method and path.
route | The route key containing the HTTP method and path. |
handler | The handler function to be called when the route is matched. |
Adds a route to the server with a specified HTTP method and path.
route | The route key containing the HTTP method and path. |
handler | The handler function to be called when the route is matched. |
binded_args | Additional arguments to bind to the handler function. |
asio::awaitable< void > dcn::Server::close | ( | ) |
Closes the server gracefully.
This function sets the _close
flag to true, signaling the server to stop accepting new connections and begin shutdown procedures. The closure operation is dispatched to the server's strand to ensure thread safety. It logs a debug message indicating that a close request has been initiated.
|
protected |
Handles a new connection by reading data from the socket and processing it until there is no more data to read or the idle interval has been reached.
sock | The socket to read from. |
asio::awaitable< void > dcn::Server::listen | ( | ) |
Listens for incoming TCP connections on the specified port.
This function sets up the server to listen for connections using the provided acceptor. It dispatches the listening operation to the server's strand to ensure thread safety. The server continues to listen until the close
flag is set to true. For each incoming connection, it spawns a new coroutine to handle the connection without blocking the server strand.
The function also manages an idle timeout using a watchdog, which ensures that the server does not remain idle beyond the specified idle_interval
.
|
protected |
Reads data from the given socket and processes incoming HTTP requests.
This function asynchronously reads data from the provided TCP socket until the connection is closed or an error occurs. It parses the incoming data into HTTP requests, finds the appropriate route handler and executes it. The resulting HTTP response is then sent back through the socket. The deadline is updated for each read operation to enforce a timeout for client inactivity.
sock | The TCP socket to read data from. |
deadline | The time point by which the read operation should complete. |
void dcn::Server::setIdleInterval | ( | std::chrono::milliseconds | idle_interval | ) |
Set the idle interval after which the server will close the connection.
idle_interval | The idle interval in milliseconds. If no requests are received from the client during this interval, the server will close the connection. |
|
protected |
Asynchronously writes data to a TCP socket.
This function sends the provided message over the specified socket using asynchronous write operations. The function yields control until the entire message is sent. It's designed to work within an awaitable context and is part of the server's coroutine-based handling of connections.
sock | The TCP socket to write the message to. |
message | The message to be sent over the socket. |