7#include <cosmos/net/IPAddress.hxx>
8#include <cosmos/net/IPSocket.hxx>
9#include <cosmos/net/UDPOptions.hxx>
23template <SocketFamily FAMILY>
28 using IPAddress =
typename IPSocketT<FAMILY>::IPAddress;
29 static inline constexpr auto TYPE = SocketType::DGRAM;
41 auto udpOptions()
const {
54 void bind(
const IPAddress &addr) {
85 return {len, filled ? std::optional<IPAddress>{addr} : std::nullopt};
96 auto sendTo(
const std::string_view data,
const IPAddress &addr,
const MessageFlags flags = MessageFlags{}) {
97 return sendTo(data.data(), data.size(), addr, flags);
103 void sendMessage(SendMessageHeader &header) {
107 void sendMessageTo(SendMessageHeader &header,
const IPAddress &addr) {
111 void receiveMessage(ReceiveMessageHeader &header) {
115 std::optional<IPAddress> receiveMessageFrom(ReceiveMessageHeader &header) {
119 return filled ? std::optional<IPAddress>{addr} : std::nullopt;
123using UDP4Socket = UDPSocketT<SocketFamily::INET>;
124using UDP6Socket = UDPSocketT<SocketFamily::INET6>;
A typesafe bit mask representation using class enums.
Base class for IPv4 or IPv6 based sockets.
void sendMessage(SendMessageHeader &header, const SocketAddress *addr=nullptr)
Sends a message over the socket using extended SendMessageHeader data.
std::pair< size_t, AddressFilledIn > receiveFrom(void *buf, size_t length, SocketAddress &addr, const MessageFlags flags=MessageFlags{})
Receive a packet, filling in the sender's address.
void connect(const SocketAddress &addr)
Establish a new connection using the given destination address.
void bind(const SocketAddress &addr)
Bind the socket to the given local address.
AddressFilledIn receiveMessage(ReceiveMessageHeader &header, SocketAddress *addr=nullptr)
Receives a message from the socket using extended ReceiveMessageHeader data.
size_t sendTo(const void *buf, size_t length, const SocketAddress &addr, const MessageFlags flags=MessageFlags{})
Send a packet to a specific destination address.
size_t receive(void *buf, size_t length, const MessageFlags flags=MessageFlags{})
Receive data from the socket, using specific receive flags.
size_t send(const void *buf, size_t length, const MessageFlags flags=MessageFlags{})
Send the given data over the socket, using specific send flags.
UDP level socket option setter/getter helper.
Template class for IPv4 and IPv6 based UDP datagram sockets.
std::pair< size_t, std::optional< IPAddress > > receiveFrom(void *buf, size_t length, const MessageFlags flags=MessageFlags{})
Receive data and get the sender's IP address.
auto sendTo(const void *buf, size_t length, const IPAddress &addr, const MessageFlags flags=MessageFlags{})
Send data to a specific IP address.
void bind(const IPAddress &addr)
Bind to the given IP address.
void connect(const IPAddress &addr)
Connect to the given IP address.