idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
connectionless_channel.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// Internal base class shared by `datagram_channel` and `raw_channel`, the
9// connectionless netconn-API channel types. Provides the connect / disconnect /
10// send / recv operations that have the same shape on both — per-message
11// addressing, default-peer support, and the buffer/span send and recv overloads.
12// Mirrors the BSD-side `connectionless_socket_base`.
13
14#include "base_channel.hpp"
15#include "sdkconfig.h"
16
17#include <idfxx/net/endpoint>
18#include <idfxx/net/error>
19#include <idfxx/net/netconn/buffer>
20
21#include <cstddef>
22#include <span>
23
25
28class connectionless_channel : public base_channel {
29public:
31 using datagram = ::idfxx::net::datagram;
32
33#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
42 void connect(const endpoint& peer) { idfxx::unwrap(try_connect(peer)); }
43#endif
44
51 [[nodiscard]] result<void> try_connect(const endpoint& peer);
52
53#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
61#endif
62
68 result<void> try_disconnect();
69
70#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
82 size_t send(buffer& buf) { return idfxx::unwrap(try_send(buf)); }
83#endif
84
93 [[nodiscard]] result<size_t> try_send(buffer& buf);
94
95#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
107 size_t send(std::span<const std::byte> data) { return idfxx::unwrap(try_send(data)); }
108#endif
109
118 [[nodiscard]] result<size_t> try_send(std::span<const std::byte> data);
119
120#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
133 size_t send_to(buffer& buf, const endpoint& to) { return idfxx::unwrap(try_send_to(buf, to)); }
134#endif
135
145 [[nodiscard]] result<size_t> try_send_to(buffer& buf, const endpoint& to);
146
147#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
160 size_t send_to(std::span<const std::byte> data, const endpoint& to) { return idfxx::unwrap(try_send_to(data, to)); }
161#endif
162
172 [[nodiscard]] result<size_t> try_send_to(std::span<const std::byte> data, const endpoint& to);
173
174#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
183 [[nodiscard]] buffer recv() { return idfxx::unwrap(try_recv()); }
184#endif
185
191 [[nodiscard]] result<buffer> try_recv();
192
193#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
211 [[nodiscard]] std::span<std::byte> recv(std::span<std::byte> buf) { return idfxx::unwrap(try_recv(buf)); }
212#endif
213
227 [[nodiscard]] result<std::span<std::byte>> try_recv(std::span<std::byte> buf);
228
229#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
240 [[nodiscard]] datagram recv_from(std::span<std::byte> buf) { return idfxx::unwrap(try_recv_from(buf)); }
241#endif
242
249 [[nodiscard]] result<datagram> try_recv_from(std::span<std::byte> buf);
250
251#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
261 [[nodiscard]] endpoint peer_endpoint() const { return idfxx::unwrap(try_peer_endpoint()); }
262#endif
263
270 [[nodiscard]] result<endpoint> try_peer_endpoint() const { return _try_getaddr(false); }
271
272protected:
273 using base_channel::base_channel;
274};
275
276} // namespace idfxx::net::netconn::detail
277
@ send
The send direction.
void connect()
Connects to the configured access point.
result< void > try_disconnect()
Disconnects from the current access point.
void disconnect()
Disconnects from the current access point.
result< void > try_connect()
Connects to the configured access point.
T unwrap(result< T > result)
Throws a std::system_error if the result is an error.
Definition error.hpp:307
A datagram received together with the sender's endpoint.
Definition endpoint.hpp:228