38 std::array<std::uint8_t, 4>
ip{};
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}; }
48 return {{255, 255, 255, 255}, p};
60[[nodiscard]] std::string
ip_to_string(
const std::array<std::uint8_t, 4> & ip);
147 [[nodiscard]]
static std::expected<UdpSocket, TransportError>
open(
155 UdpSocket(
const UdpSocket &) =
delete;
156 UdpSocket &
operator=(
const UdpSocket &) =
delete;
160 [[nodiscard]]
bool is_open() const noexcept {
return fd_ >= 0; }
169 [[nodiscard]] std::expected<std::size_t, TransportError>
send_to(
170 std::span<const std::byte> data,
const Endpoint & to)
const;
176 [[nodiscard]] std::expected<std::size_t, TransportError>
recv_batch(
177 std::span<Datagram> out)
const;
180 [[nodiscard]] std::expected<Datagram, TransportError>
recv_one(std::span<std::byte> buffer)
const;
209 [[nodiscard]]
static std::expected<Poller, TransportError>
create();
216 Poller(
const Poller &) =
delete;
217 Poller &
operator=(
const Poller &) =
delete;
221 [[nodiscard]] std::expected<void, TransportError>
add(
222 const UdpSocket & socket, std::uint64_t tag);
226 [[nodiscard]] std::size_t
size() const noexcept {
return entries_.size(); }
232 std::chrono::milliseconds timeout);
237 [[nodiscard]]
bool woken() const noexcept {
return woken_; }
245 void close_wake_fds() noexcept;
247 std::vector<Entry> entries_;
248 std::vector<ReadyEvent> ready_;
249 int wake_read_fd_ = -1;
250 int wake_write_fd_ = -1;
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