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
20#include <idfxx/net/endpoint>
21#include <idfxx/net/error>
22#include <idfxx/net/stream_socket>
23
24#include <chrono>
25#include <optional>
26#include <string_view>
27
28namespace idfxx::net {
29
50class [[nodiscard]] listener : public detail::ip_socket_base {
51public:
65
67 static constexpr int default_backlog = 5;
68
75 struct config {
76 address_family family = address_family::ipv4;
77 int backlog = default_backlog;
81 bool reuse_address = true;
82#ifdef CONFIG_LWIP_IPV6
88 bool ipv6_only = false;
89#endif
92 bool non_blocking = false;
95 std::optional<std::string_view> bind_to_device = std::nullopt;
96 };
97
98 // ----- construction -----
99
100#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
109 explicit listener(const endpoint& bind_addr);
110
122 explicit listener(const endpoint& bind_addr, const config& cfg);
123
138 explicit listener(port_number port);
139
152 explicit listener(port_number port, const config& cfg);
153#endif
154
162
171
179
188
189 // ----- accept -----
190
191#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
202 [[nodiscard]] stream_socket accept() { return idfxx::unwrap(try_accept()); }
203
212 [[nodiscard]] accepted_connection accept_with_peer() { return idfxx::unwrap(try_accept_with_peer()); }
213
224 template<typename Rep, typename Period>
225 [[nodiscard]] stream_socket accept_for(const std::chrono::duration<Rep, Period>& timeout) {
226 return idfxx::unwrap(_try_accept_for(std::chrono::ceil<std::chrono::milliseconds>(timeout)));
227 }
228#endif
229
236
243
250 template<typename Rep, typename Period>
251 [[nodiscard]] result<stream_socket> try_accept_for(const std::chrono::duration<Rep, Period>& timeout) {
252 return _try_accept_for(std::chrono::ceil<std::chrono::milliseconds>(timeout));
253 }
254
255private:
256 explicit listener(int fd, address_family fam) noexcept
257 : ip_socket_base(fd, fam) {}
258
259 [[nodiscard]] result<stream_socket> _try_accept_for(std::chrono::milliseconds timeout);
260};
261
262} // namespace idfxx::net
263
// end of idfxx_net_listener
Address/port pair identifying a transport endpoint.
Definition endpoint.hpp:129
A TCP listening socket.
Definition listener.hpp:50
static result< listener > make(const endpoint &bind_addr)
Creates and listens on a TCP listener at the given address.
stream_socket accept_for(const std::chrono::duration< Rep, Period > &timeout)
Accepts a single connection within the given timeout.
Definition listener.hpp:225
static result< listener > make(port_number port, const config &cfg)
Creates a TCP listener bound to all local interfaces on the given port.
static result< listener > make(port_number port)
Creates a TCP listener bound to all local interfaces on the given port.
stream_socket accept()
Accepts a single incoming connection.
Definition listener.hpp:202
result< stream_socket > try_accept_for(const std::chrono::duration< Rep, Period > &timeout)
Accepts a single connection within the given timeout.
Definition listener.hpp:251
listener(port_number port)
Creates a TCP listener bound to all local interfaces on the given port.
listener(const endpoint &bind_addr)
Creates and listens on a TCP listener at the given address.
accepted_connection accept_with_peer()
Accepts a single incoming connection and returns the peer's endpoint.
Definition listener.hpp:212
static result< listener > make(const endpoint &bind_addr, const config &cfg)
Creates and listens on a TCP listener with the given configuration.
result< accepted_connection > try_accept_with_peer()
Accepts a single incoming connection and returns the peer's endpoint.
result< stream_socket > try_accept()
Accepts a single incoming connection.
listener(const endpoint &bind_addr, const config &cfg)
Creates and listens on a TCP listener with the given configuration.
listener(port_number port, const config &cfg)
Creates a TCP listener bound to all local interfaces on the given port.
A TCP stream socket.
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
@ timeout
Operation timed out.
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 connection together with the pee...
Definition listener.hpp:61
endpoint peer
The peer's address and port.
Definition listener.hpp:63
stream_socket socket
The connected client socket.
Definition listener.hpp:62
Listener configuration.
Definition listener.hpp:75