uvw 3.1.0
|
The UDP handle. More...
#include <udp.h>
Public Member Functions | |
int | init () final |
Initializes the handle. The actual socket is created lazily. | |
int | open (os_socket_handle socket) |
Opens an existing file descriptor or SOCKET as a UDP handle. | |
int | connect (const sockaddr &addr) |
Associates the handle to a remote address and port (either IPv4 or IPv6). | |
int | connect (const std::string &ip, unsigned int port) |
Associates the handle to a remote address and port (either IPv4 or IPv6). | |
int | connect (socket_address addr) |
Associates the handle to a remote address and port (either IPv4 or IPv6). | |
int | disconnect () |
Disconnects the handle. | |
socket_address | peer () const noexcept |
Gets the remote address to which the handle is connected, if any. | |
int | bind (const sockaddr &addr, udp_flags opts=udp_flags::_UVW_ENUM) |
Binds the UDP handle to an IP address and port. | |
int | bind (const std::string &ip, unsigned int port, udp_flags opts=udp_flags::_UVW_ENUM) |
Binds the UDP handle to an IP address and port. | |
int | bind (socket_address addr, udp_flags opts=udp_flags::_UVW_ENUM) |
Binds the UDP handle to an IP address and port. | |
socket_address | sock () const noexcept |
Get the local IP and port of the UDP handle. | |
bool | multicast_membership (const std::string &multicast, const std::string &iface, membership ms) |
Sets membership for a multicast address. | |
bool | multicast_loop (bool enable=true) |
Sets IP multicast loop flag. | |
bool | multicast_ttl (int val) |
Sets the multicast ttl. | |
bool | multicast_interface (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. | |
int | send (const sockaddr &addr, std::unique_ptr< char[]> data, unsigned int len) |
Sends data over the UDP socket. | |
int | send (const std::string &ip, unsigned int port, std::unique_ptr< char[]> data, unsigned int len) |
Sends data over the UDP socket. | |
int | send (socket_address addr, std::unique_ptr< char[]> data, unsigned int len) |
Sends data over the UDP socket. | |
int | send (const sockaddr &addr, char *data, unsigned int len) |
Sends data over the UDP socket. | |
int | send (const std::string &ip, unsigned int port, char *data, unsigned int len) |
Sends data over the UDP socket. | |
int | send (socket_address addr, char *data, unsigned int len) |
Sends data over the UDP socket. | |
int | try_send (const sockaddr &addr, std::unique_ptr< char[]> data, unsigned int len) |
Sends data over the UDP socket. | |
int | try_send (const std::string &ip, unsigned int port, std::unique_ptr< char[]> data, unsigned int len) |
Sends data over the UDP socket. | |
int | try_send (socket_address addr, std::unique_ptr< char[]> data, unsigned int len) |
Sends data over the UDP socket. | |
int | try_send (const sockaddr &addr, char *data, unsigned int len) |
Sends data over the UDP socket. | |
int | try_send (const std::string &ip, unsigned int port, char *data, unsigned int len) |
Sends data over the UDP socket. | |
int | try_send (socket_address addr, char *data, unsigned int len) |
Sends data over the UDP socket. | |
int | recv () |
Prepares for receiving data. | |
int | stop () |
Stops listening for incoming datagrams. | |
size_t | send_queue_size () const noexcept |
Gets the number of bytes queued for sending. | |
size_t | send_queue_count () const noexcept |
Number of send requests currently in the queue awaiting to be processed. | |
Public Member Functions inherited from uvw::handle< udp_handle, uv_udp_t, send_event, udp_data_event > | |
handle_category | category () const noexcept |
Gets the category of the handle. | |
handle_type | 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 | send_buffer_size () |
Gets the size of the send buffer used for the socket. | |
int | send_buffer_size (int value) |
Sets the size of the send buffer used for the socket. | |
int | recv_buffer_size () |
Gets the size of the receive buffer used for the socket. | |
int | recv_buffer_size (int value) |
Sets the size of the receive buffer used for the socket. | |
os_file_descriptor | fd () const |
Gets the platform dependent file descriptor equivalent. | |
Public Member Functions inherited from uvw::resource< T, U, E > | |
template<typename R = void> | |
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. | |
Public Member Functions inherited from uvw::uv_type< U > | |
virtual int | init () |
Initializes the handle. | |
loop & | parent () const noexcept |
Gets the loop from which the resource was originated. | |
const U * | raw () const noexcept |
Gets the underlying raw data structure. | |
U * | raw () noexcept |
Gets the underlying raw data structure. | |
Public Member Functions inherited from uvw::emitter< T, E... > | |
void | on (listener_t< U > f) |
Registers a long-lived listener with the event emitter. | |
void | reset () noexcept |
Disconnects the listener for the given event type. | |
void | reset () noexcept |
Disconnects all listeners. | |
bool | has () const noexcept |
Checks if there is a listener registered for the specific event. | |
The UDP 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 udp_handle
through a loop
, arguments follow:
See the official documentation for further details.
int uvw::udp_handle::bind | ( | const sockaddr & | addr, |
udp_flags | opts = udp_flags::_UVW_ENUM |
||
) |
Binds the UDP handle to an IP address and port.
Available flags are:
udp_handle::udp_flags::IPV6ONLY
udp_handle::udp_flags::UDP_PARTIAL
udp_handle::udp_flags::REUSEADDR
udp_handle::udp_flags::UDP_MMSG_CHUNK
udp_handle::udp_flags::UDP_MMSG_FREE
udp_handle::udp_flags::UDP_LINUX_RECVERR
udp_handle::udp_flags::UDP_RECVMMSG
See the official documentation for further details.
addr | Initialized sockaddr_in or sockaddr_in6 data structure. |
opts | Optional additional flags. |
int uvw::udp_handle::bind | ( | const std::string & | ip, |
unsigned int | port, | ||
udp_flags | opts = udp_flags::_UVW_ENUM |
||
) |
Binds the UDP handle to an IP address and port.
Available flags are:
udp_handle::udp_flags::IPV6ONLY
udp_handle::udp_flags::UDP_PARTIAL
udp_handle::udp_flags::REUSEADDR
udp_handle::udp_flags::UDP_MMSG_CHUNK
udp_handle::udp_flags::UDP_MMSG_FREE
udp_handle::udp_flags::UDP_LINUX_RECVERR
udp_handle::udp_flags::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. |
int uvw::udp_handle::bind | ( | socket_address | addr, |
udp_flags | opts = udp_flags::_UVW_ENUM |
||
) |
Binds the UDP handle to an IP address and port.
Available flags are:
udp_handle::udp_flags::IPV6ONLY
udp_handle::udp_flags::UDP_PARTIAL
udp_handle::udp_flags::REUSEADDR
udp_handle::udp_flags::UDP_MMSG_CHUNK
udp_handle::udp_flags::UDP_MMSG_FREE
udp_handle::udp_flags::UDP_LINUX_RECVERR
udp_handle::udp_flags::UDP_RECVMMSG
See the official documentation for further details.
addr | A valid instance of socket_address. |
opts | Optional additional flags. |
bool uvw::udp_handle::broadcast | ( | bool | enable = false | ) |
Sets broadcast on or off.
enable | True to set broadcast on, false otherwise. |
int uvw::udp_handle::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.
addr | Initialized sockaddr_in or sockaddr_in6 data structure. |
int uvw::udp_handle::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.
ip | The address to which to bind. |
port | The port to which to bind. |
int uvw::udp_handle::connect | ( | socket_address | 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.
addr | A valid instance of socket_address. |
int uvw::udp_handle::disconnect | ( | ) |
Disconnects the handle.
Trying to disconnect a handle that is not connected isn't allowed.
|
finalvirtual |
Initializes the handle. The actual socket is created lazily.
Reimplemented from uvw::uv_type< U >.
bool uvw::udp_handle::multicast_interface | ( | const std::string & | iface | ) |
Sets the multicast interface to send or receive data on.
iface | Interface address. |
bool uvw::udp_handle::multicast_loop | ( | 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::udp_handle::multicast_membership | ( | const std::string & | multicast, |
const std::string & | iface, | ||
membership | ms | ||
) |
Sets membership for a multicast address.
Available values for ms
are:
udp_handle::membership::LEAVE_GROUP
udp_handle::membership::JOIN_GROUP
multicast | Multicast address to set membership for. |
iface | Interface address. |
ms | Action to be performed. |
bool uvw::udp_handle::multicast_ttl | ( | int | val | ) |
Sets the multicast ttl.
val | A value in the range [1, 255] . |
int uvw::udp_handle::open | ( | os_socket_handle | 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.
int uvw::udp_handle::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 UDP data event will be emitted when the handle receives data.
int uvw::udp_handle::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 send event will be emitted when the data have been sent.
addr | Initialized sockaddr_in or sockaddr_in6 data structure. |
data | The data to be sent. |
len | The lenght of the submitted data. |
int uvw::udp_handle::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 send event will be emitted when the data have been sent.
addr | Initialized sockaddr_in or sockaddr_in6 data structure. |
data | The data to be sent. |
len | The lenght of the submitted data. |
int uvw::udp_handle::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 send event will be emitted when the data have been sent.
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::udp_handle::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 send event will be emitted when the data have been sent.
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::udp_handle::send | ( | socket_address | 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 send event will be emitted when the data have been sent.
addr | A valid instance of socket_address. |
data | The data to be sent. |
len | The lenght of the submitted data. |
int uvw::udp_handle::send | ( | socket_address | 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 send event will be emitted when the data have been sent.
addr | A valid instance of socket_address. |
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::udp_handle::stop | ( | ) |
Stops listening for incoming datagrams.
int uvw::udp_handle::try_send | ( | 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::udp_handle::try_send | ( | 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::udp_handle::try_send | ( | 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::udp_handle::try_send | ( | 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. |
int uvw::udp_handle::try_send | ( | socket_address | 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 | A valid instance of socket_address. |
data | The data to be sent. |
len | The lenght of the submitted data. |
int uvw::udp_handle::try_send | ( | socket_address | 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 | A valid instance of socket_address. |
data | The data to be sent. |
len | The lenght of the submitted data. |
bool uvw::udp_handle::ttl | ( | int | val | ) |
Sets the time to live.
val | A value in the range [1, 255] . |