-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSocketLibErrors.cpp
More file actions
286 lines (281 loc) · 9.03 KB
/
SocketLibErrors.cpp
File metadata and controls
286 lines (281 loc) · 9.03 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
#include "SocketLibErrors.h"
namespace SocketLib
{
Error GetError(bool p_errno)
{
#ifdef WIN32
return TranslateError(WSAGetLastError(), p_errno);
#else
if (p_errno == true)
return TranslateError(errno, p_errno);
else
{
return TranslateError(h_errno, p_errno);
}
#endif // WIN32
return ESeriousError;
}
#ifdef WIN32
Error TranslateError(int p_error, bool p_errno)
{
switch (p_error)
{
case WSAEINTR:
return EInterrupted;
case WSAEACCES:
return EAccessDenied;
case WSAEFAULT:
case WSAEINVAL:
return EInvalidParameter;
case WSAEMFILE:
return ENoSocketsAvailable;
case WSAEWOULDBLOCK:
return EOperationWouldBlock;
case WSAEINPROGRESS:
case WSAEALREADY:
return EInProgress;
case WSAENOTSOCK:
return EInvalidSocket;
case WSAEDESTADDRREQ:
return EAddressRequired;
case WSAEMSGSIZE:
return EMessageTooLong;
case WSAEPROTOTYPE:
return EProtocolNotSupportedBySocket;
case WSAENOPROTOOPT:
return EBadProtocolOption;
case WSAEPROTONOSUPPORT:
return EProtocolNotSupported;
case WSAESOCKTNOSUPPORT:
return EInvalidSocketType;
case WSAEOPNOTSUPP:
return EOperationNotSupported;
case WSAEPFNOSUPPORT:
return EProtocolFamilyNotSupported;
case WSAEAFNOSUPPORT:
return EAddressFamilyNotSupported;
case WSAEADDRINUSE:
return EAddressInUse;
case WSAEADDRNOTAVAIL:
return EAddressNotAvailable;
case WSAENETDOWN:
return ENetworkDown;
case WSAENETUNREACH:
return ENetworkUnreachable;
case WSAENETRESET:
return ENetworkReset;
case WSAECONNABORTED:
return EConnectionAborted;
case WSAECONNRESET:
return EConnectionReset;
case WSAENOBUFS:
return ENoMemory;
case WSAEISCONN:
return EAlreadyConnected;
case WSAENOTCONN:
return ENotConnected;
case WSAESHUTDOWN:
return EShutDown;
case WSAETIMEDOUT:
return ETimedOut;
case WSAECONNREFUSED:
return EConnectionRefused;
case WSAEHOSTDOWN:
return EHostDown;
case WSAEHOSTUNREACH:
return EHostUnreachable;
case WSAHOST_NOT_FOUND:
return EDNSNotFound;
case WSATRY_AGAIN:
return EDNSError;
case WSANO_DATA:
return ENoDNSData;
default:
return ESeriousError;
}
}
#else
Error TranslateError(int p_error, bool p_errno)
{
// for the linux version, we need to check if we're using errno
// or h_errno. Lucky for us, both error reporting mechanisms
// return the same values for different errors (d'oh). So,
// the code checks the errno and the h_errno error codes in
// different switch statements.
if (p_errno == true)
{
switch (p_error)
{
case EINTR:
return EInterrupted;
case EACCES:
return EAccessDenied;
case EFAULT:
case EINVAL:
return EInvalidParameter;
case EMFILE:
return ENoSocketsAvailable;
case EWOULDBLOCK:
return EOperationWouldBlock;
case EINPROGRESS:
case EALREADY:
return EInProgress;
case ENOTSOCK:
return EInvalidSocket;
case EDESTADDRREQ:
return EAddressRequired;
case EMSGSIZE:
return EMessageTooLong;
case EPROTOTYPE:
return EProtocolNotSupportedBySocket;
case ENOPROTOOPT:
return EBadProtocolOption;
case EPROTONOSUPPORT:
return EProtocolNotSupported;
case ESOCKTNOSUPPORT:
return EInvalidSocketType;
case EOPNOTSUPP:
return EOperationNotSupported;
case EPFNOSUPPORT:
return EProtocolFamilyNotSupported;
case EAFNOSUPPORT:
return EAddressFamilyNotSupported;
case EADDRINUSE:
return EAddressInUse;
case EADDRNOTAVAIL:
return EAddressNotAvailable;
case ENETDOWN:
return ENetworkDown;
case ENETUNREACH:
return ENetworkUnreachable;
case ENETRESET:
return ENetworkReset;
case ECONNABORTED:
return EConnectionAborted;
case ECONNRESET:
return EConnectionReset;
case ENOBUFS:
return ENoMemory;
case EISCONN:
return EAlreadyConnected;
case ENOTCONN:
return ENotConnected;
case ESHUTDOWN:
return EShutDown;
case ETIMEDOUT:
return ETimedOut;
case ECONNREFUSED:
return EConnectionRefused;
case EHOSTDOWN:
return EHostDown;
case EHOSTUNREACH:
return EHostUnreachable;
default:
return ESeriousError;
}
}
else
{
switch (p_error)
{
case HOST_NOT_FOUND:
return EDNSNotFound;
case TRY_AGAIN:
return EDNSError;
case NO_DATA:
return ENoDNSData;
default:
return ESeriousError;
}
}
}
#endif
Exception::Exception(Error p_code)
{
m_code = p_code;
if (p_code == ENotAvailable)
{
p_code = p_code;
}
}
Error Exception::ErrorCode()
{
return m_code;
}
std::string Exception::PrintError()
{
switch (m_code)
{
case EOperationWouldBlock:
return "Nonblocking socket operation would have blocked";
case EInProgress:
return "This operation is already in progress";
case EInvalidSocket:
return "The socket was not valid";
case EAddressRequired:
return "A destination address is required";
case EMessageTooLong:
return "The message was too long";
case EProtocolNotSupported:
return "The protocol is not supported";
case EProtocolNotSupportedBySocket:
return "The socket type is not supported";
case EOperationNotSupported:
return "The operation is not supported";
case EProtocolFamilyNotSupported:
return "The protocol family is not supported";
case EAddressFamilyNotSupported:
return "The operation is not supported by the address family";
case EAddressInUse:
return "The address is already in use";
case EAddressNotAvailable:
return "The address is not available to use";
case ENetworkDown:
return "The network is down";
case ENetworkUnreachable:
return "The destination network is unreachable";
case ENetworkReset:
return "The network connection has been reset";
case EConnectionAborted:
return "The network connection has been aborted due to software error";
case EConnectionReset:
return "Connection has been closed by the other side";
case ENoMemory:
return "There was insufficient system memory to complete the operation";
case EAlreadyConnected:
return "The socket is already connected";
case ENotConnected:
return "The socket is not connected";
case EShutDown:
return "The socket has already been shut down";
case ETimedOut:
return "The connection timed out";
case EConnectionRefused:
return "The connection was refused";
case EHostDown:
return "The host is down";
case EHostUnreachable:
return "The host is unreachable";
case EDNSNotFound:
return "DNS lookup is not found";
case EDNSError:
return "Host not found due to error; try again";
case ENoDNSData:
return "Address found, but has no valid data";
case EInterrupted:
return "Operation was interrupted";
case ENoSocketsAvailable:
return "No more sockets available";
case EInvalidParameter:
return "Operation has an invalid parameter";
case EInvalidSocketType:
return "Socket type is invalid";
case EAccessDenied:
return "Access to this operation was denied";
case ESocketLimitReached:
return "The manager has reached its maximum number of sockets";
default:
return "undefined or serious error";
}
}
}