livox_mid360_core
C++ driver library for the Livox Mid-360 LiDAR
Loading...
Searching...
No Matches
transport.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
12#pragma once
13
14#include <array>
15#include <chrono>
16#include <cstddef>
17#include <cstdint>
18#include <expected>
19#include <optional>
20#include <span>
21#include <string>
22#include <string_view>
23#include <vector>
24
26
27LIVOX_MID360_API_BEGIN
28namespace livox::mid360
29{
30
31// ---------------------------------------------------------------------------
32// Endpoint
33// ---------------------------------------------------------------------------
34
37{
38 std::array<std::uint8_t, 4> ip{};
39 std::uint16_t port = 0;
40
42 [[nodiscard]] static constexpr Endpoint any(std::uint16_t p) { return {{0, 0, 0, 0}, p}; }
44 [[nodiscard]] static constexpr Endpoint loopback(std::uint16_t p) { return {{127, 0, 0, 1}, p}; }
46 [[nodiscard]] static constexpr Endpoint broadcast(std::uint16_t p)
47 {
48 return {{255, 255, 255, 255}, p};
49 }
50
52 friend constexpr bool operator==(const Endpoint &, const Endpoint &) = default;
53};
54
56[[nodiscard]] std::optional<Endpoint> parse_endpoint(std::string_view text);
58[[nodiscard]] std::string to_string(const Endpoint & ep);
60[[nodiscard]] std::string ip_to_string(const std::array<std::uint8_t, 4> & ip);
61
62// ---------------------------------------------------------------------------
63// Errors
64// ---------------------------------------------------------------------------
65
84
86[[nodiscard]] std::string_view to_string(TransportErrorCode code);
87
90{
92 int errno_value = 0;
93
95 friend constexpr bool operator==(const TransportError &, const TransportError &) = default;
96};
97
99[[nodiscard]] std::string to_string(const TransportError & err);
100
101// ---------------------------------------------------------------------------
102// Datagram
103// ---------------------------------------------------------------------------
104
108{
109 std::span<std::byte> data;
114 std::uint64_t recv_time_ns = 0;
115};
116
119inline constexpr std::size_t kMaxDatagramSize = 1500;
120
121// ---------------------------------------------------------------------------
122// UdpSocket
123// ---------------------------------------------------------------------------
124
127{
130 bool reuse_address = true;
131 bool broadcast = false;
134 std::size_t recv_buffer_bytes = 0;
137 std::optional<std::array<std::uint8_t, 4>> multicast_group;
138 std::string bind_to_device;
139};
140
142class UdpSocket
143{
144public:
147 [[nodiscard]] static std::expected<UdpSocket, TransportError> open(
148 Endpoint bind_to, const SocketOptions & options = {});
149
150 UdpSocket() = default;
152 UdpSocket(UdpSocket && other) noexcept;
154 UdpSocket & operator=(UdpSocket && other) noexcept;
155 UdpSocket(const UdpSocket &) = delete;
156 UdpSocket & operator=(const UdpSocket &) = delete;
157 ~UdpSocket();
158
160 [[nodiscard]] bool is_open() const noexcept { return fd_ >= 0; }
162 [[nodiscard]] int native_handle() const noexcept { return fd_; }
164 [[nodiscard]] Endpoint local_endpoint() const noexcept { return local_; }
166 [[nodiscard]] std::expected<std::size_t, TransportError> recv_buffer_bytes() const;
167
169 [[nodiscard]] std::expected<std::size_t, TransportError> send_to(
170 std::span<const std::byte> data, const Endpoint & to) const;
171
176 [[nodiscard]] std::expected<std::size_t, TransportError> recv_batch(
177 std::span<Datagram> out) const;
178
180 [[nodiscard]] std::expected<Datagram, TransportError> recv_one(std::span<std::byte> buffer) const;
181
183 void close() noexcept;
184
185private:
186 int fd_ = -1;
187 Endpoint local_{};
188};
189
190// ---------------------------------------------------------------------------
191// Poller
192// ---------------------------------------------------------------------------
193
196{
197 std::uint64_t tag = 0;
198 bool readable = false;
199 bool error = false;
200};
201
204class Poller
205{
206public:
209 [[nodiscard]] static std::expected<Poller, TransportError> create();
210
211 Poller() = default;
213 Poller(Poller && other) noexcept;
215 Poller & operator=(Poller && other) noexcept;
216 Poller(const Poller &) = delete;
217 Poller & operator=(const Poller &) = delete;
218 ~Poller();
219
221 [[nodiscard]] std::expected<void, TransportError> add(
222 const UdpSocket & socket, std::uint64_t tag);
224 void remove(std::uint64_t tag);
226 [[nodiscard]] std::size_t size() const noexcept { return entries_.size(); }
227
231 [[nodiscard]] std::expected<std::span<const ReadyEvent>, TransportError> wait(
232 std::chrono::milliseconds timeout);
233
235 void wake() const noexcept;
237 [[nodiscard]] bool woken() const noexcept { return woken_; }
238
239private:
240 struct Entry
241 {
242 int fd;
243 std::uint64_t tag;
244 };
245 void close_wake_fds() noexcept;
246
247 std::vector<Entry> entries_;
248 std::vector<ReadyEvent> ready_;
249 int wake_read_fd_ = -1;
250 int wake_write_fd_ = -1;
251 bool woken_ = false;
252};
253
254} // namespace livox::mid360
255LIVOX_MID360_API_END
std::size_t size() const noexcept
Number of registered sockets.
Definition transport.hpp:226
bool woken() const noexcept
True if the last wait() returned because of wake().
Definition transport.hpp:237
std::expected< std::span< const ReadyEvent >, TransportError > wait(std::chrono::milliseconds timeout)
Block until at least one socket is readable, wake() is called, or timeout elapses.
void wake() const noexcept
Interrupt a pending or future wait(). Thread-safe.
Poller & operator=(Poller &&other) noexcept
Closes the current wake descriptor and takes over the state of other.
static std::expected< Poller, TransportError > create()
Create a Poller with its wake descriptor (eventfd on Linux, a pipe elsewhere).
void remove(std::uint64_t tag)
Unregister by tag. Unknown tags are ignored.
Poller(Poller &&other) noexcept
Takes over the registrations and the wake descriptor of other.
std::expected< void, TransportError > add(const UdpSocket &socket, std::uint64_t tag)
Register a socket. tag is returned in ReadyEvent; tags must be unique.
Non-blocking IPv4 UDP socket. Move-only; closes the descriptor on destruction.
Definition transport.hpp:143
UdpSocket & operator=(UdpSocket &&other) noexcept
Closes the current descriptor and takes over the one of other.
std::expected< std::size_t, TransportError > recv_buffer_bytes() const
Effective SO_RCVBUF as reported by the kernel.
std::expected< Datagram, TransportError > recv_one(std::span< std::byte > buffer) const
Convenience: receive a single datagram into buffer.
Endpoint local_endpoint() const noexcept
Address actually bound (port resolved when 0 was requested).
Definition transport.hpp:164
static std::expected< UdpSocket, TransportError > open(Endpoint bind_to, const SocketOptions &options={})
Create a non-blocking socket and bind it to bind_to (port 0 = ephemeral).
std::expected< std::size_t, TransportError > send_to(std::span< const std::byte > data, const Endpoint &to) const
Send one datagram. Returns bytes sent (always data.size() for UDP on success).
bool is_open() const noexcept
True while the socket holds a descriptor.
Definition transport.hpp:160
std::expected< std::size_t, TransportError > recv_batch(std::span< Datagram > out) const
Receive up to out.size() datagrams without blocking.
UdpSocket(UdpSocket &&other) noexcept
Takes over the descriptor; other is left closed.
void close() noexcept
Close the descriptor. Safe to call on a closed socket.
int native_handle() const noexcept
Raw descriptor (for Poller and tests). -1 when closed.
Definition transport.hpp:162
Symbol visibility helpers.
The livox_mid360_core library: everything declared by the public headers.
Definition bytes.hpp:14
TransportErrorCode
Category of a TransportError.
Definition transport.hpp:69
@ kSocketCreate
socket(), eventfd() or pipe() failed
Definition transport.hpp:70
@ kBind
bind() or getsockname() failed
Definition transport.hpp:71
@ kOther
any other failure
Definition transport.hpp:82
@ kInterrupted
EINTR.
Definition transport.hpp:79
@ kWouldBlock
nothing pending on a non-blocking socket (EAGAIN)
Definition transport.hpp:77
@ kSetOption
a socket or descriptor option could not be set
Definition transport.hpp:72
@ kNetworkUnreachable
ENETUNREACH, EHOSTUNREACH, EADDRNOTAVAIL, EACCES.
Definition transport.hpp:74
@ kAddressInUse
EADDRINUSE.
Definition transport.hpp:73
@ kSendFailed
sendto() failed with another errno
Definition transport.hpp:75
@ kMessageTooLong
EMSGSIZE.
Definition transport.hpp:76
@ kClosed
the socket or Poller is closed (EBADF, ENOTSOCK)
Definition transport.hpp:80
std::optional< Endpoint > parse_endpoint(std::string_view text)
Parse "a.b.c.d" (port = 0) or "a.b.c.d:port". Returns nullopt on malformed input.
constexpr std::size_t kMaxDatagramSize
Maximum datagram size worth allocating for: Ethernet MTU.
Definition transport.hpp:119
std::string ip_to_string(const std::array< std::uint8_t, 4 > &ip)
Format only the address part, "a.b.c.d".
std::string_view to_string(Event::Kind kind) noexcept
Name of an event kind, e.g. "state_changed".
@ kTimeout
no matching ACK within all attempts
Definition session.hpp:46
@ kInvalidArgument
rejected before sending; see error_key for the offending key
Definition session.hpp:51
One received UDP datagram.
Definition transport.hpp:108
std::uint64_t recv_time_ns
Host receive time in nanoseconds since the Unix epoch (CLOCK_REALTIME).
Definition transport.hpp:114
std::span< std::byte > data
received bytes
Definition transport.hpp:109
Endpoint from
sender address
Definition transport.hpp:110
IPv4 address + UDP port. Mid-360 is IPv4 only.
Definition transport.hpp:37
friend constexpr bool operator==(const Endpoint &, const Endpoint &)=default
Equal when address and port match.
static constexpr Endpoint any(std::uint16_t p)
0.0.0.0:p (all interfaces).
Definition transport.hpp:42
static constexpr Endpoint loopback(std::uint16_t p)
127.0.0.1:p.
Definition transport.hpp:44
static constexpr Endpoint broadcast(std::uint16_t p)
255.255.255.255:p.
Definition transport.hpp:46
std::uint16_t port
UDP port, 0 = any / ephemeral.
Definition transport.hpp:39
std::array< std::uint8_t, 4 > ip
address bytes in network order (a.b.c.d)
Definition transport.hpp:38
One socket reported by Poller::wait().
Definition transport.hpp:196
std::uint64_t tag
tag given to Poller::add()
Definition transport.hpp:197
bool readable
data is pending (POLLIN)
Definition transport.hpp:198
bool error
POLLERR, POLLHUP or POLLNVAL.
Definition transport.hpp:199
Options of UdpSocket::open().
Definition transport.hpp:127
std::optional< std::array< std::uint8_t, 4 > > multicast_group
Reserved (#2 design): not implemented, must stay at default.
Definition transport.hpp:137
bool broadcast
SO_BROADCAST, needed to send to 255.255.255.255.
Definition transport.hpp:131
std::size_t recv_buffer_bytes
Requested SO_RCVBUF in bytes; 0 leaves the OS default untouched.
Definition transport.hpp:134
std::string bind_to_device
reserved, see multicast_group
Definition transport.hpp:138
bool reuse_address
SO_REUSEADDR, applied only when binding to an explicit port: an ephemeral port (port 0) is always exc...
Definition transport.hpp:130
Error of a transport call: a category plus the errno it came from.
Definition transport.hpp:90
TransportErrorCode code
category
Definition transport.hpp:91
friend constexpr bool operator==(const TransportError &, const TransportError &)=default
Equal when code and errno match.
int errno_value
Original errno, 0 if not applicable.
Definition transport.hpp:92