forked from focus-creative-games/il2cpp_plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocket.cpp
More file actions
388 lines (308 loc) · 12 KB
/
Socket.cpp
File metadata and controls
388 lines (308 loc) · 12 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include "il2cpp-config.h"
#if IL2CPP_SUPPORT_SOCKETS
#include <map>
#include "os/Socket.h"
#include "os/Atomic.h"
#include "os/Mutex.h"
#include "utils/Expected.h"
#include "Baselib.h"
#include "Cpp/ReentrantLock.h"
#if IL2CPP_TARGET_POSIX || IL2CPP_SUPPORT_SOCKETS_POSIX_API
# include "os/Posix/SocketImpl.h"
#elif IL2CPP_TARGET_WINDOWS
# include "os/Win32/SocketImpl.h"
#elif IL2CPP_TARGET_SWITCH || IL2CPP_TARGET_SWITCH2
# include "os/SocketImpl.h"
#else
# include "os/Generic/SocketImpl.h"
#endif
namespace il2cpp
{
namespace os
{
typedef std::map<SocketHandle, Socket&> SocketHandleTable;
baselib::ReentrantLock g_SocketHandleTableMutex;
SocketHandleTable g_SocketHandleTable;
SocketHandle CreateSocketHandle(Socket* socket)
{
// Get the handle from the socket file descripter.
SocketHandle newHandle = (SocketHandle)socket->GetDescriptor();
// If the descripter is invalid we revert to the old method
if (newHandle == kInvalidSocketHandle)
{
IL2CPP_ASSERT(0 && "Attempted to get an invalid socket handle. Should not be in this method.");
return kInvalidSocketHandle;
}
// Add to table.
{
FastAutoLock lock(&g_SocketHandleTableMutex);
auto insertRes = g_SocketHandleTable.insert(SocketHandleTable::value_type(newHandle, *socket));
IL2CPP_ASSERT(insertRes.second && "Attempted to add a handle to the map that was already there.");
}
return newHandle;
}
Socket* AcquireSocketHandle(SocketHandle handle)
{
if (handle == kInvalidSocketHandle)
return NULL;
FastAutoLock lock(&g_SocketHandleTableMutex);
// Look up in table.
SocketHandleTable::iterator iter = g_SocketHandleTable.find(handle);
if (iter == g_SocketHandleTable.end())
return NULL;
// Increase reference count.
Socket& socket = iter->second;
++socket.m_RefCount;
return &socket;
}
void ReleaseSocketHandle(SocketHandle handle, Socket* socketToRelease, bool forceTableRemove)
{
if (handle == kInvalidSocketHandle || !socketToRelease)
{
IL2CPP_ASSERT(0 && "Invalid argument in ReleaseSocketHandle");
return;
}
Socket* socketToDelete = NULL;
{
FastAutoLock lock(&g_SocketHandleTableMutex);
IL2CPP_ASSERT(socketToRelease->m_RefCount && "Invalid ref count for Socket");
--socketToRelease->m_RefCount;
// Look up in table.
SocketHandleTable::iterator iter = g_SocketHandleTable.find(handle);
if (iter != g_SocketHandleTable.end() &&
((socketToRelease == &iter->second && !socketToRelease->m_RefCount) || forceTableRemove))
{
g_SocketHandleTable.erase(iter);
}
if (!socketToRelease->m_RefCount)
{
// Kill socket. Should be the only place where we directly delete sockets that
// have made it past the creation step.
socketToDelete = socketToRelease;
}
}
// Perform the deletion after we have released the lock so we don't unnecessarily
// prevent other threads from accessing the table.
if (socketToDelete)
delete socketToDelete;
}
void Socket::Startup()
{
return SocketImpl::Startup();
}
void Socket::Cleanup()
{
return SocketImpl::Cleanup();
}
WaitStatus Socket::GetHostName(std::string &name)
{
return SocketImpl::GetHostName(name);
}
WaitStatus Socket::GetHostByAddr(const std::string &address, std::string &name, std::vector<std::string> &aliases, std::vector<std::string> &addr_list)
{
return SocketImpl::GetHostByAddr(address, name, aliases, addr_list);
}
WaitStatus Socket::GetHostByName(const std::string &host, std::string &name, std::vector<std::string> &aliases, std::vector<std::string> &addresses)
{
return SocketImpl::GetHostByName(host, name, aliases, addresses);
}
WaitStatus Socket::GetHostByName(const std::string &host, std::string &name, int32_t &family, std::vector<std::string> &aliases, std::vector<void*> &addr_list, int32_t &addr_size)
{
return SocketImpl::GetHostByName(host, name, family, aliases, addr_list, addr_size);
}
Socket::Socket(ThreadStatusCallback thread_status_callback)
: m_Socket(new SocketImpl(thread_status_callback)), m_RefCount(1)
{
}
Socket::~Socket()
{
if (!IsClosed())
Close();
delete m_Socket;
m_Socket = 0;
}
int64_t Socket::GetDescriptor()
{
if (IsClosed())
return -1;
return m_Socket->GetDescriptor();
}
WaitStatus Socket::Create(int64_t fd, int32_t family, int32_t type, int32_t protocol)
{
return m_Socket->Create((SocketImpl::SocketDescriptor)fd, family, type, protocol);
}
WaitStatus Socket::Create(AddressFamily family, SocketType type, ProtocolType protocol)
{
return m_Socket->Create(family, type, protocol);
}
bool Socket::IsClosed()
{
return m_Socket->IsClosed();
}
void Socket::Close()
{
m_Socket->Close();
}
ErrorCode Socket::GetLastError() const
{
return m_Socket->GetLastError();
}
WaitStatus Socket::SetBlocking(bool blocking)
{
return m_Socket->SetBlocking(blocking);
}
WaitStatus Socket::Bind(const char *path)
{
return m_Socket->Bind(path);
}
WaitStatus Socket::Bind(uint32_t address, uint16_t port)
{
return m_Socket->Bind(address, port);
}
WaitStatus Socket::Bind(const char *address, uint16_t port)
{
return m_Socket->Bind(address, port);
}
utils::Expected<WaitStatus> Socket::Bind(uint8_t address[ipv6AddressSize], uint32_t scope, uint16_t port)
{
return m_Socket->Bind(address, scope, port);
}
WaitStatus Socket::Connect(const char *path)
{
return m_Socket->Connect(path);
}
WaitStatus Socket::Connect(uint32_t address, uint16_t port)
{
return m_Socket->Connect(address, port);
}
utils::Expected<WaitStatus> Socket::Connect(uint8_t address[ipv6AddressSize], uint32_t scope, uint16_t port)
{
return m_Socket->Connect(address, scope, port);
}
WaitStatus Socket::Disconnect(bool reuse)
{
return m_Socket->Disconnect(reuse);
}
WaitStatus Socket::Shutdown(int32_t how)
{
return m_Socket->Shutdown(how);
}
WaitStatus Socket::GetLocalEndPointInfo(EndPointInfo &info)
{
return m_Socket->GetLocalEndPointInfo(info);
}
WaitStatus Socket::GetRemoteEndPointInfo(EndPointInfo &info)
{
return m_Socket->GetRemoteEndPointInfo(info);
}
WaitStatus Socket::Listen(int32_t backlog)
{
return m_Socket->Listen(backlog);
}
WaitStatus Socket::Accept(Socket **socket)
{
return m_Socket->Accept(socket);
}
WaitStatus Socket::Receive(const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len)
{
return m_Socket->Receive(data, count, flags, len);
}
WaitStatus Socket::ReceiveArray(WSABuf *wsabufs, int32_t count, int32_t *len, SocketFlags c_flags)
{
return m_Socket->ReceiveArray(wsabufs, count, len, c_flags);
}
WaitStatus Socket::Send(const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len)
{
return m_Socket->Send(data, count, flags, len);
}
WaitStatus Socket::SendArray(WSABuf *wsabufs, int32_t count, int32_t *sent, SocketFlags c_flags)
{
return m_Socket->SendArray(wsabufs, count, sent, c_flags);
}
WaitStatus Socket::SendTo(uint32_t address, uint16_t port, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len)
{
return m_Socket->SendTo(address, port, data, count, flags, len);
}
utils::Expected<WaitStatus> Socket::SendTo(const char *path, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len)
{
return m_Socket->SendTo(path, data, count, flags, len);
}
utils::Expected<WaitStatus> Socket::SendTo(uint8_t address[ipv6AddressSize], uint32_t scope, uint16_t port, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len)
{
return m_Socket->SendTo(address, scope, port, data, count, flags, len);
}
WaitStatus Socket::RecvFrom(uint32_t address, uint16_t port, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len, os::EndPointInfo &ep)
{
return m_Socket->RecvFrom(address, port, data, count, flags, len, ep);
}
utils::Expected<WaitStatus> Socket::RecvFrom(const char *path, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len, os::EndPointInfo &ep)
{
return m_Socket->RecvFrom(path, data, count, flags, len, ep);
}
utils::Expected<WaitStatus> Socket::RecvFrom(uint8_t address[ipv6AddressSize], uint32_t scope, uint16_t port, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len, os::EndPointInfo &ep)
{
return m_Socket->RecvFrom(address, scope, port, data, count, flags, len, ep);
}
WaitStatus Socket::Available(int32_t *amount)
{
return m_Socket->Available(amount);
}
WaitStatus Socket::Ioctl(int32_t command, const uint8_t *in_data, int32_t in_len, uint8_t *out_data, int32_t out_len, int32_t *written)
{
return m_Socket->Ioctl(command, in_data, in_len, out_data, out_len, written);
}
WaitStatus Socket::GetSocketOption(SocketOptionLevel level, SocketOptionName name, uint8_t *buffer, int32_t *length)
{
return m_Socket->GetSocketOption(level, name, buffer, length);
}
WaitStatus Socket::GetSocketOptionFull(SocketOptionLevel level, SocketOptionName name, int32_t *first, int32_t *second)
{
return m_Socket->GetSocketOptionFull(level, name, first, second);
}
WaitStatus Socket::Poll(std::vector<PollRequest> &requests, int32_t count, int32_t timeout, int32_t *result, int32_t *error)
{
return SocketImpl::Poll(requests, count, timeout, result, error);
}
WaitStatus Socket::Poll(std::vector<PollRequest> &requests, int32_t timeout, int32_t *result, int32_t *error)
{
return SocketImpl::Poll(requests, timeout, result, error);
}
WaitStatus Socket::Poll(PollRequest& request, int32_t timeout, int32_t *result, int32_t *error)
{
return SocketImpl::Poll(request, timeout, result, error);
}
WaitStatus Socket::SetSocketOption(SocketOptionLevel level, SocketOptionName name, int32_t value)
{
return m_Socket->SetSocketOption(level, name, value);
}
WaitStatus Socket::SetSocketOptionLinger(SocketOptionLevel level, SocketOptionName name, bool enabled, int32_t seconds)
{
return m_Socket->SetSocketOptionLinger(level, name, enabled, seconds);
}
WaitStatus Socket::SetSocketOptionArray(SocketOptionLevel level, SocketOptionName name, const uint8_t *buffer, int32_t length)
{
return m_Socket->SetSocketOptionArray(level, name, buffer, length);
}
WaitStatus Socket::SetSocketOptionMembership(SocketOptionLevel level, SocketOptionName name, uint32_t group_address, uint32_t local_address)
{
return m_Socket->SetSocketOptionMembership(level, name, group_address, local_address);
}
#if IL2CPP_SUPPORT_IPV6
WaitStatus Socket::SetSocketOptionMembership(SocketOptionLevel level, SocketOptionName name, IPv6Address ipv6, uint64_t interfaceOffset)
{
return m_Socket->SetSocketOptionMembership(level, name, ipv6, interfaceOffset);
}
#endif
#if IL2CPP_SUPPORT_IPV6_SUPPORT_QUERY
bool Socket::IsIPv6Supported()
{
return SocketImpl::IsIPv6Supported();
}
#endif
WaitStatus Socket::SendFile(const char *filename, TransmitFileBuffers *buffers, TransmitFileOptions options)
{
return m_Socket->SendFile(filename, buffers, options);
}
}
}
#endif