WPILibC++  2019.3.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
wpi::uv::Udp Class Referencefinal

UDP handle. More...

#include <Udp.h>

Inheritance diagram for wpi::uv::Udp:
wpi::uv::HandleImpl< Udp, uv_udp_t > wpi::uv::Handle

Public Member Functions

 Udp (const private_init &)
 
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 (const Twine &ip, unsigned int port, unsigned int flags=0)
 Bind the handle to an IPv4 address and port. More...
 
void Bind6 (const Twine &ip, unsigned int port, unsigned int flags=0)
 Bind the handle to an IPv6 address and port. More...
 
sockaddr_storage GetSock ()
 Get the current address to which the handle is bound. More...
 
void SetMembership (const Twine &multicastAddr, const Twine &interfaceAddr, uv_membership membership)
 Set membership for a multicast address. More...
 
void SetMulticastLoop (bool enabled)
 Set IP multicast loop flag. More...
 
void SetMulticastTtl (int ttl)
 Set the multicast TTL. More...
 
void SetMulticastInterface (const Twine &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, ArrayRef< Buffer > bufs, const std::shared_ptr< UdpSendReq > &req)
 Send data over the UDP socket. More...
 
void Send (const sockaddr_in &addr, ArrayRef< Buffer > bufs, const std::shared_ptr< UdpSendReq > &req)
 
void Send (const sockaddr_in6 &addr, ArrayRef< Buffer > bufs, const std::shared_ptr< UdpSendReq > &req)
 
void Send (const sockaddr &addr, ArrayRef< Buffer > bufs, std::function< void(MutableArrayRef< Buffer >, Error)> callback)
 Send data over the UDP socket. More...
 
void Send (const sockaddr_in &addr, ArrayRef< Buffer > bufs, std::function< void(MutableArrayRef< Buffer >, Error)> callback)
 
void Send (const sockaddr_in6 &addr, ArrayRef< Buffer > bufs, std::function< void(MutableArrayRef< Buffer >, Error)> callback)
 
int TrySend (const sockaddr &addr, ArrayRef< 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, ArrayRef< Buffer > bufs)
 
int TrySend (const sockaddr_in6 &addr, ArrayRef< Buffer > bufs)
 
void StartRecv ()
 Prepare for receiving data. More...
 
void StopRecv ()
 Stop listening for incoming datagrams.
 
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< Udpshared_from_this ()
 
std::shared_ptr< const Udpshared_from_this () const
 
uv_udp_tGetRaw () const noexcept
 Get the underlying handle data structure. More...
 
- Public Member Functions inherited from wpi::uv::Handle
 Handle (const Handle &)=delete
 
 Handle (Handle &&)=delete
 
Handleoperator= (const Handle &)=delete
 
Handleoperator= (Handle &&)=delete
 
Type GetType () const noexcept
 Get the type of the handle. More...
 
StringRef GetTypeName () const noexcept
 Get the name of the type of the handle. More...
 
std::shared_ptr< LoopGetLoop () const noexcept
 Get the loop where this handle runs. More...
 
LoopGetLoopRef () 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 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_tGetRawHandle () 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)
 Report an error. More...
 

Static Public Member Functions

static std::shared_ptr< UdpCreate (Loop &loop, unsigned int flags=AF_UNSPEC)
 Create a UDP handle. More...
 
static std::shared_ptr< UdpCreate (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< Errorerror
 Error signal.
 
sig::Signal closed
 Closed signal.
 

Additional Inherited Members

- Public Types inherited from wpi::uv::Handle
using Type = uv_handle_type
 
- 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)
 
- 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)
 

Detailed Description

UDP handle.

UDP handles encapsulate UDP communication for both clients and servers.

Member Function Documentation

void wpi::uv::Udp::Bind ( const sockaddr &  addr,
unsigned int  flags = 0 
)
inline

Bind the handle to an IPv4 or IPv6 address and port.

Parameters
addrInitialized sockaddr_in or sockaddr_in6 data structure.
flagsOptional additional flags.
void wpi::uv::Udp::Bind ( const Twine ip,
unsigned int  port,
unsigned int  flags = 0 
)

Bind the handle to an IPv4 address and port.

Parameters
ipThe address to which to bind.
portThe port to which to bind.
flagsOptional additional flags.
void wpi::uv::Udp::Bind6 ( const Twine ip,
unsigned int  port,
unsigned int  flags = 0 
)

Bind the handle to an IPv6 address and port.

Parameters
ipThe address to which to bind.
portThe port to which to bind.
flagsOptional additional flags.
static std::shared_ptr<Udp> wpi::uv::Udp::Create ( Loop loop,
unsigned int  flags = AF_UNSPEC 
)
static

Create a UDP handle.

Parameters
loopLoop object where this handle runs.
flagsFlags
static std::shared_ptr<Udp> wpi::uv::Udp::Create ( const std::shared_ptr< Loop > &  loop,
unsigned int  flags = AF_UNSPEC 
)
inlinestatic

Create a UDP handle.

Parameters
loopLoop object where this handle runs.
flagsFlags
size_t wpi::uv::Udp::GetSendQueueCount ( ) const
inlinenoexcept

Gets the amount of queued packets waiting to be sent.

Returns
Amount of queued packets waiting to be sent.
size_t wpi::uv::Udp::GetSendQueueSize ( ) const
inlinenoexcept

Gets the amount of queued bytes waiting to be sent.

Returns
Amount of queued bytes waiting to be sent.
sockaddr_storage wpi::uv::Udp::GetSock ( )

Get the current address to which the handle is bound.

Returns
The address (will be zeroed if an error occurred).
void wpi::uv::Udp::Open ( uv_os_sock_t  sock)
inline

Open an existing file descriptor or SOCKET as a UDP handle.

Parameters
sockA valid socket handle (either a file descriptor or a SOCKET).
void wpi::uv::Udp::Send ( const sockaddr &  addr,
ArrayRef< 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.

Parameters
addrsockaddr_in or sockaddr_in6 with the address and port of the remote peer.
bufsThe buffers to be written to the stream.
reqwrite request
void wpi::uv::Udp::Send ( const sockaddr &  addr,
ArrayRef< Buffer bufs,
std::function< void(MutableArrayRef< 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.

Parameters
addrsockaddr_in or sockaddr_in6 with the address and port of the remote peer.
bufsThe buffers to be sent.
callbackCallback function to call when the data has been sent.
void wpi::uv::Udp::SetBroadcast ( bool  enabled)
inline

Set broadcast on or off.

Parameters
enabledTrue for enabled, false for disabled
void wpi::uv::Udp::SetMembership ( const Twine multicastAddr,
const Twine interfaceAddr,
uv_membership  membership 
)

Set membership for a multicast address.

Parameters
multicastAddrMulticast address to set membership for
interfaceAddrInterface address
membershipShould be UV_JOIN_GROUP or UV_LEAVE_GROUP
void wpi::uv::Udp::SetMulticastInterface ( const Twine interfaceAddr)

Set the multicast interface to send or receive data on.

Parameters
interfaceAddrInterface address
void wpi::uv::Udp::SetMulticastLoop ( bool  enabled)
inline

Set IP multicast loop flag.

Makes multicast packets loop back to local sockets.

Parameters
enabledTrue for enabled, false for disabled
void wpi::uv::Udp::SetMulticastTtl ( int  ttl)
inline

Set the multicast TTL.

Parameters
ttlTime to live (1-255)
void wpi::uv::Udp::SetTtl ( int  ttl)
inline

Set the time to live (TTL).

Parameters
ttlTime 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.

int wpi::uv::Udp::TrySend ( const sockaddr &  addr,
ArrayRef< Buffer bufs 
)
inline

Same as Send(), but won't queue a send request if it can't be completed immediately.

Parameters
addrsockaddr_in or sockaddr_in6 with the address and port of the remote peer.
bufsThe buffers to be send.
Returns
Number of bytes sent.

Member Data Documentation

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.


The documentation for this class was generated from the following file: