uvw 2.12.1
|
#include <udp.h>
Public Member Functions | |
bool | init () |
Initializes the handle. The actual socket is created lazily. | |
void | open (OSSocketHandle socket) |
Opens an existing file descriptor or SOCKET as a UDP handle. | |
void | bind (const sockaddr &addr, Flags< Bind > opts=Flags< Bind >{}) |
Binds the UDP handle to an IP address and port. | |
void | connect (const sockaddr &addr) |
Associates the handle to a remote address and port (either IPv4 or IPv6). | |
template<typename I = IPv4> | |
void | connect (const std::string &ip, unsigned int port) |
Associates the handle to a remote address and port (either IPv4 or IPv6). | |
template<typename I = IPv4> | |
void | connect (Addr addr) |
Associates the handle to a remote address and port (either IPv4 or IPv6). | |
void | disconnect () |
Disconnects the handle. | |
template<typename I = IPv4> | |
Addr | peer () const noexcept |
Gets the remote address to which the handle is connected, if any. | |
template<typename I = IPv4> | |
void | bind (const std::string &ip, unsigned int port, Flags< Bind > opts=Flags< Bind >{}) |
Binds the UDP handle to an IP address and port. | |
template<typename I = IPv4> | |
void | bind (Addr addr, Flags< Bind > opts=Flags< Bind >{}) |
Binds the UDP handle to an IP address and port. | |
template<typename I = IPv4> | |
Addr | sock () const noexcept |
Get the local IP and port of the UDP handle. | |
template<typename I = IPv4> | |
bool | multicastMembership (const std::string &multicast, const std::string &iface, Membership membership) |
Sets membership for a multicast address. | |
bool | multicastLoop (bool enable=true) |
Sets IP multicast loop flag. | |
bool | multicastTtl (int val) |
Sets the multicast ttl. | |
template<typename I = IPv4> | |
bool | multicastInterface (const std::string &iface) |
Sets the multicast interface to send or receive data on. | |
bool | broadcast (bool enable=false) |
Sets broadcast on or off. | |
bool | ttl (int val) |
Sets the time to live. | |
void | send (const sockaddr &addr, std::unique_ptr< char[]> data, unsigned int len) |
Sends data over the UDP socket. | |
template<typename I = IPv4> | |
void | send (const std::string &ip, unsigned int port, std::unique_ptr< char[]> data, unsigned int len) |
Sends data over the UDP socket. | |
template<typename I = IPv4> | |
void | send (Addr addr, std::unique_ptr< char[]> data, unsigned int len) |
Sends data over the UDP socket. | |
void | send (const sockaddr &addr, char *data, unsigned int len) |
Sends data over the UDP socket. | |
template<typename I = IPv4> | |
void | send (const std::string &ip, unsigned int port, char *data, unsigned int len) |
Sends data over the UDP socket. | |
template<typename I = IPv4> | |
void | send (Addr addr, char *data, unsigned int len) |
Sends data over the UDP socket. | |
template<typename I = IPv4> | |
int | trySend (const sockaddr &addr, std::unique_ptr< char[]> data, unsigned int len) |
Sends data over the UDP socket. | |
template<typename I = IPv4> | |
int | trySend (const std::string &ip, unsigned int port, std::unique_ptr< char[]> data, unsigned int len) |
Sends data over the UDP socket. | |
template<typename I = IPv4> | |
int | trySend (Addr addr, std::unique_ptr< char[]> data, unsigned int len) |
Sends data over the UDP socket. | |
template<typename I = IPv4> | |
int | trySend (const sockaddr &addr, char *data, unsigned int len) |
Sends data over the UDP socket. | |
template<typename I = IPv4> | |
int | trySend (const std::string &ip, unsigned int port, char *data, unsigned int len) |
Sends data over the UDP socket. | |
template<typename I = IPv4> | |
int | trySend (Addr addr, char *data, unsigned int len) |
Sends data over the UDP socket. | |
template<typename I = IPv4> | |
void | recv () |
Prepares for receiving data. | |
void | stop () |
Stops listening for incoming datagrams. | |
size_t | sendQueueSize () const noexcept |
Gets the number of bytes queued for sending. | |
size_t | sendQueueCount () const noexcept |
Number of send requests currently in the queue awaiting to be processed. | |
![]() | |
HandleCategory | category () const noexcept |
Gets the category of the handle. | |
HandleType | type () const noexcept |
Gets the type of the handle. | |
bool | active () const noexcept |
Checks if the handle is active. | |
bool | closing () const noexcept |
Checks if a handle is closing or closed. | |
void | close () noexcept |
Request handle to be closed. | |
void | reference () noexcept |
Reference the given handle. | |
void | unreference () noexcept |
Unreference the given handle. | |
bool | referenced () const noexcept |
Checks if the given handle referenced. | |
std::size_t | size () const noexcept |
Returns the size of the underlying handle type. | |
int | sendBufferSize () |
Gets the size of the send buffer used for the socket. | |
bool | sendBufferSize (int value) |
Sets the size of the send buffer used for the socket. | |
int | recvBufferSize () |
Gets the size of the receive buffer used for the socket. | |
bool | recvBufferSize (int value) |
Sets the size of the receive buffer used for the socket. | |
OSFileDescriptor | fd () const |
Gets the platform dependent file descriptor equivalent. | |
![]() | |
std::shared_ptr< R > | data () const |
Gets user-defined data. uvw won't use this field in any case. | |
void | data (std::shared_ptr< void > uData) |
Sets arbitrary data. uvw won't use this field in any case. | |
![]() | |
Loop & | loop () const noexcept |
Gets the loop from which the resource was originated. | |
const uv_udp_t * | raw () const noexcept |
Gets the underlying raw data structure. | |
uv_udp_t * | raw () noexcept |
Gets the underlying raw data structure. | |
![]() | |
Connection< E > | on (Listener< E > f) |
Registers a long-lived listener with the event emitter. | |
Connection< E > | once (Listener< E > f) |
Registers a short-lived listener with the event emitter. | |
void | erase (Connection< E > conn) noexcept |
Disconnects a listener from the event emitter. | |
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. | |
bool | empty () const noexcept |
Checks if there are listeners registered with the event emitter. | |
Additional Inherited Members | |
![]() | |
![]() | |
static std::shared_ptr< UDPHandle > | create (Args &&...args) |
Creates a new resource of the given type. | |
![]() | |
![]() | |
![]() | |
![]() | |
![]() | |
![]() |
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.
void uvw::UDPHandle::bind | ( | Addr | addr, |
Flags< Bind > | opts = Flags< Bind >{} ) |
Binds the UDP handle to an IP address and port.
Available flags are:
UDPHandle::Bind::IPV6ONLY
UDPHandle::Bind::UDP_PARTIAL
UDPHandle::Bind::REUSEADDR
UDPHandle::Bind::UDP_MMSG_CHUNK
UDPHandle::Bind::UDP_MMSG_FREE
UDPHandle::Bind::UDP_LINUX_RECVERR
UDPHandle::Bind::UDP_RECVMMSG
See the official documentation for further details.
addr | A valid instance of Addr. |
opts | Optional additional flags. |
Binds the UDP handle to an IP address and port.
Available flags are:
UDPHandle::Bind::IPV6ONLY
UDPHandle::Bind::UDP_PARTIAL
UDPHandle::Bind::REUSEADDR
UDPHandle::Bind::UDP_MMSG_CHUNK
UDPHandle::Bind::UDP_MMSG_FREE
UDPHandle::Bind::UDP_LINUX_RECVERR
UDPHandle::Bind::UDP_RECVMMSG
See the official documentation for further details.
addr | Initialized sockaddr_in or sockaddr_in6 data structure. |
opts | Optional additional flags. |
void uvw::UDPHandle::bind | ( | const std::string & | ip, |
unsigned int | port, | ||
Flags< Bind > | opts = Flags< Bind >{} ) |
Binds the UDP handle to an IP address and port.
Available flags are:
UDPHandle::Bind::IPV6ONLY
UDPHandle::Bind::UDP_PARTIAL
UDPHandle::Bind::REUSEADDR
UDPHandle::Bind::UDP_MMSG_CHUNK
UDPHandle::Bind::UDP_MMSG_FREE
UDPHandle::Bind::UDP_LINUX_RECVERR
UDPHandle::Bind::UDP_RECVMMSG
See the official documentation for further details.
ip | The IP address to which to bind. |
port | The port to which to bind. |
opts | Optional additional flags. |
bool uvw::UDPHandle::broadcast | ( | bool | enable = false | ) |
Sets broadcast on or off.
enable | True to set broadcast on, false otherwise. |
void uvw::UDPHandle::connect | ( | Addr | addr | ) |
Associates the handle to a remote address and port (either IPv4 or IPv6).
Every message sent by this handle is automatically sent to the given destination.
Trying to call this function on an already connected handle isn't allowed.
An ErrorEvent event is emitted in case of errors during the connection.
addr | A valid instance of Addr. |
void uvw::UDPHandle::connect | ( | const sockaddr & | addr | ) |
Associates the handle to a remote address and port (either IPv4 or IPv6).
Every message sent by this handle is automatically sent to the given destination.
Trying to call this function on an already connected handle isn't allowed.
An ErrorEvent event is emitted in case of errors during the connection.
addr | Initialized sockaddr_in or sockaddr_in6 data structure. |
void uvw::UDPHandle::connect | ( | const std::string & | ip, |
unsigned int | port ) |
Associates the handle to a remote address and port (either IPv4 or IPv6).
Every message sent by this handle is automatically sent to the given destination.
Trying to call this function on an already connected handle isn't allowed.
An ErrorEvent event is emitted in case of errors during the connection.
ip | The address to which to bind. |
port | The port to which to bind. |
void uvw::UDPHandle::disconnect | ( | ) |
Disconnects the handle.
Trying to disconnect a handle that is not connected isn't allowed.
An ErrorEvent event is emitted in case of errors.
bool uvw::UDPHandle::init | ( | ) |
Initializes the handle. The actual socket is created lazily.
bool uvw::UDPHandle::multicastInterface | ( | const std::string & | iface | ) |
Sets the multicast interface to send or receive data on.
iface | Interface address. |
bool uvw::UDPHandle::multicastLoop | ( | bool | enable = true | ) |
Sets IP multicast loop flag.
This makes multicast packets loop back to local sockets.
enable | True to enable multicast loop, false otherwise. |
bool uvw::UDPHandle::multicastMembership | ( | const std::string & | multicast, |
const std::string & | iface, | ||
Membership | membership ) |
Sets membership for a multicast address.
Available values for membership
are:
UDPHandle::Membership::LEAVE_GROUP
UDPHandle::Membership::JOIN_GROUP
multicast | Multicast address to set membership for. |
iface | Interface address. |
membership | Action to be performed. |
bool uvw::UDPHandle::multicastTtl | ( | int | val | ) |
Sets the multicast ttl.
val | A value in the range [1, 255] . |
void uvw::UDPHandle::open | ( | OSSocketHandle | socket | ) |
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.
socket | A valid socket handle (either a file descriptor or a SOCKET). |
|
noexcept |
Gets the remote address to which the handle is connected, if any.
void uvw::UDPHandle::recv | ( | ) |
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.
void uvw::UDPHandle::send | ( | Addr | addr, |
char * | data, | ||
unsigned int | len ) |
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.
addr | A valid instance of Addr. |
data | The data to be sent. |
len | The lenght of the submitted data. |
void uvw::UDPHandle::send | ( | Addr | addr, |
std::unique_ptr< char[]> | data, | ||
unsigned int | len ) |
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.
addr | A valid instance of Addr. |
data | The data to be sent. |
len | The lenght of the submitted data. |
void uvw::UDPHandle::send | ( | const sockaddr & | addr, |
char * | data, | ||
unsigned int | len ) |
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.
addr | Initialized sockaddr_in or sockaddr_in6 data structure. |
data | The data to be sent. |
len | The lenght of the submitted data. |
void uvw::UDPHandle::send | ( | const sockaddr & | addr, |
std::unique_ptr< char[]> | data, | ||
unsigned int | len ) |
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.
addr | Initialized sockaddr_in or sockaddr_in6 data structure. |
data | The data to be sent. |
len | The lenght of the submitted data. |
void uvw::UDPHandle::send | ( | const std::string & | ip, |
unsigned int | port, | ||
char * | data, | ||
unsigned int | len ) |
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.
ip | The address to which to send data. |
port | The port to which to send data. |
data | The data to be sent. |
len | The lenght of the submitted data. |
void uvw::UDPHandle::send | ( | const std::string & | ip, |
unsigned int | port, | ||
std::unique_ptr< char[]> | data, | ||
unsigned int | len ) |
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.
ip | The address to which to send data. |
port | The port to which to send data. |
data | The data to be sent. |
len | The lenght of the submitted data. |
|
noexcept |
Number of send requests currently in the queue awaiting to be processed.
|
noexcept |
Gets the number of bytes queued for sending.
It strictly shows how much information is currently queued.
|
noexcept |
Get the local IP and port of the UDP handle.
int uvw::UDPHandle::trySend | ( | Addr | addr, |
char * | data, | ||
unsigned int | len ) |
int uvw::UDPHandle::trySend | ( | Addr | addr, |
std::unique_ptr< char[]> | data, | ||
unsigned int | len ) |
int uvw::UDPHandle::trySend | ( | const sockaddr & | addr, |
char * | data, | ||
unsigned int | len ) |
Sends data over the UDP socket.
Same as send()
, but it won’t queue a send request if it can’t be completed immediately.
addr | Initialized sockaddr_in or sockaddr_in6 data structure. |
data | The data to be sent. |
len | The lenght of the submitted data. |
int uvw::UDPHandle::trySend | ( | const sockaddr & | addr, |
std::unique_ptr< char[]> | data, | ||
unsigned int | len ) |
Sends data over the UDP socket.
Same as send()
, but it won’t queue a send request if it can’t be completed immediately.
addr | Initialized sockaddr_in or sockaddr_in6 data structure. |
data | The data to be sent. |
len | The lenght of the submitted data. |
int uvw::UDPHandle::trySend | ( | const std::string & | ip, |
unsigned int | port, | ||
char * | data, | ||
unsigned int | len ) |
Sends data over the UDP socket.
Same as send()
, but it won’t queue a send request if it can’t be completed immediately.
ip | The address to which to send data. |
port | The port to which to send data. |
data | The data to be sent. |
len | The lenght of the submitted data. |
int uvw::UDPHandle::trySend | ( | const std::string & | ip, |
unsigned int | port, | ||
std::unique_ptr< char[]> | data, | ||
unsigned int | len ) |
Sends data over the UDP socket.
Same as send()
, but it won’t queue a send request if it can’t be completed immediately.
ip | The address to which to send data. |
port | The port to which to send data. |
data | The data to be sent. |
len | The lenght of the submitted data. |
bool uvw::UDPHandle::ttl | ( | int | val | ) |
Sets the time to live.
val | A value in the range [1, 255] . |