idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
ip_socket_base.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
7
8// Narrow internal base class shared by every public socket type — `listener`,
9// `stream_socket`, `datagram_socket`, `raw_socket`. Holds the file descriptor,
10// address family, lifecycle (move, close), `bind` / `local_endpoint`,
11// `wait_readable`, and the family-generic socket options that are meaningful
12// even on a listening (accept-only) socket.
13//
14// Members that imply peer-oriented I/O (`connect`, `recv`, `peer_endpoint`,
15// `set_send_buffer`, `set_send_timeout`, `wait_writable`, `get_send_buffer`)
16// live on `ip_io_socket_base`, which sits between this class and the
17// connectable socket types — keeping them off `listener` at compile time.
18//
19// Public inheritance with no virtual functions: there is no polymorphism,
20// derived destructors run first, and the base destructor calls `close()` to
21// guarantee the descriptor is released even if a derived class forgets.
22
23#include "sdkconfig.h"
24
25#include <idfxx/net/endpoint>
26#include <idfxx/net/error>
27
28#include <chrono>
29#include <cstddef>
30#include <cstdint>
31#include <optional>
32#include <string_view>
33
34namespace idfxx::net::detail {
35
36class ip_socket_base {
37public:
39 ~ip_socket_base() noexcept;
40
41 ip_socket_base(const ip_socket_base&) = delete;
42 ip_socket_base& operator=(const ip_socket_base&) = delete;
43
44 ip_socket_base(ip_socket_base&& other) noexcept;
45 ip_socket_base& operator=(ip_socket_base&& other) noexcept;
46
54 [[nodiscard]] int idf_handle() const noexcept { return _fd; }
55
57 [[nodiscard]] address_family family() const noexcept { return _family; }
58
60 [[nodiscard]] bool is_open() const noexcept { return _fd >= 0; }
61
63 void close() noexcept;
64
71 [[nodiscard]] std::optional<endpoint> local_endpoint() const noexcept;
72
73#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
83 void bind(const endpoint& addr) { idfxx::unwrap(try_bind(addr)); }
84#endif
85
94 [[nodiscard]] result<void> try_bind(const endpoint& addr);
95
103 void set_non_blocking(bool on) noexcept;
104
112 void set_reuse_address(bool on) noexcept;
113
121 void set_recv_buffer(size_t bytes) noexcept;
122
129 void set_ttl(uint8_t ttl) noexcept;
130
137 void set_tos(uint8_t tos) noexcept;
138
148 void set_dscp(dscp value) noexcept;
149
161 template<typename Rep, typename Period>
162 void set_recv_timeout(const std::chrono::duration<Rep, Period>& t) noexcept {
163 _set_recv_timeout(std::chrono::ceil<std::chrono::milliseconds>(t));
164 }
165
166#ifdef CONFIG_LWIP_IPV6
177 void set_ipv6_only(bool on) noexcept;
178#endif
179
180#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
191 void set_bind_to_device(std::string_view ifname) { idfxx::unwrap(try_set_bind_to_device(ifname)); }
192#endif
193
203 [[nodiscard]] result<void> try_set_bind_to_device(std::string_view ifname);
204
205#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
214 template<typename Rep, typename Period>
215 void wait_readable(const std::chrono::duration<Rep, Period>& timeout) {
216 idfxx::unwrap(_try_wait_readable(std::chrono::ceil<std::chrono::milliseconds>(timeout)));
217 }
218#endif
219
226 template<typename Rep, typename Period>
227 [[nodiscard]] result<void> try_wait_readable(const std::chrono::duration<Rep, Period>& timeout) {
228 return _try_wait_readable(std::chrono::ceil<std::chrono::milliseconds>(timeout));
229 }
230
235 [[nodiscard]] size_t get_recv_buffer() const noexcept;
236
245 [[nodiscard]] std::error_code get_last_error() const noexcept;
246
257 [[nodiscard]] int idf_last_error() const noexcept;
258
259protected:
260 ip_socket_base() noexcept = default;
261 ip_socket_base(int fd, address_family fam) noexcept
262 : _fd(fd)
263 , _family(fam) {}
264
265 void _set_recv_timeout(std::chrono::milliseconds t) noexcept;
266 [[nodiscard]] result<void> _try_wait_readable(std::chrono::milliseconds timeout);
267
268 int _fd = -1;
269 address_family _family = address_family::ipv4;
270};
271
272} // namespace idfxx::net::detail
273
family
DS18x20 device family identifiers.
Definition ds18x20.hpp:45
address_family
Address family.
Definition endpoint.hpp:37
T unwrap(result< T > result)
Throws a std::system_error if the result is an error.
Definition error.hpp:307