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

Implements a simple non-blocking SSL stream client. More...

#include "sslclient.h"

Inheritance diagram for dpp::ssl_client:
Collaboration diagram for dpp::ssl_client:

Public Member Functions

 ssl_client (const std::string &_hostname, const std::string &_port="443", bool plaintext_downgrade=false, bool reuse=false)
 Connect to a specified host and port. More...
 
virtual ~ssl_client ()
 Destroy the ssl_client object. More...
 
virtual void close ()
 Close socket connection. More...
 
uint64_t get_bytes_in ()
 Get total bytes received. More...
 
uint64_t get_bytes_out ()
 Get the bytes out objectGet total bytes sent. More...
 
std::string get_cipher ()
 Get SSL cipher name. More...
 
virtual bool handle_buffer (std::string &buffer)
 Handle input from the input buffer. More...
 
virtual void log (dpp::loglevel severity, const std::string &msg) const
 Log a message. More...
 
void read_loop ()
 Nonblocking I/O loop. More...
 
virtual void write (const std::string &data)
 Write to the output buffer. More...
 

Public Attributes

socket_callback_t custom_readable_fd
 Attaching an additional file descriptor to this function will send notifications when there is data to read. More...
 
socket_notification_t custom_readable_ready
 This event will be called when you can read from the custom fd. More...
 
socket_callback_t custom_writeable_fd
 Attaching an additional file descriptor to this function will send notifications when you are able to write to the socket. More...
 
socket_notification_t custom_writeable_ready
 This event will be called when you can write to a custom fd. More...
 
bool keepalive
 True if we are keeping the connection alive after it has finished. More...
 

Protected Member Functions

virtual void connect ()
 Start SSL connection and connect to TCP endpoint. More...
 
virtual void one_second_timer ()
 Called every second. More...
 

Protected Attributes

std::string buffer
 Input buffer received from socket. More...
 
uint64_t bytes_in
 Bytes in. More...
 
uint64_t bytes_out
 Bytes out. More...
 
std::string cipher
 SSL cipher in use. More...
 
std::string hostname
 Hostname connected to. More...
 
time_t last_tick
 For timers. More...
 
bool make_new
 True if we are establishing a new connection, false if otherwise. More...
 
bool nonblocking
 True if in nonblocking mode. More...
 
std::string obuffer
 Output buffer for sending to socket. More...
 
bool plaintext
 True for a plain text connection. More...
 
std::string port
 Port connected to. More...
 
dpp::socket sfd
 Raw file descriptor of connection. More...
 
openssl_connectionssl
 Openssl opaque contexts. More...
 

Detailed Description

Implements a simple non-blocking SSL stream client.

Note
although the design is non-blocking the run() method will execute in an infinite loop until the socket disconnects. This is intended to be run within a std::thread.

Constructor & Destructor Documentation

◆ ssl_client()

dpp::ssl_client::ssl_client ( const std::string &  _hostname,
const std::string &  _port = "443",
bool  plaintext_downgrade = false,
bool  reuse = false 
)

Connect to a specified host and port.

Throws std::runtime_error on fatal error.

Parameters
_hostnameThe hostname to connect to
_portthe Port number to connect to
plaintext_downgradeSet to true to connect using plaintext only, without initialising SSL.
reuseAttempt to reuse previous connections for this hostname and port, if available Note that no Discord endpoints will function when downgraded. This option is provided only for connection to non-Discord addresses such as within dpp::cluster::request().
Exceptions
dpp::exceptionFailed to initialise connection

References dpp::close_socket(), connect(), dpp::err_connect_failure, hostname, INVALID_SOCKET, keepalive, dpp::keepalives, make_new, plaintext, port, dpp::set_signal_handler(), sfd, dpp::openssl_connection::ssl, and ssl.

Here is the call graph for this function:

◆ ~ssl_client()

dpp::ssl_client::~ssl_client ( )
virtual

Destroy the ssl_client object.

Member Function Documentation

◆ close()

void dpp::ssl_client::close ( )
virtual

◆ connect()

void dpp::ssl_client::connect ( )
protectedvirtual

◆ get_bytes_in()

uint64_t dpp::ssl_client::get_bytes_in ( )

Get total bytes received.

Returns
uint64_t bytes received

References bytes_in.

◆ get_bytes_out()

uint64_t dpp::ssl_client::get_bytes_out ( )

Get the bytes out objectGet total bytes sent.

Returns
uint64_t bytes sent

References bytes_out.

◆ get_cipher()

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

Get SSL cipher name.

Returns
std::string ssl cipher name

References cipher.

◆ handle_buffer()

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

Handle input from the input buffer.

This function will be called until all data in the buffer has been processed and the buffer is empty.

Parameters
bufferthe buffer content. Will be modified removing any processed front elements
Returns
bool True if the socket should remain connected

Reimplemented in dpp::https_client, and dpp::websocket_client.

Referenced by read_loop().

Here is the caller graph for this function:

◆ log()

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

Log a message.

Parameters
severityseverity of log message
msgLog message to send

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

Referenced by read_loop().

Here is the caller graph for this function:

◆ one_second_timer()

void dpp::ssl_client::one_second_timer ( )
protectedvirtual

Called every second.

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

Referenced by read_loop().

Here is the caller graph for this function:

◆ read_loop()

void dpp::ssl_client::read_loop ( )

◆ write()

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

Write to the output buffer.

Parameters
dataData to be written to the buffer
Note
The data may not be written immediately and may be written at a later time to the socket.

Reimplemented in dpp::websocket_client.

References dpp::err_ssl_write, dpp::err_write, INVALID_SOCKET, nonblocking, obuffer, plaintext, sfd, dpp::openssl_connection::ssl, and ssl.

Member Data Documentation

◆ buffer

std::string dpp::ssl_client::buffer
protected

Input buffer received from socket.

Referenced by close(), dpp::discord_client::handle_frame(), and read_loop().

◆ bytes_in

uint64_t dpp::ssl_client::bytes_in
protected

Bytes in.

Referenced by get_bytes_in(), and read_loop().

◆ bytes_out

uint64_t dpp::ssl_client::bytes_out
protected

Bytes out.

Referenced by get_bytes_out(), and read_loop().

◆ cipher

std::string dpp::ssl_client::cipher
protected

SSL cipher in use.

Referenced by connect(), and get_cipher().

◆ custom_readable_fd

socket_callback_t dpp::ssl_client::custom_readable_fd

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.

Referenced by dpp::discord_voice_client::handle_frame(), and read_loop().

◆ custom_readable_ready

socket_notification_t dpp::ssl_client::custom_readable_ready

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

Referenced by dpp::discord_voice_client::handle_frame(), and read_loop().

◆ custom_writeable_fd

socket_callback_t dpp::ssl_client::custom_writeable_fd

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).

Referenced by dpp::discord_voice_client::handle_frame(), and read_loop().

◆ custom_writeable_ready

socket_notification_t dpp::ssl_client::custom_writeable_ready

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

Referenced by dpp::discord_voice_client::handle_frame(), and read_loop().

◆ hostname

std::string dpp::ssl_client::hostname
protected

Hostname connected to.

Referenced by close(), connect(), and ssl_client().

◆ keepalive

bool dpp::ssl_client::keepalive

True if we are keeping the connection alive after it has finished.

Referenced by close(), and ssl_client().

◆ last_tick

time_t dpp::ssl_client::last_tick
protected

For timers.

Referenced by read_loop().

◆ make_new

bool dpp::ssl_client::make_new
protected

True if we are establishing a new connection, false if otherwise.

Referenced by connect(), and ssl_client().

◆ nonblocking

bool dpp::ssl_client::nonblocking
protected

True if in nonblocking mode.

The socket switches to nonblocking mode once ReadLoop is called.

Referenced by connect(), read_loop(), and write().

◆ obuffer

std::string dpp::ssl_client::obuffer
protected

Output buffer for sending to socket.

Referenced by close(), read_loop(), and write().

◆ plaintext

bool dpp::ssl_client::plaintext
protected

True for a plain text connection.

Referenced by close(), connect(), read_loop(), ssl_client(), and write().

◆ port

std::string dpp::ssl_client::port
protected

Port connected to.

Referenced by close(), connect(), and ssl_client().

◆ sfd

dpp::socket dpp::ssl_client::sfd
protected

Raw file descriptor of connection.

Referenced by close(), connect(), dpp::discord_client::one_second_timer(), read_loop(), ssl_client(), and write().

◆ ssl

openssl_connection* dpp::ssl_client::ssl
protected

Openssl opaque contexts.

Referenced by close(), connect(), read_loop(), ssl_client(), and write().


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