Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
orbit::http::websocket::WebSocketConnection Class Reference

Represents an active WebSocket connection. More...

#include <WebSocketConnection.hpp>

Collaboration diagram for orbit::http::websocket::WebSocketConnection:

Classes

struct  Transport
 Where frames go. Lets the protocol logic run without a socket (tests). More...
 

Public Member Functions

 WebSocketConnection (server::Connection &underlying_connection, bool enable_deflate=false)
 
 WebSocketConnection (Transport transport, bool enable_deflate=false)
 
 ~WebSocketConnection ()
 
void on_message (std::function< void(const std::string &)> handler)
 Registers a callback to be called when a message is received.
 
void on_binary_message (std::function< void(const std::string &)> handler)
 Registers a callback for binary messages. When set, binary messages go here and on_message() receives only text; when not set, on_message() receives both, as before.
 
void on_close (std::function< void()> handler)
 Registers a callback to be called when the connection is closed.
 
void send (const std::string &message)
 Sends a text message over the WebSocket connection. Safe to call from any thread.
 
void send_binary (const std::string &data)
 Sends a binary message. Safe to call from any thread.
 
void close (uint16_t status_code=1000)
 Closes the WebSocket connection gracefully.
 
void ping ()
 
void handle_transport_closed ()
 Internal: the underlying transport has gone away (peer disconnect, timeout or I/O error). Fires the close handler once, if it has not already run, and makes further send() calls no-ops.
 
void set_max_message_size (size_t bytes)
 Sets the largest message payload accepted from the client, after decompression. Larger messages close the connection with 1009.
 
const HttpRequest * handshake_request () const
 The HTTP request that opened this WebSocket: path, query, headers, cookies, and whatever the route's middleware set on it (user from jwt_auth(), session, ...).
 
void set_handshake_request (const HttpRequest *request)
 Set by the server around the handler call.
 
void process_raw_data (std::vector< char > &buffer)
 

Private Member Functions

void send_frame (uint8_t opcode, const std::string &payload)
 
void write_frame_locked (uint8_t first_byte, const std::string &payload)
 
void fail_connection (uint16_t status_code)
 
bool deliver_message (uint8_t opcode, bool compressed, std::string payload)
 
void handle_close_frame (const std::string &payload)
 
void init_streams ()
 
void cleanup_streams ()
 
std::string deflate_payload (const std::string &payload)
 
bool inflate_payload (const std::string &payload, std::string &out)
 

Private Attributes

Transport transport_
 
const HttpRequest * handshake_request_ {nullptr}
 
std::function< void(const std::string &)> message_handler_
 
std::function< void(const std::string &)> binary_handler_
 
std::function< void()> close_handler_
 
std::atomic< bool > is_closed_ {false}
 
size_t max_message_size_ {16 * 1024 * 1024}
 
std::mutex send_mutex_
 
uint8_t fragment_opcode_ {0}
 
bool fragment_compressed_ {false}
 
std::string fragment_payload_
 
bool deflate_enabled_ {false}
 
z_stream inflate_stream_ {}
 
z_stream deflate_stream_ {}
 
bool streams_initialized_ {false}
 

Detailed Description

Represents an active WebSocket connection.

Constructor & Destructor Documentation

◆ WebSocketConnection() [1/2]

orbit::http::websocket::WebSocketConnection::WebSocketConnection ( server::Connection &  underlying_connection,
bool  enable_deflate = false 
)
explicit

◆ WebSocketConnection() [2/2]

orbit::http::websocket::WebSocketConnection::WebSocketConnection ( Transport  transport,
bool  enable_deflate = false 
)
explicit

◆ ~WebSocketConnection()

orbit::http::websocket::WebSocketConnection::~WebSocketConnection ( )

Member Function Documentation

◆ cleanup_streams()

void orbit::http::websocket::WebSocketConnection::cleanup_streams ( )
private

◆ close()

void orbit::http::websocket::WebSocketConnection::close ( uint16_t  status_code = 1000)

Closes the WebSocket connection gracefully.

◆ deflate_payload()

std::string orbit::http::websocket::WebSocketConnection::deflate_payload ( const std::string &  payload)
private

◆ deliver_message()

bool orbit::http::websocket::WebSocketConnection::deliver_message ( uint8_t  opcode,
bool  compressed,
std::string  payload 
)
private

◆ fail_connection()

void orbit::http::websocket::WebSocketConnection::fail_connection ( uint16_t  status_code)
private

◆ handle_close_frame()

void orbit::http::websocket::WebSocketConnection::handle_close_frame ( const std::string &  payload)
private

◆ handle_transport_closed()

void orbit::http::websocket::WebSocketConnection::handle_transport_closed ( )

Internal: the underlying transport has gone away (peer disconnect, timeout or I/O error). Fires the close handler once, if it has not already run, and makes further send() calls no-ops.

◆ handshake_request()

const HttpRequest * orbit::http::websocket::WebSocketConnection::handshake_request ( ) const
inline

The HTTP request that opened this WebSocket: path, query, headers, cookies, and whatever the route's middleware set on it (user from jwt_auth(), session, ...).

Available only while the route's handler runs (EventRouter's on_connect included); null afterwards. Copy what the connection needs to keep, such as the user id, into its own state.

◆ inflate_payload()

bool orbit::http::websocket::WebSocketConnection::inflate_payload ( const std::string &  payload,
std::string &  out 
)
private

◆ init_streams()

void orbit::http::websocket::WebSocketConnection::init_streams ( )
private

◆ on_binary_message()

void orbit::http::websocket::WebSocketConnection::on_binary_message ( std::function< void(const std::string &)>  handler)

Registers a callback for binary messages. When set, binary messages go here and on_message() receives only text; when not set, on_message() receives both, as before.

◆ on_close()

void orbit::http::websocket::WebSocketConnection::on_close ( std::function< void()>  handler)

Registers a callback to be called when the connection is closed.

Parameters
handlerThe callback function.

◆ on_message()

void orbit::http::websocket::WebSocketConnection::on_message ( std::function< void(const std::string &)>  handler)

Registers a callback to be called when a message is received.

Parameters
handlerThe callback function.

◆ ping()

void orbit::http::websocket::WebSocketConnection::ping ( )

Sends a ping; the peer's pong is ordinary traffic, so it keeps an idle timeout from closing a healthy connection.

◆ process_raw_data()

void orbit::http::websocket::WebSocketConnection::process_raw_data ( std::vector< char > &  buffer)

◆ send()

void orbit::http::websocket::WebSocketConnection::send ( const std::string &  message)

Sends a text message over the WebSocket connection. Safe to call from any thread.

Parameters
messageThe message to send.
Examples
/home/runner/work/orbit-framework/orbit-framework/include/orbit/websocket/EventRouter.hpp.

◆ send_binary()

void orbit::http::websocket::WebSocketConnection::send_binary ( const std::string &  data)

Sends a binary message. Safe to call from any thread.

◆ send_frame()

void orbit::http::websocket::WebSocketConnection::send_frame ( uint8_t  opcode,
const std::string &  payload 
)
private

◆ set_handshake_request()

void orbit::http::websocket::WebSocketConnection::set_handshake_request ( const HttpRequest *  request)
inline

Set by the server around the handler call.

◆ set_max_message_size()

void orbit::http::websocket::WebSocketConnection::set_max_message_size ( size_t  bytes)
inline

Sets the largest message payload accepted from the client, after decompression. Larger messages close the connection with 1009.

Parameters
bytesLimit in bytes (default 16 MiB).

◆ write_frame_locked()

void orbit::http::websocket::WebSocketConnection::write_frame_locked ( uint8_t  first_byte,
const std::string &  payload 
)
private

Member Data Documentation

◆ binary_handler_

std::function<void(const std::string&)> orbit::http::websocket::WebSocketConnection::binary_handler_
private

◆ close_handler_

std::function<void()> orbit::http::websocket::WebSocketConnection::close_handler_
private

◆ deflate_enabled_

bool orbit::http::websocket::WebSocketConnection::deflate_enabled_ {false}
private

◆ deflate_stream_

z_stream orbit::http::websocket::WebSocketConnection::deflate_stream_ {}
private

◆ fragment_compressed_

bool orbit::http::websocket::WebSocketConnection::fragment_compressed_ {false}
private

◆ fragment_opcode_

uint8_t orbit::http::websocket::WebSocketConnection::fragment_opcode_ {0}
private

◆ fragment_payload_

std::string orbit::http::websocket::WebSocketConnection::fragment_payload_
private

◆ handshake_request_

const HttpRequest* orbit::http::websocket::WebSocketConnection::handshake_request_ {nullptr}
private

◆ inflate_stream_

z_stream orbit::http::websocket::WebSocketConnection::inflate_stream_ {}
private

◆ is_closed_

std::atomic<bool> orbit::http::websocket::WebSocketConnection::is_closed_ {false}
private

◆ max_message_size_

size_t orbit::http::websocket::WebSocketConnection::max_message_size_ {16 * 1024 * 1024}
private

◆ message_handler_

std::function<void(const std::string&)> orbit::http::websocket::WebSocketConnection::message_handler_
private

◆ send_mutex_

std::mutex orbit::http::websocket::WebSocketConnection::send_mutex_
private

◆ streams_initialized_

bool orbit::http::websocket::WebSocketConnection::streams_initialized_ {false}
private

◆ transport_

Transport orbit::http::websocket::WebSocketConnection::transport_
private

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