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

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

#include <wsclient.h>

+ Inheritance diagram for dpp::WSClient:
+ Collaboration diagram for dpp::WSClient:

Public Member Functions

 WSClient (const std::string &hostname, const std::string &port="443", const std::string &urlpath="")
 
virtual ~WSClient ()
 
virtual void write (const std::string &data)
 Write to websocket. Encapsulates data in frames if the status is CONNECTED. More...
 
virtual bool HandleBuffer (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...
 
uint64_t GetBytesOut ()
 
uint64_t GetBytesIn ()
 
void ReadLoop ()
 Nonblocking I/O loop. More...
 
virtual void log (dpp::loglevel severity, const std::string &msg)
 Log a message. More...
 

Public Attributes

std::function< int()> custom_readable_fd
 Attaching an additional file descriptor to this function will send notifications when there is data to read. More...
 
std::function< int()> 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 ()
 
WSState GetState ()
 
virtual void OneSecondTimer ()
 

Protected Attributes

std::string buffer
 
std::string obuffer
 
bool nonblocking
 
int sfd
 
SSL * ssl
 
SSL_CTX * ctx
 
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

◆ WSClient()

dpp::WSClient::WSClient ( const std::string &  hostname,
const std::string &  port = "443",
const std::string &  urlpath = "" 
)

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

◆ ~WSClient()

virtual dpp::WSClient::~WSClient ( )
virtual

Destructor

Member Function Documentation

◆ close()

virtual void dpp::WSClient::close ( )
virtual

Close websocket.

Reimplemented from dpp::SSLClient.

◆ Connect()

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

(Re)connect

Reimplemented from dpp::SSLClient.

◆ Error()

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

Called upon error frame.

Parameters
errorcodeThe error code from the websocket server

Reimplemented in dpp::DiscordClient, and dpp::DiscordVoiceClient.

◆ GetBytesIn()

uint64_t dpp::SSLClient::GetBytesIn ( )
inherited

Get total bytes received

◆ GetBytesOut()

uint64_t dpp::SSLClient::GetBytesOut ( )
inherited

Get total bytes sent

◆ GetState()

WSState dpp::WSClient::GetState ( )
protected

Get websocket state

Returns
websocket state

◆ HandleBuffer()

virtual bool dpp::WSClient::HandleBuffer ( 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::SSLClient.

◆ HandleFrame()

virtual bool dpp::WSClient::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::DiscordClient, and dpp::DiscordVoiceClient.

◆ log()

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

Log a message.

Parameters
severityseverity of log message
msgLog message to send

Reimplemented in dpp::DiscordClient, and dpp::DiscordVoiceClient.

◆ OneSecondTimer()

virtual void dpp::SSLClient::OneSecondTimer ( )
protectedvirtualinherited

Called every second

Reimplemented in dpp::DiscordClient, and dpp::DiscordVoiceClient.

◆ ReadLoop()

void dpp::SSLClient::ReadLoop ( )
inherited

Nonblocking I/O loop.

◆ write()

virtual void dpp::WSClient::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::SSLClient.

Member Data Documentation

◆ buffer

std::string dpp::SSLClient::buffer
protectedinherited

Input buffer received from openssl

◆ bytes_in

uint64_t dpp::SSLClient::bytes_in
protectedinherited

Bytes in

◆ bytes_out

uint64_t dpp::SSLClient::bytes_out
protectedinherited

Bytes out

◆ cipher

std::string dpp::SSLClient::cipher
protectedinherited

SSL cipher in use

◆ ctx

SSL_CTX* dpp::SSLClient::ctx
protectedinherited

OpenSSL context

◆ custom_readable_fd

std::function<int()> dpp::SSLClient::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::SSLClient::custom_readable_ready
inherited

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

◆ custom_writeable_fd

std::function<int()> dpp::SSLClient::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::SSLClient::custom_writeable_ready
inherited

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

◆ hostname

std::string dpp::SSLClient::hostname
protectedinherited

Hostname connected to

◆ last_tick

time_t dpp::SSLClient::last_tick
protectedinherited

For timers

◆ nonblocking

bool dpp::SSLClient::nonblocking
protectedinherited

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

◆ obuffer

std::string dpp::SSLClient::obuffer
protectedinherited

Output buffer for sending to openssl

◆ port

std::string dpp::SSLClient::port
protectedinherited

Port connected to

◆ sfd

int dpp::SSLClient::sfd
protectedinherited

Raw file descriptor of connection

◆ ssl

SSL* dpp::SSLClient::ssl
protectedinherited

OpenSSL session

D++ 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