Decentralised Art Server
High-performance C++ backend that exposes HTML interface and a secure REST API for managing Performative Transactions entities
 
Loading...
Searching...
No Matches
dcn::Server Class Reference

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< voidlisten ()
 
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< voidclose ()
 Closes the server gracefully.
 

Protected Member Functions

asio::awaitable< voidhandleConnection (asio::ip::tcp::socket sock)
 
asio::awaitable< voidreadData (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< voidwriteData (asio::ip::tcp::socket &sock, std::string message)
 Asynchronously writes data to a TCP socket.
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ Server()

dcn::Server::Server ( asio::io_context &  io_context,
asio::ip::tcp::endpoint  endpoint 
)

◆ ~Server()

dcn::Server::~Server ( )
default

Member Function Documentation

◆ addRoute() [1/2]

template<class F >
void dcn::Server::addRoute ( RouteKey  route,
F &&  handler 
)
inline

Adds a route to the server with a specified HTTP method and path.

Parameters
routeThe route key containing the HTTP method and path.
handlerThe handler function to be called when the route is matched.

◆ addRoute() [2/2]

template<class F , class ... Args>
void dcn::Server::addRoute ( RouteKey  route,
F &&  handler,
Args &&...  binded_args 
)
inline

Adds a route to the server with a specified HTTP method and path.

Parameters
routeThe route key containing the HTTP method and path.
handlerThe handler function to be called when the route is matched.
binded_argsAdditional arguments to bind to the handler function.

◆ close()

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.

◆ handleConnection()

asio::awaitable< void > dcn::Server::handleConnection ( asio::ip::tcp::socket  sock)
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.

Parameters
sockThe socket to read from.
Returns
An awaitable that resolves when the connection has been closed.

◆ listen()

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.

◆ readData()

asio::awaitable< void > dcn::Server::readData ( asio::ip::tcp::socket &  sock,
std::chrono::steady_clock::time_point &  deadline 
)
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.

Parameters
sockThe TCP socket to read data from.
deadlineThe time point by which the read operation should complete.

◆ setIdleInterval()

void dcn::Server::setIdleInterval ( std::chrono::milliseconds  idle_interval)

Set the idle interval after which the server will close the connection.

Parameters
idle_intervalThe idle interval in milliseconds. If no requests are received from the client during this interval, the server will close the connection.

◆ writeData()

asio::awaitable< void > dcn::Server::writeData ( asio::ip::tcp::socket &  sock,
std::string  message 
)
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.

Parameters
sockThe TCP socket to write the message to.
messageThe message to be sent over the socket.

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