-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.cpp
More file actions
336 lines (289 loc) · 8.85 KB
/
client.cpp
File metadata and controls
336 lines (289 loc) · 8.85 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
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
#include <set>
#include <array>
#include <atomic>
#include <vector>
#include <cstdio>
#include <thread>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <functional>
#include <poll.h>
#include <fcntl.h>
#include <netdb.h>
#include <unistd.h>
#include <pthread.h>
#include <stropts.h>
#include <sys/types.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "common.h"
class FDList {
public:
std::vector<int> fds;
~FDList() {
for(int fd: fds)
close(fd);
}
};
class FDCloser {
public:
int fd;
FDCloser(int _fd): fd(_fd) {}
~FDCloser() { close(fd); }
};
class PollRSelector: public RSelector {
protected:
std::vector<pollfd> fds;
std::vector<pollfd>::iterator current_free;
std::vector<pollfd>::iterator current_ready;
public:
PollRSelector(int fd_count) {
fds.resize(fd_count);
current_free = fds.begin();
current_ready = current_free;
std::memset(&fds[0], 0, sizeof(fds[0]) * fd_count);
}
bool add_fd(int sockfd) {
if (current_free == fds.end()) {
std::cerr << "no space left in fd pool\n";
return false;
}
current_free->fd = sockfd;
current_free->events = POLLIN;
++current_free;
return true;
}
bool wait(long int=-1) {
int rv = poll(&fds[0], current_free - fds.begin(), -1);
if (-1 == rv) {
std::perror("poll(fds, ..., -1) fails");
return false;
}
current_ready = fds.begin();
return true;
}
bool next(int & sockfd, uint32_t & flags) {
for(;fds.end() != current_ready; ++current_ready) {
if (0 == current_ready->revents or current_ready->fd == -1)
continue;
sockfd = current_ready->fd;
flags = current_ready->revents;
++current_ready;
return true;
}
return false;
}
void remove_current_ready() {
(current_ready - 1)->fd = -1;
}
};
const unsigned long NS_TO_S = 1000 * 1000 * 1000;
unsigned long time_ns() {
struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
return spec.tv_sec * NS_TO_S + spec.tv_nsec;
}
bool wait_for_conn(int sock_count,
std::vector<int> & sockets,
const char * ip,
const int port,
const int listen_queue,
void (*ready_for_connect)(),
std::function<void(int)> * on_sock_cb,
bool async=false)
{
(void)ip;
sockaddr_in server, client;
int master_sock = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == master_sock){
perror("Could not create socket");
return false;
}
FDCloser _master_sock(master_sock);
int enable = 1;
if (setsockopt(master_sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) < 0)
perror("setsockopt(SO_REUSEADDR) failed");
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(port);
if( 0 > bind(master_sock, (sockaddr *)&server , sizeof(server))) {
perror("bind failed. Error");
return 1;
}
listen(master_sock, listen_queue);
socklen_t sock_data_len = sizeof(client);
if (nullptr != ready_for_connect)
ready_for_connect();
for(int i = 0; i < sock_count; ++i){
int client_sock = accept(master_sock, (sockaddr *)&client, &sock_data_len);
if (client_sock < 0) {
perror("accept failed");
return false;
}
if(async) {
int flags = fcntl(client_sock, F_GETFL, 0);
if (flags < 0) {
std::perror("fcntl(client_sock, F_GETFL, 0)");
return false;
}
if (fcntl(client_sock, F_SETFL, flags | O_NONBLOCK) < 0) {
std::perror("fcntl(client_sock, F_SETFL, flags | O_NONBLOCK)");
return false;
}
}
sockets.push_back(client_sock);
if (nullptr != on_sock_cb) {
(*on_sock_cb)(client_sock);
}
}
return true;
}
bool process_message(int sockfd, const char * message, int message_len) {
char buffer[message_len];
int bc = recv(sockfd, buffer, message_len, 0);
if (0 > bc) {
if (ECONNRESET != errno)
std::perror("recv(sockfd, buffer.begin(), buffer.size(), 0)");
return false;
} else if (0 == bc) {
return false;
} else if (message_len != bc){
std::perror("partial message");
return false;
}
if (message_len != write(sockfd, message, message_len)) {
std::perror("write(sockfd, message, std::strlen(message))");
return false;
}
return true;
}
void th_func(int sockfd, const char * message, int msize) {
while(process_message(sockfd, message, msize));
}
extern "C"
int run_test_th(const char * ip,
const int port,
const int th_count,
int msize,
int listen_queue,
void (*ready_for_connect)(),
void (*preparation_done)(),
void (*test_done)())
{
char message[msize];
std::memset(message, 'X', msize);
FDList sockets;
std::vector<std::thread> threads;
std::function<void(int)> cb = [&](int sock){
threads.emplace_back(th_func, sock, &message[0], msize);
};
if (not wait_for_conn(th_count,
sockets.fds,
ip,
port,
listen_queue,
ready_for_connect,
&cb,
false))
return 1;
if (nullptr != preparation_done)
preparation_done();
for(auto & th: threads)
th.join();
if (nullptr != test_done)
test_done();
return 0;
}
int run_test(RSelector & selector,
const char * ip,
const int port,
const int th_count,
const int msize,
const int listen_queue,
void (*ready_for_connect)(),
void (*preparation_done)(),
void (*test_done)())
{
int fd_left = th_count;
char message[msize];
std::memset(message, 'X', msize);
FDList sockets;
if (not wait_for_conn(th_count, sockets.fds, ip, port, listen_queue, ready_for_connect, nullptr, false))
return 1;
for(int sockfd: sockets.fds)
if (not selector.add_fd(sockfd))
return 1;
if (nullptr != preparation_done)
preparation_done();
while(fd_left > 0) {
if (not selector.wait())
return 1;
uint32_t events;
int sockfd;
while(selector.next(sockfd, events)) {
bool close_sock = false;
if ((events & POLLHUP) or (events & POLLERR)) {
close_sock = true;
} else if (events & POLLNVAL) {
std::cerr << "Poll - POLLNVAL for fd " << sockfd;
std::cerr << " val " << events << "\n";
close_sock = true;
} else if (events & POLLIN) {
close_sock = not process_message(sockfd, message, msize);
} else if (0 != events) {
std::cerr << "Poll - ??? for fd " << sockfd;
std::cerr << " val " << events << "\n";
close_sock = true;
}
if (close_sock) {
selector.remove_current_ready();
--fd_left;
}
}
}
if (nullptr != test_done)
test_done();
return 0;
}
extern "C"
int run_test_epoll(const char * ip,
const int port,
const int th_count,
int msize,
int listen_queue,
void (*ready_for_connect)(),
void (*preparation_done)(),
void (*test_done)())
{
EPollRSelector eps(th_count);
if (not eps.ok())
return 1;
return run_test(eps, ip, port, th_count, msize,
listen_queue,
ready_for_connect, preparation_done, test_done);
}
extern "C"
int run_test_poll(const char * ip,
const int port,
const int th_count,
int msize,
int listen_queue,
void (*ready_for_connect)(),
void (*preparation_done)(),
void (*test_done)())
{
PollRSelector eps(th_count);
return run_test(eps, ip, port, th_count, msize, listen_queue, ready_for_connect, preparation_done, test_done);
}
extern "C"
int set_rr_prio() {
int policy;
struct sched_param param;
pthread_getschedparam(pthread_self(), &policy, ¶m);
param.sched_priority = sched_get_priority_max(SCHED_RR);
if ( 0 > pthread_setschedparam(pthread_self(), SCHED_RR, ¶m))
return 1;
return 0;
}