|
Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
|
Represents an active client connection, handling request parsing and response writing. More...
#include <Connection.hpp>


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 | |
| Connection & | operator= (const Connection &)=delete |
| void | start () |
| Starts processing the connection. | |
| void | on_removed (bool notify_now=false) |
| void | set_timeouts (const ConnectionTimeouts &timeouts) |
| Sets the timeouts used for this connection. Call before start(). | |
| void | set_limits (const ConnectionLimits &limits) |
| void | ping_if_websocket () |
| Event-loop thread: sends a WebSocket ping if this is a WebSocket. | |
| 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 | on_server_shutdown () |
| 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::Proactor & | proactor () override |
| Gets the underlying Proactor to dispatch async operations. | |
| concurrency::ThreadPool & | thread_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 orbit::http::ResponseWriter | |
| virtual | ~ResponseWriter ()=default |
| virtual void | add_interceptor (Interceptor interceptor)=0 |
| Adds an interceptor to modify the response before it is sent. | |
| bool | has_responded () const |
| True once anything of a response to the current request has been sent: a full response, its headers, a chunk or an event. | |
| void | set_error_sink (ErrorSink sink) |
| uint64_t | request_generation () const |
| bool | is_open () const |
| False once the client can no longer receive anything: the connection (HTTP/1.1) or stream (HTTP/2, HTTP/3) closed, because the client left, a timeout fired or the server shut down. | |
| void | on_close (std::function< void()> callback) |
Runs callback once when the writer closes (see is_open()), or right away if it already has. | |
Private Member Functions | |
| void | process_request () |
| void | send_data (std::string_view data, bool close_after=false) |
| void | send_continue_if_expected () |
| Sends "100 Continue" once per request to a client that asked for it. | |
| void | arm_timer (std::chrono::milliseconds timeout) |
| void | arm_timer_for_current_phase () |
| bool | is_message_stream () const |
| void | send_error (http::HttpStatus status, const std::string &message) |
| void | trigger_read () |
| void | on_read_complete (ssize_t bytes_read) |
| void | trigger_write () |
| void | on_write_complete (ssize_t bytes_written) |
| void | on_sendfile_complete (ssize_t bytes_written) |
| void | process_streaming_data () |
| std::unordered_map< std::string, std::string > | default_headers_snapshot () |
| template<typename Submit > | |
| bool | submit_io (Submit &&submit) |
| RequestState | check_request_state () |
| void | drain_tls_output_locked () |
| bool | has_pending_output (bool &close_requested, bool &resume) |
| void | continue_after_response () |
Additional Inherited Members | |
Public Types inherited from orbit::http::ResponseWriter | |
| using | Interceptor = std::function< void(HttpResponse &)> |
| using | ErrorSink = std::function< void(std::exception_ptr, std::shared_ptr< ResponseWriter >)> |
| Receives an exception that escaped asynchronous handler code, such as a coroutine handler that threw after a co_await. | |
Static Public Member Functions inherited from orbit::http::ResponseWriter | |
| static void | report_async_exception (const std::shared_ptr< ResponseWriter > &writer, uint64_t generation, std::exception_ptr error) noexcept |
| Reports an exception from asynchronous handler code. | |
Protected Member Functions inherited from orbit::http::ResponseWriter | |
| void | mark_responded () |
| Implementations call this whenever they write part of a response. | |
| void | reset_responded () |
| void | mark_closed () noexcept |
Represents an active client connection, handling request parsing and response writing.
| orbit::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.
| socket | The connection socket. |
| client_ip | The client's IP address. |
| proactor | The Proactor for async I/O. |
| router | The application router. |
| manager | The connection manager. |
| thread_pool | The application thread pool. |
| timer_manager | The timer manager for timeouts. |
| max_body_size | Maximum allowed request body size. |
| tls_context | The TLS context, if applicable. |
| orbit::server::Connection::~Connection | ( | ) |
|
delete |
|
override |
|
private |
|
private |
|
private |
|
inline |
Gets the client's IP address.
|
private |
|
private |
|
private |
|
overridevirtual |
Ends a chunked response stream.
Implements orbit::http::ResponseWriter.
|
private |
|
private |
| void orbit::server::Connection::mark_for_close | ( | ) |
Marks the connection to be closed after writing completes.
|
private |
| void orbit::server::Connection::on_removed | ( | bool | notify_now = false | ) |
Called by ConnectionManager when the connection is dropped, before its pending I/O is cancelled. No new I/O is submitted afterwards. Writers held elsewhere then report closed (on_close callbacks): on a pool thread, or right away when notify_now (the server is going away and the pool with it).
|
private |
| void orbit::server::Connection::on_server_shutdown | ( | ) |
Event-loop thread, repeatedly while the server drains: closes the connection if it is idle, otherwise makes its current work the last (Connection: close, HTTP/2 GOAWAY, WebSocket close 1001).
|
private |
|
delete |
| void orbit::server::Connection::ping_if_websocket | ( | ) |
Event-loop thread: sends a WebSocket ping if this is a WebSocket.
|
inlineoverridevirtual |
Gets the underlying Proactor to dispatch async operations.
Implements orbit::http::ResponseWriter.
|
private |
|
private |
|
overridevirtual |
Asynchronously streams the incoming HTTP request body.
| on_data | Callback invoked when a body chunk is received. |
| on_end | Callback invoked when the entire body has been read. |
Implements orbit::http::ResponseWriter.
|
overridevirtual |
Sends a complete HTTP response.
Takes ownership of any file descriptors managed by the response.
| response | The HttpResponse to send. |
Implements orbit::http::ResponseWriter.
|
private |
Sends "100 Continue" once per request to a client that asked for it.
|
private |
|
private |
|
overridevirtual |
Sends only the HTTP headers.
Useful for streaming bodies or Server-Sent Events.
| response | The HttpResponse containing the headers to send. |
Implements orbit::http::ResponseWriter.
|
overridevirtual |
Sends a Server-Sent Events (SSE) message.
| data | The event data payload. |
| event | The event type/name (optional). |
| id | The event ID (optional). |
Implements orbit::http::ResponseWriter.
|
overridevirtual |
Adds a default header that will be included in the final response.
| key | The header name. |
| value | The header value. |
Implements orbit::http::ResponseWriter.
|
inline |
|
inline |
Sets the timeouts used for this connection. Call before start().
| void orbit::server::Connection::start | ( | ) |
Starts processing the connection.
|
inlineprivate |
|
inlineoverridevirtual |
Gets the thread pool to offload blocking tasks.
Implements orbit::http::ResponseWriter.
|
private |
|
private |
|
overridevirtual |
Upgrades the connection to a raw bi-directional byte stream.
| on_data | Callback invoked when data is received. |
| on_close | Callback invoked when the stream is closed. |
Implements orbit::http::ResponseWriter.
| void orbit::server::Connection::upgrade_to_websocket | ( | std::unique_ptr< http::websocket::WebSocketConnection > | ws_conn | ) |
Upgrades the connection to a WebSocket.
| ws_conn | The WebSocket connection instance. |
|
overridevirtual |
Streams a chunk of data (for Chunked Transfer Encoding).
| chunk | The data chunk to write. |
Implements orbit::http::ResponseWriter.
| void orbit::server::Connection::write_raw | ( | const std::vector< char > & | data | ) |
Writes raw data to the connection.
| data | The raw data to write. |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
mutableprivate |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
mutableprivate |
|
private |