CppServer  1.0.4.0
C++ Server Library
Public Member Functions | Protected Member Functions | List of all members
CppServer::WS::WSServer Class Reference

WebSocket server. More...

#include <ws_server.h>

Inheritance diagram for CppServer::WS::WSServer:
CppServer::HTTP::HTTPServer CppServer::WS::WebSocket CppServer::Asio::TCPServer

Public Member Functions

 WSServer (const WSServer &)=delete
 
 WSServer (WSServer &&)=delete
 
virtual ~WSServer ()=default
 
WSServeroperator= (const WSServer &)=delete
 
WSServeroperator= (WSServer &&)=delete
 
virtual bool CloseAll ()
 
virtual bool CloseAll (int status)
 
virtual bool CloseAll (int status, const void *buffer, size_t size)
 
virtual bool CloseAll (int status, std::string_view text)
 
bool Multicast (const void *buffer, size_t size) override
 Multicast data to all connected WebSocket sessions. More...
 
size_t MulticastText (const void *buffer, size_t size)
 
size_t MulticastText (std::string_view text)
 
size_t MulticastBinary (const void *buffer, size_t size)
 
size_t MulticastBinary (std::string_view text)
 
size_t MulticastPing (const void *buffer, size_t size)
 
size_t MulticastPing (std::string_view text)
 
- Public Member Functions inherited from CppServer::HTTP::HTTPServer
 HTTPServer (const HTTPServer &)=delete
 
 HTTPServer (HTTPServer &&)=delete
 
virtual ~HTTPServer ()=default
 
HTTPServeroperator= (const HTTPServer &)=delete
 
HTTPServeroperator= (HTTPServer &&)=delete
 
CppCommon::FileCache & cache () noexcept
 Get the static content cache. More...
 
const CppCommon::FileCache & cache () const noexcept
 
void AddStaticContent (const CppCommon::Path &path, const std::string &prefix="/", const CppCommon::Timespan &timeout=CppCommon::Timespan::hours(1))
 Add static content cache. More...
 
void RemoveStaticContent (const CppCommon::Path &path)
 Remove static content cache. More...
 
void ClearStaticContent ()
 Clear static content cache. More...
 
void Watchdog (const CppCommon::UtcTimestamp &utc=CppCommon::UtcTimestamp())
 Watchdog the static content cache. More...
 
- Public Member Functions inherited from CppServer::Asio::TCPServer
 TCPServer (const std::shared_ptr< Service > &service, int port, InternetProtocol protocol=InternetProtocol::IPv4)
 Initialize TCP server with a given Asio service and port number. More...
 
 TCPServer (const std::shared_ptr< Service > &service, const std::string &address, int port)
 Initialize TCP server with a given Asio service, server address and port number. More...
 
 TCPServer (const std::shared_ptr< Service > &service, const asio::ip::tcp::endpoint &endpoint)
 Initialize TCP server with a given Asio service and endpoint. More...
 
 TCPServer (const TCPServer &)=delete
 
 TCPServer (TCPServer &&)=delete
 
virtual ~TCPServer ()=default
 
TCPServeroperator= (const TCPServer &)=delete
 
TCPServeroperator= (TCPServer &&)=delete
 
const CppCommon::UUID & id () const noexcept
 Get the server Id. More...
 
std::shared_ptr< Service > & service () noexcept
 Get the Asio service. More...
 
std::shared_ptr< asio::io_service > & io_service () noexcept
 Get the Asio IO service. More...
 
asio::io_service::strand & strand () noexcept
 Get the Asio service strand for serialized handler execution. More...
 
asio::ip::tcp::endpoint & endpoint () noexcept
 Get the server endpoint. More...
 
asio::ip::tcp::acceptor & acceptor () noexcept
 Get the server acceptor. More...
 
const std::string & address () const noexcept
 Get the server address. More...
 
int port () const noexcept
 Get the server port number. More...
 
uint64_t connected_sessions () const noexcept
 Get the number of sessions connected to the server. More...
 
uint64_t bytes_pending () const noexcept
 Get the number of bytes pending sent by the server. More...
 
uint64_t bytes_sent () const noexcept
 Get the number of bytes sent by the server. More...
 
uint64_t bytes_received () const noexcept
 Get the number of bytes received by the server. More...
 
bool option_keep_alive () const noexcept
 Get the option: keep alive. More...
 
bool option_no_delay () const noexcept
 Get the option: no delay. More...
 
bool option_reuse_address () const noexcept
 Get the option: reuse address. More...
 
bool option_reuse_port () const noexcept
 Get the option: reuse port. More...
 
bool IsStarted () const noexcept
 Is the server started? More...
 
virtual bool Start ()
 Start the server. More...
 
virtual bool Stop ()
 Stop the server. More...
 
virtual bool Restart ()
 Restart the server. More...
 
virtual bool Multicast (std::string_view text)
 Multicast text to all connected sessions. More...
 
virtual bool DisconnectAll ()
 Disconnect all connected sessions. More...
 
std::shared_ptr< TCPSessionFindSession (const CppCommon::UUID &id)
 Find a session with a given Id. More...
 
void SetupKeepAlive (bool enable) noexcept
 Setup option: keep alive. More...
 
void SetupNoDelay (bool enable) noexcept
 Setup option: no delay. More...
 
void SetupReuseAddress (bool enable) noexcept
 Setup option: reuse address. More...
 
void SetupReusePort (bool enable) noexcept
 Setup option: reuse port. More...
 

Protected Member Functions

std::shared_ptr< Asio::TCPSessionCreateSession (const std::shared_ptr< Asio::TCPServer > &server) override
 
- Protected Member Functions inherited from CppServer::HTTP::HTTPServer
std::shared_ptr< Asio::TCPSessionCreateSession (const std::shared_ptr< Asio::TCPServer > &server) override
 
- Protected Member Functions inherited from CppServer::Asio::TCPServer
virtual std::shared_ptr< TCPSessionCreateSession (const std::shared_ptr< TCPServer > &server)
 Create TCP session factory method. More...
 
virtual void onStarted ()
 Handle server started notification. More...
 
virtual void onStopped ()
 Handle server stopped notification. More...
 
virtual void onConnected (std::shared_ptr< TCPSession > &session)
 Handle session connected notification. More...
 
virtual void onDisconnected (std::shared_ptr< TCPSession > &session)
 Handle session disconnected notification. More...
 
virtual void onError (int error, const std::string &category, const std::string &message)
 Handle error notification. More...
 
- Protected Member Functions inherited from CppServer::WS::WebSocket
virtual void onWSConnecting (HTTP::HTTPRequest &request)
 Handle WebSocket client connecting notification. More...
 
virtual void onWSConnected (const HTTP::HTTPResponse &response)
 Handle WebSocket client connected notification. More...
 
virtual bool onWSConnecting (const HTTP::HTTPRequest &request, HTTP::HTTPResponse &response)
 Handle WebSocket server session validating notification. More...
 
virtual void onWSConnected (const HTTP::HTTPRequest &request)
 Handle WebSocket server session connected notification. More...
 
virtual void onWSDisconnected ()
 Handle WebSocket client disconnected notification. More...
 
virtual void onWSReceived (const void *buffer, size_t size)
 Handle WebSocket received notification. More...
 
virtual void onWSClose (const void *buffer, size_t size, int status=1000)
 Handle WebSocket client close notification. More...
 
virtual void onWSPing (const void *buffer, size_t size)
 Handle WebSocket ping notification. More...
 
virtual void onWSPong (const void *buffer, size_t size)
 Handle WebSocket pong notification. More...
 
virtual void onWSError (const std::string &message)
 Handle WebSocket error notification. More...
 
virtual void SendResponse (const HTTP::HTTPResponse &response)
 Send WebSocket server upgrade response. More...
 
 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. More...
 
bool PerformClientUpgrade (const HTTP::HTTPResponse &response, const CppCommon::UUID &id)
 Perform WebSocket client upgrade. More...
 
bool PerformServerUpgrade (const HTTP::HTTPRequest &request, HTTP::HTTPResponse &response)
 Perform WebSocket server upgrade. More...
 
void PrepareSendFrame (uint8_t opcode, bool mask, const void *buffer, size_t size, int status=0)
 Prepare WebSocket send frame. More...
 
void PrepareReceiveFrame (const void *buffer, size_t size)
 Prepare WebSocket receive frame. More...
 
size_t RequiredReceiveFrameSize ()
 Required WebSocket receive frame size. More...
 
void ClearWSBuffers ()
 Clear WebSocket send/receive buffers. More...
 
void InitWSNonce ()
 Initialize WebSocket random nonce. More...
 

Additional Inherited Members

- Protected Attributes inherited from CppServer::Asio::TCPServer
std::shared_mutex _sessions_lock
 
std::map< CppCommon::UUID, std::shared_ptr< TCPSession > > _sessions
 
- Protected Attributes inherited from CppServer::WS::WebSocket
bool _ws_handshaked {false}
 Handshaked flag. More...
 
uint8_t _ws_opcode
 Received frame opcode. More...
 
bool _ws_frame_received {false}
 Received frame flag. More...
 
bool _ws_final_received {false}
 Received final flag. More...
 
size_t _ws_header_size {0}
 Received frame header size. More...
 
size_t _ws_payload_size {0}
 Received frame payload size. More...
 
std::vector< uint8_t > _ws_receive_frame_buffer
 Receive frame buffer. More...
 
std::vector< uint8_t > _ws_receive_final_buffer
 Receive final buffer. More...
 
uint8_t _ws_receive_mask [4]
 Receive mask. More...
 
std::mutex _ws_send_lock
 Send buffer lock. More...
 
std::vector< uint8_t > _ws_send_buffer
 Send buffer. More...
 
uint8_t _ws_send_mask [4]
 Send mask. More...
 
std::array< uint8_t, 16 > _ws_nonce
 WebSocket random nonce of 16 bytes. More...
 
- Static Protected Attributes inherited from CppServer::WS::WebSocket
static const uint8_t WS_FIN = 0x80
 Final frame. More...
 
static const uint8_t WS_TEXT = 0x01
 Text frame. More...
 
static const uint8_t WS_BINARY = 0x02
 Binary frame. More...
 
static const uint8_t WS_CLOSE = 0x08
 Close frame. More...
 
static const uint8_t WS_PING = 0x09
 Ping frame. More...
 
static const uint8_t WS_PONG = 0x0A
 Pong frame. More...
 

Detailed Description

WebSocket server.

WebSocket server is used to communicate with clients using WebSocket protocol.

https://en.wikipedia.org/wiki/WebSocket

Thread-safe.

Examples
ws_chat_server.cpp.

Definition at line 28 of file ws_server.h.

Constructor & Destructor Documentation

◆ WSServer() [1/2]

CppServer::WS::WSServer::WSServer ( const WSServer )
delete

◆ WSServer() [2/2]

CppServer::WS::WSServer::WSServer ( WSServer &&  )
delete

◆ ~WSServer()

virtual CppServer::WS::WSServer::~WSServer ( )
virtualdefault

Member Function Documentation

◆ CloseAll() [1/4]

virtual bool CppServer::WS::WSServer::CloseAll ( )
inlinevirtual

Definition at line 41 of file ws_server.h.

◆ CloseAll() [2/4]

virtual bool CppServer::WS::WSServer::CloseAll ( int  status)
inlinevirtual

Definition at line 42 of file ws_server.h.

◆ CloseAll() [3/4]

bool CppServer::WS::WSServer::CloseAll ( int  status,
const void *  buffer,
size_t  size 
)
virtual

Definition at line 14 of file ws_server.cpp.

◆ CloseAll() [4/4]

bool CppServer::WS::WSServer::CloseAll ( int  status,
std::string_view  text 
)
virtual

Definition at line 25 of file ws_server.cpp.

◆ CreateSession()

std::shared_ptr<Asio::TCPSession> CppServer::WS::WSServer::CreateSession ( const std::shared_ptr< Asio::TCPServer > &  server)
inlineoverrideprotected
Examples
ws_chat_server.cpp.

Definition at line 62 of file ws_server.h.

◆ Multicast()

bool CppServer::WS::WSServer::Multicast ( const void *  buffer,
size_t  size 
)
overridevirtual

Multicast data to all connected WebSocket sessions.

Reimplemented from CppServer::Asio::TCPServer.

Definition at line 36 of file ws_server.cpp.

◆ MulticastBinary() [1/2]

size_t CppServer::WS::WSServer::MulticastBinary ( const void *  buffer,
size_t  size 
)
inline

Definition at line 54 of file ws_server.h.

◆ MulticastBinary() [2/2]

size_t CppServer::WS::WSServer::MulticastBinary ( std::string_view  text)
inline

Definition at line 55 of file ws_server.h.

◆ MulticastPing() [1/2]

size_t CppServer::WS::WSServer::MulticastPing ( const void *  buffer,
size_t  size 
)
inline

Definition at line 58 of file ws_server.h.

◆ MulticastPing() [2/2]

size_t CppServer::WS::WSServer::MulticastPing ( std::string_view  text)
inline

Definition at line 59 of file ws_server.h.

◆ MulticastText() [1/2]

size_t CppServer::WS::WSServer::MulticastText ( const void *  buffer,
size_t  size 
)
inline

Definition at line 50 of file ws_server.h.

◆ MulticastText() [2/2]

size_t CppServer::WS::WSServer::MulticastText ( std::string_view  text)
inline

Definition at line 51 of file ws_server.h.

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

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