D++ (DPP)
C++ Discord API Bot Library
dpp::socket_listener< T, typename > Struct Template Referenceabstract

Listens on a TCP socket for new connections, and whenever a new connection is received, accept it and spawn a new connection of type T. More...

#include <socket_listener.h>

+ Inheritance diagram for dpp::socket_listener< T, typename >:
+ Collaboration diagram for dpp::socket_listener< T, typename >:

Public Member Functions

 socket_listener (cluster *owner, const std::string_view address, uint16_t port, socket_listener_type type=li_plaintext, const std::string &private_key="", const std::string &public_key="")
 Create a new socket listener (TCP server) More...
 
 ~socket_listener ()
 Destructor, detaches on_socket_close event. More...
 
virtual void handle_accept (socket sfd, const struct socket_events &e)
 Handle a new incoming socket with accept() Accepts a new connection, and calls emplace() if valid. More...
 
virtual void emplace (socket newfd)=0
 Emplace a new connection into the connection map for the server. This is a factory function which must be implemented by the deriving class. More...
 

Public Attributes

raii_socket fd
 The listening socket for incoming connections. More...
 
std::unordered_map< socket, std::unique_ptr< T > > connections
 Active connections for the server of type T. More...
 
clustercreator {nullptr}
 Cluster creator. More...
 
bool plaintext {true}
 True if plain text connections to the server are allowed. More...
 
std::string private_key_file
 Private key PEM file path, if running an SSL server. More...
 
std::string public_key_file
 Public key PEM file path, if running an SSL server. More...
 
event_handle close_event
 Event to handle socket removal from the connection map. More...
 
socket_events events
 Socket events for listen socket in the socket engine. More...
 

Detailed Description

template<typename T, typename = std::enable_if_t<std::is_base_of_v<ssl_connection, T>>>
struct dpp::socket_listener< T, typename >

Listens on a TCP socket for new connections, and whenever a new connection is received, accept it and spawn a new connection of type T.

Template Parameters
Ttype for socket connection, must be derived from ssl_connection

Constructor & Destructor Documentation

◆ socket_listener()

template<typename T , typename = std::enable_if_t<std::is_base_of_v<ssl_connection, T>>>
dpp::socket_listener< T, typename >::socket_listener ( cluster owner,
const std::string_view  address,
uint16_t  port,
socket_listener_type  type = li_plaintext,
const std::string &  private_key = "",
const std::string &  public_key = "" 
)
inline

Create a new socket listener (TCP server)

Parameters
ownerOwning cluster
addressIP address to bind the listening socket to, use 0.0.0.0 to bind all interfaces
portPort number to bind the listening socket to
typeType of server, plaintext or SSL
private_keyFor SSL servers, a path to the PEM private key file
public_keyFor SSL servers, a path to the PEM public key file
Exceptions
connection_exceptionon failure to bind or listen to the port/interface

◆ ~socket_listener()

template<typename T , typename = std::enable_if_t<std::is_base_of_v<ssl_connection, T>>>
dpp::socket_listener< T, typename >::~socket_listener ( )
inline

Destructor, detaches on_socket_close event.

Member Function Documentation

◆ emplace()

template<typename T , typename = std::enable_if_t<std::is_base_of_v<ssl_connection, T>>>
virtual void dpp::socket_listener< T, typename >::emplace ( socket  newfd)
pure virtual

Emplace a new connection into the connection map for the server. This is a factory function which must be implemented by the deriving class.

Parameters
newfdFile descriptor for new connection

Implemented in dpp::http_server.

◆ handle_accept()

template<typename T , typename = std::enable_if_t<std::is_base_of_v<ssl_connection, T>>>
virtual void dpp::socket_listener< T, typename >::handle_accept ( socket  sfd,
const struct socket_events e 
)
inlinevirtual

Handle a new incoming socket with accept() Accepts a new connection, and calls emplace() if valid.

Parameters
sfdFile descriptor for listening socket
esocket events for the listening socket

Member Data Documentation

◆ close_event

template<typename T , typename = std::enable_if_t<std::is_base_of_v<ssl_connection, T>>>
event_handle dpp::socket_listener< T, typename >::close_event

Event to handle socket removal from the connection map.

◆ connections

template<typename T , typename = std::enable_if_t<std::is_base_of_v<ssl_connection, T>>>
std::unordered_map<socket, std::unique_ptr<T> > dpp::socket_listener< T, typename >::connections

Active connections for the server of type T.

◆ creator

template<typename T , typename = std::enable_if_t<std::is_base_of_v<ssl_connection, T>>>
cluster* dpp::socket_listener< T, typename >::creator {nullptr}

Cluster creator.

◆ events

template<typename T , typename = std::enable_if_t<std::is_base_of_v<ssl_connection, T>>>
socket_events dpp::socket_listener< T, typename >::events

Socket events for listen socket in the socket engine.

◆ fd

template<typename T , typename = std::enable_if_t<std::is_base_of_v<ssl_connection, T>>>
raii_socket dpp::socket_listener< T, typename >::fd

The listening socket for incoming connections.

◆ plaintext

template<typename T , typename = std::enable_if_t<std::is_base_of_v<ssl_connection, T>>>
bool dpp::socket_listener< T, typename >::plaintext {true}

True if plain text connections to the server are allowed.

◆ private_key_file

template<typename T , typename = std::enable_if_t<std::is_base_of_v<ssl_connection, T>>>
std::string dpp::socket_listener< T, typename >::private_key_file

Private key PEM file path, if running an SSL server.

◆ public_key_file

template<typename T , typename = std::enable_if_t<std::is_base_of_v<ssl_connection, T>>>
std::string dpp::socket_listener< T, typename >::public_key_file

Public key PEM file path, if running an SSL server.

D++ Library version 10.1.1D++ Library version 10.1.0D++ 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