Implements a websocket client based on the SSL client. More...
#include "wsclient.h"
Public Member Functions | |
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. More... | |
virtual | ~websocket_client ()=default |
Destroy the websocket client object. More... | |
virtual void | close () |
Close websocket. More... | |
virtual void | error (uint32_t errorcode) |
Called upon error frame. 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) |
Processes incoming frames from the SSL socket input buffer. More... | |
virtual bool | handle_frame (const std::string &buffer) |
Receives raw frame content only without headers. More... | |
virtual void | log (dpp::loglevel severity, const std::string &msg) const |
Log a message. More... | |
virtual void | one_second_timer () |
Fires every second from the underlying socket I/O loop, used for sending websocket pings. More... | |
void | read_loop () |
Nonblocking I/O loop. More... | |
void | send_close_packet () |
Send OP_CLOSE error code 1000 to the other side of the connection. More... | |
virtual void | write (const std::string &data) |
Write to websocket. 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 () |
(Re)connect More... | |
ws_state | get_state () |
Get websocket state. 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_connection * | ssl |
Openssl opaque contexts. More... | |
Implements a websocket client based on the SSL 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.
hostname | Hostname to connect to |
port | Port to connect to |
urlpath | The URL path components of the HTTP request to send |
opcode | The encoding type to use, either OP_BINARY or OP_TEXT |
References dpp::HTTP_HEADERS, dpp::http_version, and write().
|
virtualdefault |
Destroy the websocket client object.
|
virtual |
Close websocket.
Reimplemented from dpp::ssl_client.
Referenced by dpp::discord_client::handle_frame().
|
protectedvirtual |
(Re)connect
Reimplemented from dpp::ssl_client.
Referenced by dpp::discord_client::discord_client(), dpp::discord_voice_client::discord_voice_client(), and dpp::discord_voice_client::discover_ip().
|
virtual |
Called upon error frame.
errorcode | The error code from the websocket server |
Reimplemented in dpp::discord_voice_client, and dpp::discord_client.
|
inherited |
|
inherited |
Get the bytes out objectGet total bytes sent.
References dpp::ssl_client::bytes_out.
|
inherited |
|
protected |
Get websocket state.
References dpp::WS_MASKBIT.
Referenced by dpp::discord_client::is_connected(), dpp::discord_voice_client::is_connected(), and dpp::discord_voice_client::one_second_timer().
|
virtual |
Processes incoming frames from the SSL socket input buffer.
buffer | The buffer contents. Can modify this value removing the head elements when processed. |
Reimplemented from dpp::ssl_client.
References dpp::CONNECTED.
|
virtual |
Receives raw frame content only without headers.
buffer | The buffer contents |
Reimplemented in dpp::discord_voice_client, and dpp::discord_client.
|
virtualinherited |
Log a message.
severity | severity of log message |
msg | Log message to send |
Reimplemented in dpp::discord_voice_client, and dpp::discord_client.
Referenced by dpp::ssl_client::read_loop().
|
virtual |
Fires every second from the underlying socket I/O loop, used for sending websocket pings.
Reimplemented from dpp::ssl_client.
Reimplemented in dpp::discord_voice_client, and dpp::discord_client.
Referenced by dpp::discord_client::one_second_timer().
|
inherited |
Nonblocking I/O loop.
std::exception | Any std::exception (or derivative) thrown from read_loop() causes reconnection of the shard |
References dpp::ssl_client::buffer, dpp::ssl_client::bytes_in, dpp::ssl_client::bytes_out, dpp::ssl_client::custom_readable_fd, dpp::ssl_client::custom_readable_ready, dpp::ssl_client::custom_writeable_fd, dpp::ssl_client::custom_writeable_ready, DPP_BUFSIZE, dpp::err_invalid_socket, dpp::err_nonblocking_failure, dpp::err_socket_error, dpp::ssl_client::handle_buffer(), INVALID_SOCKET, dpp::ssl_client::last_tick, dpp::ll_warning, dpp::ssl_client::log(), dpp::ssl_client::nonblocking, dpp::ssl_client::obuffer, dpp::ssl_client::one_second_timer(), dpp::ssl_client::plaintext, dpp::set_nonblocking(), dpp::ssl_client::sfd, dpp::openssl_connection::ssl, and dpp::ssl_client::ssl.
void dpp::websocket_client::send_close_packet | ( | ) |
Send OP_CLOSE error code 1000 to the other side of the connection.
This indicates graceful close.
References dpp::ssl_client::close(), and dpp::HTTP_HEADERS.
|
virtual |
Write to websocket.
Encapsulates data in frames if the status is CONNECTED.
data | The data to send. |
Reimplemented from dpp::ssl_client.
Referenced by dpp::discord_client::handle_frame(), dpp::discord_voice_client::handle_frame(), dpp::discord_client::one_second_timer(), dpp::discord_voice_client::one_second_timer(), and websocket_client().
|
protectedinherited |
Input buffer received from socket.
Referenced by dpp::ssl_client::close(), dpp::discord_client::handle_frame(), and dpp::ssl_client::read_loop().
|
protectedinherited |
Bytes in.
Referenced by dpp::ssl_client::get_bytes_in(), and dpp::ssl_client::read_loop().
|
protectedinherited |
Bytes out.
Referenced by dpp::ssl_client::get_bytes_out(), and dpp::ssl_client::read_loop().
|
protectedinherited |
SSL cipher in use.
Referenced by dpp::ssl_client::connect(), and dpp::ssl_client::get_cipher().
|
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.
Referenced by dpp::discord_voice_client::handle_frame(), and dpp::ssl_client::read_loop().
|
inherited |
This event will be called when you can read from the custom fd.
Referenced by dpp::discord_voice_client::handle_frame(), and dpp::ssl_client::read_loop().
|
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).
Referenced by dpp::discord_voice_client::handle_frame(), and dpp::ssl_client::read_loop().
|
inherited |
This event will be called when you can write to a custom fd.
Referenced by dpp::discord_voice_client::handle_frame(), and dpp::ssl_client::read_loop().
|
protectedinherited |
Hostname connected to.
Referenced by dpp::ssl_client::close(), dpp::ssl_client::connect(), and dpp::ssl_client::ssl_client().
|
inherited |
True if we are keeping the connection alive after it has finished.
Referenced by dpp::ssl_client::close(), and dpp::ssl_client::ssl_client().
|
protectedinherited |
For timers.
Referenced by dpp::ssl_client::read_loop().
|
protectedinherited |
True if we are establishing a new connection, false if otherwise.
Referenced by dpp::ssl_client::connect(), and dpp::ssl_client::ssl_client().
|
protectedinherited |
True if in nonblocking mode.
The socket switches to nonblocking mode once ReadLoop is called.
Referenced by dpp::ssl_client::connect(), dpp::ssl_client::read_loop(), and dpp::ssl_client::write().
|
protectedinherited |
Output buffer for sending to socket.
Referenced by dpp::ssl_client::close(), dpp::ssl_client::read_loop(), and dpp::ssl_client::write().
|
protectedinherited |
True for a plain text connection.
Referenced by dpp::ssl_client::close(), dpp::ssl_client::connect(), dpp::ssl_client::read_loop(), dpp::ssl_client::ssl_client(), and dpp::ssl_client::write().
|
protectedinherited |
Port connected to.
Referenced by dpp::ssl_client::close(), dpp::ssl_client::connect(), and dpp::ssl_client::ssl_client().
|
protectedinherited |
Raw file descriptor of connection.
Referenced by dpp::ssl_client::close(), dpp::ssl_client::connect(), dpp::discord_client::one_second_timer(), dpp::ssl_client::read_loop(), dpp::ssl_client::ssl_client(), and dpp::ssl_client::write().
|
protectedinherited |
Openssl opaque contexts.
Referenced by dpp::ssl_client::close(), dpp::ssl_client::connect(), dpp::ssl_client::read_loop(), dpp::ssl_client::ssl_client(), and dpp::ssl_client::write().