2017-09-22 12:27:21 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2018-01-21 11:39:19 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#if !defined(_WIN32) && !defined(_WIN64)
|
2017-09-22 12:27:21 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <poll.h>
|
2018-01-21 11:39:19 +08:00
|
|
|
#endif
|
2017-09-22 12:27:21 +08:00
|
|
|
#include "lib_acl.h"
|
|
|
|
#include "fiber/lib_fiber.h"
|
|
|
|
|
2018-01-21 11:39:19 +08:00
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
# define POLL WSAPoll
|
|
|
|
# define CLOSE acl_fiber_close
|
|
|
|
# define LISTEN acl_fiber_listen
|
|
|
|
# define ACCEPT acl_fiber_accept
|
|
|
|
# define snprintf _snprintf
|
|
|
|
#else
|
|
|
|
# define SOCKET int
|
|
|
|
# define INVALID_SOCKET -1
|
2018-01-21 15:41:19 +08:00
|
|
|
# define POLL poll
|
2018-01-21 11:39:19 +08:00
|
|
|
# define CLOSE close
|
|
|
|
# define LISTEN listen
|
|
|
|
# define ACCEPT accept
|
|
|
|
#endif
|
|
|
|
|
2017-09-22 12:27:21 +08:00
|
|
|
static int __nconnect = 0;
|
|
|
|
static int __count = 0;
|
2018-01-21 11:39:19 +08:00
|
|
|
static int __socket_count = 0;
|
2017-09-22 12:27:21 +08:00
|
|
|
static char __listen_ip[64];
|
|
|
|
static int __listen_port = 9001;
|
|
|
|
static int __listen_qlen = 64;
|
|
|
|
static int __rw_timeout = 0;
|
|
|
|
static int __echo_data = 1;
|
|
|
|
static int __stack_size = 32000;
|
|
|
|
|
|
|
|
static int check_read(int fd, int timeout)
|
|
|
|
{
|
|
|
|
struct pollfd pfd;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
memset(&pfd, 0, sizeof(struct pollfd));
|
|
|
|
pfd.fd = fd;
|
|
|
|
pfd.events = POLLIN;
|
|
|
|
|
2018-01-21 11:39:19 +08:00
|
|
|
n = POLL(&pfd, 1, timeout);
|
2017-09-22 12:27:21 +08:00
|
|
|
if (n < 0) {
|
2018-01-21 11:39:19 +08:00
|
|
|
printf("poll error: %s\r\n", acl_last_serror());
|
2017-09-22 12:27:21 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
return 0;
|
|
|
|
if (pfd.revents & POLLIN)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void echo_client(ACL_FIBER *fiber acl_unused, void *ctx)
|
|
|
|
{
|
2018-01-21 11:39:19 +08:00
|
|
|
SOCKET *cfd = (SOCKET *) ctx;
|
2017-09-22 12:27:21 +08:00
|
|
|
char buf[8192];
|
|
|
|
int ret;
|
|
|
|
|
2018-01-21 11:39:19 +08:00
|
|
|
__socket_count++;
|
2017-09-22 12:27:21 +08:00
|
|
|
printf("client fiber-%d: fd: %d\r\n", acl_fiber_self(), *cfd);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (__rw_timeout > 0) {
|
|
|
|
ret = check_read(*cfd, __rw_timeout * 1000);
|
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
if (ret == 0) {
|
2018-01-21 11:39:19 +08:00
|
|
|
printf("read timeout fd=%u\r\n", *cfd);
|
2017-09-22 12:27:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-21 11:39:19 +08:00
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
ret = acl_fiber_recv(*cfd, buf, sizeof(buf), 0);
|
|
|
|
#else
|
2017-09-22 12:27:21 +08:00
|
|
|
ret = read(*cfd, buf, sizeof(buf));
|
2018-01-21 11:39:19 +08:00
|
|
|
#endif
|
2017-09-22 12:27:21 +08:00
|
|
|
if (ret == 0) {
|
|
|
|
printf("read close by peer fd: %d, %s\r\n",
|
2018-01-21 11:39:19 +08:00
|
|
|
*cfd, acl_last_serror());
|
2017-09-22 12:27:21 +08:00
|
|
|
break;
|
|
|
|
} else if (ret < 0) {
|
2018-01-21 11:39:19 +08:00
|
|
|
if (acl_last_error() == EINTR) {
|
2017-09-22 12:27:21 +08:00
|
|
|
printf("catch a EINTR signal\r\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-01-21 11:39:19 +08:00
|
|
|
printf("read error %s, fd: %u\n", acl_last_serror(), *cfd);
|
2017-09-22 12:27:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
__count++;
|
|
|
|
|
|
|
|
if (!__echo_data)
|
|
|
|
continue;
|
|
|
|
|
2018-01-21 11:39:19 +08:00
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
if (acl_fiber_send(*cfd, buf, ret, 0) < 0) {
|
|
|
|
#else
|
2017-09-22 12:27:21 +08:00
|
|
|
if (write(*cfd, buf, ret) < 0) {
|
2018-01-21 11:39:19 +08:00
|
|
|
#endif
|
2017-09-22 12:27:21 +08:00
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
printf("write error, fd: %d\r\n", *cfd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-21 11:39:19 +08:00
|
|
|
__socket_count--;
|
|
|
|
printf("%s: close %d, socket_count=%d\r\n",
|
|
|
|
__FUNCTION__, *cfd, __socket_count);
|
|
|
|
CLOSE(*cfd);
|
2017-09-22 12:27:21 +08:00
|
|
|
free(cfd);
|
|
|
|
|
|
|
|
if (--__nconnect == 0) {
|
|
|
|
printf("\r\n----total read/write: %d----\r\n", __count);
|
|
|
|
__count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fiber_accept(ACL_FIBER *fiber acl_unused, void *ctx acl_unused)
|
|
|
|
{
|
2018-01-21 11:39:19 +08:00
|
|
|
SOCKET lfd;
|
|
|
|
int on = 1;
|
2017-09-22 12:27:21 +08:00
|
|
|
struct sockaddr_in sa;
|
|
|
|
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sin_family = AF_INET;
|
|
|
|
sa.sin_port = htons(__listen_port);
|
|
|
|
sa.sin_addr.s_addr = inet_addr(__listen_ip);
|
|
|
|
|
|
|
|
lfd = socket(AF_INET, SOCK_STREAM, 0);
|
2018-01-21 11:39:19 +08:00
|
|
|
if (lfd == INVALID_SOCKET)
|
2017-09-22 12:27:21 +08:00
|
|
|
abort();
|
|
|
|
|
2018-01-21 11:39:19 +08:00
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, (const char *) &on, sizeof(on))) {
|
|
|
|
#else
|
2017-09-22 12:27:21 +08:00
|
|
|
if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) {
|
2018-01-21 11:39:19 +08:00
|
|
|
#endif
|
|
|
|
printf("setsockopt error %s\r\n", acl_last_serror());
|
2017-09-22 12:27:21 +08:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bind(lfd, (struct sockaddr *) &sa, sizeof(struct sockaddr)) < 0) {
|
2018-01-21 11:39:19 +08:00
|
|
|
printf("bind error %s\r\n", acl_last_serror());
|
2017-09-22 12:27:21 +08:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2018-01-21 11:39:19 +08:00
|
|
|
if (LISTEN(lfd, 128) < 0) {
|
|
|
|
printf("listen error %s\r\n", acl_last_serror());
|
2017-09-22 12:27:21 +08:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("fiber-%d listen %s:%d ok\r\n",
|
|
|
|
acl_fiber_self(), __listen_ip, __listen_port);
|
|
|
|
|
|
|
|
for (;;) {
|
2018-01-21 11:39:19 +08:00
|
|
|
int len = sizeof(sa);
|
|
|
|
SOCKET *fd;
|
|
|
|
SOCKET cfd = ACCEPT(lfd, (struct sockaddr *)& sa, (socklen_t *) &len);
|
|
|
|
if (cfd == INVALID_SOCKET) {
|
|
|
|
printf("accept error %s\r\n", acl_last_serror());
|
2017-09-22 12:27:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-01-21 11:39:19 +08:00
|
|
|
fd = malloc(sizeof(SOCKET));
|
2017-09-22 12:27:21 +08:00
|
|
|
assert(fd != NULL);
|
|
|
|
*fd = cfd;
|
|
|
|
|
|
|
|
__nconnect++;
|
2018-01-21 11:39:19 +08:00
|
|
|
printf("accept one, fd: %u\r\n", cfd);
|
2017-09-22 12:27:21 +08:00
|
|
|
acl_fiber_create(echo_client, fd, __stack_size);
|
|
|
|
}
|
|
|
|
|
2018-01-21 11:39:19 +08:00
|
|
|
CLOSE(lfd);
|
2017-09-22 12:27:21 +08:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2018-01-28 17:11:57 +08:00
|
|
|
#define SCHEDULE_AUTO
|
|
|
|
|
|
|
|
#ifndef SCHEDULE_AUTO
|
2017-09-22 12:27:21 +08:00
|
|
|
static void fiber_memcheck(ACL_FIBER *fiber acl_unused, void *ctx acl_unused)
|
|
|
|
{
|
|
|
|
while (1) {
|
2018-01-21 11:39:19 +08:00
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
acl_fiber_delay(1000);
|
|
|
|
#else
|
2017-09-22 12:27:21 +08:00
|
|
|
sleep(1);
|
2018-01-21 11:39:19 +08:00
|
|
|
#endif
|
2017-09-22 12:27:21 +08:00
|
|
|
acl_default_meminfo();
|
|
|
|
}
|
|
|
|
}
|
2018-01-28 17:11:57 +08:00
|
|
|
#endif
|
2017-09-22 12:27:21 +08:00
|
|
|
|
|
|
|
static void usage(const char *procname)
|
|
|
|
{
|
|
|
|
printf("usage: %s -h [help]\r\n"
|
2018-01-01 22:50:11 +08:00
|
|
|
" -e event_mode [kernel|select|poll]\r\n"
|
|
|
|
" -s listen_ip\r\n"
|
|
|
|
" -p listen_port\r\n"
|
|
|
|
" -r rw_timeout\r\n"
|
|
|
|
" -q listen_queue\r\n"
|
|
|
|
" -z stack_size\r\n"
|
|
|
|
" -S [if using single IO, default: no]\r\n", procname);
|
2017-09-22 12:27:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2018-01-01 22:50:11 +08:00
|
|
|
int ch, event_mode = FIBER_EVENT_KERNEL;
|
2017-09-22 12:27:21 +08:00
|
|
|
|
|
|
|
snprintf(__listen_ip, sizeof(__listen_ip), "%s", "127.0.0.1");
|
|
|
|
|
2018-01-01 22:50:11 +08:00
|
|
|
while ((ch = getopt(argc, argv, "hs:p:r:q:Sz:e:")) > 0) {
|
2017-09-22 12:27:21 +08:00
|
|
|
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 'q':
|
|
|
|
__listen_qlen = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
__echo_data = 0;
|
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
__stack_size = atoi(optarg);
|
|
|
|
break;
|
2018-01-01 22:50:11 +08:00
|
|
|
case 'e':
|
|
|
|
if (strcasecmp(optarg, "select") == 0)
|
|
|
|
event_mode = FIBER_EVENT_SELECT;
|
|
|
|
else if (strcasecmp(optarg, "poll") == 0)
|
|
|
|
event_mode = FIBER_EVENT_POLL;
|
|
|
|
break;
|
2017-09-22 12:27:21 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-21 11:39:19 +08:00
|
|
|
#if !defined(_WIN32) && !defined(_WIN64)
|
2017-09-22 12:27:21 +08:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2018-01-21 11:39:19 +08:00
|
|
|
#endif
|
|
|
|
acl_lib_init();
|
2017-09-22 12:27:21 +08:00
|
|
|
acl_msg_stdout_enable(1);
|
2018-01-22 17:31:00 +08:00
|
|
|
acl_fiber_msg_stdout_enable(1);
|
2017-09-22 12:27:21 +08:00
|
|
|
|
2018-01-28 17:11:57 +08:00
|
|
|
#ifdef SCHEDULE_AUTO
|
|
|
|
acl_fiber_schedule_init(1);
|
|
|
|
acl_fiber_schedule_set_event(event_mode);
|
|
|
|
#endif
|
|
|
|
|
2017-09-22 12:27:21 +08:00
|
|
|
printf("%s: call fiber_creater\r\n", __FUNCTION__);
|
|
|
|
acl_fiber_create(fiber_accept, NULL, 32768);
|
|
|
|
|
2018-01-28 17:11:57 +08:00
|
|
|
#ifndef SCHEDULE_AUTO
|
|
|
|
acl_fiber_create(fiber_memcheck, NULL, 64000);
|
|
|
|
|
2017-09-22 12:27:21 +08:00
|
|
|
printf("call fiber_schedule\r\n");
|
2018-01-01 22:50:11 +08:00
|
|
|
acl_fiber_schedule_with(event_mode);
|
2018-01-28 17:11:57 +08:00
|
|
|
#endif
|
2017-09-22 12:27:21 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|