-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathmain.c
268 lines (227 loc) · 5.71 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <fiber/libfiber.h>
#include "../patch.h"
static int __nconnect = 0;
static int __count = 0;
static int __socket_count = 0;
static char __listen_ip[64];
static int __listen_port = 9001;
static int __listen_qlen = 64;
static int __rw_timeout = 0;
static int __use_setsockopt = 0;
static int __echo_data = 1;
static int __stack_size = 128000;
static int check_read(SOCKET fd, int timeout)
{
struct pollfd pfd;
int n;
memset(&pfd, 0, sizeof(struct pollfd));
pfd.fd = fd;
pfd.events = POLLIN;
n = acl_fiber_poll(&pfd, 1, timeout);
if (n < 0) {
printf("poll error: %s\r\n", strerror(errno));
return -1;
}
if (n == 0) {
return 0;
}
if (pfd.revents & POLLIN) {
return 1;
} else {
return 0;
}
}
static int set_timeout(SOCKET fd, int opt, int timeo)
{
# if defined(_WIN32) || defined(_WIN64)
timeout *= 1000; // From seconds to millisecond.
if (setsockopt(fd, SOL_SOCKET, opt, (const char*) &timeo, sizeof(timeo)) < 0) {
printf("setsockopt error=%s, timeout=%d, opt=%d, fd=%d\r\n",
strerror(errno), timeo, opt, (int) fd);
return -1;
}
# else // Must be Linux or __APPLE__.
struct timeval tm;
tm.tv_sec = timeo;
tm.tv_usec = 0;
if (setsockopt(fd, SOL_SOCKET, opt, &tm, sizeof(tm)) < 0) {
printf("setsockopt error=%s, timeout=%d, opt=%d, fd=%d\r\n",
strerror(errno), timeo, opt, (int) fd);
return -1;
}
# endif
return 0;
}
static int set_read_timeout(SOCKET fd, int timeo)
{
return set_timeout(fd, SO_RCVTIMEO, timeo);
}
static void echo_client(ACL_FIBER *fiber, void *ctx)
{
SOCKET *pfd = (SOCKET *) ctx;
SOCKET fd = *pfd;
char buf[8192];
int ret;
if (__rw_timeout > 0 && __use_setsockopt) {
if (set_read_timeout(fd, __rw_timeout) == -1) {
printf("set_read_timeout error=%s, fd=%d\r\n",
strerror(errno), (int) fd);
} else {
printf("set_read_timeout ok, fd=%d\r\n", (int) fd);
}
}
(void) fiber;
__socket_count++;
printf("client fiber-%d: fd: %d\r\n", acl_fiber_self(), fd);
while (1) {
if (__rw_timeout > 0 && !__use_setsockopt) {
ret = check_read(fd, __rw_timeout * 1000);
if (ret < 0)
break;
if (ret == 0) {
printf("read timeout fd=%u\r\n", fd);
break;
}
}
ret = acl_fiber_recv(fd, buf, sizeof(buf) - 1, 0);
if (ret == 0) {
printf("read close by peer fd: %d, %s\r\n",
fd, strerror(errno));
break;
} else if (ret < 0) {
if (errno == EINTR) {
printf("catch a EINTR signal\r\n");
continue;
}
printf("read error %s, fd: %u\n", strerror(errno), fd);
break;
}
// buf[ret] = 0; printf("buf=%s\r\n", buf);
__count++;
if (!__echo_data)
continue;
if (acl_fiber_send(fd, buf, ret, 0) < 0) {
if (errno == EINTR)
continue;
printf("write error, fd: %d\r\n", fd);
break;
}
}
__socket_count--;
printf("%s: close %d, socket_count=%d\r\n",
__FUNCTION__, fd, __socket_count);
socket_close(fd);
free(pfd);
if (--__nconnect == 0) {
printf("\r\n----total read/write: %d----\r\n", __count);
__count = 0;
}
}
static void fiber_accept(ACL_FIBER *fiber, void *ctx)
{
SOCKET lfd = socket_listen(__listen_ip, __listen_port);
(void) fiber;
(void) ctx;
printf("fiber-%d listen %s:%d ok\r\n",
acl_fiber_self(), __listen_ip, __listen_port);
for (;;) {
SOCKET *pfd;
SOCKET cfd = socket_accept(lfd);
if (cfd == INVALID_SOCKET) {
printf("accept error %s\r\n", strerror(errno));
break;
}
pfd = malloc(sizeof(SOCKET));
assert(pfd != NULL);
*pfd = cfd;
__nconnect++;
printf("accept one, fd: %u, %p\r\n", cfd, pfd);
acl_fiber_create(echo_client, pfd, __stack_size);
}
socket_close(lfd);
exit(0);
}
//#define SCHEDULE_AUTO
#ifndef SCHEDULE_AUTO
static void fiber_memcheck(ACL_FIBER *fiber, void *ctx)
{
(void) fiber;
(void) ctx;
while (1) {
acl_fiber_delay(1000);
}
}
#endif
static void usage(const char *procname)
{
printf("usage: %s -h [help]\r\n"
" -e event_mode [kernel|select|poll]\r\n"
" -s listen_ip\r\n"
" -p listen_port\r\n"
" -r rw_timeout\r\n"
" -O [if using setsockopt to set IO timeout, default: false]\r\n"
" -q listen_queue\r\n"
" -z stack_size\r\n"
" -S [if using single IO, default: no]\r\n", procname);
}
int main(int argc, char *argv[])
{
int ch, event_mode = FIBER_EVENT_KERNEL;
snprintf(__listen_ip, sizeof(__listen_ip), "%s", "127.0.0.1");
socket_init();
while ((ch = getopt(argc, argv, "hs:p:r:Oq:Sz:e:")) > 0) {
switch (ch) {
case 'h':
usage(argv[0]);
return 0;
case 's':
snprintf(__listen_ip, sizeof(__listen_ip), "%s", optarg);
break;
case 'p':
__listen_port = atoi(optarg);
break;
case 'r':
__rw_timeout = atoi(optarg);
break;
case 'O':
__use_setsockopt = 1;
break;
case 'q':
__listen_qlen = atoi(optarg);
break;
case 'S':
__echo_data = 0;
break;
case 'z':
__stack_size = atoi(optarg);
break;
case 'e':
if (strcmp(optarg, "select") == 0)
event_mode = FIBER_EVENT_SELECT;
else if (strcmp(optarg, "poll") == 0)
event_mode = FIBER_EVENT_POLL;
break;
default:
break;
}
}
acl_fiber_msg_stdout_enable(1);
#ifdef SCHEDULE_AUTO
acl_fiber_schedule_init(1);
acl_fiber_schedule_set_event(event_mode);
#endif
printf("%s: call fiber_creater\r\n", __FUNCTION__);
acl_fiber_create(fiber_accept, NULL, __stack_size);
#ifndef SCHEDULE_AUTO
acl_fiber_create(fiber_memcheck, NULL, __stack_size);
printf("call fiber_schedule\r\n");
acl_fiber_schedule_with(event_mode);
#endif
socket_end();
return 0;
}