idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
listener.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Chris Leishman
3
4#pragma once
5
17#include "sdkconfig.h"
18
19#include <idfxx/net/endpoint>
20#include <idfxx/net/error>
22#include <idfxx/net/netconn/stream_channel>
23
24namespace idfxx::net::netconn {
25
43class [[nodiscard]] listener : public detail::base_channel {
44public:
58
60 static constexpr int default_backlog = 5;
61
62 // ----- construction (unbound) -----
63
64#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
74#endif
75
78
79#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
89#endif
90
98
99 // ----- construction (bind + listen) -----
100
101#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
113 explicit listener(const endpoint& bind_addr, int backlog = default_backlog);
114
127 explicit listener(port_number port, int backlog = default_backlog);
128#endif
129
139 [[nodiscard]] static result<listener> make(const endpoint& bind_addr, int backlog = default_backlog);
140
151 [[nodiscard]] static result<listener> make(port_number port, int backlog = default_backlog);
152
153 // ----- listen / accept -----
154
155#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
164 void listen(int backlog = default_backlog) { idfxx::unwrap(try_listen(backlog)); }
165#endif
166
173 [[nodiscard]] result<void> try_listen(int backlog = default_backlog);
174
175#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
184 [[nodiscard]] stream_channel accept() { return idfxx::unwrap(try_accept()); }
185
194 [[nodiscard]] accepted_channel accept_with_peer() { return idfxx::unwrap(try_accept_with_peer()); }
195#endif
196
203
210
211private:
212 listener(::netconn* conn, address_family fam) noexcept
213 : base_channel(conn, fam) {}
214};
215
216} // namespace idfxx::net::netconn
217
// end of idfxx_net_netconn_listener
Address/port pair identifying a transport endpoint.
Definition endpoint.hpp:129
A TCP listening netconn.
Definition listener.hpp:43
stream_channel accept()
Accepts an incoming TCP connection.
Definition listener.hpp:184
listener(port_number port, int backlog=default_backlog)
Creates a TCP listening netconn bound to all local interfaces on port.
static result< listener > make(address_family fam)
Creates an unbound TCP listening netconn for the given address family.
result< stream_channel > try_accept()
Accepts an incoming TCP connection.
accepted_channel accept_with_peer()
Accepts an incoming TCP connection and reports the peer's endpoint.
Definition listener.hpp:194
listener(const endpoint &bind_addr, int backlog=default_backlog)
Creates a TCP listening netconn bound to bind_addr and listening.
listener()
Creates an unbound TCP listening netconn with default address family (IPv4).
static result< listener > make()
Creates an unbound TCP listening netconn (IPv4).
static result< listener > make(port_number port, int backlog=default_backlog)
Creates a TCP listening netconn bound to all local interfaces on port.
result< accepted_channel > try_accept_with_peer()
Accepts an incoming TCP connection and reports the peer's endpoint.
result< void > try_listen(int backlog=default_backlog)
Marks the netconn as listening.
static result< listener > make(const endpoint &bind_addr, int backlog=default_backlog)
Creates a TCP listening netconn bound to bind_addr and listening.
listener(address_family fam)
Creates an unbound TCP listening netconn for the given address family.
void listen(int backlog=default_backlog)
Marks the netconn as listening.
Definition listener.hpp:164
address_family
Address family.
Definition endpoint.hpp:37
uint16_t port_number
Port number type for transport endpoints.
Definition endpoint.hpp:60
T unwrap(result< T > result)
Throws a std::system_error if the result is an error.
Definition error.hpp:307
std::expected< T, std::error_code > result
result type wrapping a value or error code.
Definition error.hpp:120
Result of accept_with_peer / try_accept_with_peer — a newly accepted channel together with the peer's...
Definition listener.hpp:54
stream_channel channel
The connected channel.
Definition listener.hpp:55
endpoint peer
The peer's address and port.
Definition listener.hpp:56