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
|
/* Project nGenEx
Destroyer Studios LLC
Copyright © 1997-2004. All Rights Reserved.
SUBSYSTEM: NetEx.lib
FILE: NetSock.cpp
AUTHOR: John DiCamillo
OVERVIEW
========
Network (IP) Socket Wrapper Implementation
*/
// WINSOCK2.H MUST COME FIRST!!
#include <winsock2.h>
#include "MemDebug.h"
#include "NetSock.h"
#include "NetLayer.h"
// +-------------------------------------------------------------------+
/**
* Server-side socket constructor
*/
NetSock::NetSock(bool str)
: stream(str), closed(false), stat(0), current_timeout(9999)
{
if (stream)
s = ::socket(AF_INET, SOCK_STREAM, 0);
else
s = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
}
/**
* Server-side socket constructor
*
* PRIVATE: used only by the accept call to build a socket for
* a client connection to this server
*/
NetSock::NetSock(SOCKET sock, bool str)
: s(sock), stream(str), closed(false), stat(0), current_timeout(9999)
{ }
/**
* Client-side socket constructor
*
* Will connect to server at "addr"
*/
NetSock::NetSock(const NetAddr& addr, bool str)
: stream(str), closed(false), stat(0), current_timeout(9999)
{
if (stream)
s = ::socket(AF_INET, SOCK_STREAM, 0);
else
s = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
connect(addr);
}
NetSock::~NetSock()
{
close();
}
// +--------------------------------------------------------------------+
int
NetSock::bind(const NetAddr& addr)
{
if (closed || s == INVALID_SOCKET) return INVALID_SOCKET;
return stat = ::bind(s, addr.GetSockAddr(), addr.GetSockAddrLength());
}
int
NetSock::connect(const NetAddr& addr)
{
if (closed || s == INVALID_SOCKET) return INVALID_SOCKET;
return stat = ::connect(s, addr.GetSockAddr(), addr.GetSockAddrLength());
}
int
NetSock::listen(int max_connections)
{
if (closed || s == INVALID_SOCKET) return INVALID_SOCKET;
return stat = ::listen(s, max_connections);
}
NetSock*
NetSock::accept(NetAddr* addr)
{
if (closed || s == INVALID_SOCKET) return 0;
SOCKET conn = INVALID_SOCKET;
if (addr) {
sockaddr a;
int asize = sizeof(a);
conn = ::accept(s, &a, &asize);
if (conn != INVALID_SOCKET && asize > 0) {
addr->SetSockAddr(&a, asize);
}
}
else {
conn = ::accept(s, 0, 0);
}
if (conn == INVALID_SOCKET)
return 0;
return new(__FILE__,__LINE__) NetSock(conn, stream);
}
// +--------------------------------------------------------------------+
int
NetSock::available()
{
if (closed || s == INVALID_SOCKET) return 0;
DWORD nbytes = 0;
if (::ioctlsocket(s, FIONREAD, &nbytes) == 0)
return (int) nbytes;
return 0;
}
// +--------------------------------------------------------------------+
int
NetSock::send(Text msg)
{
if (closed || s == INVALID_SOCKET) return INVALID_SOCKET;
return stat = ::send(s, msg.data(), msg.length(), 0);
}
Text
NetSock::recv()
{
if (closed || s == INVALID_SOCKET) return "";
static char rbuf[8192];
int rlen = -1;
if (stream) {
rlen = ::recv(s, rbuf, sizeof(rbuf), 0);
}
else {
rlen = ::recvfrom(s, rbuf, sizeof(rbuf), 0, 0, 0);
}
if (rlen < 0) {
stat = NetLayer::GetLastError();
switch (stat) {
case WSAENETDOWN:
case WSAENETRESET:
case WSAEINTR:
case WSAESHUTDOWN:
case WSAECONNABORTED:
case WSAECONNRESET:
case WSAETIMEDOUT:
close();
break;
case WSAEWOULDBLOCK:
stat = WSAEWOULDBLOCK;
break;
}
return Text();
}
else if (rlen == 0) {
return Text();
}
return Text(rbuf, rlen);
}
// +--------------------------------------------------------------------+
int
NetSock::sendto(Text msg, const NetAddr& dest)
{
if (closed || s == INVALID_SOCKET) return INVALID_SOCKET;
return stat = ::sendto(s, msg.data(), msg.length(),
0, dest.GetSockAddr(), dest.GetSockAddrLength());
}
Text
NetSock::recvfrom(NetAddr* a)
{
if (closed || s == INVALID_SOCKET) return "";
static char rbuf[4096];
int rlen = 0;
if (a) {
int addrlen = a->GetSockAddrLength();
rlen = ::recvfrom(s, rbuf, sizeof(rbuf), 0, a->GetSockAddr(), &addrlen);
a->InitFromSockAddr();
}
else {
rlen = ::recvfrom(s, rbuf, sizeof(rbuf), 0, 0, 0);
}
if (rlen < 0) {
stat = NetLayer::GetLastError();
return Text();
}
else if (rlen == 0) {
return Text();
}
return Text(rbuf, rlen);
}
// +--------------------------------------------------------------------+
int
NetSock::select(SELECT_TYPE t, long seconds, long microseconds)
{
if (closed || s == INVALID_SOCKET) return INVALID_SOCKET;
FD_SET fd;
ZeroMemory(&fd, sizeof(fd));
FD_SET(s, &fd);
TIMEVAL timeval = {seconds, microseconds};
TIMEVAL* timeout = &timeval;
if (t == SELECT_READ)
return stat = ::select(1, &fd, 0, 0, timeout);
else if (t == SELECT_WRITE)
return stat = ::select(1, 0, &fd, 0, timeout);
else if (t == (SELECT_READ|SELECT_WRITE))
return stat = ::select(1, &fd, &fd, 0, timeout);
return 0;
}
// +--------------------------------------------------------------------+
int
NetSock::set_timeout(int msecs)
{
if (closed || s == INVALID_SOCKET) return 0;
if (msecs == current_timeout) return 1;
// zero timeout means non-blocking
if (msecs == 0) {
u_long nonblocking = 1;
if (::ioctlsocket(s, FIONBIO, &nonblocking) == SOCKET_ERROR) {
stat = NetLayer::GetLastError();
return stat;
}
}
// non-zero timeout means blocking
else {
if (current_timeout == 0) {
u_long nonblocking = 0; // disable non-blocking
if (::ioctlsocket(s, FIONBIO, &nonblocking) == SOCKET_ERROR) {
stat = NetLayer::GetLastError();
return stat;
}
}
// max timeout means infinite wait
if (msecs >= NET_MAX_TIMEOUT) {
int maxto = 0;
::setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char*) &maxto, sizeof(maxto));
::setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char*) &maxto, sizeof(maxto));
}
// otherwise, set the timeout
else {
::setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char*) &msecs, sizeof(msecs));
::setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char*) &msecs, sizeof(msecs));
}
}
current_timeout = msecs;
return 1;
}
// +--------------------------------------------------------------------+
int
NetSock::shutdown_input()
{
if (closed || s == INVALID_SOCKET) return INVALID_SOCKET;
::shutdown(s, SD_RECEIVE);
return 0;
}
// +--------------------------------------------------------------------+
int
NetSock::shutdown_output()
{
if (closed || s == INVALID_SOCKET) return INVALID_SOCKET;
::shutdown(s, SD_SEND);
return 0;
}
// +--------------------------------------------------------------------+
int
NetSock::close()
{
if (s != INVALID_SOCKET && !closed) {
::shutdown(s, SD_BOTH);
::closesocket(s);
closed = true;
}
return 0;
}
// +--------------------------------------------------------------------+
DWORD
NetSock::max_packet_size() const
{
DWORD size = 0;
int len = sizeof(size);
::getsockopt(s, SOL_SOCKET, 0x2003, (char*) &size, &len);
return size;
}
|