uvw  1.3.0
Public Member Functions | List of all members
uvw::UDPHandle Class Referencefinal

The UDPHandle handle. More...

#include <udp.hpp>

Inheritance diagram for uvw::UDPHandle:
Inheritance graph
[legend]
Collaboration diagram for uvw::UDPHandle:
Collaboration graph
[legend]

Public Member Functions

bool init ()
 Initializes the handle. The actual socket is created lazily. More...
 
void open (OSSocketHandle socket)
 Opens an existing file descriptor or SOCKET as a UDP handle. More...
 
template<typename I = IPv4>
void bind (std::string ip, unsigned int port, Flags< Bind > opts=Flags< Bind >{})
 Binds the UDP handle to an IP address and port. More...
 
template<typename I = IPv4>
void bind (Addr addr, Flags< Bind > opts=Flags< Bind >{})
 Binds the UDP handle to an IP address and port. More...
 
template<typename I = IPv4>
Addr sock () const noexcept
 Get the local IP and port of the UDP handle. More...
 
template<typename I = IPv4>
bool multicastMembership (std::string multicast, std::string iface, Membership membership)
 Sets membership for a multicast address. More...
 
bool multicastLoop (bool enable=true)
 Sets IP multicast loop flag. More...
 
bool multicastTtl (int val)
 Sets the multicast ttl. More...
 
template<typename I = IPv4>
bool multicastInterface (std::string iface)
 Sets the multicast interface to send or receive data on. More...
 
bool broadcast (bool enable=false)
 Sets broadcast on or off. More...
 
bool ttl (int val)
 Sets the time to live. More...
 
template<typename I = IPv4>
void send (std::string ip, unsigned int port, std::unique_ptr< char[]> data, unsigned int len)
 Sends data over the UDP socket. More...
 
template<typename I = IPv4>
void send (Addr addr, std::unique_ptr< char[]> data, unsigned int len)
 Sends data over the UDP socket. More...
 
template<typename I = IPv4>
void send (std::string ip, unsigned int port, char *data, unsigned int len)
 Sends data over the UDP socket. More...
 
template<typename I = IPv4>
void send (Addr addr, char *data, unsigned int len)
 Sends data over the UDP socket. More...
 
template<typename I = IPv4>
int trySend (std::string ip, unsigned int port, std::unique_ptr< char[]> data, unsigned int len)
 Sends data over the UDP socket. More...
 
template<typename I = IPv4>
int trySend (Addr addr, std::unique_ptr< char[]> data, unsigned int len)
 Sends data over the UDP socket. More...
 
template<typename I = IPv4>
int trySend (std::string ip, unsigned int port, char *data, unsigned int len)
 Sends data over the UDP socket. More...
 
template<typename I = IPv4>
int trySend (Addr addr, char *data, unsigned int len)
 Sends data over the UDP socket. More...
 
template<typename I = IPv4>
void recv ()
 Prepares for receiving data. More...
 
void stop ()
 Stops listening for incoming datagrams.
 
- Public Member Functions inherited from uvw::Handle< UDPHandle, uv_udp_t >
HandleCategory category () const noexcept override
 Gets the category of the handle. More...
 
HandleType type () const noexcept override
 Gets the type of the handle. More...
 
bool active () const noexcept override
 Checks if the handle is active. More...
 
bool closing () const noexcept override
 Checks if a handle is closing or closed. More...
 
void close () noexcept override
 Request handle to be closed. More...
 
void reference () noexcept override
 Reference the given handle. More...
 
void unreference () noexcept override
 Unreference the given handle. More...
 
bool referenced () const noexcept override
 Checks if the given handle referenced. More...
 
std::size_t size () const noexcept
 Returns the size of the underlying handle type. More...
 
int sendBufferSize ()
 Gets the size of the send buffer used for the socket. More...
 
bool sendBufferSize (int value)
 Sets the size of the send buffer used for the socket. More...
 
int recvBufferSize ()
 Gets the size of the receive buffer used for the socket. More...
 
bool recvBufferSize (int value)
 Sets the size of the receive buffer used for the socket. More...
 
OSFileDescriptor fileno () const
 Gets the platform dependent file descriptor equivalent. More...
 
- Public Member Functions inherited from uvw::UnderlyingType< UDPHandle, uv_udp_t >
Looploop () const noexcept
 Gets the loop from which the resource was originated. More...
 
- Public Member Functions inherited from uvw::Emitter< UDPHandle >
Connection< E > on (Listener< E > f)
 Registers a long-lived listener with the event emitter. More...
 
Connection< E > once (Listener< E > f)
 Registers a short-lived listener with the event emitter. More...
 
void erase (Connection< E > conn) noexcept
 Disconnects a listener from the event emitter. More...
 
void clear () noexcept
 Disconnects all the listeners for the given event type.
 
void clear () noexcept
 Disconnects all the listeners.
 
bool empty () const noexcept
 Checks if there are listeners registered for the specific event. More...
 
bool empty () const noexcept
 Checks if there are listeners registered with the event emitter. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from uvw::UnderlyingType< UDPHandle, uv_udp_t >
static std::shared_ptr< UDPHandlecreate (Args &&... args)
 Creates a new resource of the given type. More...
 

Detailed Description

The UDPHandle handle.

UDP handles encapsulate UDP communication for both clients and servers.
By default, IPv4 is used as a template parameter. The handle already supports IPv6 out-of-the-box by using uvw::IPv6.

To create an UDPHandle through a Loop, arguments follow:

See the official documentation for further details.

Definition at line 99 of file udp.hpp.

Member Function Documentation

◆ bind() [1/2]

template<typename I = IPv4>
void uvw::UDPHandle::bind ( std::string  ip,
unsigned int  port,
Flags< Bind >  opts = Flags<Bind>{} 
)
inline

Binds the UDP handle to an IP address and port.

Available flags are:

  • UDPHandle::Bind::IPV6ONLY
  • UDPHandle::Bind::REUSEADDR

See the official documentation for further details.

Parameters
ipThe IP address to which to bind.
portThe port to which to bind.
optsOptional additional flags.

Definition at line 177 of file udp.hpp.

◆ bind() [2/2]

template<typename I = IPv4>
void uvw::UDPHandle::bind ( Addr  addr,
Flags< Bind >  opts = Flags<Bind>{} 
)
inline

Binds the UDP handle to an IP address and port.

Available flags are:

  • UDPHandle::Bind::IPV6ONLY
  • UDPHandle::Bind::REUSEADDR

See the official documentation for further details.

Parameters
addrA valid instance of Addr.
optsOptional additional flags.

Definition at line 199 of file udp.hpp.

◆ broadcast()

bool uvw::UDPHandle::broadcast ( bool  enable = false)
inline

Sets broadcast on or off.

Parameters
enableTrue to set broadcast on, false otherwise.
Returns
True in case of success, false otherwise.

Definition at line 266 of file udp.hpp.

◆ init()

bool uvw::UDPHandle::init ( )
inline

Initializes the handle. The actual socket is created lazily.

Returns
True in case of success, false otherwise.

Definition at line 138 of file udp.hpp.

◆ multicastInterface()

template<typename I = IPv4>
bool uvw::UDPHandle::multicastInterface ( std::string  iface)
inline

Sets the multicast interface to send or receive data on.

Parameters
ifaceInterface address.
Returns
True in case of success, false otherwise.

Definition at line 257 of file udp.hpp.

◆ multicastLoop()

bool uvw::UDPHandle::multicastLoop ( bool  enable = true)
inline

Sets IP multicast loop flag.

This makes multicast packets loop back to local sockets.

Parameters
enableTrue to enable multicast loop, false otherwise.
Returns
True in case of success, false otherwise.

Definition at line 238 of file udp.hpp.

◆ multicastMembership()

template<typename I = IPv4>
bool uvw::UDPHandle::multicastMembership ( std::string  multicast,
std::string  iface,
Membership  membership 
)
inline

Sets membership for a multicast address.

Available values for membership are:

  • UDPHandle::Membership::LEAVE_GROUP
  • UDPHandle::Membership::JOIN_GROUP
Parameters
multicastMulticast address to set membership for.
ifaceInterface address.
membershipAction to be performed.
Returns
True in case of success, false otherwise.

Definition at line 226 of file udp.hpp.

◆ multicastTtl()

bool uvw::UDPHandle::multicastTtl ( int  val)
inline

Sets the multicast ttl.

Parameters
valA value in the range [1, 255].
Returns
True in case of success, false otherwise.

Definition at line 247 of file udp.hpp.

◆ open()

void uvw::UDPHandle::open ( OSSocketHandle  socket)
inline

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

The passed file descriptor or SOCKET is not checked for its type, but it’s required that it represents a valid datagram socket.

See the official documentation for further details.

Parameters
socketA valid socket handle (either a file descriptor or a SOCKET).

Definition at line 156 of file udp.hpp.

◆ recv()

template<typename I = IPv4>
void uvw::UDPHandle::recv ( )
inline

Prepares for receiving data.

Note that 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.

An UDPDataEvent event will be emitted when the handle receives data.
An ErrorEvent event will be emitted in case of errors.

Definition at line 496 of file udp.hpp.

◆ send() [1/4]

template<typename I = IPv4>
void uvw::UDPHandle::send ( std::string  ip,
unsigned int  port,
std::unique_ptr< char[]>  data,
unsigned int  len 
)
inline

Sends data over the UDP socket.

Note that 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.

The handle takes the ownership of the data and it is in charge of delete them.

A SendEvent event will be emitted when the data have been sent.
An ErrorEvent event will be emitted in case of errors.

Parameters
ipThe address to which to send data.
portThe port to which to send data.
dataThe data to be sent.
lenThe lenght of the submitted data.

Definition at line 298 of file udp.hpp.

◆ send() [2/4]

template<typename I = IPv4>
void uvw::UDPHandle::send ( Addr  addr,
std::unique_ptr< char[]>  data,
unsigned int  len 
)
inline

Sends data over the UDP socket.

Note that 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.

The handle takes the ownership of the data and it is in charge of delete them.

A SendEvent event will be emitted when the data have been sent.
An ErrorEvent event will be emitted in case of errors.

Parameters
addrA valid instance of Addr.
dataThe data to be sent.
lenThe lenght of the submitted data.

Definition at line 334 of file udp.hpp.

◆ send() [3/4]

template<typename I = IPv4>
void uvw::UDPHandle::send ( std::string  ip,
unsigned int  port,
char *  data,
unsigned int  len 
)
inline

Sends data over the UDP socket.

Note that 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.

The handle doesn't take the ownership of the data. Be sure that their lifetime overcome the one of the request.

A SendEvent event will be emitted when the data have been sent.
An ErrorEvent event will be emitted in case of errors.

Parameters
ipThe address to which to send data.
portThe port to which to send data.
dataThe data to be sent.
lenThe lenght of the submitted data.

Definition at line 357 of file udp.hpp.

◆ send() [4/4]

template<typename I = IPv4>
void uvw::UDPHandle::send ( Addr  addr,
char *  data,
unsigned int  len 
)
inline

Sends data over the UDP socket.

Note that 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.

The handle doesn't take the ownership of the data. Be sure that their lifetime overcome the one of the request.

A SendEvent event will be emitted when the data have been sent.
An ErrorEvent event will be emitted in case of errors.

Parameters
addrA valid instance of Addr.
dataThe data to be sent.
lenThe lenght of the submitted data.

Definition at line 393 of file udp.hpp.

◆ sock()

template<typename I = IPv4>
Addr uvw::UDPHandle::sock ( ) const
inlinenoexcept

Get the local IP and port of the UDP handle.

Returns
A valid instance of Addr, an empty one in case of errors.

Definition at line 208 of file udp.hpp.

◆ trySend() [1/4]

template<typename I = IPv4>
int uvw::UDPHandle::trySend ( std::string  ip,
unsigned int  port,
std::unique_ptr< char[]>  data,
unsigned int  len 
)
inline

Sends data over the UDP socket.

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

Parameters
ipThe address to which to send data.
portThe port to which to send data.
dataThe data to be sent.
lenThe lenght of the submitted data.
Returns
Number of bytes written.

Definition at line 410 of file udp.hpp.

◆ trySend() [2/4]

template<typename I = IPv4>
int uvw::UDPHandle::trySend ( Addr  addr,
std::unique_ptr< char[]>  data,
unsigned int  len 
)
inline

Sends data over the UDP socket.

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

Parameters
addrA valid instance of Addr.
dataThe data to be sent.
lenThe lenght of the submitted data.
Returns
Number of bytes written.

Definition at line 437 of file udp.hpp.

◆ trySend() [3/4]

template<typename I = IPv4>
int uvw::UDPHandle::trySend ( std::string  ip,
unsigned int  port,
char *  data,
unsigned int  len 
)
inline

Sends data over the UDP socket.

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

Parameters
ipThe address to which to send data.
portThe port to which to send data.
dataThe data to be sent.
lenThe lenght of the submitted data.
Returns
Number of bytes written.

Definition at line 454 of file udp.hpp.

◆ trySend() [4/4]

template<typename I = IPv4>
int uvw::UDPHandle::trySend ( Addr  addr,
char *  data,
unsigned int  len 
)
inline

Sends data over the UDP socket.

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

Parameters
addrA valid instance of Addr.
dataThe data to be sent.
lenThe lenght of the submitted data.
Returns
Number of bytes written.

Definition at line 481 of file udp.hpp.

◆ ttl()

bool uvw::UDPHandle::ttl ( int  val)
inline

Sets the time to live.

Parameters
valA value in the range [1, 255].
Returns
True in case of success, false otherwise.

Definition at line 275 of file udp.hpp.


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