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);
105 void Bind(std::string_view ip,
unsigned int port,
unsigned int flags = 0);
114 void Bind6(std::string_view ip,
unsigned int port,
unsigned int flags = 0);
127 Connect(
reinterpret_cast<const sockaddr&
>(addr));
131 Connect(
reinterpret_cast<const sockaddr&
>(addr));
141 void Connect(std::string_view ip,
unsigned int port);
150 void Connect6(std::string_view ip,
unsigned int port);
183 std::string_view interfaceAddr,
184 std::string_view sourceAddr,
247 void Send(
const sockaddr& addr, std::span<const Buffer> bufs,
248 const std::shared_ptr<UdpSendReq>& req);
250 void Send(
const sockaddr_in& addr, std::span<const Buffer> bufs,
251 const std::shared_ptr<UdpSendReq>& req) {
252 Send(
reinterpret_cast<const sockaddr&
>(addr), bufs, req);
255 void Send(
const sockaddr_in6& addr, std::span<const Buffer> bufs,
256 const std::shared_ptr<UdpSendReq>& req) {
257 Send(
reinterpret_cast<const sockaddr&
>(addr), bufs, req);
267 void Send(std::span<const Buffer> bufs,
268 const std::shared_ptr<UdpSendReq>& req);
287 void Send(
const sockaddr& addr, std::span<const Buffer> bufs,
288 std::function<
void(std::span<Buffer>,
Error)> callback);
290 void Send(
const sockaddr_in& addr, std::span<const Buffer> bufs,
291 std::function<
void(std::span<Buffer>,
Error)> callback) {
292 Send(
reinterpret_cast<const sockaddr&
>(addr), bufs, std::move(callback));
295 void Send(
const sockaddr_in6& addr, std::span<const Buffer> bufs,
296 std::function<
void(std::span<Buffer>,
Error)> callback) {
297 Send(
reinterpret_cast<const sockaddr&
>(addr), bufs, std::move(callback));
307 void Send(std::span<const Buffer> bufs,
308 std::function<
void(std::span<Buffer>,
Error)> callback);
319 int TrySend(
const sockaddr& addr, std::span<const Buffer> bufs) {
321 static_cast<unsigned>(bufs.size()), &addr);
329 int TrySend(
const sockaddr_in& addr, std::span<const Buffer> bufs) {
330 return TrySend(
reinterpret_cast<const sockaddr&
>(addr), bufs);
333 int TrySend(
const sockaddr_in6& addr, std::span<const Buffer> bufs) {
334 return TrySend(
reinterpret_cast<const sockaddr&
>(addr), bufs);
346 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
Definition Handle.h:305
Event loop.
Definition Loop.h:37
Request.
Definition Request.h:135
uv_udp_send_t * GetRaw() noexcept
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:333
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:394
void Send(const sockaddr_in &addr, std::span< const Buffer > bufs, const std::shared_ptr< UdpSendReq > &req)
Definition Udp.h:250
void SetBroadcast(bool enabled)
Set broadcast on or off.
Definition Udp.h:218
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:386
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:374
void SetMulticastLoop(bool enabled)
Set IP multicast loop flag.
Definition Udp.h:193
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:380
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:227
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:329
int TrySend(std::span< const Buffer > bufs)
Variant of TrySend() for connected sockets.
Definition Udp.h:344
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:319
void Send(const sockaddr_in &addr, std::span< const Buffer > bufs, std::function< void(std::span< Buffer >, Error)> callback)
Definition Udp.h:290
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:295
void SetMulticastTtl(int ttl)
Set the multicast TTL.
Definition Udp.h:202
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:367
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:255
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
Definition PointerIntPair.h:280
flags
Definition http_parser.h:206
size_t send_queue_count
Definition uv.h:724
UV_HANDLE_FIELDS size_t send_queue_size
Definition uv.h:720
UV_REQ_FIELDS uv_udp_t * handle
Definition uv.h:731
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:414
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)