idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
base_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 the netconn channel types
9// (`stream_channel`, `datagram_channel`, `raw_channel`, `listener`). Holds
10// the owning `::netconn*` pointer, the address family, and the family-generic
11// options that belong on every type.
12
13#include "sdkconfig.h"
14
15#include <idfxx/net/endpoint>
16#include <idfxx/net/error>
17
18#include <chrono>
19#include <cstdint>
20#include <optional>
21
22// Forward declaration from lwIP.
23struct netconn;
24
26
32
38
39class base_channel {
40public:
42 ~base_channel() noexcept;
43
44 base_channel(const base_channel&) = delete;
45 base_channel& operator=(const base_channel&) = delete;
46
47 base_channel(base_channel&& other) noexcept;
48 base_channel& operator=(base_channel&& other) noexcept;
49
56 [[nodiscard]] ::netconn* idf_handle() const noexcept { return _conn; }
57
59 [[nodiscard]] address_family family() const noexcept { return _family; }
60
62 [[nodiscard]] bool is_open() const noexcept { return _conn != nullptr; }
63
70 [[nodiscard]] std::optional<endpoint> local_endpoint() const noexcept;
71
72#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
81 void bind(const endpoint& addr) { idfxx::unwrap(try_bind(addr)); }
82#endif
83
85 [[nodiscard]] result<void> try_bind(const endpoint& addr);
86
92 template<typename Rep, typename Period>
93 void set_recv_timeout(const std::chrono::duration<Rep, Period>& t) noexcept {
94 _set_recv_timeout(std::chrono::ceil<std::chrono::milliseconds>(t));
95 }
96
103 void set_non_blocking(bool on) noexcept;
104
106 [[nodiscard]] int get_recv_timeout() const noexcept;
107
108protected:
109 base_channel() noexcept = default;
110 base_channel(::netconn* conn, address_family fam) noexcept
111 : _conn(conn)
112 , _family(fam) {}
113
114 // Queries the netconn for its local (local == true) or peer (local ==
115 // false) address. Shared by `local_endpoint` and the derived channels'
116 // `peer_endpoint`. Returns `errc::invalid_state` on a moved-from channel.
117 [[nodiscard]] result<endpoint> _try_getaddr(bool local) const;
118
119 void _set_recv_timeout(std::chrono::milliseconds t) noexcept;
120
121 ::netconn* _conn = nullptr;
122 address_family _family = address_family::ipv4;
123};
124
125} // namespace idfxx::net::netconn::detail
126
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
std::expected< T, std::error_code > result
result type wrapping a value or error code.
Definition error.hpp:120