D++ (DPP)
C++ Discord API Bot Library
dpp::websocket_client Class Reference

Implements a websocket client based on the SSL client. More...

#include <wsclient.h>

+ Inheritance diagram for dpp::websocket_client:
+ Collaboration diagram for dpp::websocket_client:

Public Member Functions

 websocket_client (const std::string &hostname, const std::string &port="443", const std::string &urlpath="", ws_opcode opcode=OP_BINARY)
 
virtual ~websocket_client ()
 
virtual void write (const std::string &data)
 Write to websocket. Encapsulates data in frames if the status is CONNECTED. More...
 
virtual bool handle_buffer (std::string &buffer)
 Processes incoming frames from the SSL socket input buffer. More...
 
virtual void close ()
 Close websocket. More...
 
virtual bool HandleFrame (const std::string &buffer)
 Receives raw frame content only without headers. More...
 
virtual void Error (uint32_t errorcode)
 Called upon error frame. More...
 
virtual void one_second_timer ()
 
uint64_t get_bytes_out ()
 
uint64_t get_bytes_in ()
 
std::string get_cipher ()
 
void read_loop ()
 Nonblocking I/O loop. More...
 
virtual void log (dpp::loglevel severity, const std::string &msg) const
 Log a message. More...
 

Public Attributes

std::function< dpp::socket()> custom_readable_fd
 Attaching an additional file descriptor to this function will send notifications when there is data to read. More...
 
std::function< dpp::socket()> custom_writeable_fd
 Attaching an additional file descriptor to this function will send notifications when you are able to write to the socket. More...
 
std::function< void()> custom_readable_ready
 This event will be called when you can read from the custom fd. More...
 
std::function< void()> custom_writeable_ready
 This event will be called when you can write to a custom fd. More...
 

Protected Member Functions

virtual void Connect ()
 
ws_state GetState ()
 

Protected Attributes

std::string buffer
 
std::string obuffer
 
bool nonblocking
 
dpp::socket sfd
 
opensslcontext * ssl
 
std::string cipher
 
time_t last_tick
 
std::string hostname
 
std::string port
 
uint64_t bytes_out
 
uint64_t bytes_in
 

Detailed Description

Implements a websocket client based on the SSL client.

Constructor & Destructor Documentation

◆ websocket_client()

dpp::websocket_client::websocket_client ( const std::string &  hostname,
const std::string &  port = "443",
const std::string &  urlpath = "",
ws_opcode  opcode = OP_BINARY 
)

Connect to a specific websocket server.

Parameters
hostnameHostname to connect to
portPort to connect to
urlpathThe URL path components of the HTTP request to send
opcodeThe encoding type to use, either OP_BINARY or OP_TEXT
Note
Voice websockets only support OP_TEXT, and other websockets must be OP_BINARY if you are going to send ETF.

◆ ~websocket_client()

virtual dpp::websocket_client::~websocket_client ( )
virtual

Destructor

Member Function Documentation

◆ close()

virtual void dpp::websocket_client::close ( )
virtual

Close websocket.

Reimplemented from dpp::ssl_client.

◆ Connect()

virtual void dpp::websocket_client::Connect ( )
protectedvirtual

(Re)connect

Reimplemented from dpp::ssl_client.

◆ Error()

virtual void dpp::websocket_client::Error ( uint32_t  errorcode)
virtual

Called upon error frame.

Parameters
errorcodeThe error code from the websocket server

Reimplemented in dpp::discord_client, and dpp::discord_voice_client.

◆ get_bytes_in()

uint64_t dpp::ssl_client::get_bytes_in ( )
inherited

Get total bytes received

◆ get_bytes_out()

uint64_t dpp::ssl_client::get_bytes_out ( )
inherited

Get total bytes sent

◆ get_cipher()

std::string dpp::ssl_client::get_cipher ( )
inherited

Get SSL cipher name

◆ GetState()

ws_state dpp::websocket_client::GetState ( )
protected

Get websocket state

Returns
websocket state

◆ handle_buffer()

virtual bool dpp::websocket_client::handle_buffer ( std::string &  buffer)
virtual

Processes incoming frames from the SSL socket input buffer.

Parameters
bufferThe buffer contents. Can modify this value removing the head elements when processed.

Reimplemented from dpp::ssl_client.

◆ HandleFrame()

virtual bool dpp::websocket_client::HandleFrame ( const std::string &  buffer)
virtual

Receives raw frame content only without headers.

Parameters
bufferThe buffer contents
Returns
True if the frame was successfully handled. False if no valid frame is in the buffer.

Reimplemented in dpp::discord_client, and dpp::discord_voice_client.

◆ log()

virtual void dpp::ssl_client::log ( dpp::loglevel  severity,
const std::string &  msg 
) const
virtualinherited

Log a message.

Parameters
severityseverity of log message
msgLog message to send

Reimplemented in dpp::discord_client, and dpp::discord_voice_client.

◆ one_second_timer()

virtual void dpp::websocket_client::one_second_timer ( )
virtual

Fires every second from the underlying socket I/O loop, used for sending webscocket pings

Reimplemented from dpp::ssl_client.

Reimplemented in dpp::discord_client, and dpp::discord_voice_client.

◆ read_loop()

void dpp::ssl_client::read_loop ( )
inherited

Nonblocking I/O loop.

Exceptions
std::exceptionAny std::exception (or derivative) thrown from read_loop() causes reconnection of the shard

◆ write()

virtual void dpp::websocket_client::write ( const std::string &  data)
virtual

Write to websocket. Encapsulates data in frames if the status is CONNECTED.

Parameters
dataThe data to send.

Reimplemented from dpp::ssl_client.

Member Data Documentation

◆ buffer

std::string dpp::ssl_client::buffer
protectedinherited

Input buffer received from openssl

◆ bytes_in

uint64_t dpp::ssl_client::bytes_in
protectedinherited

Bytes in

◆ bytes_out

uint64_t dpp::ssl_client::bytes_out
protectedinherited

Bytes out

◆ cipher

std::string dpp::ssl_client::cipher
protectedinherited

SSL cipher in use

◆ custom_readable_fd

std::function<dpp::socket()> dpp::ssl_client::custom_readable_fd
inherited

Attaching an additional file descriptor to this function will send notifications when there is data to read.

NOTE: Only hook this if you NEED it as it can increase CPU usage of the thread! Returning -1 means that you don't want to be notified.

◆ custom_readable_ready

std::function<void()> dpp::ssl_client::custom_readable_ready
inherited

This event will be called when you can read from the custom fd.

◆ custom_writeable_fd

std::function<dpp::socket()> dpp::ssl_client::custom_writeable_fd
inherited

Attaching an additional file descriptor to this function will send notifications when you are able to write to the socket.

NOTE: Only hook this if you NEED it as it can increase CPU usage of the thread! You should toggle this to -1 when you do not have anything to write otherwise it'll keep triggering repeatedly (it is level triggered).

◆ custom_writeable_ready

std::function<void()> dpp::ssl_client::custom_writeable_ready
inherited

This event will be called when you can write to a custom fd.

◆ hostname

std::string dpp::ssl_client::hostname
protectedinherited

Hostname connected to

◆ last_tick

time_t dpp::ssl_client::last_tick
protectedinherited

For timers

◆ nonblocking

bool dpp::ssl_client::nonblocking
protectedinherited

True if in nonblocking mode. The socket switches to nonblocking mode once ReadLoop is called.

◆ obuffer

std::string dpp::ssl_client::obuffer
protectedinherited

Output buffer for sending to openssl

◆ port

std::string dpp::ssl_client::port
protectedinherited

Port connected to

◆ sfd

dpp::socket dpp::ssl_client::sfd
protectedinherited

Raw file descriptor of connection

◆ ssl

opensslcontext* dpp::ssl_client::ssl
protectedinherited

Openssl opaque contexts

D++ Library version 10.0.35D++ Library version 10.0.34D++ Library version 10.0.33D++ Library version 10.0.32D++ Library version 10.0.31D++ Library version 10.0.30D++ Library version 10.0.29D++ Library version 10.0.28D++ Library version 10.0.27D++ Library version 10.0.26D++ Library version 10.0.25D++ Library version 10.0.24D++ Library version 10.0.23D++ Library version 10.0.22D++ Library version 10.0.21D++ Library version 10.0.20D++ Library version 10.0.19D++ Library version 10.0.18D++ Library version 10.0.17D++ Library version 10.0.16D++ Library version 10.0.15D++ Library version 10.0.14D++ Library version 10.0.13D++ Library version 10.0.12D++ Library version 10.0.11D++ Library version 10.0.10D++ Library version 10.0.9D++ Library version 10.0.8D++ Library version 10.0.7D++ Library version 10.0.6D++ Library version 10.0.5D++ Library version 10.0.4D++ Library version 10.0.3D++ Library version 10.0.2D++ Library version 10.0.1D++ Library version 10.0.0D++ Library version 9.0.19D++ Library version 9.0.18D++ Library version 9.0.17D++ Library version 9.0.16D++ Library version 9.0.15D++ Library version 9.0.14D++ Library version 9.0.13D++ Library version 9.0.12D++ Library version 9.0.11D++ Library version 9.0.10D++ Library version 9.0.9D++ Library version 9.0.8D++ Library version 9.0.7D++ Library version 9.0.6D++ Library version 9.0.5D++ Library version 9.0.4D++ Library version 9.0.3D++ Library version 9.0.2D++ Library version 9.0.1D++ Library version 9.0.0D++ Library version 1.0.2D++ Library version 1.0.1D++ Library version 1.0.0