5#ifndef WPINET_UV_UDP_H_
6#define WPINET_UV_UDP_H_
47 struct private_init {};
50 explicit Udp(
const private_init&) {}
51 ~Udp() noexcept override = default;
60 unsigned int flags = AF_UNSPEC);
69 unsigned int flags = AF_UNSPEC) {
86 void Bind(
const sockaddr& addr,
unsigned int flags = 0) {
90 void Bind(
const sockaddr_in& addr,
unsigned int flags = 0) {
91 Bind(
reinterpret_cast<const sockaddr&
>(addr),
flags);
94 void Bind(
const sockaddr_in6& addr,
unsigned int flags = 0) {
95 Bind(
reinterpret_cast<const sockaddr&
>(addr),
flags);
127 Connect(
reinterpret_cast<const sockaddr&
>(addr));
131 Connect(
reinterpret_cast<const sockaddr&
>(addr));
248 void Send(
const sockaddr& addr, std::span<const Buffer> bufs,
249 const std::shared_ptr<UdpSendReq>& req);
251 void Send(
const sockaddr_in& addr, std::span<const Buffer> bufs,
252 const std::shared_ptr<UdpSendReq>& req) {
253 Send(
reinterpret_cast<const sockaddr&
>(addr), bufs, req);
256 void Send(
const sockaddr_in6& addr, std::span<const Buffer> bufs,
257 const std::shared_ptr<UdpSendReq>& req) {
258 Send(
reinterpret_cast<const sockaddr&
>(addr), bufs, req);
268 void Send(std::span<const Buffer> bufs,
269 const std::shared_ptr<UdpSendReq>& req);
288 void Send(
const sockaddr& addr, std::span<const Buffer> bufs,
289 std::function<
void(std::span<Buffer>,
Error)> callback);
291 void Send(
const sockaddr_in& addr, std::span<const Buffer> bufs,
292 std::function<
void(std::span<Buffer>,
Error)> callback) {
293 Send(
reinterpret_cast<const sockaddr&
>(addr), bufs, std::move(callback));
296 void Send(
const sockaddr_in6& addr, std::span<const Buffer> bufs,
297 std::function<
void(std::span<Buffer>,
Error)> callback) {
298 Send(
reinterpret_cast<const sockaddr&
>(addr), bufs, std::move(callback));
308 void Send(std::span<const Buffer> bufs,
309 std::function<
void(std::span<Buffer>,
Error)> callback);
320 int TrySend(
const sockaddr& addr, std::span<const Buffer> bufs) {
322 static_cast<unsigned>(bufs.size()), &addr);
330 int TrySend(
const sockaddr_in& addr, std::span<const Buffer> bufs) {
331 return TrySend(
reinterpret_cast<const sockaddr&
>(addr), bufs);
334 int TrySend(
const sockaddr_in6& addr, std::span<const Buffer> bufs) {
335 return TrySend(
reinterpret_cast<const sockaddr&
>(addr), bufs);
347 static_cast<unsigned>(bufs.size()),
nullptr);
SignalBase is an implementation of the observer pattern, through the use of an emitting object and sl...
Definition: Signal.h:495
Error code.
Definition: Error.h:15
void ReportError(int err) const
Report an error.
Definition: Handle.h:252
bool Invoke(F &&f, Args &&... args) const
Definition: Handle.h:267
Handle.
Definition: Handle.h:290
uv_udp_t * GetRaw() const noexcept
Get the underlying handle data structure.
Definition: Handle.h:305
Event loop.
Definition: Loop.h:37
Request.
Definition: Request.h:135
uv_udp_send_t * GetRaw() noexcept
Get the underlying request data structure.
Definition: Request.h:150
UDP handle.
Definition: Udp.h:46
void SetSourceMembership(std::string_view multicastAddr, std::string_view interfaceAddr, std::string_view sourceAddr, uv_membership membership)
Set membership for a source-specific multicast group.
sockaddr_storage GetPeer()
Get the remote IP and port on connected UDP handles.
void Open(uv_os_sock_t sock)
Open an existing file descriptor or SOCKET as a UDP handle.
Definition: Udp.h:78
int TrySend(const sockaddr_in6 &addr, std::span< const Buffer > bufs)
Definition: Udp.h:334
void Bind(std::string_view ip, unsigned int port, unsigned int flags=0)
Bind the handle to an IPv4 address and port.
sig::Signal< Buffer &, size_t, const sockaddr &, unsigned > received
Signal generated for each incoming datagram.
Definition: Udp.h:395
void Send(const sockaddr_in &addr, std::span< const Buffer > bufs, const std::shared_ptr< UdpSendReq > &req)
Definition: Udp.h:251
void SetBroadcast(bool enabled)
Set broadcast on or off.
Definition: Udp.h:219
void Connect6(std::string_view ip, unsigned int port)
Associate the handle to an IPv6 address and port, so every message sent by this handle is automatical...
void Bind(const sockaddr_in6 &addr, unsigned int flags=0)
Definition: Udp.h:94
sockaddr_storage GetSock()
Get the current address to which the handle is bound.
Udp(const private_init &)
Definition: Udp.h:50
void Bind(const sockaddr &addr, unsigned int flags=0)
Bind the handle to an IPv4 or IPv6 address and port.
Definition: Udp.h:86
size_t GetSendQueueCount() const noexcept
Gets the amount of queued packets waiting to be sent.
Definition: Udp.h:387
bool IsUsingRecvmmsg() const
Returns true if the UDP handle was created with the UV_UDP_RECVMMSG flag and the platform supports re...
Definition: Udp.h:375
void SetMulticastLoop(bool enabled)
Set IP multicast loop flag.
Definition: Udp.h:194
void Bind(const sockaddr_in &addr, unsigned int flags=0)
Definition: Udp.h:90
void Bind6(std::string_view ip, unsigned int port, unsigned int flags=0)
Bind the handle to an IPv6 address and port.
size_t GetSendQueueSize() const noexcept
Gets the amount of queued bytes waiting to be sent.
Definition: Udp.h:381
void Send(std::span< const Buffer > bufs, const std::shared_ptr< UdpSendReq > &req)
Variant of Send() for connected sockets.
void SetMembership(std::string_view multicastAddr, std::string_view interfaceAddr, uv_membership membership)
Set membership for a multicast address.
void SetTtl(int ttl)
Set the time to live (TTL).
Definition: Udp.h:228
void Send(const sockaddr &addr, std::span< const Buffer > bufs, std::function< void(std::span< Buffer >, Error)> callback)
Send data over the UDP socket.
int TrySend(const sockaddr_in &addr, std::span< const Buffer > bufs)
Definition: Udp.h:330
int TrySend(std::span< const Buffer > bufs)
Variant of TrySend() for connected sockets.
Definition: Udp.h:345
void Send(std::span< const Buffer > bufs, std::function< void(std::span< Buffer >, Error)> callback)
Variant of Send() for connected sockets.
void Connect(std::string_view ip, unsigned int port)
Associate the handle to an IPv4 address and port, so every message sent by this handle is automatical...
int TrySend(const sockaddr &addr, std::span< const Buffer > bufs)
Same as Send(), but won't queue a send request if it can't be completed immediately.
Definition: Udp.h:320
void Send(const sockaddr_in &addr, std::span< const Buffer > bufs, std::function< void(std::span< Buffer >, Error)> callback)
Definition: Udp.h:291
void Connect(const sockaddr &addr)
Associate the handle to a remote address and port, so every message sent by this handle is automatica...
Definition: Udp.h:122
void Send(const sockaddr &addr, std::span< const Buffer > bufs, const std::shared_ptr< UdpSendReq > &req)
Send data over the UDP socket.
void Send(const sockaddr_in6 &addr, std::span< const Buffer > bufs, std::function< void(std::span< Buffer >, Error)> callback)
Definition: Udp.h:296
void SetMulticastTtl(int ttl)
Set the multicast TTL.
Definition: Udp.h:203
void StartRecv()
Prepare for receiving data.
void Connect(const sockaddr_in &addr)
Definition: Udp.h:126
~Udp() noexcept override=default
void StopRecv()
Stop listening for incoming datagrams.
Definition: Udp.h:368
void Connect(const sockaddr_in6 &addr)
Definition: Udp.h:130
void Send(const sockaddr_in6 &addr, std::span< const Buffer > bufs, const std::shared_ptr< UdpSendReq > &req)
Definition: Udp.h:256
void SetMulticastInterface(std::string_view interfaceAddr)
Set the multicast interface to send or receive data on.
static std::shared_ptr< Udp > Create(Loop &loop, unsigned int flags=AF_UNSPEC)
Create a UDP handle.
UDP send request.
Definition: Udp.h:29
Udp & GetUdp() const
Definition: Udp.h:33
sig::Signal< Error > complete
Send completed signal.
Definition: Udp.h:39
basic_string_view< char > string_view
Definition: core.h:501
Definition: WebSocket.h:27
size_t send_queue_count
Definition: uv.h:696
UV_HANDLE_FIELDS size_t send_queue_size
Definition: uv.h:692
UV_REQ_FIELDS uv_udp_t * handle
Definition: uv.h:703
flags
Definition: http_parser.h:206
int uv_os_sock_t
Definition: unix.h:118
UV_EXTERN int uv_udp_bind(uv_udp_t *handle, const struct sockaddr *addr, unsigned int flags)
UV_EXTERN int uv_udp_set_ttl(uv_udp_t *handle, int ttl)
UV_EXTERN int uv_udp_recv_stop(uv_udp_t *handle)
uv_membership
Definition: uv.h:412
UV_EXTERN int uv_udp_set_multicast_ttl(uv_udp_t *handle, int ttl)
UV_EXTERN int uv_udp_open(uv_udp_t *handle, uv_os_sock_t sock)
UV_EXTERN int uv_udp_try_send(uv_udp_t *handle, const uv_buf_t bufs[], unsigned int nbufs, const struct sockaddr *addr)
UV_EXTERN int uv_udp_connect(uv_udp_t *handle, const struct sockaddr *addr)
UV_EXTERN int uv_udp_using_recvmmsg(const uv_udp_t *handle)
UV_EXTERN int uv_udp_set_multicast_loop(uv_udp_t *handle, int on)
UV_EXTERN int uv_udp_set_broadcast(uv_udp_t *handle, int on)