trantor
Non-blocking I/O cross-platform TCP network library, using C++14
Loading...
Searching...
No Matches
trantor::TcpServer Class Reference

This class represents a TCP server. More...

#include <trantor/net/TcpServer.h>

Inheritance diagram for trantor::TcpServer:
Collaboration diagram for trantor::TcpServer:

Public Member Functions

 TcpServer (EventLoop *loop, const InetAddress &address, std::string name, bool reUseAddr=true, bool reUsePort=true)
 Construct a new TCP server instance.
void start ()
 Start the server.
void stop ()
 Stop the server.
void setIoLoopNum (size_t num)
 Set the number of event loops in which the I/O of connections to the server is handled. An EventLoopThreadPool is created and managed by TcpServer.
void setIoLoopThreadPool (const std::shared_ptr< EventLoopThreadPool > &pool)
 Set the event loops pool in which the I/O of connections to the server is handled. A shared_ptr of EventLoopThreadPool is copied.
void setIoLoops (const std::vector< trantor::EventLoop * > &ioLoops)
 Set the event loops in which the I/O of connections to the server is handled. The loops are managed by caller. Caller should ensure that ioLoops lives longer than TcpServer.
void setRecvMessageCallback (const RecvMessageCallback &cb)
 Set the message callback.
void setRecvMessageCallback (RecvMessageCallback &&cb)
void setConnectionCallback (const ConnectionCallback &cb)
 Set the connection callback.
void setConnectionCallback (ConnectionCallback &&cb)
void setWriteCompleteCallback (const WriteCompleteCallback &cb)
 Set the write complete callback.
void setWriteCompleteCallback (WriteCompleteCallback &&cb)
void setBeforeListenSockOptCallback (SockOptCallback cb)
 Set the before listen setsockopt callback.
void setAfterAcceptSockOptCallback (SockOptCallback cb)
 Set the after accept setsockopt callback.
const std::string & name () const
 Get the name of the server.
std::string ipPort () const
 Get the IP and port string of the server.
const trantor::InetAddressaddress () const
 Get the address of the server.
EventLoopgetLoop () const
 Get the event loop of the server.
std::vector< EventLoop * > getIoLoops () const
 Get the I/O event loops of the server.
void kickoffIdleConnections (size_t timeout)
 An idle connection is a connection that has no read or write, kick off it after timeout seconds.
void enableSSL (const std::string &certPath, const std::string &keyPath, bool useOldTLS=false, const std::vector< std::pair< std::string, std::string > > &sslConfCmds={}, const std::string &caPath="")
 Enable SSL encryption.
void enableSSL (TLSPolicyPtr policy)
 Enable SSL encryption.
void reloadSSL ()
 Reload the SSL context.

Detailed Description

This class represents a TCP server.

Constructor & Destructor Documentation

◆ TcpServer()

trantor::TcpServer::TcpServer ( EventLoop * loop,
const InetAddress & address,
std::string name,
bool reUseAddr = true,
bool reUsePort = true )

Construct a new TCP server instance.

Parameters
loopThe event loop in which the acceptor of the server is handled.
addressThe address of the server.
nameThe name of the server.
reUseAddrThe SO_REUSEADDR option.
reUsePortThe SO_REUSEPORT option.

Member Function Documentation

◆ address()

const trantor::InetAddress & trantor::TcpServer::address ( ) const

Get the address of the server.

Returns
const trantor::InetAddress&

◆ enableSSL()

void trantor::TcpServer::enableSSL ( const std::string & certPath,
const std::string & keyPath,
bool useOldTLS = false,
const std::vector< std::pair< std::string, std::string > > & sslConfCmds = {},
const std::string & caPath = "" )

Enable SSL encryption.

Parameters
certPathThe path of the certificate file.
keyPathThe path of the private key file.
useOldTLSIf true, the TLS 1.0 and 1.1 are supported by the server.
sslConfCmdsThe commands used to call the SSL_CONF_cmd function in OpenSSL.
caPathThe path of the certificate authority file.
Note
It's well known that TLS 1.0 and 1.1 are not considered secure in
  1. And it's a good practice to only use TLS 1.2 and above.

◆ getIoLoops()

std::vector< EventLoop * > trantor::TcpServer::getIoLoops ( ) const
inline

Get the I/O event loops of the server.

Returns
std::vector<EventLoop *>

◆ getLoop()

EventLoop * trantor::TcpServer::getLoop ( ) const
inline

Get the event loop of the server.

Returns
EventLoop*

◆ ipPort()

std::string trantor::TcpServer::ipPort ( ) const

Get the IP and port string of the server.

Returns
const std::string

◆ kickoffIdleConnections()

void trantor::TcpServer::kickoffIdleConnections ( size_t timeout)
inline

An idle connection is a connection that has no read or write, kick off it after timeout seconds.

Parameters
timeout

◆ name()

const std::string & trantor::TcpServer::name ( ) const
inline

Get the name of the server.

Returns
const std::string&

◆ reloadSSL()

void trantor::TcpServer::reloadSSL ( )

Reload the SSL context.

Note
Call this function when the certificate or private key is updated. The server will reload the SSL context and use the new certificate and private key. new connections will use the new SSL context.

◆ setAfterAcceptSockOptCallback()

void trantor::TcpServer::setAfterAcceptSockOptCallback ( SockOptCallback cb)

Set the after accept setsockopt callback.

Parameters
cbThis callback will be called after accept

◆ setBeforeListenSockOptCallback()

void trantor::TcpServer::setBeforeListenSockOptCallback ( SockOptCallback cb)

Set the before listen setsockopt callback.

Parameters
cbThis callback will be called before the listen

◆ setConnectionCallback()

void trantor::TcpServer::setConnectionCallback ( const ConnectionCallback & cb)
inline

Set the connection callback.

Parameters
cbThe callback is called when a connection is established or closed.

◆ setIoLoopNum()

void trantor::TcpServer::setIoLoopNum ( size_t num)
inline

Set the number of event loops in which the I/O of connections to the server is handled. An EventLoopThreadPool is created and managed by TcpServer.

Parameters
num

◆ setIoLoops()

void trantor::TcpServer::setIoLoops ( const std::vector< trantor::EventLoop * > & ioLoops)
inline

Set the event loops in which the I/O of connections to the server is handled. The loops are managed by caller. Caller should ensure that ioLoops lives longer than TcpServer.

Parameters
ioLoops

◆ setIoLoopThreadPool()

void trantor::TcpServer::setIoLoopThreadPool ( const std::shared_ptr< EventLoopThreadPool > & pool)
inline

Set the event loops pool in which the I/O of connections to the server is handled. A shared_ptr of EventLoopThreadPool is copied.

Parameters
pool

◆ setRecvMessageCallback()

void trantor::TcpServer::setRecvMessageCallback ( const RecvMessageCallback & cb)
inline

Set the message callback.

Parameters
cbThe callback is called when some data is received on a connection to the server.

◆ setWriteCompleteCallback()

void trantor::TcpServer::setWriteCompleteCallback ( const WriteCompleteCallback & cb)
inline

Set the write complete callback.

Parameters
cbThe callback is called when data to send is written to the socket of a connection.

The documentation for this class was generated from the following file: