WPILibC++ 2023.4.3
|
UDP handle. More...
#include <wpinet/uv/Udp.h>
Public Member Functions | |
Udp (const private_init &) | |
~Udp () noexcept override=default | |
void | Open (uv_os_sock_t sock) |
Open an existing file descriptor or SOCKET as a UDP handle. More... | |
void | Bind (const sockaddr &addr, unsigned int flags=0) |
Bind the handle to an IPv4 or IPv6 address and port. More... | |
void | Bind (const sockaddr_in &addr, unsigned int flags=0) |
void | Bind (const sockaddr_in6 &addr, unsigned int flags=0) |
void | Bind (std::string_view ip, unsigned int port, unsigned int flags=0) |
Bind the handle to an IPv4 address and port. More... | |
void | Bind6 (std::string_view ip, unsigned int port, unsigned int flags=0) |
Bind the handle to an IPv6 address and port. More... | |
void | Connect (const sockaddr &addr) |
Associate the handle to a remote address and port, so every message sent by this handle is automatically sent to that destination. More... | |
void | Connect (const sockaddr_in &addr) |
void | Connect (const sockaddr_in6 &addr) |
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 automatically sent to that destination. More... | |
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 automatically sent to that destination. More... | |
sockaddr_storage | GetPeer () |
Get the remote IP and port on connected UDP handles. More... | |
sockaddr_storage | GetSock () |
Get the current address to which the handle is bound. More... | |
void | SetMembership (std::string_view multicastAddr, std::string_view interfaceAddr, uv_membership membership) |
Set membership for a multicast address. More... | |
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. More... | |
void | SetMulticastLoop (bool enabled) |
Set IP multicast loop flag. More... | |
void | SetMulticastTtl (int ttl) |
Set the multicast TTL. More... | |
void | SetMulticastInterface (std::string_view interfaceAddr) |
Set the multicast interface to send or receive data on. More... | |
void | SetBroadcast (bool enabled) |
Set broadcast on or off. More... | |
void | SetTtl (int ttl) |
Set the time to live (TTL). More... | |
void | Send (const sockaddr &addr, std::span< const Buffer > bufs, const std::shared_ptr< UdpSendReq > &req) |
Send data over the UDP socket. More... | |
void | Send (const sockaddr_in &addr, std::span< const Buffer > bufs, const std::shared_ptr< UdpSendReq > &req) |
void | Send (const sockaddr_in6 &addr, std::span< const Buffer > bufs, const std::shared_ptr< UdpSendReq > &req) |
void | Send (std::span< const Buffer > bufs, const std::shared_ptr< UdpSendReq > &req) |
Variant of Send() for connected sockets. More... | |
void | Send (const sockaddr &addr, std::span< const Buffer > bufs, std::function< void(std::span< Buffer >, Error)> callback) |
Send data over the UDP socket. More... | |
void | Send (const sockaddr_in &addr, std::span< const Buffer > bufs, std::function< void(std::span< Buffer >, Error)> callback) |
void | Send (const sockaddr_in6 &addr, std::span< const Buffer > bufs, std::function< void(std::span< Buffer >, Error)> callback) |
void | Send (std::span< const Buffer > bufs, std::function< void(std::span< Buffer >, Error)> callback) |
Variant of Send() for connected sockets. More... | |
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. More... | |
int | TrySend (const sockaddr_in &addr, std::span< const Buffer > bufs) |
int | TrySend (const sockaddr_in6 &addr, std::span< const Buffer > bufs) |
int | TrySend (std::span< const Buffer > bufs) |
Variant of TrySend() for connected sockets. More... | |
void | StartRecv () |
Prepare for receiving data. More... | |
void | StopRecv () |
Stop listening for incoming datagrams. More... | |
bool | IsUsingRecvmmsg () const |
Returns true if the UDP handle was created with the UV_UDP_RECVMMSG flag and the platform supports recvmmsg(2), false otherwise. More... | |
size_t | GetSendQueueSize () const noexcept |
Gets the amount of queued bytes waiting to be sent. More... | |
size_t | GetSendQueueCount () const noexcept |
Gets the amount of queued packets waiting to be sent. More... | |
Public Member Functions inherited from wpi::uv::HandleImpl< Udp, uv_udp_t > | |
std::shared_ptr< Udp > | shared_from_this () |
std::shared_ptr< const Udp > | shared_from_this () const |
uv_udp_t * | GetRaw () const noexcept |
Get the underlying handle data structure. More... | |
Public Member Functions inherited from wpi::uv::Handle | |
Handle (const Handle &)=delete | |
Handle (Handle &&)=delete | |
Handle & | operator= (const Handle &)=delete |
Handle & | operator= (Handle &&)=delete |
virtual | ~Handle () noexcept |
Type | GetType () const noexcept |
Get the type of the handle. More... | |
std::string_view | GetTypeName () const noexcept |
Get the name of the type of the handle. More... | |
std::shared_ptr< Loop > | GetLoop () const noexcept |
Get the loop where this handle runs. More... | |
Loop & | GetLoopRef () const noexcept |
Get the loop where this handle runs. More... | |
bool | IsActive () const noexcept |
Check if the handle is active. More... | |
bool | IsClosing () const noexcept |
Check if a handle is closing or closed. More... | |
void | Close () noexcept |
Request handle to be closed. More... | |
void | SetLoopClosing (bool loopClosing) noexcept |
Set if the loop is closing. More... | |
bool | IsLoopClosing () const noexcept |
Get the loop closing status. More... | |
void | Reference () noexcept |
Reference the given handle. More... | |
void | Unreference () noexcept |
Unreference the given handle. More... | |
bool | HasReference () const noexcept |
Check if the given handle is referenced. More... | |
size_t | RawSize () const noexcept |
Return the size of the underlying handle type. More... | |
uv_handle_t * | GetRawHandle () const noexcept |
Get the underlying handle data structure. More... | |
void | SetBufferAllocator (std::function< Buffer(size_t)> alloc, std::function< void(Buffer &)> dealloc) |
Set the functions used for allocating and releasing buffers. More... | |
void | FreeBuf (Buffer &buf) const noexcept |
Free a buffer. More... | |
template<typename T = void> | |
std::shared_ptr< T > | GetData () const |
Gets user-defined data. More... | |
void | SetData (std::shared_ptr< void > data) |
Sets user-defined data. More... | |
void | ReportError (int err) const |
Report an error. More... | |
Static Public Member Functions | |
static std::shared_ptr< Udp > | Create (Loop &loop, unsigned int flags=AF_UNSPEC) |
Create a UDP handle. More... | |
static std::shared_ptr< Udp > | Create (const std::shared_ptr< Loop > &loop, unsigned int flags=AF_UNSPEC) |
Create a UDP handle. More... | |
Public Attributes | |
sig::Signal< Buffer &, size_t, const sockaddr &, unsigned > | received |
Signal generated for each incoming datagram. More... | |
Public Attributes inherited from wpi::uv::Handle | |
sig::Signal< Error > | error |
Error signal. More... | |
sig::Signal | closed |
Closed signal. More... | |
Additional Inherited Members | |
Public Types inherited from wpi::uv::Handle | |
using | Type = uv_handle_type |
Protected Member Functions inherited from wpi::uv::HandleImpl< Udp, uv_udp_t > | |
HandleImpl () | |
Protected Member Functions inherited from wpi::uv::Handle | |
Handle (uv_handle_t *uv_handle) | |
void | Keep () noexcept |
void | Release () noexcept |
void | ForceClosed () noexcept |
template<typename F , typename... Args> | |
bool | Invoke (F &&f, Args &&... args) const |
Static Protected Member Functions inherited from wpi::uv::Handle | |
static void | AllocBuf (uv_handle_t *handle, size_t size, uv_buf_t *buf) |
static void | DefaultFreeBuf (Buffer &buf) |
UDP handle.
UDP handles encapsulate UDP communication for both clients and servers.
|
inlineexplicit |
|
overridedefaultnoexcept |
|
inline |
Bind the handle to an IPv4 or IPv6 address and port.
addr | Initialized sockaddr_in or sockaddr_in6 data structure. |
flags | Optional additional flags. |
|
inline |
|
inline |
void wpi::uv::Udp::Bind | ( | std::string_view | ip, |
unsigned int | port, | ||
unsigned int | flags = 0 |
||
) |
Bind the handle to an IPv4 address and port.
ip | The address to which to bind. |
port | The port to which to bind. |
flags | Optional additional flags. |
void wpi::uv::Udp::Bind6 | ( | std::string_view | ip, |
unsigned int | port, | ||
unsigned int | flags = 0 |
||
) |
Bind the handle to an IPv6 address and port.
ip | The address to which to bind. |
port | The port to which to bind. |
flags | Optional additional flags. |
|
inline |
Associate the handle to a remote address and port, so every message sent by this handle is automatically sent to that destination.
addr | Initialized sockaddr_in or sockaddr_in6 data structure. |
|
inline |
|
inline |
void wpi::uv::Udp::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 automatically sent to that destination.
ip | The address to which to bind. |
port | The port to which to bind. |
void wpi::uv::Udp::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 automatically sent to that destination.
ip | The address to which to bind. |
port | The port to which to bind. |
flags | Optional additional flags. |
|
inlinestatic |
Create a UDP handle.
loop | Loop object where this handle runs. |
flags | Flags |
|
static |
Create a UDP handle.
loop | Loop object where this handle runs. |
flags | Flags |
sockaddr_storage wpi::uv::Udp::GetPeer | ( | ) |
Get the remote IP and port on connected UDP handles.
|
inlinenoexcept |
Gets the amount of queued packets waiting to be sent.
|
inlinenoexcept |
Gets the amount of queued bytes waiting to be sent.
sockaddr_storage wpi::uv::Udp::GetSock | ( | ) |
Get the current address to which the handle is bound.
|
inline |
Returns true if the UDP handle was created with the UV_UDP_RECVMMSG flag and the platform supports recvmmsg(2), false otherwise.
|
inline |
Open an existing file descriptor or SOCKET as a UDP handle.
sock | A valid socket handle (either a file descriptor or a SOCKET). |
void wpi::uv::Udp::Send | ( | const sockaddr & | addr, |
std::span< const Buffer > | bufs, | ||
const std::shared_ptr< UdpSendReq > & | req | ||
) |
Send data over the UDP socket.
If the socket has not previously been bound with Bind() it will be bound to 0.0.0.0 (the "all interfaces" IPv4 address) and a random port number.
Data are written in order. The lifetime of the data pointers passed in the bufs
parameter must exceed the lifetime of the send request. The callback can be used to free data after the request completes.
HandleSendComplete() will be called on the request object when the data has been written. HandleSendComplete() is called even if an error occurs. HandleError() will be called on the request object in case of errors.
addr | sockaddr_in or sockaddr_in6 with the address and port of the remote peer. |
bufs | The buffers to be written to the stream. |
req | write request |
void wpi::uv::Udp::Send | ( | const sockaddr & | addr, |
std::span< const Buffer > | bufs, | ||
std::function< void(std::span< Buffer >, Error)> | callback | ||
) |
Send data over the UDP socket.
If the socket has not previously been bound with Bind() it will be bound to 0.0.0.0 (the "all interfaces" IPv4 address) and a random port number.
Data are written in order. The lifetime of the data pointers passed in the bufs
parameter must exceed the lifetime of the send request. The callback can be used to free data after the request completes.
The callback will be called when the data has been sent. Errors will be reported via the error signal.
addr | sockaddr_in or sockaddr_in6 with the address and port of the remote peer. |
bufs | The buffers to be sent. |
callback | Callback function to call when the data has been sent. |
|
inline |
|
inline |
|
inline |
|
inline |
void wpi::uv::Udp::Send | ( | std::span< const Buffer > | bufs, |
const std::shared_ptr< UdpSendReq > & | req | ||
) |
Variant of Send() for connected sockets.
Cannot be used with connectionless sockets.
bufs | The buffers to be written to the stream. |
req | write request |
void wpi::uv::Udp::Send | ( | std::span< const Buffer > | bufs, |
std::function< void(std::span< Buffer >, Error)> | callback | ||
) |
Variant of Send() for connected sockets.
Cannot be used with connectionless sockets.
bufs | The buffers to be written to the stream. |
callback | Callback function to call when the data has been sent. |
|
inline |
Set broadcast on or off.
enabled | True for enabled, false for disabled |
void wpi::uv::Udp::SetMembership | ( | std::string_view | multicastAddr, |
std::string_view | interfaceAddr, | ||
uv_membership | membership | ||
) |
Set membership for a multicast address.
multicastAddr | Multicast address to set membership for |
interfaceAddr | Interface address |
membership | Should be UV_JOIN_GROUP or UV_LEAVE_GROUP |
void wpi::uv::Udp::SetMulticastInterface | ( | std::string_view | interfaceAddr | ) |
Set the multicast interface to send or receive data on.
interfaceAddr | Interface address |
|
inline |
Set IP multicast loop flag.
Makes multicast packets loop back to local sockets.
enabled | True for enabled, false for disabled |
|
inline |
Set the multicast TTL.
ttl | Time to live (1-255) |
void wpi::uv::Udp::SetSourceMembership | ( | std::string_view | multicastAddr, |
std::string_view | interfaceAddr, | ||
std::string_view | sourceAddr, | ||
uv_membership | membership | ||
) |
Set membership for a source-specific multicast group.
multicastAddr | Multicast address to set membership for |
interfaceAddr | Interface address |
sourceAddr | Source address |
membership | Should be UV_JOIN_GROUP or UV_LEAVE_GROUP |
|
inline |
Set the time to live (TTL).
ttl | Time to live (1-255) |
void wpi::uv::Udp::StartRecv | ( | ) |
Prepare for receiving data.
If the socket has not previously been bound with Bind() it is bound to 0.0.0.0 (the "all interfaces" IPv4 address) and a random port number.
A received signal will be emitted for each received data packet until StopRecv()
is called.
|
inline |
Stop listening for incoming datagrams.
|
inline |
Same as Send(), but won't queue a send request if it can't be completed immediately.
addr | sockaddr_in or sockaddr_in6 with the address and port of the remote peer. |
bufs | The buffers to be send. |
|
inline |
|
inline |
|
inline |
Variant of TrySend() for connected sockets.
Cannot be used with connectionless sockets.
bufs | The buffers to be written to the stream. |
sig::Signal<Buffer&, size_t, const sockaddr&, unsigned> wpi::uv::Udp::received |
Signal generated for each incoming datagram.
Parameters are the buffer, the number of bytes received, the address of the sender, and flags.