![]() |
D++ (DPP)
C++ Discord API Bot Library
|
Implements a simple non-blocking SSL stream connection. More...
#include <sslconnection.h>
Public Member Functions | |
ssl_connection (cluster *creator, 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. More... | |
ssl_connection (cluster *creator, socket fd, uint16_t port, bool plaintext_downgrade=false, const std::string &private_key="", const std::string &public_key="") | |
Accept a new connection from listen()/accept() socket. More... | |
virtual | ~ssl_connection () |
Destroy the ssl_connection object. More... | |
virtual void | close () |
Close socket connection. More... | |
void | complete_handshake (const struct socket_events *ev) |
Called while SSL handshake is in progress. If the handshake completes, the state of the socket is progressed to an established state. More... | |
void | enable_raw_tracing () |
For low-level debugging, calling this function will enable low level I/O logging for this connection to the logger. This can be very loud, and output a lot of data, so only enable it selectively where you need it. 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... | |
uint64_t | get_unique_id () const |
Every request made has a unique ID. This increments for every request, starting at 1. You can use this for statistics, or to associate requests and replies in external event loops. More... | |
virtual bool | handle_buffer (std::string &buffer) |
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. More... | |
virtual void | log (dpp::loglevel severity, const std::string &msg) const |
Log a message. More... | |
void | on_error (dpp::socket fd, const struct dpp::socket_events &, int error_code) |
Called when there is an error on the TCP socket. More... | |
void | on_read (dpp::socket fd, const struct dpp::socket_events &ev) |
Called when the TCP socket has data to read. More... | |
void | on_write (dpp::socket fd, const struct dpp::socket_events &e) |
Called when the TCP socket can be written to without blocking. More... | |
void | read_loop () |
Set up non blocking I/O and configure on_read, on_write and on_error. More... | |
void | socket_write (const std::string_view data) |
Write to the output buffer. More... | |
Public Attributes | |
bool | keepalive |
True if we are keeping the connection alive after it has finished. More... | |
class cluster * | owner |
Owning cluster. More... | |
std::string | private_key_file |
Private key PEM file path for inbound SSL connections. More... | |
std::string | public_key_file |
Public key PEM file path for inbound SSL connections. More... | |
Protected Member Functions | |
virtual void | connect () |
Start SSL connection and connect to TCP endpoint. More... | |
void | do_raw_trace (const std::string &message) const |
If raw_trace is set to true, log a debug message for this connection. More... | |
virtual void | on_buffer_drained () |
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... | |
uint8_t | connect_retries {0} |
How many times we retried connect() More... | |
bool | connected {false} |
True if connection is completed. More... | |
std::string | hostname |
Hostname connected to. More... | |
time_t | last_tick |
For timers. 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... | |
bool | raw_trace {false} |
Set this to true to log all IO to debug for this connection. This is an internal developer facility. Do not enable it unless you need to, as it will be very noisy. More... | |
dpp::socket | sfd |
Raw file descriptor of connection. More... | |
openssl_connection * | ssl |
Openssl opaque contexts. More... | |
time_t | start |
Start time of connection. More... | |
bool | tcp_connect_done {false} |
True if tcp connect() succeeded. More... | |
timer | timer_handle |
Timer handle for one second timer. More... | |
uint64_t | unique_id |
Unique ID of socket used as a nonce You can use this to identify requests vs reply if you want. D++ itself only sets this, and does not use it in any logic. It starts at 1 and increments for each request made. More... | |
Implements a simple non-blocking SSL stream connection.
dpp::ssl_connection::ssl_connection | ( | cluster * | creator, |
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.
creator | Creating cluster |
_hostname | The hostname to connect to |
_port | the Port number to connect to |
plaintext_downgrade | Set to true to connect using plaintext only, without initialising SSL. |
reuse | Attempt 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(). |
dpp::exception | Failed to initialise connection |
dpp::ssl_connection::ssl_connection | ( | cluster * | creator, |
socket | fd, | ||
uint16_t | port, | ||
bool | plaintext_downgrade = false , |
||
const std::string & | private_key = "" , |
||
const std::string & | public_key = "" |
||
) |
Accept a new connection from listen()/accept() socket.
creator | Creating cluster |
fd | Socket file descriptor assigned by accept() |
port | Port the new fd came from |
plaintext_downgrade | Set to true to connect using plaintext only, without initialising SSL. |
private_key | if plaintext_downgrade is set to false, a private key PEM file for SSL connections |
public_key | if plaintext_downgrade is set to false, a public key PEM file for SSL connections |
|
virtual |
Destroy the ssl_connection object.
|
virtual |
Close socket connection.
Reimplemented in dpp::http_server_request, dpp::https_client, and dpp::websocket_client.
void dpp::ssl_connection::complete_handshake | ( | const struct socket_events * | ev | ) |
Called while SSL handshake is in progress. If the handshake completes, the state of the socket is progressed to an established state.
ev | Socket events for the socket |
|
protectedvirtual |
Start SSL connection and connect to TCP endpoint.
dpp::exception | Failed to initialise connection |
Reimplemented in dpp::http_server_request, dpp::https_client, and dpp::websocket_client.
|
protected |
If raw_trace is set to true, log a debug message for this connection.
message | debug message |
void dpp::ssl_connection::enable_raw_tracing | ( | ) |
For low-level debugging, calling this function will enable low level I/O logging for this connection to the logger. This can be very loud, and output a lot of data, so only enable it selectively where you need it.
Generally, you won't need this, it is a library development utility.
uint64_t dpp::ssl_connection::get_bytes_in | ( | ) |
Get total bytes received.
uint64_t dpp::ssl_connection::get_bytes_out | ( | ) |
Get the bytes out objectGet total bytes sent.
std::string dpp::ssl_connection::get_cipher | ( | ) |
Get SSL cipher name.
uint64_t dpp::ssl_connection::get_unique_id | ( | ) | const |
Every request made has a unique ID. This increments for every request, starting at 1. You can use this for statistics, or to associate requests and replies in external event loops.
|
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.
buffer | the buffer content. Will be modified removing any processed front elements |
Reimplemented in dpp::http_server_request, dpp::https_client, and dpp::websocket_client.
|
virtual |
Log a message.
severity | severity of log message |
msg | Log message to send |
Reimplemented in dpp::discord_client, and dpp::discord_voice_client.
|
protectedvirtual |
Reimplemented in dpp::http_server_request.
void dpp::ssl_connection::on_error | ( | dpp::socket | fd, |
const struct dpp::socket_events & | , | ||
int | error_code | ||
) |
Called when there is an error on the TCP socket.
fd | File descriptor |
error_code | Error code |
void dpp::ssl_connection::on_read | ( | dpp::socket | fd, |
const struct dpp::socket_events & | ev | ||
) |
Called when the TCP socket has data to read.
fd | File descriptor |
ev | Socket events |
void dpp::ssl_connection::on_write | ( | dpp::socket | fd, |
const struct dpp::socket_events & | e | ||
) |
Called when the TCP socket can be written to without blocking.
fd | File descriptor |
e | Socket events |
|
protectedvirtual |
Called every second.
Reimplemented in dpp::discord_client, dpp::discord_voice_client, dpp::http_server_request, dpp::https_client, and dpp::websocket_client.
void dpp::ssl_connection::read_loop | ( | ) |
Set up non blocking I/O and configure on_read, on_write and on_error.
std::exception | Any std::exception (or derivative) thrown from read_loop() indicates setup failed |
void dpp::ssl_connection::socket_write | ( | const std::string_view | data | ) |
Write to the output buffer.
data | Data to be written to the buffer. |
|
protected |
Input buffer received from socket.
|
protected |
Bytes in.
|
protected |
Bytes out.
|
protected |
SSL cipher in use.
|
protected |
How many times we retried connect()
|
protected |
True if connection is completed.
|
protected |
Hostname connected to.
bool dpp::ssl_connection::keepalive |
True if we are keeping the connection alive after it has finished.
|
protected |
For timers.
|
protected |
Output buffer for sending to socket.
class cluster* dpp::ssl_connection::owner |
Owning cluster.
|
protected |
True for a plain text connection.
|
protected |
Port connected to.
std::string dpp::ssl_connection::private_key_file |
Private key PEM file path for inbound SSL connections.
std::string dpp::ssl_connection::public_key_file |
Public key PEM file path for inbound SSL connections.
|
protected |
Set this to true to log all IO to debug for this connection. This is an internal developer facility. Do not enable it unless you need to, as it will be very noisy.
|
protected |
Raw file descriptor of connection.
|
protected |
Openssl opaque contexts.
|
protected |
Start time of connection.
|
protected |
True if tcp connect() succeeded.
|
protected |
Timer handle for one second timer.
|
protected |
Unique ID of socket used as a nonce You can use this to identify requests vs reply if you want. D++ itself only sets this, and does not use it in any logic. It starts at 1 and increments for each request made.