CppServer 1.0.6.0
C++ Server Library
Loading...
Searching...
No Matches
CppServer::WS::WebSocket Class Reference

WebSocket utility class. More...

#include <ws.h>

Inheritance diagram for CppServer::WS::WebSocket:
CppServer::WS::WSClient CppServer::WS::WSSClient CppServer::WS::WSSServer CppServer::WS::WSSSession CppServer::WS::WSServer CppServer::WS::WSSession

Public Member Functions

 WebSocket ()
 WebSocket (const WebSocket &)=delete
 WebSocket (WebSocket &&)=delete
 ~WebSocket ()=default
WebSocketoperator= (const WebSocket &)=delete
WebSocketoperator= (WebSocket &&)=delete
std::string_view ws_nonce () const noexcept
 Get the WebSocket random nonce.
bool PerformClientUpgrade (const HTTP::HTTPResponse &response, const CppCommon::UUID &id)
 Perform WebSocket client upgrade.
bool PerformServerUpgrade (const HTTP::HTTPRequest &request, HTTP::HTTPResponse &response)
 Perform WebSocket server upgrade.
void PrepareSendFrame (uint8_t opcode, bool mask, const void *buffer, size_t size, int status=0)
 Prepare WebSocket send frame.
void PrepareReceiveFrame (const void *buffer, size_t size)
 Prepare WebSocket receive frame.
size_t RequiredReceiveFrameSize ()
 Required WebSocket receive frame size.
void ClearWSBuffers ()
 Clear WebSocket send/receive buffers.
void InitWSNonce ()
 Initialize WebSocket random nonce.

Static Public Attributes

static const uint8_t WS_FIN = 0x80
 Final frame.
static const uint8_t WS_TEXT = 0x01
 Text frame.
static const uint8_t WS_BINARY = 0x02
 Binary frame.
static const uint8_t WS_CLOSE = 0x08
 Close frame.
static const uint8_t WS_PING = 0x09
 Ping frame.
static const uint8_t WS_PONG = 0x0A
 Pong frame.

Protected Member Functions

virtual void onWSConnecting (HTTP::HTTPRequest &request)
 Handle WebSocket client connecting notification.
virtual void onWSConnected (const HTTP::HTTPResponse &response)
 Handle WebSocket client connected notification.
virtual bool onWSConnecting (const HTTP::HTTPRequest &request, HTTP::HTTPResponse &response)
 Handle WebSocket server session validating notification.
virtual void onWSConnected (const HTTP::HTTPRequest &request)
 Handle WebSocket server session connected notification.
virtual void onWSDisconnecting ()
 Handle WebSocket disconnecting notification.
virtual void onWSDisconnected ()
 Handle WebSocket disconnected notification.
virtual void onWSReceived (const void *buffer, size_t size)
 Handle WebSocket received notification.
virtual void onWSClose (const void *buffer, size_t size, int status=1000)
 Handle WebSocket client close notification.
virtual void onWSPing (const void *buffer, size_t size)
 Handle WebSocket ping notification.
virtual void onWSPong (const void *buffer, size_t size)
 Handle WebSocket pong notification.
virtual void onWSError (const std::string &message)
 Handle WebSocket error notification.
virtual void SendResponse (const HTTP::HTTPResponse &response)
 Send WebSocket server upgrade response.

Protected Attributes

bool _ws_handshaked {false}
 Handshaked flag.
uint8_t _ws_opcode
 Received frame opcode.
bool _ws_frame_received {false}
 Received frame flag.
bool _ws_final_received {false}
 Received final flag.
size_t _ws_header_size {0}
 Received frame header size.
size_t _ws_payload_size {0}
 Received frame payload size.
std::vector< uint8_t > _ws_receive_frame_buffer
 Receive frame buffer.
std::vector< uint8_t > _ws_receive_final_buffer
 Receive final buffer.
uint8_t _ws_receive_mask [4]
 Receive mask.
std::mutex _ws_send_lock
 Send buffer lock.
std::vector< uint8_t > _ws_send_buffer
 Send buffer.
uint8_t _ws_send_mask [4]
 Send mask.
std::array< uint8_t, 16 > _ws_nonce
 WebSocket random nonce of 16 bytes.

Detailed Description

WebSocket utility class.

Definition at line 29 of file ws.h.

Constructor & Destructor Documentation

◆ WebSocket() [1/3]

CppServer::WS::WebSocket::WebSocket ( )
inline

Definition at line 45 of file ws.h.

◆ WebSocket() [2/3]

CppServer::WS::WebSocket::WebSocket ( const WebSocket & )
delete

◆ WebSocket() [3/3]

CppServer::WS::WebSocket::WebSocket ( WebSocket && )
delete

◆ ~WebSocket()

CppServer::WS::WebSocket::~WebSocket ( )
default

Member Function Documentation

◆ ClearWSBuffers()

void CppServer::WS::WebSocket::ClearWSBuffers ( )

Clear WebSocket send/receive buffers.

Definition at line 486 of file ws.cpp.

◆ InitWSNonce()

void CppServer::WS::WebSocket::InitWSNonce ( )

Initialize WebSocket random nonce.

Definition at line 21 of file ws.cpp.

◆ onWSClose()

virtual void CppServer::WS::WebSocket::onWSClose ( const void * buffer,
size_t size,
int status = 1000 )
inlineprotectedvirtual

Handle WebSocket client close notification.

Parameters
buffer- Received buffer
size- Received buffer size
status- WebSocket status (defualt is 1000)

Reimplemented in CppServer::WS::WSClient, CppServer::WS::WSSClient, CppServer::WS::WSSession, and CppServer::WS::WSSSession.

Definition at line 149 of file ws.h.

◆ onWSConnected() [1/2]

virtual void CppServer::WS::WebSocket::onWSConnected ( const HTTP::HTTPRequest & request)
inlineprotectedvirtual

Handle WebSocket server session connected notification.

Parameters
request- WebSocket upgrade HTTP request

Definition at line 129 of file ws.h.

◆ onWSConnected() [2/2]

virtual void CppServer::WS::WebSocket::onWSConnected ( const HTTP::HTTPResponse & response)
inlineprotectedvirtual

Handle WebSocket client connected notification.

Parameters
response- WebSocket upgrade HTTP response
Examples
ws_chat_client.cpp, ws_chat_server.cpp, wss_chat_client.cpp, and wss_chat_server.cpp.

Definition at line 112 of file ws.h.

◆ onWSConnecting() [1/2]

virtual bool CppServer::WS::WebSocket::onWSConnecting ( const HTTP::HTTPRequest & request,
HTTP::HTTPResponse & response )
inlineprotectedvirtual

Handle WebSocket server session validating notification.

Notification is called when WebSocket client is connecting to the server. You can handle the connection and validate WebSocket upgrade HTTP request.

Parameters
request- WebSocket upgrade HTTP request
response- WebSocket upgrade HTTP response
Returns
'true' if the WebSocket update request is valid, 'false' if the WebSocket update request is not valid

Definition at line 124 of file ws.h.

◆ onWSConnecting() [2/2]

virtual void CppServer::WS::WebSocket::onWSConnecting ( HTTP::HTTPRequest & request)
inlineprotectedvirtual

Handle WebSocket client connecting notification.

Notification is called when WebSocket client is connecting to the server. You can handle the connection and change WebSocket upgrade HTTP request by providing your own headers.

Parameters
request- WebSocket upgrade HTTP request
Examples
ws_chat_client.cpp, and wss_chat_client.cpp.

Definition at line 107 of file ws.h.

◆ onWSDisconnected()

virtual void CppServer::WS::WebSocket::onWSDisconnected ( )
inlineprotectedvirtual

Handle WebSocket disconnected notification.

Examples
ws_chat_client.cpp, ws_chat_server.cpp, wss_chat_client.cpp, and wss_chat_server.cpp.

Definition at line 134 of file ws.h.

◆ onWSDisconnecting()

virtual void CppServer::WS::WebSocket::onWSDisconnecting ( )
inlineprotectedvirtual

Handle WebSocket disconnecting notification.

Definition at line 132 of file ws.h.

◆ onWSError()

virtual void CppServer::WS::WebSocket::onWSError ( const std::string & message)
inlineprotectedvirtual

Handle WebSocket error notification.

Parameters
message- Error message

Reimplemented in CppServer::WS::WSClient, CppServer::WS::WSSClient, CppServer::WS::WSSession, and CppServer::WS::WSSSession.

Definition at line 167 of file ws.h.

◆ onWSPing()

virtual void CppServer::WS::WebSocket::onWSPing ( const void * buffer,
size_t size )
inlineprotectedvirtual

Handle WebSocket ping notification.

Parameters
buffer- Received buffer
size- Received buffer size

Reimplemented in CppServer::WS::WSClient, CppServer::WS::WSSClient, CppServer::WS::WSSession, and CppServer::WS::WSSSession.

Definition at line 155 of file ws.h.

◆ onWSPong()

virtual void CppServer::WS::WebSocket::onWSPong ( const void * buffer,
size_t size )
inlineprotectedvirtual

Handle WebSocket pong notification.

Parameters
buffer- Received buffer
size- Received buffer size

Definition at line 161 of file ws.h.

◆ onWSReceived()

virtual void CppServer::WS::WebSocket::onWSReceived ( const void * buffer,
size_t size )
inlineprotectedvirtual

Handle WebSocket received notification.

Parameters
buffer- Received buffer
size- Received buffer size
Examples
ws_chat_client.cpp, ws_chat_server.cpp, wss_chat_client.cpp, and wss_chat_server.cpp.

Definition at line 141 of file ws.h.

◆ operator=() [1/2]

WebSocket & CppServer::WS::WebSocket::operator= ( const WebSocket & )
delete

◆ operator=() [2/2]

WebSocket & CppServer::WS::WebSocket::operator= ( WebSocket && )
delete

◆ PerformClientUpgrade()

bool CppServer::WS::WebSocket::PerformClientUpgrade ( const HTTP::HTTPResponse & response,
const CppCommon::UUID & id )

Perform WebSocket client upgrade.

Parameters
response- WebSocket upgrade HTTP response
id- WebSocket client Id
Returns
'true' if the WebSocket was successfully upgrade, 'false' if the WebSocket was not upgrade

Definition at line 26 of file ws.cpp.

◆ PerformServerUpgrade()

bool CppServer::WS::WebSocket::PerformServerUpgrade ( const HTTP::HTTPRequest & request,
HTTP::HTTPResponse & response )

Perform WebSocket server upgrade.

Parameters
request- WebSocket upgrade HTTP request
response- WebSocket upgrade HTTP response
Returns
'true' if the WebSocket was successfully upgrade, 'false' if the WebSocket was not upgrade

Definition at line 103 of file ws.cpp.

◆ PrepareReceiveFrame()

void CppServer::WS::WebSocket::PrepareReceiveFrame ( const void * buffer,
size_t size )

Prepare WebSocket receive frame.

Parameters
buffer- Received buffer
size- Received buffer size

Definition at line 275 of file ws.cpp.

◆ PrepareSendFrame()

void CppServer::WS::WebSocket::PrepareSendFrame ( uint8_t opcode,
bool mask,
const void * buffer,
size_t size,
int status = 0 )

Prepare WebSocket send frame.

Parameters
opcode- WebSocket opcode
mask- WebSocket mask
buffer- Buffer to send
size- Buffer size
status- WebSocket status (defualt is 0)

Definition at line 214 of file ws.cpp.

◆ RequiredReceiveFrameSize()

size_t CppServer::WS::WebSocket::RequiredReceiveFrameSize ( )

Required WebSocket receive frame size.

Definition at line 460 of file ws.cpp.

◆ SendResponse()

virtual void CppServer::WS::WebSocket::SendResponse ( const HTTP::HTTPResponse & response)
inlineprotectedvirtual

Send WebSocket server upgrade response.

Parameters
response- WebSocket upgrade HTTP response

Definition at line 204 of file ws.h.

◆ ws_nonce()

std::string_view CppServer::WS::WebSocket::ws_nonce ( ) const
inlinenoexcept

Get the WebSocket random nonce.

Examples
ws_chat_client.cpp, and wss_chat_client.cpp.

Definition at line 54 of file ws.h.

Member Data Documentation

◆ _ws_final_received

bool CppServer::WS::WebSocket::_ws_final_received {false}
protected

Received final flag.

Definition at line 178 of file ws.h.

◆ _ws_frame_received

bool CppServer::WS::WebSocket::_ws_frame_received {false}
protected

Received frame flag.

Definition at line 176 of file ws.h.

◆ _ws_handshaked

bool CppServer::WS::WebSocket::_ws_handshaked {false}
protected

Handshaked flag.

Definition at line 171 of file ws.h.

◆ _ws_header_size

size_t CppServer::WS::WebSocket::_ws_header_size {0}
protected

Received frame header size.

Definition at line 180 of file ws.h.

◆ _ws_nonce

std::array<uint8_t, 16> CppServer::WS::WebSocket::_ws_nonce
protected

WebSocket random nonce of 16 bytes.

Definition at line 198 of file ws.h.

◆ _ws_opcode

uint8_t CppServer::WS::WebSocket::_ws_opcode
protected

Received frame opcode.

Definition at line 174 of file ws.h.

◆ _ws_payload_size

size_t CppServer::WS::WebSocket::_ws_payload_size {0}
protected

Received frame payload size.

Definition at line 182 of file ws.h.

◆ _ws_receive_final_buffer

std::vector<uint8_t> CppServer::WS::WebSocket::_ws_receive_final_buffer
protected

Receive final buffer.

Definition at line 186 of file ws.h.

◆ _ws_receive_frame_buffer

std::vector<uint8_t> CppServer::WS::WebSocket::_ws_receive_frame_buffer
protected

Receive frame buffer.

Definition at line 184 of file ws.h.

◆ _ws_receive_mask

uint8_t CppServer::WS::WebSocket::_ws_receive_mask[4]
protected

Receive mask.

Definition at line 188 of file ws.h.

◆ _ws_send_buffer

std::vector<uint8_t> CppServer::WS::WebSocket::_ws_send_buffer
protected

Send buffer.

Definition at line 193 of file ws.h.

◆ _ws_send_lock

std::mutex CppServer::WS::WebSocket::_ws_send_lock
protected

Send buffer lock.

Definition at line 191 of file ws.h.

◆ _ws_send_mask

uint8_t CppServer::WS::WebSocket::_ws_send_mask[4]
protected

Send mask.

Definition at line 195 of file ws.h.

◆ WS_BINARY

const uint8_t CppServer::WS::WebSocket::WS_BINARY = 0x02
static

Binary frame.

Definition at line 37 of file ws.h.

◆ WS_CLOSE

const uint8_t CppServer::WS::WebSocket::WS_CLOSE = 0x08
static

Close frame.

Definition at line 39 of file ws.h.

◆ WS_FIN

const uint8_t CppServer::WS::WebSocket::WS_FIN = 0x80
static

Final frame.

Definition at line 33 of file ws.h.

◆ WS_PING

const uint8_t CppServer::WS::WebSocket::WS_PING = 0x09
static

Ping frame.

Definition at line 41 of file ws.h.

◆ WS_PONG

const uint8_t CppServer::WS::WebSocket::WS_PONG = 0x0A
static

Pong frame.

Definition at line 43 of file ws.h.

◆ WS_TEXT

const uint8_t CppServer::WS::WebSocket::WS_TEXT = 0x01
static

Text frame.

Definition at line 35 of file ws.h.


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