libcosmos
Linux C++ System Programming Library
Loading...
Searching...
No Matches
UDPSocket.hxx
1#pragma once
2
3// C++
4#include <optional>
5
6// cosmos
7#include <cosmos/net/IPAddress.hxx>
8#include <cosmos/net/IPSocket.hxx>
9#include <cosmos/net/UDPOptions.hxx>
10
11namespace cosmos {
12
14
23template <SocketFamily FAMILY>
25 public IPSocketT<FAMILY> {
26public: // types
27
28 using IPAddress = typename IPSocketT<FAMILY>::IPAddress;
29 static inline constexpr auto TYPE = SocketType::DGRAM;
30
31public: // functions
32
33 explicit UDPSocketT(const SocketFlags flags = SocketFlags{SocketFlag::CLOEXEC}) :
34 IPSocketT<FAMILY>{TYPE, flags} {
35 }
36
37 auto udpOptions() {
38 return UDPOptions{this->m_fd};
39 }
40
41 auto udpOptions() const {
42 return UDPOptions{this->m_fd};
43 }
44
46
54 void bind(const IPAddress &addr) {
55 return Socket::bind(addr);
56 }
57
59
69 void connect(const IPAddress &addr) {
70 return Socket::connect(addr);
71 }
72
74
80 std::pair<size_t, std::optional<IPAddress>> receiveFrom(
81 void *buf, size_t length, const MessageFlags flags = MessageFlags{}) {
82 IPAddress addr;
83 auto [len, filled] = Socket::receiveFrom(buf, length, addr, flags);
84
85 return {len, filled ? std::optional<IPAddress>{addr} : std::nullopt};
86 }
87
89
92 auto sendTo(const void *buf, size_t length, const IPAddress &addr, const MessageFlags flags = MessageFlags{}) {
93 return Socket::sendTo(buf, length, addr, flags);
94 }
95
96 auto sendTo(const std::string_view data, const IPAddress &addr, const MessageFlags flags = MessageFlags{}) {
97 return sendTo(data.data(), data.size(), addr, flags);
98 }
99
100 using Socket::send;
101 using Socket::receive;
102
103 void sendMessage(SendMessageHeader &header) {
104 return Socket::sendMessage(header);
105 }
106
107 void sendMessageTo(SendMessageHeader &header, const IPAddress &addr) {
108 return Socket::sendMessage(header, &addr);
109 }
110
111 void receiveMessage(ReceiveMessageHeader &header) {
112 (void)Socket::receiveMessage(header);
113 }
114
115 std::optional<IPAddress> receiveMessageFrom(ReceiveMessageHeader &header) {
116 IPAddress addr;
117 auto filled = Socket::receiveMessage(header, &addr);
118
119 return filled ? std::optional<IPAddress>{addr} : std::nullopt;
120 }
121};
122
123using UDP4Socket = UDPSocketT<SocketFamily::INET>;
124using UDP6Socket = UDPSocketT<SocketFamily::INET6>;
125
126} // end ns
A typesafe bit mask representation using class enums.
Definition BitMask.hxx:19
Base class for IPv4 or IPv6 based sockets.
Definition IPSocket.hxx:21
void sendMessage(SendMessageHeader &header, const SocketAddress *addr=nullptr)
Sends a message over the socket using extended SendMessageHeader data.
Definition Socket.cxx:126
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.
Definition Socket.cxx:94
void connect(const SocketAddress &addr)
Establish a new connection using the given destination address.
Definition Socket.cxx:33
void bind(const SocketAddress &addr)
Bind the socket to the given local address.
Definition Socket.cxx:27
AddressFilledIn receiveMessage(ReceiveMessageHeader &header, SocketAddress *addr=nullptr)
Receives a message from the socket using extended ReceiveMessageHeader data.
Definition Socket.cxx:145
size_t sendTo(const void *buf, size_t length, const SocketAddress &addr, const MessageFlags flags=MessageFlags{})
Send a packet to a specific destination address.
Definition Socket.cxx:75
size_t receive(void *buf, size_t length, const MessageFlags flags=MessageFlags{})
Receive data from the socket, using specific receive flags.
Definition Socket.cxx:84
size_t send(const void *buf, size_t length, const MessageFlags flags=MessageFlags{})
Send the given data over the socket, using specific send flags.
Definition Socket.cxx:66
UDP level socket option setter/getter helper.
Template class for IPv4 and IPv6 based UDP datagram sockets.
Definition UDPSocket.hxx:25
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.
Definition UDPSocket.hxx:80
auto sendTo(const void *buf, size_t length, const IPAddress &addr, const MessageFlags flags=MessageFlags{})
Send data to a specific IP address.
Definition UDPSocket.hxx:92
void bind(const IPAddress &addr)
Bind to the given IP address.
Definition UDPSocket.hxx:54
void connect(const IPAddress &addr)
Connect to the given IP address.
Definition UDPSocket.hxx:69