Orbit Framework
An ultra-modern, asynchronous, cross-platform C++ Web Framework.
Loading...
Searching...
No Matches
server::Connection Class Reference

Represents an active client connection, handling request parsing and response writing. More...

#include <Connection.hpp>

Inheritance diagram for server::Connection:
Collaboration diagram for server::Connection:

Public Member Functions

 Connection (network::Socket socket, const std::string &client_ip, network::Proactor &proactor, const routing::Router &router, ConnectionManager &manager, concurrency::ThreadPool &thread_pool, TimerManager &timer_manager, size_t max_body_size, network::TlsContext *tls_context=nullptr)
 Constructs a new Connection.
 
 ~Connection ()
 
 Connection (const Connection &)=delete
 
Connectionoperator= (const Connection &)=delete
 
void start ()
 Starts processing the connection.
 
void write_raw (const std::vector< char > &data)
 Writes raw data to the connection.
 
void mark_for_close ()
 Marks the connection to be closed after writing completes.
 
void upgrade_to_websocket (std::unique_ptr< http::websocket::WebSocketConnection > ws_conn)
 Upgrades the connection to a WebSocket.
 
const std::string & client_ip () const
 Gets the client's IP address.
 
void add_interceptor (std::function< void(http::HttpResponse &)> interceptor) override
 
void set_header (const std::string &key, const std::string &value) override
 Adds a default header that will be included in the final response.
 
network::Proactorproactor () override
 Gets the underlying Proactor to dispatch async operations.
 
concurrency::ThreadPoolthread_pool () override
 Gets the thread pool to offload blocking tasks.
 
void send (http::HttpResponse &&response) override
 Sends a complete HTTP response.
 
void send_headers (http::HttpResponse &response) override
 Sends only the HTTP headers.
 
void write_chunk (std::string_view chunk) override
 Streams a chunk of data (for Chunked Transfer Encoding).
 
void end () override
 Ends a chunked response stream.
 
void send_sse_event (std::string_view data, std::string_view event="", std::string_view id="") override
 Sends a Server-Sent Events (SSE) message.
 
void upgrade_to_raw_stream (std::function< void(std::string_view)> on_data, std::function< void()> on_close) override
 Upgrades the connection to a raw bi-directional byte stream.
 
void read_body_stream (std::function< void(std::string_view)> on_data, std::function< void()> on_end) override
 Asynchronously streams the incoming HTTP request body.
 
- Public Member Functions inherited from http::ResponseWriter
virtual ~ResponseWriter ()=default
 
virtual void add_interceptor (Interceptor interceptor)=0
 Adds an interceptor to modify the response before it is sent.
 

Additional Inherited Members

- Public Types inherited from http::ResponseWriter
using Interceptor = std::function< void(HttpResponse &)>
 

Detailed Description

Represents an active client connection, handling request parsing and response writing.

Constructor & Destructor Documentation

◆ Connection() [1/2]

server::Connection::Connection ( network::Socket  socket,
const std::string &  client_ip,
network::Proactor proactor,
const routing::Router router,
ConnectionManager manager,
concurrency::ThreadPool thread_pool,
TimerManager timer_manager,
size_t  max_body_size,
network::TlsContext tls_context = nullptr 
)

Constructs a new Connection.

Parameters
socketThe connection socket.
client_ipThe client's IP address.
proactorThe Proactor for async I/O.
routerThe application router.
managerThe connection manager.
thread_poolThe application thread pool.
timer_managerThe timer manager for timeouts.
max_body_sizeMaximum allowed request body size.
tls_contextThe TLS context, if applicable.

◆ ~Connection()

server::Connection::~Connection ( )

◆ Connection() [2/2]

server::Connection::Connection ( const Connection )
delete

Member Function Documentation

◆ add_interceptor()

void server::Connection::add_interceptor ( std::function< void(http::HttpResponse &)>  interceptor)
override

◆ client_ip()

const std::string & server::Connection::client_ip ( ) const
inline

Gets the client's IP address.

Returns
The IP address string.

◆ end()

void server::Connection::end ( )
overridevirtual

Ends a chunked response stream.

Implements http::ResponseWriter.

◆ mark_for_close()

void server::Connection::mark_for_close ( )

Marks the connection to be closed after writing completes.

◆ operator=()

Connection & server::Connection::operator= ( const Connection )
delete

◆ proactor()

network::Proactor & server::Connection::proactor ( )
inlineoverridevirtual

Gets the underlying Proactor to dispatch async operations.

Returns
A reference to the network::Proactor.

Implements http::ResponseWriter.

◆ read_body_stream()

void server::Connection::read_body_stream ( std::function< void(std::string_view)>  on_data,
std::function< void()>  on_end 
)
overridevirtual

Asynchronously streams the incoming HTTP request body.

Parameters
on_dataCallback invoked when a body chunk is received.
on_endCallback invoked when the entire body has been read.

Implements http::ResponseWriter.

◆ send()

void server::Connection::send ( http::HttpResponse &&  response)
overridevirtual

Sends a complete HTTP response.

Takes ownership of any file descriptors managed by the response.

Parameters
responseThe HttpResponse to send.

Implements http::ResponseWriter.

◆ send_headers()

void server::Connection::send_headers ( http::HttpResponse response)
overridevirtual

Sends only the HTTP headers.

Useful for streaming bodies or Server-Sent Events.

Parameters
responseThe HttpResponse containing the headers to send.

Implements http::ResponseWriter.

◆ send_sse_event()

void server::Connection::send_sse_event ( std::string_view  data,
std::string_view  event = "",
std::string_view  id = "" 
)
overridevirtual

Sends a Server-Sent Events (SSE) message.

Parameters
dataThe event data payload.
eventThe event type/name (optional).
idThe event ID (optional).

Implements http::ResponseWriter.

◆ set_header()

void server::Connection::set_header ( const std::string &  key,
const std::string &  value 
)
overridevirtual

Adds a default header that will be included in the final response.

Parameters
keyThe header name.
valueThe header value.

Implements http::ResponseWriter.

◆ start()

void server::Connection::start ( )

Starts processing the connection.

◆ thread_pool()

concurrency::ThreadPool & server::Connection::thread_pool ( )
inlineoverridevirtual

Gets the thread pool to offload blocking tasks.

Returns
A reference to the concurrency::ThreadPool.

Implements http::ResponseWriter.

◆ upgrade_to_raw_stream()

void server::Connection::upgrade_to_raw_stream ( std::function< void(std::string_view)>  on_data,
std::function< void()>  on_close 
)
overridevirtual

Upgrades the connection to a raw bi-directional byte stream.

Parameters
on_dataCallback invoked when data is received.
on_closeCallback invoked when the stream is closed.

Implements http::ResponseWriter.

◆ upgrade_to_websocket()

void server::Connection::upgrade_to_websocket ( std::unique_ptr< http::websocket::WebSocketConnection ws_conn)

Upgrades the connection to a WebSocket.

Parameters
ws_connThe WebSocket connection instance.

◆ write_chunk()

void server::Connection::write_chunk ( std::string_view  chunk)
overridevirtual

Streams a chunk of data (for Chunked Transfer Encoding).

Parameters
chunkThe data chunk to write.

Implements http::ResponseWriter.

◆ write_raw()

void server::Connection::write_raw ( const std::vector< char > &  data)

Writes raw data to the connection.

Parameters
dataThe raw data to write.

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