libcosmos
Linux C++ System Programming Library
Loading...
Searching...
No Matches
UnixDatagramSocket.hxx
1#pragma once
2
3// C++
4#include <optional>
5
6// cosmos
7#include <cosmos/net/Socket.hxx>
8#include <cosmos/net/UnixAddress.hxx>
9#include <cosmos/net/UnixOptions.hxx>
10
11namespace cosmos {
12
14
19 public Socket {
20public: // types
21
22 static inline constexpr auto TYPE = SocketType::DGRAM;
23
24public: // functions
25
26 explicit UnixDatagramSocket(const SocketFlags flags = SocketFlags{SocketFlag::CLOEXEC}) :
27 Socket{SocketFamily::UNIX, TYPE, flags} {
28 }
29
30 explicit UnixDatagramSocket(const FileDescriptor fd, const AutoCloseFD auto_close = AutoCloseFD{true}) :
31 Socket{fd, auto_close} {}
32
33 auto unixOptions() {
34 return UnixOptions{this->m_fd};
35 }
36
37 auto unixOptions() const {
38 return UnixOptions{this->m_fd};
39 }
40
42
50 void bind(const UnixAddress &addr) {
51 return Socket::bind(addr);
52 }
53
55
65 void connect(const UnixAddress &addr) {
66 return Socket::connect(addr);
67 }
68
69 std::pair<size_t, std::optional<UnixAddress>> receiveFrom(
70 void *buf, size_t length, const MessageFlags flags = MessageFlags{}) {
71 UnixAddress addr;
72 auto [len, filled] = Socket::receiveFrom(buf, length, addr, flags);
73
74 return {len, filled ? std::optional<UnixAddress>{addr} : std::nullopt};
75 }
76
77 auto sendTo(const void *buf, size_t length, const UnixAddress &addr, const MessageFlags flags = MessageFlags{}) {
78 return Socket::sendTo(buf, length, addr, flags);
79 }
80
81 auto sendTo(const std::string_view data, const UnixAddress &addr, const MessageFlags flags = MessageFlags{}) {
82 return sendTo(data.data(), data.size(), addr, flags);
83 }
84
85 using Socket::send;
86 using Socket::receive;
87
88 void sendMessage(SendMessageHeader &header) {
89 return Socket::sendMessage(header);
90 }
91
92 void sendMessageTo(SendMessageHeader &header, const UnixAddress &addr) {
93 return Socket::sendMessage(header, &addr);
94 }
95
96 void receiveMessage(ReceiveMessageHeader &header) {
97 (void)Socket::receiveMessage(header);
98 }
99
100 std::optional<UnixAddress> receiveMessageFrom(ReceiveMessageHeader &header) {
101 UnixAddress addr;
102 auto filled = Socket::receiveMessage(header, &addr);
103
104 return filled ? std::optional<UnixAddress>{addr} : std::nullopt;
105 }
106};
107
108} // end ns
A typesafe bit mask representation using class enums.
Definition BitMask.hxx:19
FileDescriptor fd() const
Allows access to the underlying fd with const semantics.
Definition FileBase.hxx:74
Thin Wrapper around OS file descriptors.
Base class for Socket types with ownership of a FileDescriptor.
Definition Socket.hxx:38
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
Address type for local UNIX domain sockets.
Implementation of a UNIX domain datagram socket.
void connect(const UnixAddress &addr)
Connect to the given UNIX address.
void bind(const UnixAddress &addr)
Bind to the given UNIX address.
UnixSocket level option setter/getter helper.