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