staticlib_pion  2.0.2
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
Public Member Functions | Static Public Member Functions | List of all members
staticlib::pion::websocket Class Reference

This class represents a WebSocket RFC 6455 connection. More...

#include <staticlib/pion/websocket.hpp>

Public Member Functions

 websocket (std::unique_ptr< http_request > req, std::shared_ptr< tcp_connection > conn, std::function< void(std::unique_ptr< websocket >)> open_handler, std::function< void(std::unique_ptr< websocket >)> message_handler, std::function< void(std::unique_ptr< websocket >)> close_handler, size_t max_receive_cache_size_bytes=(1024 *1024), size_t max_cached_frames_count=1024)
 Constructor.
 ~websocket () STATICLIB_NOEXCEPT
 Destructor, closes the TCP connection.
 websocket (const websocket &)
 Deleted copy constructor.
websocket & operator= (const websocket &)
 Deleted copy-assignment operator.
const std::string & get_id ()
 ID for this WebSocket connection.
http_request & get_request ()
 Reference to handshake request.
tcp_connection & get_connection ()
 Reference to TCP connection.
sl::websocket::frame_type message_type ()
 Type (opcode) of the currently cached frame.
websocket_detail::msg_data_src message_data ()
 Source that can be used to read unmasked payload data from one or more (for continuation) received frames.
void write (sl::io::span< const char > data)
 Write payload content.
void write_nocopy (sl::io::span< const char > data)
 Write payload content; the data written is not copied, and therefore must persist until the message has finished sending.

Static Public Member Functions

static bool is_websocket_upgrade (const http_request &req)
 Checks whether specified request is a WebSocket handshake.
static void start (std::unique_ptr< websocket > self)
 Starts the processing of WebSocker connection: asynchronously send handshake response and reads incoming messages.
static void receive (std::unique_ptr< websocket > self)
 Asynchronously receives the incoming messages.
static void send (std::unique_ptr< websocket > self, sl::websocket::frame_type msg_type=sl::websocket::frame_type::text)
 Asynchronously sends the buffered payload using the specified frame type.
template<typename Handler >
static void send_message (std::unique_ptr< websocket > self, sl::websocket::frame_type msg_type, Handler handler)
 Asynchronously sends the buffered payload using the specified frame type.
static void broadcast (std::vector< std::shared_ptr< tcp_connection >> &connections, sl::io::span< const char > msg, sl::websocket::frame_type msg_type=sl::websocket::frame_type::text)
 Broadcasts the specified message as a WebSocket frame to every specified connection.
static void close (std::unique_ptr< websocket > self, const close_status &status=close_status::normal)
 Sends close frame to client and closes the underlying TCP connection.

Detailed Description

This class represents a WebSocket RFC 6455 connection.

Constructor & Destructor Documentation

staticlib::pion::websocket::websocket ( std::unique_ptr< http_request >  req,
std::shared_ptr< tcp_connection >  conn,
std::function< void(std::unique_ptr< websocket >)>  open_handler,
std::function< void(std::unique_ptr< websocket >)>  message_handler,
std::function< void(std::unique_ptr< websocket >)>  close_handler,
size_t  max_receive_cache_size_bytes = (1024*1024),
size_t  max_cached_frames_count = 1024 
)
inline

Constructor.

Parameters
reqHTTP request with handshake
connTCP connection used for handshake
open_handlerhandler for open events
message_handlerhandler for message events
close_handlerhandler for closeevents, MUST NOT be used to send or receive messages
max_receive_cache_size_bytesmax allowed size of the receive buffer, 1MB by default
max_cached_frames_countmax allowed number of continuation frames, 1024 by default
staticlib::pion::websocket::~websocket ( )
inline

Destructor, closes the TCP connection.

staticlib::pion::websocket::websocket ( const websocket &  )

Deleted copy constructor.

Member Function Documentation

static void staticlib::pion::websocket::broadcast ( std::vector< std::shared_ptr< tcp_connection >> &  connections,
sl::io::span< const char >  msg,
sl::websocket::frame_type  msg_type = sl::websocket::frame_type::text 
)
inlinestatic

Broadcasts the specified message as a WebSocket frame to every specified connection.

Thread-safe: may be called concurrently from any thread.

May cause the broadcast message to interleave with other frames (put in the middle of continuation sequence, or in the middle to TCP fragmented sequence), if target connection is used for outbound traffic at the the time of broadcast.

Parameters
connectionslist of connections to broadcast
msgmessage to broadcast
msg_typeframe type (opcode) to use
static void staticlib::pion::websocket::close ( std::unique_ptr< websocket >  self,
const close_status &  status = close_status::normal 
)
inlinestatic

Sends close frame to client and closes the underlying TCP connection.

Parameters
selfwebsocket instance
statusclose status to include with close frame
tcp_connection& staticlib::pion::websocket::get_connection ( )
inline

Reference to TCP connection.

Returns
connection
const std::string& staticlib::pion::websocket::get_id ( )
inline

ID for this WebSocket connection.

Returns
value of Sec-WebSocket-Key request handshake header
http_request& staticlib::pion::websocket::get_request ( )
inline

Reference to handshake request.

Returns
handshake request
static bool staticlib::pion::websocket::is_websocket_upgrade ( const http_request &  req)
inlinestatic

Checks whether specified request is a WebSocket handshake.

websocket_detail::msg_data_src staticlib::pion::websocket::message_data ( )
inline

Source that can be used to read unmasked payload data from one or more (for continuation) received frames.

Invalidated on send or receive operation over this instance

Returns
unmasked payload data
sl::websocket::frame_type staticlib::pion::websocket::message_type ( )
inline

Type (opcode) of the currently cached frame.

Returns
opcode of the last cached frame, invalid type if no frames are cached
websocket& staticlib::pion::websocket::operator= ( const websocket &  )

Deleted copy-assignment operator.

static void staticlib::pion::websocket::receive ( std::unique_ptr< websocket >  self)
inlinestatic

Asynchronously receives the incoming messages.

Parameters
selfwebsocket instance
static void staticlib::pion::websocket::send ( std::unique_ptr< websocket >  self,
sl::websocket::frame_type  msg_type = sl::websocket::frame_type::text 
)
inlinestatic

Asynchronously sends the buffered payload using the specified frame type.

Parameters
selfwebsocket instance
msg_typeframe opcode to use
template<typename Handler >
static void staticlib::pion::websocket::send_message ( std::unique_ptr< websocket >  self,
sl::websocket::frame_type  msg_type,
Handler  handler 
)
inlinestatic

Asynchronously sends the buffered payload using the specified frame type.

Handler is invoked after send is completed successfully.

Parameters
selfwebsocket instance
msg_typeframe opcode to use
handlerhandler function to invoke on successful send
static void staticlib::pion::websocket::start ( std::unique_ptr< websocket >  self)
inlinestatic

Starts the processing of WebSocker connection: asynchronously send handshake response and reads incoming messages.

Parameters
selfwebsocket instance
void staticlib::pion::websocket::write ( sl::io::span< const char >  data)
inline

Write payload content.

Parameters
datato append to the payload content
void staticlib::pion::websocket::write_nocopy ( sl::io::span< const char >  data)
inline

Write payload content; the data written is not copied, and therefore must persist until the message has finished sending.

Parameters
datathe data to append to the payload content

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