-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connection.cpp
326 lines (278 loc) · 10.4 KB
/
Connection.cpp
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
#include "Connection.hpp"
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cassert>
#include <cstring>
//NOTE: much of the sockets code herein is based on http-tweak's single-header http server
// see: https://github.com/ixchow/http-tweak
//Also, some help and examples for getaddrinfo from: https://beej.us/guide/bgnet/html/multi/syscalls.html
//---------------------------------
//Polling helper used by both server and client:
void poll_connections(
char const *where,
std::list< Connection > &connections,
std::function< void(Connection *, Connection::Event event) > const &on_event,
double timeout,
SOCKET listen_socket = INVALID_SOCKET) {
fd_set read_fds, write_fds;
FD_ZERO(&read_fds);
FD_ZERO(&write_fds);
int max = 0;
//add listen_socket to fd_set if needed:
if (listen_socket != INVALID_SOCKET) {
max = std::max(max, int(listen_socket));
FD_SET(listen_socket, &read_fds);
}
//add each connection's socket to read (and possibly write) sets:
for (auto c : connections) {
if (c.socket != INVALID_SOCKET) {
max = std::max(max, int(c.socket));
FD_SET(c.socket, &read_fds);
if (!c.send_buffer.empty()) {
FD_SET(c.socket, &write_fds);
}
}
}
{ //wait (until timeout) for sockets' data to become available:
struct timeval tv;
tv.tv_sec = std::lround(std::floor(timeout));
tv.tv_usec = std::lround((timeout - std::floor(timeout)) * 1e6);
//NOTE: on windows nfds is ignored -- https://msdn.microsoft.com/en-us/library/windows/desktop/ms740141(v=vs.85).aspx
int ret = select(max + 1, &read_fds, &write_fds, NULL, &tv);
if (ret < 0) {
std::cerr << "[" << where << "] Select returned an error; will attempt to read/write anyway." << std::endl;
} else if (ret == 0) {
//nothing to read or write.
return;
}
}
//add new connections as needed:
if (listen_socket != INVALID_SOCKET && FD_ISSET(listen_socket, &read_fds)) {
SOCKET got = accept(listen_socket, NULL, NULL);
if (got == INVALID_SOCKET) {
//oh well.
} else {
#ifdef _WIN32
unsigned long one = 1;
if (0 == ioctlsocket(got, FIONBIO, &one)) {
#else
{
#endif
connections.emplace_back();
connections.back().socket = got;
std::cerr << "[" << where << "] client connected on " << connections.back().socket << "." << std::endl; //INFO
if (on_event) on_event(&connections.back(), Connection::OnOpen);
}
}
}
const uint32_t BufferSize = 20000;
static thread_local uint8_t *buffer = new uint8_t[BufferSize];
//process requests:
for (auto &c : connections) {
//only read from valid sockets marked readable:
if (c.socket == INVALID_SOCKET || !FD_ISSET(c.socket, &read_fds)) continue;
ssize_t ret = recv(c.socket, buffer, BufferSize, MSG_DONTWAIT);
if (ret < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
//~no problem~ but no data
} else if (ret <= 0 || ret > (ssize_t)BufferSize) {
//~problem~ so remove connection
if (ret == 0) {
std::cerr << "[" << where << "] port closed, disconnecting." << std::endl;
} else if (ret < 0) {
std::cerr << "[" << where << "] recv() returned error " << errno << "(" << strerror(errno) << "), disconnecting." << std::endl;
} else {
std::cerr << "[" << where << "] recv() returned strange number of bytes, disconnecting." << std::endl;
}
c.close();
if (on_event) on_event(&c, Connection::OnClose);
} else { //ret > 0
c.recv_buffer.insert(c.recv_buffer.end(), buffer, buffer + ret);
if (on_event) on_event(&c, Connection::OnRecv);
}
}
//process responses:
for (auto &c : connections) {
//don't bother with connections unless they are valid, have something to send, and are marked writable:
if (c.socket == INVALID_SOCKET || c.send_buffer.empty() || !FD_ISSET(c.socket, &write_fds)) continue;
#ifdef _WIN32
ssize_t ret = send(c.socket, reinterpret_cast< char const * >(c.send_buffer.data()), int(c.send_buffer.size()), MSG_DONTWAIT);
#else
ssize_t ret = send(c.socket, reinterpret_cast< char const * >(c.send_buffer.data()), c.send_buffer.size(), MSG_DONTWAIT);
#endif
if (ret < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
//~no problem~, but don't keep trying
break;
} else if (ret <= 0 || ret > (ssize_t)c.send_buffer.size()) {
if (ret < 0) {
std::cerr << "[" << where << "] send() returned error " << errno << ", disconnecting." << std::endl;
} else { assert(ret == 0 || ret > (ssize_t)c.send_buffer.size());
std::cerr << "[" << where << "] send() returned strange number of bytes [" << ret << " of " << c.send_buffer.size() << "], disconnecting." << std::endl;
}
c.close();
if (on_event) on_event(&c, Connection::OnClose);
} else { //ret seems reasonable
c.send_buffer.erase(c.send_buffer.begin(), c.send_buffer.begin() + ret);
}
}
}
//---------------------------------
Server::Server(std::string const &port) {
#ifdef _WIN32
{ //init winsock:
WSADATA info;
if (WSAStartup((2 << 8) | 2, &info) != 0) {
throw std::runtime_error("WSAStartup failed.");
}
}
#endif
{ //use getaddrinfo to look up how to bind to port:
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
struct addrinfo *res = nullptr;
int ret = getaddrinfo(NULL, port.c_str(), &hints, &res);
if (ret != 0) {
throw std::runtime_error("getaddrinfo error: " + std::string(gai_strerror(ret)));
}
std::cout << "[Server::Server] binding to " << port << ":" << std::endl;
//based on example code in the 'man getaddrinfo' man page on OSX:
for (struct addrinfo *info = res; info != nullptr; info = info->ai_next) {
{ //DEBUG: dump info about this address:
std::cout << "\ttrying ";
char ip[INET6_ADDRSTRLEN];
if (info->ai_family == AF_INET) {
struct sockaddr_in *s = reinterpret_cast< struct sockaddr_in * >(info->ai_addr);
inet_ntop(res->ai_family, &s->sin_addr, ip, sizeof(ip));
std::cout << ip << ":" << ntohs(s->sin_port);
} else if (info->ai_family == AF_INET6) {
struct sockaddr_in6 *s = reinterpret_cast< struct sockaddr_in6 * >(info->ai_addr);
inet_ntop(res->ai_family, &s->sin6_addr, ip, sizeof(ip));
std::cout << ip << ":" << ntohs(s->sin6_port);
} else {
std::cout << "[unknown ai_family]";
}
std::cout << "... "; std::cout.flush();
}
SOCKET s = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
if (s == INVALID_SOCKET) {
std::cout << "(failed to create socket: " << strerror(errno) << ")" << std::endl;
continue;
}
{ //make it okay to reuse port:
#ifdef _WIN32
BOOL one = TRUE;
int ret = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast< const char * >(&one), sizeof(one));
#else
int one = 1;
int ret = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
#endif
if (ret != 0) {
std::cout << "[note: couldn't set SO_REUSEADDR] " << std::endl;
}
}
int ret = bind(s, info->ai_addr, int(info->ai_addrlen));
if (ret < 0) {
std::cout << "(failed to bind: " << strerror(errno) << ")" << std::endl;
continue;
}
std::cout << "success!" << std::endl;
listen_socket = s;
break;
}
freeaddrinfo(res);
}
if (listen_socket == INVALID_SOCKET) {
throw std::runtime_error("Failed to bind to port " + port);
}
{ //listen on socket
int ret = ::listen(listen_socket, 5);
if (ret < 0) {
closesocket(listen_socket);
throw std::system_error(errno, std::system_category(), "failed to listen on socket");
}
}
}
void Server::poll(std::function< void(Connection *, Connection::Event event) > const &on_event, double timeout) {
poll_connections("Server::poll", connections, on_event, timeout, listen_socket);
//reap closed clients:
for (auto connection = connections.begin(); connection != connections.end(); /*later*/) {
auto old = connection;
++connection;
if (old->socket == INVALID_SOCKET) {
connections.erase(old);
}
}
}
Connection* Client::connect(std::string const &host, std::string const &port) {
connections.emplace_back();
auto& connection = connections.back();
#ifdef _WIN32
{ //init winsock:
WSADATA info;
if (WSAStartup((2 << 8) | 2, &info) != 0) {
throw std::runtime_error("WSAStartup failed.");
}
}
#endif
{ //use getaddrinfo to look up how to bind to host/port:
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
struct addrinfo *res = nullptr;
int ret = getaddrinfo(host.c_str(), port.c_str(), &hints, &res);
if (ret != 0) {
throw std::runtime_error("getaddrinfo error: " + std::string(gai_strerror(ret)));
}
std::cout << "[Client::Client] connecting to " << host << ":" << port << ":" << std::endl;
//based on example code in the 'man getaddrinfo' man page on OSX:
for (struct addrinfo *info = res; info != nullptr; info = info->ai_next) {
{ //DEBUG: dump info about this address:
std::cout << "\ttrying ";
char ip[INET6_ADDRSTRLEN];
if (info->ai_family == AF_INET) {
struct sockaddr_in *s = reinterpret_cast< struct sockaddr_in * >(info->ai_addr);
inet_ntop(res->ai_family, &s->sin_addr, ip, sizeof(ip));
std::cout << ip << ":" << ntohs(s->sin_port);
} else if (info->ai_family == AF_INET6) {
struct sockaddr_in6 *s = reinterpret_cast< struct sockaddr_in6 * >(info->ai_addr);
inet_ntop(res->ai_family, &s->sin6_addr, ip, sizeof(ip));
std::cout << ip << ":" << ntohs(s->sin6_port);
} else {
std::cout << "[unknown ai_family]";
}
std::cout << "... "; std::cout.flush();
}
SOCKET s = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
if (s == INVALID_SOCKET) {
std::cout << "(failed to create socket: " << strerror(errno) << ")" << std::endl;
continue;
}
int ret = ::connect(s, info->ai_addr, int(info->ai_addrlen));
if (ret < 0) {
std::cout << "(failed to connect: " << strerror(errno) << ")" << std::endl;
continue;
}
std::cout << "success!" << std::endl;
connection.socket = s;
return &connection;
}
freeaddrinfo(res);
return nullptr;
}
}
void Client::poll(std::function< void(Connection *, Connection::Event event) > const &on_event, double timeout) {
poll_connections("Client::poll", connections, on_event, timeout, INVALID_SOCKET);
//reap closed clients:
for (auto connection = connections.begin(); connection != connections.end(); /*later*/) {
auto old = connection;
++connection;
if (old->socket == INVALID_SOCKET) {
connections.erase(old);
}
}
}