-
Notifications
You must be signed in to change notification settings - Fork 93
/
sockets.cpp
382 lines (341 loc) · 11.4 KB
/
sockets.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include "fastcgi++/log.hpp"
#include "fastcgi++/sockets.hpp"
#include <vector>
#include <map>
#include <random>
#include <algorithm>
#include <iterator>
#include <mutex>
#include <thread>
#include <string>
#include <condition_variable>
const unsigned int chunkSize=1024;
const unsigned int tranCount=768;
const unsigned int socketCount=512;
const unsigned int maxConc=64;
const unsigned int seed=2006;
std::string port;
bool done;
std::mutex doneMutex;
Fastcgipp::SocketGroup* serverGroup;
void client()
{
Fastcgipp::SocketGroup group;
struct Buffer
{
std::vector<char> buffer;
std::vector<char> data;
std::vector<char>::const_iterator send;
std::vector<char>::iterator receive;
int count;
};
std::map<Fastcgipp::Socket, Buffer> buffers;
unsigned int maxSockets=0;
unsigned int sends=0;
std::mt19937 rd(seed);
std::mt19937_64 rd_64(seed);
std::bernoulli_distribution boolDist(0.75);
unsigned int state;
while(maxSockets<socketCount || buffers.size())
{
// Any data waiting for us to recieve?
do
{
const Fastcgipp::Socket socket=group.poll(false);
if(socket.valid())
{
auto pair = buffers.find(socket);
if(pair == buffers.end())
FAIL_LOG("Got a valid socket client side that isn't "\
"accounted for")
auto& buffer = pair->second;
const ssize_t read = socket.read(
&*buffer.receive,
buffer.buffer.end()-buffer.receive);
if(read<0)
{
if(!(buffer.receive == buffer.buffer.begin() &&
(buffer.send == buffer.data.cend() ||
buffer.send == buffer.data.cbegin())))
FAIL_LOG("Socket killed when it's not done echoing")
if(buffer.send == buffer.data.cend())
--sends;
buffers.erase(pair);
continue;
}
if(buffer.send != buffer.data.cend())
FAIL_LOG("Poll gave us a socket we are sending on")
buffer.receive += read;
if(buffer.receive == buffer.buffer.end())
{
if(buffer.data != buffer.buffer)
FAIL_LOG("Echoed data does not match that which was "\
"sent")
++buffer.count;
if(buffer.count == tranCount)
{
socket.close();
buffers.erase(pair);
}
else
{
++sends;
buffer.send = buffer.data.cbegin();
buffer.receive = buffer.buffer.begin();
for(
auto i = reinterpret_cast<uint64_t*>(
&*buffer.data.begin());
i < reinterpret_cast<uint64_t*>(
&*buffer.data.end());
++i)
*i = rd_64();
}
}
}
else
break;
} while(boolDist(rd));
// Check for dirty sockets
for(auto buffer=buffers.cbegin(); buffer != buffers.cend(); ++buffer)
if(!buffer->first.valid())
FAIL_LOG("We've got a dirty socket client side")
const unsigned int connects = std::min(
socketCount-maxSockets,
unsigned(maxConc-buffers.size()));
// Do we initiate a connection, send data or wait?
if(connects>0 || sends>0)
{
std::discrete_distribution<> dist({
double(connects),
double(sends)});
state=dist(rd);
}
else
state=2;
switch(state)
{
case 0:
{
unsigned int count = connects;
do
{
const Fastcgipp::Socket socket =
group.connect("127.0.0.1", port.c_str());
if(!socket.valid())
FAIL_LOG("Unable to connect to server")
Buffer& buffer(buffers[socket]);
buffer.buffer.resize(chunkSize);
buffer.data.resize(chunkSize);
for(
auto i = reinterpret_cast<uint64_t*>(
&*buffer.data.begin());
i < reinterpret_cast<uint64_t*>(
&*buffer.data.end());
++i)
*i = rd_64();
buffer.send = buffer.data.cbegin();
buffer.receive = buffer.buffer.begin();
buffer.count = 0;
++maxSockets;
++sends;
} while(boolDist(rd) && --count>0);
break;
}
case 1:
{
unsigned int count=sends;
do
{
auto pair = buffers.begin();
while(pair != buffers.end() && (!pair->first.valid() ||
pair->second.data.end() == pair->second.send))
++pair;
if(pair == buffers.end())
break;
const Fastcgipp::Socket& socket = pair->first;
Buffer& buffer = pair->second;
const ssize_t sent = socket.write(
&*buffer.send,
buffer.data.end()-buffer.send);
if(sent < 0)
break;
else if(sent == 0)
FAIL_LOG("We failed sending data!")
buffer.send += sent;
if(buffer.send == buffer.data.end())
--sends;
} while(boolDist(rd) && --count>0);
break;
}
default:
if(maxSockets<socketCount || buffers.size())
group.poll(true);
break;
}
}
if(group.size())
FAIL_LOG("Client has active sockets when it shouldn't")
std::lock_guard<std::mutex> lock(doneMutex);
done=true;
serverGroup->wake();
}
std::condition_variable cv;
std::mutex cvMutex;
void server()
{
std::unique_lock<std::mutex> cvLock(cvMutex);
struct Buffer
{
std::vector<char> data;
std::vector<char>::iterator position;
bool sending;
};
Fastcgipp::SocketGroup group;
serverGroup = &group;
if(!group.listen("127.0.0.1", port.c_str()))
FAIL_LOG("Unable to listen")
cv.notify_all();
cvLock.unlock();
std::map<Fastcgipp::Socket, Buffer> buffers;
std::unique_lock<std::mutex> lock(doneMutex);
bool flushed;
unsigned int sends=0;
std::mt19937 rd(seed);
std::bernoulli_distribution killSocket(0.0001);
while(!done)
{
lock.unlock();
// Transmit data
flushed = true;
for(auto it=buffers.begin(); it!=buffers.end();)
{
const Fastcgipp::Socket& socket = it->first;
Buffer& buffer = it->second;
if(socket.valid() && buffer.sending)
{
const ssize_t sent = socket.write(
&*buffer.position,
buffer.data.end()-buffer.position);
if(sent>0)
{
buffer.position += sent;
if(buffer.position == buffer.data.end())
{
buffer.sending = false;
buffer.position = buffer.data.begin();
--sends;
if(killSocket(rd))
{
socket.close();
it=buffers.erase(it);
continue;
}
}
else
flushed = false;
}
}
++it;
}
// Any data waiting for us to receive?
{
const Fastcgipp::Socket socket = group.poll(flushed);
if(socket.valid())
{
auto pair = buffers.find(socket);
if(pair == buffers.end())
{
pair = buffers.insert(std::make_pair(
socket,
Buffer())).first;
pair->second.data.resize(chunkSize);
pair->second.position = pair->second.data.begin();
}
Buffer& buffer = pair->second;
if(buffer.sending)
FAIL_LOG("Got a valid socket for reception that is now "\
"in sending mode")
const size_t desired = buffer.data.end()-buffer.position;
if(desired == 0)
FAIL_LOG("Trying to read zero bytes on server")
const ssize_t read = socket.read(
&*buffer.position,
desired);
if(read<0)
{
buffers.erase(pair);
lock.lock();
continue;
}
buffer.position += read;
if(buffer.position == buffer.data.end())
{
buffer.position = buffer.data.begin();
buffer.sending = true;
++sends;
}
}
}
lock.lock();
}
lock.unlock();
if(group.size())
FAIL_LOG("Server has active sockets when it shouldn't")
}
#include "fastcgi++/config.hpp"
#if defined FASTCGIPP_UNIX || defined FASTCGIPP_LINUX
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
#include <sstream>
#include <sys/stat.h>
#include <fcntl.h>
unsigned int openfds()
{
std::ostringstream ss;
ss << "/proc/" << getpid() << "/fd";
DIR* const directory = opendir(ss.str().c_str());
dirent* file;
unsigned int count = 0;
char path[1024];
ss << '/';
while((file = readdir(directory)) != nullptr) {
++count;
auto c = readlink((ss.str() + file->d_name).c_str(), path, sizeof(path) - 1);
if(c > 0) {
path[c] = 0;
INFO_LOG("fd " << file->d_name << " -> " << path);
} else {
INFO_LOG("fd " << file->d_name)
}
}
closedir(directory);
return count;
}
#elif
unsigned int openfds()
{
return 0;
}
#endif
int main()
{
const auto initialFds = openfds();
{
std::random_device trueRand;
std::uniform_int_distribution<> portDist(2048, 65534);
port = std::to_string(portDist(trueRand));
}
done=false;
std::thread serverThread(server);
{
std::unique_lock<std::mutex> cvLock(cvMutex);
cv.wait(cvLock);
}
client();
serverThread.join();
if(openfds() != initialFds)
FAIL_LOG("There are leftover file descriptors after they should all "\
"have been closed");
return 0;
}