Repository navigation
Expand file tree
/
Copy pathSocketPlatform.cpp
More file actions
133 lines (122 loc) · 3.72 KB
/
Copy pathSocketPlatform.cpp
File metadata and controls
133 lines (122 loc) · 3.72 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
#include "SocketPlatform.h"
#include <array>
#include <cerrno>
#include <cstring>
#ifdef _WIN32
#include <windows.h>
#else
#include <fcntl.h>
#include <poll.h>
#include <unistd.h>
#endif
#include "NetworkRuntime.h"
NetworkUtilsInternal::NativeSocket NetworkUtilsInternal::createSocket(
int32_t aFamily,
int32_t aType,
int32_t aProtocol) noexcept {
if (!ensureNetworkRuntime()) return INVALID_NATIVE_SOCKET;
return socket(aFamily, aType, aProtocol);
}
void NetworkUtilsInternal::closeSocket(NativeSocket aSocket) noexcept {
if (aSocket == INVALID_NATIVE_SOCKET) return;
#ifdef _WIN32
closesocket(aSocket);
#else
close(aSocket);
#endif
}
int32_t NetworkUtilsInternal::lastSocketError() noexcept {
#ifdef _WIN32
return WSAGetLastError();
#else
return errno;
#endif
}
bool NetworkUtilsInternal::setNonBlocking(NativeSocket aSocket) noexcept {
#ifdef _WIN32
u_long lEnabled = 1;
return ioctlsocket(aSocket, FIONBIO, &lEnabled) == 0;
#else
const int32_t lFlags = fcntl(aSocket, F_GETFL, 0);
return lFlags >= 0 && fcntl(aSocket, F_SETFL, lFlags | O_NONBLOCK) == 0;
#endif
}
bool NetworkUtilsInternal::isConnectInProgress(int32_t aError) noexcept {
#ifdef _WIN32
return aError == WSAEWOULDBLOCK || aError == WSAEINPROGRESS || aError == WSAEINVAL;
#else
return aError == EINPROGRESS || aError == EWOULDBLOCK || aError == EAGAIN;
#endif
}
bool NetworkUtilsInternal::waitForWritable(
NativeSocket aSocket,
int32_t aTimeoutMs,
bool& rTimedOut) noexcept {
rTimedOut = false;
#ifdef _WIN32
fd_set lWritableSockets;
fd_set lExceptionSockets;
FD_ZERO(&lWritableSockets);
FD_ZERO(&lExceptionSockets);
FD_SET(aSocket, &lWritableSockets);
FD_SET(aSocket, &lExceptionSockets);
timeval lTimeout{};
lTimeout.tv_sec = static_cast<long>(aTimeoutMs / 1000);
lTimeout.tv_usec = static_cast<long>((aTimeoutMs % 1000) * 1000);
// Winsock reports a completed connection in the write set and a failed
// connection in the exception set. WSAPoll does not reliably wake for a
// refused non-blocking connect on all supported Windows versions.
const int32_t lResult = select(
0, nullptr, &lWritableSockets, &lExceptionSockets, &lTimeout);
#else
pollfd lPollDescriptor{};
lPollDescriptor.fd = aSocket;
lPollDescriptor.events = POLLOUT;
const int32_t lResult = poll(&lPollDescriptor, 1, aTimeoutMs);
#endif
if (lResult == 0) {
rTimedOut = true;
return false;
}
return lResult > 0;
}
int32_t NetworkUtilsInternal::socketPendingError(NativeSocket aSocket) noexcept {
int32_t lError = 0;
#ifdef _WIN32
int32_t lLength = static_cast<int32_t>(sizeof(lError));
#else
socklen_t lLength = static_cast<socklen_t>(sizeof(lError));
#endif
if (getsockopt(
aSocket,
SOL_SOCKET,
SO_ERROR,
reinterpret_cast<char*>(&lError),
&lLength) != 0) {
return lastSocketError();
}
return lError;
}
std::string NetworkUtilsInternal::socketErrorMessage(int32_t aError) {
if (aError == 0) return {};
#ifdef _WIN32
std::array<char, 512> lBuffer{};
const DWORD lLength = FormatMessageA(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr,
static_cast<DWORD>(aError),
0,
lBuffer.data(),
static_cast<DWORD>(lBuffer.size()),
nullptr);
if (lLength == 0) return "Windows socket error " + std::to_string(aError);
std::string lMessage(lBuffer.data(), lLength);
while (!lMessage.empty() &&
(lMessage.back() == '\r' || lMessage.back() == '\n' || lMessage.back() == ' ')) {
lMessage.pop_back();
}
return lMessage;
#else
return std::strerror(aError);
#endif
}