-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwin.cpp
More file actions
269 lines (225 loc) · 6.91 KB
/
win.cpp
File metadata and controls
269 lines (225 loc) · 6.91 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
#ifdef _WIN32
#include "sockportable.h"
#include <cstring>
#include <iostream>
#include <chrono>
#include <thread>
#include <cstring>
#include <WS2tcpip.h>
namespace sockets {
sockaddr_in buildAddress(const AddressIn& address);
AddressIn fillAddress(const sockaddr_in& addr);
OperationResult parseReturnValue(int value, int expectedValue);
void init()
{
WSADATA data;
WSAStartup(MAKEWORD(2, 2), &data);
}
Socket createSocket(int domain, int type, int protocol)
{
SOCKET sock = WSASocketW(domain, type, protocol, nullptr, 0, WSA_FLAG_OVERLAPPED);
Socket result;
result.isValid = sock != -1;
result.socket = sock;
result.type = type;
return result;
}
bool bindSocket(Socket& socket, const AddressIn& address)
{
sockaddr_in addr = buildAddress(address);
if (bind(socket.socket, (sockaddr*)&addr, sizeof(addr)) < 0)
return false;
socket.address = address;
return true;
}
bool listen(Socket& socket, int numberOfConnections)
{
if (::listen(socket.socket, numberOfConnections) < 0)
return false;
return true;
}
Socket accept(const Socket& socket, AddressIn& outputAddress)
{
sockaddr_in output;
size_t sz = sizeof(output);
Socket sock;
memset(&output, 0, sizeof(output));
SOCKET result = ::accept(socket.socket, (sockaddr*)&output, (int*)&sz);
outputAddress = fillAddress(output);
sock.isValid = true;
sock.socket = result;
sock.address = outputAddress;
return sock;
}
bool connect(const Socket& socket, const AddressIn& outputAddress)
{
sockaddr_in output = buildAddress(outputAddress);
size_t sz = sizeof(output);
Socket sock;
int result = ::connect(socket.socket, (sockaddr*)&output, (unsigned)sz);
return result != -1;
}
sockaddr_in buildAddress(const AddressIn& address)
{
sockaddr_in addr;
in_addr p;
long ip;
memset(&addr, 0, sizeof(addr));
inet_pton(address.domain, address.ip, &ip);
p.s_addr = ip;
addr.sin_family = address.domain;
addr.sin_port = htons(address.port);
addr.sin_addr = p;
return addr;
}
AddressIn fillAddress(const sockaddr_in& addr)
{
AddressIn result;
char buff[18] = { 0 };
result.domain = addr.sin_family;
std::string tmp = inet_ntop(AF_INET, (PVOID)&addr.sin_addr, buff, sizeof(buff));
memcpy(result.ip, tmp.c_str(), tmp.length());
result.port = ntohs(addr.sin_port);
return result;
}
void closeSocket(const Socket& socket)
{
shutdown(socket.socket, SD_BOTH);
closesocket(socket.socket);
}
bool connect(Socket& socket, AddressIn& outputAddress)
{
sockaddr_in output = buildAddress(outputAddress);
size_t sz = sizeof(output);
Socket sock;
int result = ::connect(socket.socket, (sockaddr*)&output, (unsigned)sz);
return result != -1;
}
bool isConnectionAlive(const Socket& socket, bool OOB)
{
char error = 0;
char buff = 0;
size_t sz = sizeof(error);
int err = 0, result = 0;
result = recv(socket.socket, &error, 1, MSG_PEEK);
err = WSAGetLastError();
if (result == -1 && (err != WSAETIMEDOUT && err != WSAEWOULDBLOCK))
return false;
getsockopt(socket.socket, SOL_SOCKET, SO_ERROR, &error, (int*)&sz);
return error == 0;
}
void setReadTimeout(const Socket& socket, int timeout)
{
int fixedTimeout = timeout * 1000;
setsockopt(socket.socket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&fixedTimeout, sizeof(DWORD));
}
void setWriteTimeout(const Socket& socket, int timeout)
{
int fixedTimeout = timeout * 1000;
setsockopt(socket.socket, SOL_SOCKET, SO_SNDTIMEO, (const char*)&fixedTimeout, sizeof(DWORD));
}
unsigned getSendBuffSize(const Socket& socket)
{
unsigned result;
size_t sz = sizeof(result);
getsockopt(socket.socket, SOL_SOCKET, SO_SNDBUF, (char*)&result, (int*)&sz);
return result;
}
void setSendBuffSize(const Socket& socket, unsigned size)
{
setsockopt(socket.socket, SOL_SOCKET, SO_SNDBUF, (const char*)&size, (int)sizeof(size));
}
unsigned getRecieveBuffSize(const Socket& socket)
{
unsigned result;
size_t sz = sizeof(result);
getsockopt(socket.socket, SOL_SOCKET, SO_RCVBUF, (char*)&result, (int*)&sz);
return result;
}
void setRecieveBuffSize(const Socket& socket, unsigned size)
{
setsockopt(socket.socket, SOL_SOCKET, SO_RCVBUF, (const char*)&size, (int)sizeof(size));
}
void enableKeepAlive(Socket& socket)
{
BOOL val = TRUE;
setsockopt(socket.socket, SOL_SOCKET, SO_KEEPALIVE, (const char*)&val, (int)sizeof(val));
}
OperationResult getData(const Socket& socket, Buffer& buff, size_t size, bool peek)
{
if (buff.getSize() < size)
return OperationResult::Error;
int result = recv(socket.socket, (char*)buff.getWritePointer(), size, peek ? MSG_PEEK : 0);
if (result > 0)
buff.setWriteOffset(result + buff.getWriteOffset());
return parseReturnValue(result, size);
}
OperationResult getDataFrom(const Socket& socket, Buffer& buff, size_t size, AddressIn& addr, bool peek)
{
if (buff.getSize() < size)
return OperationResult::Error;
sockaddr_in ad;
size_t sz = sizeof(ad);
memset(&ad, 0, sz);
int result = recvfrom(socket.socket, (char*)buff.getWritePointer(), size, peek ? MSG_PEEK : 0, (sockaddr*)&ad, (int*)&sz);
OperationResult retVal = parseReturnValue(result, size);
if (result > 0)
{
addr = fillAddress(ad);
buff.setWriteOffset(result + buff.getWriteOffset());
}
else if (result < 0 && retVal == OperationResult::Success)
{
addr = fillAddress(ad);
buff.setWriteOffset(size + buff.getWriteOffset());
}
return retVal;
}
OperationResult sendData(const Socket& socket, Buffer& data, size_t size)
{
if (data.getSize() < size)
return OperationResult::Error;
int result = send(socket.socket, (char*)data.getReadPointer(), size, 0);
if (result > 0)
data.setReadOffset(result + data.getReadOffset());
return parseReturnValue(result, size);
}
OperationResult sendDataTo(const Socket &socket, Buffer &data, size_t size, const AddressIn &addr)
{
if (data.getSize() < size)
return OperationResult::Error;
sockaddr_in ad = buildAddress(addr);
int result = sendto(socket.socket, (char*)data.getReadPointer(), size, 0, (sockaddr*)&ad, sizeof(ad));
if (result > 0)
data.setReadOffset(result + data.getReadOffset());
return parseReturnValue(result, size);
}
OperationResult parseReturnValue(int value, int expectedValue)
{
if (value > 0)
{
if (value != expectedValue)
return OperationResult::PartiallyFinished;
return OperationResult::Success;
}
if (value == 0)
return OperationResult::ConnectionClosed;
int err = WSAGetLastError();
switch (err)
{
case WSAEWOULDBLOCK:
case WSAETIMEDOUT:
return OperationResult::Timeout;
case WSAEMSGSIZE:
return OperationResult::Success;
default:
break;
}
return OperationResult::Error;
}
void end()
{
WSACleanup();
}
}
#endif