/
thread-honest.c
323 lines (242 loc) · 6.38 KB
/
thread-honest.c
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
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <ev.h>
#include <pthread.h>
#include "header.h"
#define LISTEN_BACKLOG 4096
#define SOCKET_TIMEOUT 60.0
__thread struct ev_loop *loop;
pthread_mutex_t accept_mtx = PTHREAD_MUTEX_INITIALIZER;
typedef struct echo_server
echo_server_t;
struct echo_server
{
ev_io wev_accept;
};
typedef struct echo_client
echo_client_t;
struct echo_client
{
struct sockaddr_in addr;
ev_io wev_recv;
ev_io wev_send;
ev_timer wev_timeout;
#define TO_SEND_SIZE 4096 * 4
char to_send [TO_SEND_SIZE];
char *to_send_beg;
size_t to_send_size;
};
int echo_client_add(echo_server_t *s, int sd, struct sockaddr_in *addr);
int echo_client_del(echo_client_t *c);
/* --- */
void echo_client_wcb_timeout(EV_P_ ev_timer *w, int tev)
{
echo_client_t *c = aux_memberof(echo_client_t, wev_timeout, w);
echo_client_del(c);
}
void echo_client_wcb_send(EV_P_ ev_io *w, int tev)
{
int sent_nb;
echo_client_t *c = aux_memberof(echo_client_t, wev_send, w);
sent_nb = aux_unix_send(w->fd, c->to_send_beg, c->to_send_size);
/* sent_nb = aux_unix_send(w->fd, OUTPUT, sizeof(OUTPUT) - 1); */
/* fprintf(stderr, "sent %d bytes (%d: %s)\n", sent_nb, errno, strerror(errno)); */
if (0 > sent_nb)
{
if (EAGAIN == errno)
{
return;
}
fprintf(stderr, "send error fd=%d (%d: %s)\n", w->fd, errno, strerror(errno));
echo_client_del(c);
return;
}
c->to_send_beg += sent_nb;
c->to_send_size -= sent_nb;
if (c->to_send_size > 0)
{
return;
}
ev_io_stop(loop, &c->wev_send);
}
void echo_client_wcb_recv(EV_P_ ev_io *w, int tev)
{
int nb;
echo_client_t *c = aux_memberof(echo_client_t, wev_recv, w);
int rem_size = c->to_send + TO_SEND_SIZE - (c->to_send_beg + c->to_send_size);
if (rem_size < 32)
{
/* fprintf(stderr, "rem_size=%d, c->to_send_size=%u, fd=%d\n", rem_size, (unsigned) c->to_send_size, w->fd); */
memcpy(c->to_send, c->to_send_beg, c->to_send_size);
c->to_send_beg = c->to_send;
rem_size = TO_SEND_SIZE - c->to_send_size;
/* fprintf(stderr, "rem_size=%d\n", rem_size); */
}
nb = aux_unix_recv(w->fd, c->to_send_beg + c->to_send_size, rem_size);
/* fprintf(stderr, "recv %d bytes (%d: %s) %.*s\n", nb, errno, strerror(errno), nb, buf); */
if (0 > nb)
{
if (EAGAIN == errno)
{
ev_timer_again(loop, &c->wev_timeout);
return;
}
fprintf(stderr, "recv error fd=%d (%d: %s)\n", w->fd, errno, strerror(errno));
echo_client_del(c);
return;
}
if (0 == nb)
{
/* fprintf(stderr, "recv done fd=%d\n", w->fd); */
/* echo_client_del(c); */
return;
}
c->to_send_size += nb;
int sent_nb;
sent_nb = aux_unix_send(w->fd, c->to_send_beg, c->to_send_size);
/* nb = aux_unix_send(w->fd, OUTPUT, sizeof(OUTPUT) - 1); */
/* fprintf(stderr, "sent %d bytes (%d: %s)\n", nb, errno, strerror(errno)); */
if (0 > sent_nb)
{
if (EAGAIN == errno)
{
ev_io_start(loop, &c->wev_send);
return;
}
fprintf(stderr, "send error fd=%d (%d: %s)\n", w->fd, errno, strerror(errno));
echo_client_del(c);
return;
}
c->to_send_beg += sent_nb;
c->to_send_size -= sent_nb;
if (c->to_send_size > 0)
{
ev_io_start(loop, &c->wev_send);
return;
}
#if 0
nb = aux_unix_send(w->fd, buf, nb);
/* fprintf(stderr, "sent %d bytes (%d: %s)\n", nb, errno, strerror(errno)); */
if (0 > nb)
{
fprintf(stderr, "send error fd=%d (%d: %s)\n", w->fd, errno, strerror(errno));
echo_client_del(c);
return;
}
ev_timer_again(loop, &c->wev_timeout);
#endif
}
int echo_client_add(echo_server_t *s, int sd, struct sockaddr_in *addr)
{
echo_client_t *c = malloc(sizeof(*c));
if (NULL == c) return -1;
c->addr.sin_family = addr->sin_family;
c->addr.sin_addr.s_addr = addr->sin_addr.s_addr;
c->addr.sin_port = addr->sin_port;
ev_io_init(&c->wev_recv, echo_client_wcb_recv, sd, EV_READ);
ev_io_init(&c->wev_send, echo_client_wcb_send, sd, EV_WRITE);
ev_timer_init(&c->wev_timeout, echo_client_wcb_timeout, 0, SOCKET_TIMEOUT);
ev_timer_again(loop, &c->wev_timeout);
ev_io_start(loop, &c->wev_recv);
c->to_send_beg = c->to_send;
c->to_send_size = 0;
return 0;
}
int echo_client_del(echo_client_t *c)
{
ev_io_stop(loop, &c->wev_recv);
ev_io_stop(loop, &c->wev_send);
ev_timer_stop(loop, &c->wev_timeout);
close(c->wev_recv.fd);
free(c);
return 0;
}
void echo_server_wcb_accept(EV_P_ ev_io *w, int tev)
{
int sd, rc;
struct sockaddr_in addr;
socklen_t addrlen;
echo_server_t *s = aux_memberof(echo_server_t, wev_accept, w);
addrlen = sizeof(addr);
pthread_mutex_lock(&accept_mtx);
sd = accept(w->fd, (struct sockaddr *) &addr, &addrlen);
pthread_mutex_unlock(&accept_mtx);
/* fprintf(stderr, "accept in thread=%d fd=%d (%d: %s)\n", (int) pthread_self(), sd, errno, strerror(errno)); */
if (0 > sd)
{
return;
}
if (0 > (rc = aux_set_nonblk(sd, 1)))
{
return;
}
#if 0
if (0 > (rc = aux_set_sckopt(sd, IPPROTO_TCP, TCP_NODELAY, 1)))
{
fprintf(stderr, "sockopt TCP_NODELAY error\n");
}
#endif
echo_client_add(s, sd, &addr);
}
int echo_socket_listen(const char *host, const char *port)
{
int sd, rc;
struct sockaddr_in addr;
sd = socket(AF_INET, SOCK_STREAM, 0);
if (0 > sd) return -1;
rc = aux_set_nonblk(sd, 1);
if (0 > rc) return -1;
rc = aux_set_sckopt(sd, SOL_SOCKET, SO_REUSEADDR, 1);
if (0 > rc) return -1;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(host);
addr.sin_port = htons(atoi(port));
rc = bind(sd, (struct sockaddr *) &addr, sizeof(addr));
if (0 > rc) return -1;
rc = listen(sd, LISTEN_BACKLOG);
if (0 > rc) return -1;
return sd;
}
void *thread_func(void *arg)
{
long sd = (long) arg;
loop = ev_loop_new(0);
echo_server_t s;
ev_io_init(&s.wev_accept, echo_server_wcb_accept, sd, EV_READ);
ev_io_start(loop, &s.wev_accept);
ev_run(loop, 0);
return NULL;
}
int main(int argc, char **argv)
{
long sd;
if (3 > argc)
{
fprintf(stderr, "Usage: %s host port\n", argv[0]);
return -1;
}
sd = echo_socket_listen(argv[1], argv[2]);
if (0 > sd) return -1;
/* --- */
int num_cores = (4 == argc) ? atoi(argv[3]) : sysconf(_SC_NPROCESSORS_ONLN);
fprintf(stderr, "found %d cores\n", num_cores);
int rc, i;
for (i = 0; i < num_cores - 1; ++i)
{
pthread_t thread_id;
rc = pthread_create(&thread_id, NULL, thread_func, (void *) sd);
if (0 != rc)
{
fprintf(stderr, "thread create error %d\n", rc);
return -1;
}
}
thread_func((void *) sd);
return 0;
}