qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
socket.h
Go to the documentation of this file.
1
24
25#include "../system/sys__socket.h"
26#include "../uri.h"
27#include <optional>
28#include <chrono>
29#include <string>
30
31#ifndef QB_IO_UDP_SOCKET_H_
32#define QB_IO_UDP_SOCKET_H_
33
34namespace qb::io::udp {
35
46class QB_API socket : private qb::io::socket {
47public:
55 constexpr static const std::size_t DefaultDatagramSize = 512;
56
62 constexpr static const std::size_t MaxDatagramSize = 65507;
63
64 // Methods inherited from the base socket (made public via using-declarations)
65 using qb::io::socket::close;
66 using qb::io::socket::get_optval;
67 using qb::io::socket::is_open;
68 using qb::io::socket::local_endpoint;
69 using qb::io::socket::native_handle;
70 using qb::io::socket::peer_endpoint;
71 using qb::io::socket::release_handle;
72 using qb::io::socket::set_nonblocking;
73 using qb::io::socket::set_optval;
74 using qb::io::socket::test_nonblocking;
75
80 socket() = default;
81
85 socket(socket const &) = delete;
86
90 socket(socket &&) = default;
91
96 socket &operator=(socket &&) = default;
97
103 socket(io::socket &&sock) noexcept;
104
110 socket &operator=(io::socket &&sock) noexcept;
111
118 bool init(int af = AF_INET) noexcept;
119
127 int bind(qb::io::endpoint const &ep) noexcept;
128
135 int bind(qb::io::uri const &u) noexcept;
136
144 int bind_v4(uint16_t port, std::string const &host = "0.0.0.0") noexcept;
145
153 int bind_v6(uint16_t port, std::string const &host = "::") noexcept;
154
161 int bind_un(std::string const &path) noexcept;
162
173 int read(void *dest, std::size_t len, qb::io::endpoint &peer) const noexcept;
174
185 int read_timeout(void *dest, std::size_t len, qb::io::endpoint &peer,
186 const std::chrono::microseconds &timeout) const noexcept;
187
198 int try_read(void *dest, std::size_t len, qb::io::endpoint &peer) const noexcept;
199
209 int write(const void *data, std::size_t len,
210 qb::io::endpoint const &to) const noexcept;
211
217 int set_buffer_size(std::size_t size) noexcept;
218
225 int set_broadcast(bool enable) noexcept;
226
235 int join_multicast_group(const std::string &group,
236 const std::string &iface = "") noexcept;
237
245 int leave_multicast_group(const std::string &group,
246 const std::string &iface = "") noexcept;
247
256 int set_multicast_ttl(int ttl) noexcept;
257
265 int set_multicast_loopback(bool enable) noexcept;
266
273 int address_family() const noexcept;
274
281 bool is_bound() const noexcept;
282
290 int disconnect() const noexcept;
291};
292
293} // namespace qb::io::udp
294
295#endif // QB_IO_UDP_SOCKET_H_
int bind(qb::io::endpoint const &ep) noexcept
Bind the socket to a specific local endpoint.
int bind_v4(uint16_t port, std::string const &host="0.0.0.0") noexcept
Bind the socket to a specific IPv4 address and port.
int bind(qb::io::uri const &u) noexcept
Bind the socket to an endpoint specified by a URI.
int read_timeout(void *dest, std::size_t len, qb::io::endpoint &peer, const std::chrono::microseconds &timeout) const noexcept
Read a datagram from the socket with a specified timeout.
socket & operator=(io::socket &&sock) noexcept
Move assignment operator from a generic qb::io::socket.
bool is_bound() const noexcept
Check if the UDP socket is currently bound to a local address and port.
int bind_un(std::string const &path) noexcept
Bind the socket to a Unix domain socket path (for datagrams).
int set_multicast_loopback(bool enable) noexcept
Set whether multicast packets sent from this socket are looped back to the local host.
socket & operator=(socket &&)=default
Default move assignment operator.
socket(socket const &)=delete
Deleted copy constructor.
int bind_v6(uint16_t port, std::string const &host="::") noexcept
Bind the socket to a specific IPv6 address and port.
int set_buffer_size(std::size_t size) noexcept
Set the socket's send and receive buffer sizes (SO_SNDBUF, SO_RCVBUF).
socket(io::socket &&sock) noexcept
Constructor from a generic qb::io::socket (move semantics).
static constexpr const std::size_t DefaultDatagramSize
Default maximum size of a UDP datagram.
Definition socket.h:55
int try_read(void *dest, std::size_t len, qb::io::endpoint &peer) const noexcept
Try to read a datagram from the socket (non-blocking attempt).
int leave_multicast_group(const std::string &group, const std::string &iface="") noexcept
Leave an IPv4 or IPv6 multicast group.
socket()=default
Default constructor.
int read(void *dest, std::size_t len, qb::io::endpoint &peer) const noexcept
Read a datagram from the socket and get the sender's endpoint.
int write(const void *data, std::size_t len, qb::io::endpoint const &to) const noexcept
Write (send) a datagram to a specific remote endpoint.
int disconnect() const noexcept
Disconnect a previously connected UDP socket or clear default remote endpoint.
socket(socket &&)=default
Default move constructor.
int join_multicast_group(const std::string &group, const std::string &iface="") noexcept
Join an IPv4 or IPv6 multicast group.
int address_family() const noexcept
Get the address family of this UDP socket (e.g., AF_INET, AF_INET6).
int set_broadcast(bool enable) noexcept
Enable or disable the SO_BROADCAST socket option.
bool init(int af=AF_INET) noexcept
Initialize (open) the UDP socket.
int set_multicast_ttl(int ttl) noexcept
Set the multicast Time-To-Live (TTL) for outgoing IPv4 packets or hop limit for IPv6.
static constexpr const std::size_t MaxDatagramSize
Maximum possible size of a UDP datagram.
Definition socket.h:62
Class for parsing, manipulating, and representing URIs.
Definition uri.h:181
URI parsing and manipulation utilities.