idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
ip_io_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// Internal base class shared by `stream_socket` and `connectionless_socket_base`
9// (i.e. by every socket type on which peer-oriented I/O makes sense: stream,
10// datagram, raw). Adds `connect`, `peer_endpoint`, `recv`, the send-side
11// timeout/buffer options, and `wait_writable` on top of `ip_socket_base`.
12//
13// `listener` inherits `ip_socket_base` directly and so does not pick up any of
14// these members.
15
16#include "sdkconfig.h"
17
19#include <idfxx/net/endpoint>
20#include <idfxx/net/error>
21
22#include <chrono>
23#include <cstddef>
24#include <span>
25
26namespace idfxx::net::detail {
27
28class ip_io_socket_base : public ip_socket_base {
29public:
30#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
37 [[nodiscard]] endpoint peer_endpoint() const { return idfxx::unwrap(try_peer_endpoint()); }
38#endif
39
41 [[nodiscard]] result<endpoint> try_peer_endpoint() const;
42
43#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
54 void connect(const endpoint& peer) { idfxx::unwrap(try_connect(peer)); }
55#endif
56
63 [[nodiscard]] result<void> try_connect(const endpoint& peer);
64
65#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
77 [[nodiscard]] std::span<std::byte> recv(std::span<std::byte> buf) { return idfxx::unwrap(try_recv(buf)); }
78#endif
79
86 [[nodiscard]] result<std::span<std::byte>> try_recv(std::span<std::byte> buf);
87
95 void set_send_buffer(size_t bytes) noexcept;
96
104 template<typename Rep, typename Period>
105 void set_send_timeout(const std::chrono::duration<Rep, Period>& t) noexcept {
106 _set_send_timeout(std::chrono::ceil<std::chrono::milliseconds>(t));
107 }
108
109#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
118 template<typename Rep, typename Period>
119 void wait_writable(const std::chrono::duration<Rep, Period>& timeout) {
120 idfxx::unwrap(_try_wait_writable(std::chrono::ceil<std::chrono::milliseconds>(timeout)));
121 }
122#endif
123
130 template<typename Rep, typename Period>
131 [[nodiscard]] result<void> try_wait_writable(const std::chrono::duration<Rep, Period>& timeout) {
132 return _try_wait_writable(std::chrono::ceil<std::chrono::milliseconds>(timeout));
133 }
134
139 [[nodiscard]] size_t get_send_buffer() const noexcept;
140
141protected:
142 using ip_socket_base::ip_socket_base;
143
144 void _set_send_timeout(std::chrono::milliseconds t) noexcept;
145 [[nodiscard]] result<void> _try_wait_writable(std::chrono::milliseconds timeout);
146};
147
148} // namespace idfxx::net::detail
149
void connect()
Connects to the configured 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