idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
datagram_socket.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
23#include <chrono>
24#include <cstddef>
25#include <cstdint>
26#include <optional>
27#include <span>
28#include <string_view>
29
30namespace idfxx::net {
31
60class [[nodiscard]] datagram_socket : public detail::connectionless_socket_base {
61public:
67 struct config {
68 address_family family = address_family::ipv4;
71 bool non_blocking = false;
74 std::optional<std::chrono::milliseconds> recv_timeout = std::nullopt;
77 std::optional<std::chrono::milliseconds> send_timeout = std::nullopt;
80 bool reuse_address = false;
83 bool broadcast = false;
84#ifdef CONFIG_LWIP_IPV6
87 bool ipv6_only = false;
88#endif
91 std::optional<std::string_view> bind_to_device = std::nullopt;
92 };
93
94 // ----- construction -----
95
96#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
104#endif
105
112
113#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
123#endif
124
132
133#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
142 explicit datagram_socket(const config& cfg);
143#endif
144
152
153 // ----- send -----
154
155#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
167 size_t send(std::span<const std::byte> buf) { return idfxx::unwrap(try_send(buf)); }
168#endif
169
177 [[nodiscard]] result<size_t> try_send(std::span<const std::byte> buf);
178
179 // ----- options -----
180
187 void set_broadcast(bool on) noexcept;
188
189 // ----- multicast (IPv4) -----
190
191#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
204 idfxx::unwrap(try_join_multicast_v4(group, interface));
205 }
206#endif
207
219
220#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
232 idfxx::unwrap(try_leave_multicast_v4(group, interface));
233 }
234#endif
235
246
255 void set_multicast_loopback(bool on) noexcept;
256
265
266#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
281 void set_multicast_interface_v4(ipv4_addr addr) { idfxx::unwrap(try_set_multicast_interface_v4(addr)); }
282#endif
283
298
299#ifdef CONFIG_LWIP_IPV6
300 // ----- multicast (IPv6) -----
301
302#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
317 }
318#endif
319
331
332#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
346 }
347#endif
348
359
360#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
377#endif
378
392 [[nodiscard]] result<void> try_set_multicast_interface_v6(uint8_t if_index);
393#endif
394
395private:
396 datagram_socket(int fd, address_family fam) noexcept
397 : connectionless_socket_base(fd, fam) {}
398};
399
400} // namespace idfxx::net
401
// end of idfxx_net_datagram_socket
A UDP datagram socket.
void set_multicast_hops(uint8_t hops) noexcept
Sets the hop limit (IPv6) / TTL (IPv4) for outgoing multicast packets.
datagram_socket()
Creates a new UDP socket with default configuration (IPv4, blocking).
result< void > try_leave_multicast_v4(ipv4_addr group, ipv4_addr interface={})
Leaves an IPv4 multicast group.
result< void > try_join_multicast_v4(ipv4_addr group, ipv4_addr interface={})
Joins an IPv4 multicast group so that the socket receives datagrams sent to that group.
void set_multicast_interface_v4(ipv4_addr addr)
Selects the local interface used for outgoing IPv4 multicast.
static result< datagram_socket > make()
Creates a new UDP socket with default configuration (IPv4, blocking).
void leave_multicast_v4(ipv4_addr group, ipv4_addr interface={})
Leaves an IPv4 multicast group.
static result< datagram_socket > make(address_family fam)
Creates a new UDP socket of the given address family.
static result< datagram_socket > make(const config &cfg)
Creates a new UDP socket with the given configuration.
datagram_socket(address_family fam)
Creates a new UDP socket of the given address family.
void join_multicast_v4(ipv4_addr group, ipv4_addr interface={})
Joins an IPv4 multicast group so that the socket receives datagrams sent to that group.
void set_broadcast(bool on) noexcept
Toggles permission to send datagrams to broadcast addresses.
datagram_socket(const config &cfg)
Creates a new UDP socket with the given configuration.
void set_multicast_loopback(bool on) noexcept
Toggles whether outgoing multicast packets are also delivered to local sockets that have joined the g...
result< void > try_set_multicast_interface_v4(ipv4_addr addr)
Selects the local interface used for outgoing IPv4 multicast.
result< size_t > try_send(std::span< const std::byte > buf)
Sends a datagram on a connected socket (after try_connect).
size_t send(std::span< const std::byte > buf)
Sends a datagram on a connected socket (after connect).
IPv4 address value type.
Definition net.hpp:41
IPv6 address value type.
Definition net.hpp:132
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
std::expected< T, std::error_code > result
result type wrapping a value or error code.
Definition error.hpp:120
Datagram socket configuration.