MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
socket.hpp
Go to the documentation of this file.
1#ifndef MXSOCKET_H
2#define MXSOCKET_H
3
6#include <optional>
7#include <string>
8#include <string_view>
9
10namespace mxnetwork {
11
14 [[nodiscard]] bool valid() const { return value.length > 0; }
15 [[nodiscard]] bool operator==(const SocketAddress &other) const;
16 };
17
18 /**
19 * @brief RAII helper that initializes and shuts down the platform socket subsystem.
20 */
22 /**
23 * @brief Initialize the socket backend for the current process.
24 */
26 /**
27 * @brief Shut down the socket backend.
28 */
30 MXNetworkInit(const MXNetworkInit &) = delete;
32 };
33
34 /**
35 * @brief Socket family and transport type.
36 */
37 enum class SocketType {
38 /** @brief No socket type selected. */
40 /** @brief IPv4 stream socket. */
42 /** @brief IPv6 stream socket. */
44 /** @brief UNIX-domain stream socket. */
46 /** @brief IPv4 datagram socket. */
48 /** @brief IPv6 datagram socket. */
50 /** @brief UNIX-domain datagram socket. */
52 };
53
54 /**
55 * @brief C++ wrapper around the MXNetwork socket API.
56 */
57 class Socket {
58 public:
59 /** @brief Construct an invalid socket wrapper. */
60 Socket() noexcept;
61 /**
62 * @brief Construct a socket wrapper for the given type.
63 * @param type Socket family and transport type.
64 */
65 Socket(SocketType type) noexcept;
66 /** @brief Destroy the socket wrapper and close the socket if needed. */
67 ~Socket() noexcept;
68 /**
69 * @brief Adopt an existing socket handle.
70 * @param sockfd Socket handle to wrap.
71 * @param type Socket family and transport type.
72 */
74 /**
75 * @brief Copy socket state from a C API structure.
76 * @param s Source socket state.
77 * @param type Socket family and transport type.
78 */
79 Socket(const MXSocket &s, SocketType type) noexcept;
80 Socket(const Socket &s) = delete;
81 /** @brief Move-construct from another socket wrapper. */
82 Socket(Socket &&s) noexcept;
83 Socket &operator=(const Socket &s) = delete;
84 /** @brief Move-assign from another socket wrapper. */
85 Socket &operator=(Socket &&s) noexcept;
86 /** @brief Return the underlying socket handle. */
87 [[nodiscard]] mx_socket_fd sockfd() const;
88 /**
89 * @brief Connect to a remote Internet endpoint.
90 * @param host Remote host name or address.
91 * @param port Remote service or port name.
92 * @return True on success.
93 */
94 bool connect(const std::string_view host, const std::string_view port);
95 /**
96 * @brief Connect to a UNIX-domain endpoint.
97 * @param path Filesystem path for the socket.
98 * @return True on success.
99 */
100 bool connect_unix(const std::string_view path);
101 /**
102 * @brief Start listening on an Internet port.
103 * @param port Service or port name.
104 * @param backlog Maximum pending connection queue length.
105 * @return True on success.
106 */
107 bool listen(std::string_view port, int backlog);
108 /**
109 * @brief Start listening on a UNIX-domain socket path.
110 * @param path Filesystem path for the socket.
111 * @param backlog Maximum pending connection queue length.
112 * @return True on success.
113 */
114 bool listen_unix(std::string_view path, int backlog);
115 /**
116 * @brief Toggle blocking mode.
117 * @param block True to enable blocking I/O.
118 * @return True on success.
119 */
120 bool setblocking(bool block);
121 /**
122 * @brief Bind an Internet socket to a local port.
123 * @param port Service or port name.
124 * @return True on success.
125 */
126 bool bind(std::string_view port);
127 /**
128 * @brief Bind a UNIX-domain socket to a local path.
129 * @param path Filesystem path for the socket.
130 * @return True on success.
131 */
132 bool bind_unix(std::string_view path);
133 /**
134 * @brief Accept an incoming connection.
135 * @return Accepted socket on success.
136 */
137 [[nodiscard]] std::optional<Socket> accept();
138
139 /**
140 * @brief Read bytes from the socket.
141 * @param buf Output buffer.
142 * @param bytes Number of bytes to read.
143 * @param flags Platform socket flags.
144 * @return Number of bytes read, or a negative error value.
145 */
146 ssize_t read(void *buf, size_t bytes, int flags);
147 /**
148 * @brief Read a line of text from the socket.
149 * @param buffer Output buffer pointer.
150 * @param len Output length.
151 * @return True on success.
152 */
153 bool readline(char **buffer, size_t *len);
154 /**
155 * @brief Write bytes to the socket.
156 * @param buf Input buffer.
157 * @param bytes Number of bytes to write.
158 * @param flags Platform socket flags.
159 * @return Number of bytes written, or a negative error value.
160 */
161 ssize_t write(const void *buf, size_t bytes, int flags);
162 /**
163 * @brief Read exactly the requested number of bytes.
164 * @param buf Output buffer.
165 * @param bytes Number of bytes to read.
166 * @return Number of bytes read, or a negative error value.
167 */
168 ssize_t read_all(void *buf, size_t bytes);
169 /**
170 * @brief Write exactly the requested number of bytes.
171 * @param buf Input buffer.
172 * @param bytes Number of bytes to write.
173 * @return Number of bytes written, or a negative error value.
174 */
175 ssize_t write_all(const void *buf, size_t bytes);
176 /**
177 * @brief Send a datagram using the configured socket type.
178 * @param buf Input buffer.
179 * @param bytes Number of bytes to send.
180 * @return Number of bytes sent, or a negative error value.
181 */
182 ssize_t sendto(const void *buf, size_t bytes);
183 /**
184 * @brief Receive a datagram using the configured socket type.
185 * @param buf Output buffer.
186 * @param bytes Maximum number of bytes to receive.
187 * @return Number of bytes received, or a negative error value.
188 */
189 ssize_t recvfrom(void *buf, size_t bytes);
190 ssize_t recvfrom(void *buf, size_t bytes, SocketAddress &address);
191 ssize_t sendto(const void *buf, size_t bytes, const SocketAddress &address);
192
193 /** @brief Return true when the socket handle is valid. */
194 [[nodiscard]] bool valid() const;
195 /** @brief Return true when the socket is open. */
196 [[nodiscard]] bool is_open() const;
197 /** @brief Close the socket if it is open. */
198 void close();
199 /** @brief Return the socket family and transport type. */
200 [[nodiscard]] SocketType socket_type() const;
201
202 protected:
205
206 private:
207 void setsocket(const MXSocket &s);
208 };
209} // namespace mxnetwork
210
211#endif
bool listen_unix(std::string_view path, int backlog)
Start listening on a UNIX-domain socket path.
Definition socket.cpp:112
ssize_t write_all(const void *buf, size_t bytes)
Write exactly the requested number of bytes.
Definition socket.cpp:203
ssize_t write(const void *buf, size_t bytes, int flags)
Write bytes to the socket.
Definition socket.cpp:195
void close()
Close the socket if it is open.
Definition socket.cpp:171
bool valid() const
Return true when the socket handle is valid.
Definition socket.cpp:163
bool is_open() const
Return true when the socket is open.
Definition socket.cpp:167
bool bind_unix(std::string_view path)
Bind a UNIX-domain socket to a local path.
Definition socket.cpp:153
bool readline(char **buffer, size_t *len)
Read a line of text from the socket.
Definition socket.cpp:191
SocketType socket_type() const
Return the socket family and transport type.
Definition socket.cpp:179
~Socket() noexcept
Destroy the socket wrapper and close the socket if needed.
Definition socket.cpp:42
ssize_t sendto(const void *buf, size_t bytes)
Send a datagram using the configured socket type.
Definition socket.cpp:207
Socket() noexcept
Construct an invalid socket wrapper.
Definition socket.cpp:31
bool bind(std::string_view port)
Bind an Internet socket to a local port.
Definition socket.cpp:138
bool listen(std::string_view port, int backlog)
Start listening on an Internet port.
Definition socket.cpp:100
SocketType type
Definition socket.hpp:204
std::optional< Socket > accept()
Accept an incoming connection.
Definition socket.cpp:120
bool setblocking(bool block)
Toggle blocking mode.
Definition socket.cpp:159
ssize_t read_all(void *buf, size_t bytes)
Read exactly the requested number of bytes.
Definition socket.cpp:199
ssize_t read(void *buf, size_t bytes, int flags)
Read bytes from the socket.
Definition socket.cpp:187
mx_socket_fd sockfd() const
Return the underlying socket handle.
Definition socket.cpp:183
Socket & operator=(const Socket &s)=delete
Socket(const Socket &s)=delete
bool connect_unix(const std::string_view path)
Connect to a UNIX-domain endpoint.
Definition socket.cpp:92
bool connect(const std::string_view host, const std::string_view port)
Connect to a remote Internet endpoint.
Definition socket.cpp:80
ssize_t recvfrom(void *buf, size_t bytes)
Receive a datagram using the configured socket type.
Definition socket.cpp:216
int mx_socket_fd
Definition mxsocket.h:48
SocketType
Socket family and transport type.
Definition socket.hpp:37
@ TYPE_INVALID
No socket type selected.
Definition socket.hpp:39
@ TYPE_UNIX_DGRAM
UNIX-domain datagram socket.
Definition socket.hpp:51
@ TYPE_INET_DGRAM
IPv4 datagram socket.
Definition socket.hpp:47
@ TYPE_INET6_DGRAM
IPv6 datagram socket.
Definition socket.hpp:49
@ TYPE_UNIX
UNIX-domain stream socket.
Definition socket.hpp:45
@ TYPE_INET6
IPv6 stream socket.
Definition socket.hpp:43
@ TYPE_INET
IPv4 stream socket.
Definition socket.hpp:41
Cross-platform socket state used by the C API.
Definition mxsocket.h:68
MXNetworkInit()
Initialize the socket backend for the current process.
Definition socket.cpp:14
MXNetworkInit(const MXNetworkInit &)=delete
MXNetworkInit & operator=(const MXNetworkInit &)=delete
~MXNetworkInit()
Shut down the socket backend.
Definition socket.cpp:25
MXSocketAddress value
Definition socket.hpp:13
bool operator==(const SocketAddress &other) const
Definition socket.cpp:9