Listens for TCP connections and establishes Sessions. More...
#include <tcpserver.h>
Public Member Functions | |
Server (EPoll &epoll, SSLContext *ctx, const int domain=AF_INET) | |
Construct a server instance. More... | |
virtual | ~Server () |
Destroy the server instance. More... | |
void | start (in_port_t port, string bindaddress, bool useSSL=false, int backlog=64) |
Start up the server. More... | |
void | start (in_port_t port, char *bindaddress, bool useSSL=false, int backlog=64) |
Start up the server. More... | |
void | stop () |
Stop the server. | |
bool | listening () |
Determine if the server is listening. More... | |
SSLContext * | ctx () |
Get the SSL context for the server. | |
![]() | |
Socket (EPoll &epoll, const int domain=AF_INET, const int socket=0, const bool blocking=false, const int events=(EPOLLIN|EPOLLRDHUP)) | |
Construct a blocking or non-blocking socket handle that responds to certain epoll events. More... | |
~Socket () | |
Closes and destroys the socket. More... | |
int | socket () const |
Return the linux socket handle. | |
int | domain () const |
Return the socket domain (AF_INET or AF_INET6) | |
virtual void | disconnect () |
Shuts down the socket gracefully. | |
Static Public Member Functions | |
static bool | printifaddrs () |
Print a list of interface addresses to cout. | |
Protected Member Functions | |
void | handleEvents (uint32_t events) override |
Called by the EPoll class when the listening socket recieves an event from the OS. More... | |
virtual Session * | createSession (const int socket, const sockaddr_in peer_address)=0 |
Called when the server needs to create a new object of the tcp::Session class. More... | |
bool | findifaddr (const string ifname, sockaddr *addr) |
Returns an interface address from an interface name and domain. | |
![]() | |
bool | setEvents (int events) |
Changes which epoll events the socket listens for. More... | |
virtual void | disconnected () |
Called when a connection is disconnected due to a network error. More... | |
EPoll & | epoll () |
Returns a reference to the epoll instance used by this socket. | |
Protected Attributes | |
std::map< int, tcp::Session * > | sessions |
Maps socket handles to their corresponding tcp::Session objects. More... | |
![]() | |
recursive_mutex | mtx |
The mutex used to provide exclusive access to the socket. | |
SocketState | state_ |
Descendant classes can manipulate the socket state directly. | |
Friends | |
class | Session |
Listens for TCP connections and establishes Sessions.
Construct an instance of tcp::server to start the server. Destroy the object to stop the server.
Definition at line 35 of file tcpserver.h.
|
inline |
|
virtual |
Destroy the server instance.
Destoying the server stops it from listening and ends all sessions by calling the Session::disconnect() method.
Definition at line 18 of file tcpserver.cpp.
|
protectedpure virtual |
Called when the server needs to create a new object of the tcp::Session class.
Users of this component need to create their own custom tcp::Session class and use this method to return an instance to it.
socket | The socket handle to pass to the constructor of tcp::Session descendant |
peer_address | The address and port of the connected peer |
|
overrideprotectedvirtual |
Called by the EPoll class when the listening socket recieves an event from the OS.
The listening socket recieves the EPOLLIN event when a new connection is available to be accepted. This is handled by tcp::Server, which calls the acceptConnection method.
Implements tcp::Socket.
Definition at line 78 of file tcpserver.cpp.
|
inline |
Determine if the server is listening.
Definition at line 71 of file tcpserver.h.
void tcp::Server::start | ( | in_port_t | port, |
char * | bindaddress, | ||
bool | useSSL = false , |
||
int | backlog = 64 |
||
) |
Start up the server.
See the other overload for documentation
Definition at line 23 of file tcpserver.cpp.
void tcp::Server::start | ( | in_port_t | port, |
string | bindaddress, | ||
bool | useSSL = false , |
||
int | backlog = 64 |
||
) |
Start up the server.
Main method to start up the server. An overloaded version takes a char* instead of a string.
port | [in] The port number to bind to |
bindaddress | [in] The interface name or IP address to bind to. Leave blank to bind to any address |
useSSL | [in] Set to true to use SSL on the connection |
backlog | [in] How many connections can be stored in the listen backlog before the server stops accepting new connections. |
Definition at line 28 of file tcpserver.cpp.
|
protected |
Maps socket handles to their corresponding tcp::Session objects.
Descendant classes may need access to the sessions map.
Definition at line 101 of file tcpserver.h.