Kea 2.7.0
isc::tcp::TcpListener Class Reference

Implements a class that listens for, accepts, and manages TCP connections. More...

#include <tcp_listener.h>

Classes

struct  IdleTimeout
 Idle connection timeout. More...
 

Public Member Functions

 TcpListener (const asiolink::IOServicePtr &io_service, const asiolink::IOAddress &server_address, const unsigned short server_port, const asiolink::TlsContextPtr &tls_context, const IdleTimeout &idle_timeout, const TcpConnectionFilterCallback &connection_filter=0)
 Constructor.
 
virtual ~TcpListener ()
 Virtual destructor.
 
const asiolink::TCPEndpointgetEndpoint () const
 Returns reference to the current listener endpoint.
 
long getIdleTimeout () const
 Returns the idle timeout (in milliseconds).
 
asiolink::IOAddress getLocalAddress () const
 Returns local address on which server is listening.
 
uint16_t getLocalPort () const
 Returns local port on which server is listening.
 
void start ()
 Starts accepting new connections.
 
void stop ()
 Stops all active connections and shuts down the service.
 
size_t usedByRemoteIp (const asiolink::IOAddress &remote_ip, size_t &total_connections)
 Returns the number of connections using a given remote IP address.
 

Protected Member Functions

void accept ()
 Creates TcpConnection instance and adds it to the pool of active connections.
 
void acceptHandler (const boost::system::error_code &ec)
 Callback invoked when the new connection is accepted.
 
virtual TcpConnectionPtr createConnection (const TcpConnectionAcceptorCallback &acceptor_callback, const TcpConnectionFilterCallback &connection_filter)
 Creates an instance of the TcpConnection.
 

Protected Attributes

TcpConnectionAcceptorPtr acceptor_
 Acceptor instance.
 
TcpConnectionFilterCallback connection_filter_
 Callback invoked during acceptance which may reject connections.
 
TcpConnectionPool connections_
 Pool of active connections.
 
boost::scoped_ptr< asiolink::TCPEndpointendpoint_
 Pointer to the endpoint representing IP address and port on which the service is running.
 
long idle_timeout_
 Timeout after which idle connection is closed by the server.
 
asiolink::IOServicePtr io_service_
 Pointer to the IO service.
 
asiolink::TlsContextPtr tls_context_
 TLS context.
 

Detailed Description

Implements a class that listens for, accepts, and manages TCP connections.

Definition at line 28 of file tcp_listener.h.

Constructor & Destructor Documentation

◆ TcpListener()

isc::tcp::TcpListener::TcpListener ( const asiolink::IOServicePtr & io_service,
const asiolink::IOAddress & server_address,
const unsigned short server_port,
const asiolink::TlsContextPtr & tls_context,
const IdleTimeout & idle_timeout,
const TcpConnectionFilterCallback & connection_filter = 0 )

Constructor.

This constructor creates new server endpoint using the specified IP address and port. It also validates other specified parameters.

This constructor does not start accepting new connections! To start accepting connections run TcpListener::start.

Parameters
io_serviceIO service to be used by the listener.
server_addressAddress on which the TCP service should run.
server_portPort number on which the TCP service should run.
tls_contextTLS context.
idle_timeoutTimeout after which an idle TCP connection is
connection_filterCallback invoked during connection acceptance that can allow or deny connections based on the remote endpoint.
Exceptions
TcpListenerErrorwhen any of the specified parameters is invalid.

Definition at line 17 of file tcp_listener.cc.

References acceptor_, endpoint_, idle_timeout_, and isc_throw.

◆ ~TcpListener()

isc::tcp::TcpListener::~TcpListener ( )
virtual

Virtual destructor.

Definition at line 48 of file tcp_listener.cc.

References stop().

+ Here is the call graph for this function:

Member Function Documentation

◆ accept()

void isc::tcp::TcpListener::accept ( )
protected

Creates TcpConnection instance and adds it to the pool of active connections.

The next accepted connection will be handled by this instance.

Definition at line 81 of file tcp_listener.cc.

References acceptHandler(), connection_filter_, connections_, createConnection(), and isc::tcp::TcpConnectionPool::start().

Referenced by acceptHandler(), and start().

+ Here is the call graph for this function:

◆ acceptHandler()

void isc::tcp::TcpListener::acceptHandler ( const boost::system::error_code & ec)
protected

Callback invoked when the new connection is accepted.

It calls TcpListener::accept to create new TcpConnection instance.

Parameters
ecError code passed to the handler. This is currently ignored.

Definition at line 92 of file tcp_listener.cc.

References accept().

Referenced by accept().

+ Here is the call graph for this function:

◆ createConnection()

TcpConnectionPtr isc::tcp::TcpListener::createConnection ( const TcpConnectionAcceptorCallback & acceptor_callback,
const TcpConnectionFilterCallback & connection_filter )
protectedvirtual

Creates an instance of the TcpConnection.

This method is virtual so as it can be overridden when customized connections are to be used, e.g. in case of unit testing.

Parameters
acceptor_callbackCallback invoked when new connection is accepted.
connection_filterCallback invoked during acceptance which may allow of deny connections based on their remote address.
Returns
Pointer to the created connection.

Definition at line 99 of file tcp_listener.cc.

References isc_throw.

Referenced by accept().

◆ getEndpoint()

const TCPEndpoint & isc::tcp::TcpListener::getEndpoint ( ) const

Returns reference to the current listener endpoint.

Definition at line 53 of file tcp_listener.cc.

References endpoint_.

Referenced by getLocalAddress(), and getLocalPort().

◆ getIdleTimeout()

long isc::tcp::TcpListener::getIdleTimeout ( ) const
inline

Returns the idle timeout (in milliseconds).

Definition at line 93 of file tcp_listener.h.

References idle_timeout_.

◆ getLocalAddress()

IOAddress isc::tcp::TcpListener::getLocalAddress ( ) const

Returns local address on which server is listening.

Definition at line 105 of file tcp_listener.cc.

References getEndpoint().

+ Here is the call graph for this function:

◆ getLocalPort()

uint16_t isc::tcp::TcpListener::getLocalPort ( ) const

Returns local port on which server is listening.

Definition at line 110 of file tcp_listener.cc.

References getEndpoint().

+ Here is the call graph for this function:

◆ start()

void isc::tcp::TcpListener::start ( )

Starts accepting new connections.

This method starts accepting and handling new TCP connections on the IP address and port number specified in the constructor.

If the method is invoked successfully, it must not be invoked again until TcpListener::stop is called.

Exceptions
TcpListenerErrorif an error occurred.

Definition at line 58 of file tcp_listener.cc.

References accept(), acceptor_, endpoint_, isc_throw, stop(), and isc::Exception::what().

+ Here is the call graph for this function:

◆ stop()

void isc::tcp::TcpListener::stop ( )

Stops all active connections and shuts down the service.

Definition at line 75 of file tcp_listener.cc.

References acceptor_, connections_, and isc::tcp::TcpConnectionPool::stopAll().

Referenced by ~TcpListener(), and start().

+ Here is the call graph for this function:

◆ usedByRemoteIp()

size_t isc::tcp::TcpListener::usedByRemoteIp ( const asiolink::IOAddress & remote_ip,
size_t & total_connections )
inline

Returns the number of connections using a given remote IP address.

Used to limit the number of connections when accepting a new one.

Parameters
remote_ipThe remote IP address.
[out]total_connectionsSize of the connection pool.
Returns
The number of connections using a given remote IP address.

Definition at line 104 of file tcp_listener.h.

References connections_, and isc::tcp::TcpConnectionPool::usedByRemoteIp().

+ Here is the call graph for this function:

Member Data Documentation

◆ acceptor_

TcpConnectionAcceptorPtr isc::tcp::TcpListener::acceptor_
protected

Acceptor instance.

Definition at line 145 of file tcp_listener.h.

Referenced by TcpListener(), start(), and stop().

◆ connection_filter_

TcpConnectionFilterCallback isc::tcp::TcpListener::connection_filter_
protected

Callback invoked during acceptance which may reject connections.

Definition at line 160 of file tcp_listener.h.

Referenced by accept().

◆ connections_

TcpConnectionPool isc::tcp::TcpListener::connections_
protected

Pool of active connections.

Definition at line 152 of file tcp_listener.h.

Referenced by accept(), stop(), and usedByRemoteIp().

◆ endpoint_

boost::scoped_ptr<asiolink::TCPEndpoint> isc::tcp::TcpListener::endpoint_
protected

Pointer to the endpoint representing IP address and port on which the service is running.

Definition at line 149 of file tcp_listener.h.

Referenced by TcpListener(), getEndpoint(), and start().

◆ idle_timeout_

long isc::tcp::TcpListener::idle_timeout_
protected

Timeout after which idle connection is closed by the server.

Definition at line 156 of file tcp_listener.h.

Referenced by TcpListener(), and getIdleTimeout().

◆ io_service_

asiolink::IOServicePtr isc::tcp::TcpListener::io_service_
protected

Pointer to the IO service.

Definition at line 139 of file tcp_listener.h.

◆ tls_context_

asiolink::TlsContextPtr isc::tcp::TcpListener::tls_context_
protected

TLS context.

Definition at line 142 of file tcp_listener.h.


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