ReXGlue SDK
Loading...
Searching...
No Matches
socket.h
Go to the documentation of this file.
1
10#pragma once
11
12#include <chrono>
13#include <span>
14#include <utility>
15
16#include <rex/platform.h>
17#include <rex/bit.h>
18#include <rex/platform/thread.h>
19#include <rex/platform/sync.h>
20#include <rex/types.h>
21#include <rex/utility.h>
22
23struct sockaddr;
24
25namespace rex::platform {
26
28using SocketHandle = i64;
29
30constexpr SocketHandle kInvalidSocket = -1;
31
38
45REX_API int SocketIOCTL(SocketHandle handle, u32 cmd, u8* arg);
46
47enum class SocketReady : u8 {
48 kNone = 0,
49 kRead = 1 << 0,
50 kWrite = 1 << 1,
51 kExcept = 1 << 2,
52};
53
54struct SocketWait {
55 SocketHandle handle = kInvalidSocket;
56 WaitHandle* event = nullptr;
57 SocketReady ready = SocketReady::kNone;
58};
59
68
75
81
88REX_API int PollSockets(std::span<const SocketWait> sockets, std::span<SocketReady> ready);
89
98REX_API WaitResult WaitSockets(std::span<const SocketWait> sockets, bool alertable,
99 std::chrono::milliseconds timeout);
100
102class Socket : public MoveOnly {
103 public:
104 Socket() = default;
105
111 explicit Socket(SocketHandle handle) : handle_(handle) {}
112
113 ~Socket() { Reset(); }
114
115 Socket(Socket&& other) noexcept : handle_(std::exchange(other.handle_, kInvalidSocket)) {}
116
117 Socket& operator=(Socket&& other) noexcept {
118 if (this != &other) {
119 Reset();
120 handle_ = std::exchange(other.handle_, kInvalidSocket);
121 }
122 return *this;
123 }
124
130 REX_API static Socket Create(i32 family, i32 type, i32 protocol);
131
132 bool IsValid() const { return handle_ != kInvalidSocket; }
133
135 SocketHandle GetHandle() const { return handle_; }
136
142 REX_API i32 Connect(const sockaddr* name, i32 name_len) const;
143
149 REX_API i32 Bind(const sockaddr* name, i32 name_len) const;
150
158 REX_API i32 GetLocalName(sockaddr* name, i32* name_len) const;
159
166 REX_API i32 Listen(i32 backlog) const;
167
175 REX_API Socket Accept(sockaddr* name, i32* name_len) const;
176
182 REX_API i32 Shutdown(i32 how) const;
183
189 REX_API i32 Recv(void* buf, i32 len, i32 flags) const;
190
198 REX_API i32 RecvFrom(void* buf, i32 len, i32 flags, sockaddr* from, i32* from_len) const;
199
205 REX_API i32 Send(const void* buf, i32 len, i32 flags) const;
206
213 REX_API i32 SendTo(const void* buf, i32 len, i32 flags, const sockaddr* to, i32 to_len) const;
214
220 REX_API i32 SetOption(i32 level, i32 name, const void* value, i32 len) const;
221
229 REX_API i32 GetOption(i32 level, i32 name, void* value, i32* len) const;
230
238 REX_API i32 GetPeerName(sockaddr* name, i32* name_len) const;
239
246 i32 IOCTL(u32 cmd, u8* arg) const { return SocketIOCTL(handle_, cmd, arg); }
247
254 i32 Prepare(WaitHandle* event) const { return SocketPrepare(handle_, event); }
255
256 private:
257 void Reset() {
258 if (IsValid()) {
259 SocketClose(std::exchange(handle_, kInvalidSocket));
260 }
261 }
262
263 SocketHandle handle_ = kInvalidSocket;
264};
265
266} // namespace rex::platform
267
269template <>
270inline constexpr bool rex::is_flags_enum<rex::platform::SocketReady> = true;
Definition socket.h:102
Socket Accept(sockaddr *name, i32 *name_len) const
i32 Connect(const sockaddr *name, i32 name_len) const
Socket(SocketHandle handle)
Definition socket.h:111
static Socket Create(i32 family, i32 type, i32 protocol)
SocketHandle GetHandle() const
Definition socket.h:135
i32 Listen(i32 backlog) const
i32 Bind(const sockaddr *name, i32 name_len) const
i32 GetPeerName(sockaddr *name, i32 *name_len) const
i32 RecvFrom(void *buf, i32 len, i32 flags, sockaddr *from, i32 *from_len) const
i32 Recv(void *buf, i32 len, i32 flags) const
i32 SendTo(const void *buf, i32 len, i32 flags, const sockaddr *to, i32 to_len) const
i32 Prepare(WaitHandle *event) const
Definition socket.h:254
i32 IOCTL(u32 cmd, u8 *arg) const
Definition socket.h:246
i32 Shutdown(i32 how) const
i32 Send(const void *buf, i32 len, i32 flags) const
i32 SetOption(i32 level, i32 name, const void *value, i32 len) const
i32 GetOption(i32 level, i32 name, void *value, i32 *len) const
i32 GetLocalName(sockaddr *name, i32 *name_len) const
Definition sync.h:58
Definition platform.h:345
WaitResult WaitSockets(std::span< const SocketWait > sockets, bool alertable, std::chrono::milliseconds timeout)
SocketReady
Definition socket.h:47
@ kRead
Definition socket.h:49
@ kExcept
Definition socket.h:51
@ kWrite
Definition socket.h:50
u32 GetSocketError()
int SocketPrepare(SocketHandle handle, WaitHandle *event)
int PollSockets(std::span< const SocketWait > sockets, std::span< SocketReady > ready)
int SocketIOCTL(SocketHandle handle, u32 cmd, u8 *arg)
bool IsSocketWouldBlock()
int SocketClose(SocketHandle handle)
i64 SocketHandle
Definition socket.h:28
WaitResult
Definition sync.h:49
constexpr bool is_flags_enum
Definition bit.h:422
Platform flags, packing macros and export markers.
#define REX_API
Definition platform.h:177
Definition socket.h:54
Wait objects and wait functions.
Integer types, byte order wrappers and size literals.
Move only function, const propagating pointer and scope guard.