idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
idfxx::net::listener Class Reference

A TCP listening socket. More...

Inheritance diagram for idfxx::net::listener:

Classes

struct  accepted_connection
 Result of accept_with_peer / try_accept_with_peer — a newly accepted connection together with the peer's endpoint. More...
 
struct  config
 Listener configuration. More...
 

Public Member Functions

 listener (const endpoint &bind_addr)
 Creates and listens on a TCP listener at the given address.
 
 listener (const endpoint &bind_addr, const config &cfg)
 Creates and listens on a TCP listener with the given configuration.
 
 listener (port_number port)
 Creates a TCP listener bound to all local interfaces on the given port.
 
 listener (port_number port, const config &cfg)
 Creates a TCP listener bound to all local interfaces on the given port.
 
stream_socket accept ()
 Accepts a single incoming connection.
 
accepted_connection accept_with_peer ()
 Accepts a single incoming connection and returns the peer's endpoint.
 
template<typename Rep , typename Period >
stream_socket accept_for (const std::chrono::duration< Rep, Period > &timeout)
 Accepts a single connection within the given timeout.
 
result< stream_sockettry_accept ()
 Accepts a single incoming connection.
 
result< accepted_connectiontry_accept_with_peer ()
 Accepts a single incoming connection and returns the peer's endpoint.
 
template<typename Rep , typename Period >
result< stream_sockettry_accept_for (const std::chrono::duration< Rep, Period > &timeout)
 Accepts a single connection within the given timeout.
 

Static Public Member Functions

static result< listenermake (const endpoint &bind_addr)
 Creates and listens on a TCP listener at the given address.
 
static result< listenermake (const endpoint &bind_addr, const config &cfg)
 Creates and listens on a TCP listener with the given configuration.
 
static result< listenermake (port_number port)
 Creates a TCP listener bound to all local interfaces on the given port.
 
static result< listenermake (port_number port, const config &cfg)
 Creates a TCP listener bound to all local interfaces on the given port.
 

Static Public Attributes

static constexpr int default_backlog = 5
 Default depth of the pending-connection queue.
 

Detailed Description

A TCP listening socket.

A listener binds to a local address, listens, and produces connected stream_socket instances on accept. accept() may be called repeatedly.

Ownership is exclusive: the listener is non-copyable and move-only. A moved-from listener may only be destroyed, move-assigned, or queried via is_open() (→ false) and idf_handle() (→ -1); calling any other method on it is undefined behavior. The destructor closes the listener if open.

while (true) {
auto [client, peer] = l.accept_with_peer();
handle(std::move(client));
}
A TCP listening socket.
Definition listener.hpp:50
std::expected< T, std::error_code > result
result type wrapping a value or error code.
Definition error.hpp:120

Definition at line 50 of file listener.hpp.

Constructor & Destructor Documentation

◆ listener() [1/4]

idfxx::net::listener::listener ( const endpoint bind_addr)
explicit

Creates and listens on a TCP listener at the given address.

Parameters
bind_addrLocal endpoint to bind and listen on.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.

◆ listener() [2/4]

idfxx::net::listener::listener ( const endpoint bind_addr,
const config cfg 
)
explicit

Creates and listens on a TCP listener with the given configuration.

Address family in cfg is overridden by the family of bind_addr.

Parameters
bind_addrLocal endpoint to bind and listen on.
cfgListener configuration.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.

◆ listener() [3/4]

idfxx::net::listener::listener ( port_number  port)
explicit

Creates a TCP listener bound to all local interfaces on the given port.

Picks the most permissive address family available at compile time: when CONFIG_LWIP_IPV6 is enabled, binds to ipv6_addr::any() and (with the default ipv6_only = false) accepts both IPv6 and IPv4-mapped clients; otherwise binds to ipv4_addr::any(). To force a specific family, use the (port, config) overload with cfg.family.

Parameters
portLocal port to listen on.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.

◆ listener() [4/4]

idfxx::net::listener::listener ( port_number  port,
const config cfg 
)
explicit

Creates a TCP listener bound to all local interfaces on the given port.

The address family in cfg selects between ipv4_addr::any() (default) and ipv6_addr::any().

Parameters
portLocal port to listen on.
cfgListener configuration.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.

Member Function Documentation

◆ accept()

stream_socket idfxx::net::listener::accept ( )
inline

Accepts a single incoming connection.

Blocks until a client connects.

Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.
Returns
The connected socket.

Definition at line 202 of file listener.hpp.

References idfxx::unwrap().

◆ accept_for()

template<typename Rep , typename Period >
stream_socket idfxx::net::listener::accept_for ( const std::chrono::duration< Rep, Period > &  timeout)
inline

Accepts a single connection within the given timeout.

Parameters
timeoutMaximum time to wait. Sub-millisecond resolutions are rounded up.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_errorif the timeout fires or on other failure.
Returns
The connected socket.

Definition at line 225 of file listener.hpp.

References idfxx::timeout, and idfxx::unwrap().

◆ accept_with_peer()

accepted_connection idfxx::net::listener::accept_with_peer ( )
inline

Accepts a single incoming connection and returns the peer's endpoint.

Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.
Returns
The accepted connection and the peer's endpoint.

Definition at line 212 of file listener.hpp.

References idfxx::unwrap().

◆ make() [1/4]

static result< listener > idfxx::net::listener::make ( const endpoint bind_addr)
static

Creates and listens on a TCP listener at the given address.

Parameters
bind_addrLocal endpoint to bind and listen on.
Returns
The new listener, or an error.

◆ make() [2/4]

static result< listener > idfxx::net::listener::make ( const endpoint bind_addr,
const config cfg 
)
static

Creates and listens on a TCP listener with the given configuration.

Parameters
bind_addrLocal endpoint to bind and listen on.
cfgListener configuration.
Returns
The new listener, or an error.

◆ make() [3/4]

static result< listener > idfxx::net::listener::make ( port_number  port)
static

Creates a TCP listener bound to all local interfaces on the given port.

Parameters
portLocal port to listen on.
Returns
The new listener, or an error.

◆ make() [4/4]

static result< listener > idfxx::net::listener::make ( port_number  port,
const config cfg 
)
static

Creates a TCP listener bound to all local interfaces on the given port.

Parameters
portLocal port to listen on.
cfgListener configuration.
Returns
The new listener, or an error.

◆ try_accept()

result< stream_socket > idfxx::net::listener::try_accept ( )

Accepts a single incoming connection.

Returns
The connected socket, or an error.

◆ try_accept_for()

template<typename Rep , typename Period >
result< stream_socket > idfxx::net::listener::try_accept_for ( const std::chrono::duration< Rep, Period > &  timeout)
inline

Accepts a single connection within the given timeout.

Parameters
timeoutMaximum time to wait. Sub-millisecond resolutions are rounded up.
Returns
The connected socket, or an error such as errc::timed_out.

Definition at line 251 of file listener.hpp.

References idfxx::timeout.

◆ try_accept_with_peer()

result< accepted_connection > idfxx::net::listener::try_accept_with_peer ( )

Accepts a single incoming connection and returns the peer's endpoint.

Returns
The accepted connection and the peer's endpoint, or an error.

Member Data Documentation

◆ default_backlog

constexpr int idfxx::net::listener::default_backlog = 5
staticconstexpr

Default depth of the pending-connection queue.

Definition at line 67 of file listener.hpp.


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