Bond's TCP Library  1.0
Bond's TCP Client/Server Library
tcp::Server Class Referenceabstract

Listens for TCP connections and establishes Sessions. More...

#include <tcpserver.h>

Collaboration diagram for tcp::Server:

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...
 
SSLContextctx ()
 Get the SSL context for the server.
 
- Public Member Functions inherited from tcp::Socket
 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 SessioncreateSession (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.
 
- Protected Member Functions inherited from tcp::Socket
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...
 
EPollepoll ()
 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...
 
- Protected Attributes inherited from tcp::Socket
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
 

Detailed Description

Listens for TCP connections and establishes Sessions.

Construct an instance of tcp::server to start the server. Destroy the object to stop the server.

Remarks
A single server instance can listen to on an IP4 or IP6 address but not both.
A single server instance can accept SSL connections, normal connections, but not both.
Override the virtual createSession() method to return a custom session descendant class.

Definition at line 35 of file tcpserver.h.

Constructor & Destructor Documentation

◆ Server()

tcp::Server::Server ( EPoll epoll,
SSLContext ctx,
const int  domain = AF_INET 
)
inline

Construct a server instance.

Parameters
domainEither AF_INET or AF_INET6

Definition at line 40 of file tcpserver.h.

40 : Socket(epoll,domain,0,false,EPOLLIN), useSSL_(ctx), ctx_(ctx) {}

◆ ~Server()

tcp::Server::~Server ( )
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.

18  {
19  if (listening())
20  stop();
21 }

Member Function Documentation

◆ createSession()

virtual Session* tcp::Server::createSession ( const int  socket,
const sockaddr_in  peer_address 
)
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.

Parameters
socketThe socket handle to pass to the constructor of tcp::Session descendant
peer_addressThe address and port of the connected peer

◆ handleEvents()

void tcp::Server::handleEvents ( uint32_t  events)
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.

78  {
79  if (listening() && (events & EPOLLIN)) {
80  acceptConnection();
81  }
82 }

◆ listening()

bool tcp::Server::listening ( )
inline

Determine if the server is listening.

Returns
Returns true if the server is listening
Returns false if the server was not able to start listening.
Returns false while the server is being destroyed.

Definition at line 71 of file tcpserver.h.

71 { return state_ == SocketState::LISTENING; }

◆ start() [1/2]

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.

24 {
25  start(port,string(bindaddress),useSSL,backlog);
26 }

◆ start() [2/2]

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.

Remarks
The interface addresses "", "0.0.0.0" and "::" all mean "bind to any address"
Parameters
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.

29 {
30  mtx.lock();
31  useSSL_ = useSSL;
32  memset(&addr_,0,sizeof(addr_));
33  if ((bindaddress == "") || (bindaddress == "0.0.0.0") || (bindaddress == "::")) {
34  if (domain() == AF_INET) {
35  reinterpret_cast<struct sockaddr_in*>(&addr_)->sin_addr.s_addr = INADDR_ANY;
36  }
37  if (domain() == AF_INET6) {
38  reinterpret_cast<struct sockaddr_in6*>(&addr_)->sin6_addr = IN6ADDR_ANY_INIT;
39  }
40  } else {
41  if (!findifaddr(bindaddress,reinterpret_cast<struct sockaddr*>(&addr_))) {
42  error("Interface " + string(bindaddress) + " not found");
43  }
44  }
45  if (domain() == AF_INET) {
46  addr_.ss_family = AF_INET;
47  reinterpret_cast<struct sockaddr_in*>(&addr_)->sin_port = htons(port);
48  }
49  if (domain() == AF_INET6) {
50  addr_.ss_family = AF_INET6;
51  reinterpret_cast<struct sockaddr_in6*>(&addr_)->sin6_port = htons(port);
52  }
53 
54  int enable = 1;
55  if (setsockopt(socket(), SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) < 0)
56  error("setsockopt","Server could not set socket option SO_REUSEADDR");
57 
58  if (bindToAddress((struct sockaddr*)&addr_,(domain() == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6)))) {
59  startListening(backlog);
60  }
61  mtx.unlock();
62 }
Here is the call graph for this function:

Member Data Documentation

◆ sessions

std::map<int,tcp::Session*> tcp::Server::sessions
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.


The documentation for this class was generated from the following files:
tcp::Server::ctx
SSLContext * ctx()
Get the SSL context for the server.
Definition: tcpserver.h:77
tcp::Server::start
void start(in_port_t port, string bindaddress, bool useSSL=false, int backlog=64)
Start up the server.
Definition: tcpserver.cpp:28
tcp::Socket::epoll
EPoll & epoll()
Returns a reference to the epoll instance used by this socket.
Definition: tcpsocket.h:144
tcp::Server::findifaddr
bool findifaddr(const string ifname, sockaddr *addr)
Returns an interface address from an interface name and domain.
Definition: tcpserver.cpp:116
tcp::error
void error(string msg)
Send an error message to the log stream.
Definition: tcpsocket.cpp:18
tcp::Socket::mtx
recursive_mutex mtx
The mutex used to provide exclusive access to the socket.
Definition: tcpsocket.h:141
tcp::Socket::state_
SocketState state_
Descendant classes can manipulate the socket state directly.
Definition: tcpsocket.h:147
tcp::Server::listening
bool listening()
Determine if the server is listening.
Definition: tcpserver.h:71
tcp::Socket::Socket
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.
Definition: tcpsocket.cpp:97
tcp::Socket::socket
int socket() const
Return the linux socket handle.
Definition: tcpsocket.h:115
tcp::Socket::domain
int domain() const
Return the socket domain (AF_INET or AF_INET6)
Definition: tcpsocket.h:118
tcp::Server::stop
void stop()
Stop the server.
Definition: tcpserver.cpp:64