2019-07-28 10:31:56 +08:00
|
|
|
#include "lib_acl.h"
|
2018-11-30 14:38:22 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "fiber/libfiber.h"
|
|
|
|
|
2022-10-27 18:14:15 +08:00
|
|
|
static int __event_type = FIBER_EVENT_KERNEL;
|
2022-05-22 13:10:56 +08:00
|
|
|
static int __stack_size = 128000;
|
|
|
|
static int __shared_stack = 0;
|
2018-11-30 14:38:22 +08:00
|
|
|
static int __rw_timeout = 0;
|
|
|
|
|
|
|
|
static int http_client(ACL_VSTREAM *cstream, const char* res, size_t len)
|
|
|
|
{
|
|
|
|
char buf[8192];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
cstream->rw_timeout = __rw_timeout;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
ret = acl_vstream_gets(cstream, buf, sizeof(buf) - 1);
|
|
|
|
if (ret == ACL_VSTREAM_EOF) {
|
|
|
|
printf("gets error: %s\r\n", acl_last_serror());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[ret] = 0;
|
|
|
|
|
|
|
|
if (strcmp(buf, "\r\n") == 0 || strcmp(buf, "\n") == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (acl_vstream_writen(cstream, res, len) == ACL_VSTREAM_EOF) {
|
|
|
|
printf("write error\r\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void echo_client(ACL_FIBER *fiber acl_unused, void *ctx)
|
|
|
|
{
|
|
|
|
ACL_VSTREAM *cstream = (ACL_VSTREAM *) ctx;
|
|
|
|
const char* res = "HTTP/1.1 200 OK\r\n"
|
|
|
|
"Date: Tue, 31 May 2016 14:20:28 GMT\r\n"
|
|
|
|
"Content-Length: 12\r\n"
|
|
|
|
"Connection: Keep-Alive\r\n"
|
|
|
|
"\r\n"
|
|
|
|
"hello world!";
|
|
|
|
size_t len = strlen(res);
|
|
|
|
|
|
|
|
while (1) {
|
2022-05-22 13:10:56 +08:00
|
|
|
if (http_client(cstream, res, len) < 0) {
|
2018-11-30 14:38:22 +08:00
|
|
|
break;
|
2022-05-22 13:10:56 +08:00
|
|
|
}
|
2018-11-30 14:38:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
acl_vstream_close(cstream);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fiber_accept(ACL_FIBER *fiber acl_unused, void *ctx)
|
|
|
|
{
|
|
|
|
ACL_VSTREAM *sstream = (ACL_VSTREAM *) ctx;
|
|
|
|
int fd;
|
2022-05-22 13:10:56 +08:00
|
|
|
ACL_FIBER_ATTR attr;
|
|
|
|
|
|
|
|
acl_fiber_attr_init(&attr);
|
|
|
|
acl_fiber_attr_setstacksize(&attr, __stack_size);
|
|
|
|
acl_fiber_attr_setsharestack(&attr, __shared_stack);
|
2018-11-30 14:38:22 +08:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
ACL_VSTREAM *cstream = acl_vstream_accept(sstream, NULL, 0);
|
|
|
|
if (cstream == NULL) {
|
|
|
|
printf("acl_vstream_accept error %s\r\n",
|
|
|
|
acl_last_serror());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = ACL_VSTREAM_SOCK(cstream);
|
2022-05-22 13:10:56 +08:00
|
|
|
acl_fiber_create2(&attr, echo_client, cstream);
|
2018-11-30 14:38:22 +08:00
|
|
|
printf("accept one over: %d\r\n", fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
acl_vstream_close(sstream);
|
|
|
|
}
|
|
|
|
|
2019-03-18 17:03:23 +08:00
|
|
|
static void* thread_main(void *ctx)
|
2018-11-30 14:38:22 +08:00
|
|
|
{
|
2019-03-18 17:03:23 +08:00
|
|
|
ACL_VSTREAM *sstream = (ACL_VSTREAM *) ctx;
|
|
|
|
|
2022-05-22 13:10:56 +08:00
|
|
|
|
2019-03-18 17:03:23 +08:00
|
|
|
printf("%s: call fiber_creater\r\n", __FUNCTION__);
|
2022-05-22 13:10:56 +08:00
|
|
|
acl_fiber_create(fiber_accept, sstream, 128000);
|
2019-03-18 17:03:23 +08:00
|
|
|
|
|
|
|
printf("call fiber_schedule\r\n");
|
2022-10-27 18:14:15 +08:00
|
|
|
acl_fiber_schedule_with(__event_type);
|
2019-03-18 17:03:23 +08:00
|
|
|
return NULL;
|
2018-11-30 14:38:22 +08:00
|
|
|
}
|
|
|
|
|
2019-03-18 17:03:23 +08:00
|
|
|
static void usage(const char *procname)
|
2018-11-30 14:38:22 +08:00
|
|
|
{
|
2019-03-18 17:03:23 +08:00
|
|
|
printf("usage: %s -h [help]\r\n"
|
|
|
|
" -s listen_addr\r\n"
|
2022-10-27 18:14:15 +08:00
|
|
|
" -e event_type[kernel|io_uring|select|poll]\r\n"
|
2019-03-18 17:03:23 +08:00
|
|
|
" -t max_threads\r\n"
|
2022-05-22 13:10:56 +08:00
|
|
|
" -r rw_timeout\r\n"
|
|
|
|
" -z stack_size[default: 128000]\r\n"
|
|
|
|
" -S [if use shared stack]\r\n",
|
|
|
|
procname);
|
2018-11-30 14:38:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char addr[64];
|
|
|
|
ACL_VSTREAM *sstream;
|
2019-03-18 17:03:23 +08:00
|
|
|
int ch, nthreads = 1, i;
|
|
|
|
ACL_ARRAY *a = acl_array_create(10);
|
|
|
|
ACL_ITER iter;
|
2018-11-30 14:38:22 +08:00
|
|
|
|
|
|
|
snprintf(addr, sizeof(addr), "%s", "127.0.0.1:9001");
|
|
|
|
|
2022-10-27 18:14:15 +08:00
|
|
|
while ((ch = getopt(argc, argv, "hs:r:t:z:Se:")) > 0) {
|
2018-11-30 14:38:22 +08:00
|
|
|
switch (ch) {
|
|
|
|
case 'h':
|
|
|
|
usage(argv[0]);
|
|
|
|
return 0;
|
|
|
|
case 's':
|
|
|
|
snprintf(addr, sizeof(addr), "%s", optarg);
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
__rw_timeout = atoi(optarg);
|
|
|
|
break;
|
2022-10-27 18:14:15 +08:00
|
|
|
case 'e':
|
|
|
|
if (strcasecmp(optarg, "poll") == 0) {
|
|
|
|
__event_type = FIBER_EVENT_POLL;
|
|
|
|
} else if (strcasecmp(optarg, "select") == 0) {
|
|
|
|
__event_type = FIBER_EVENT_SELECT;
|
|
|
|
} else if (strcasecmp(optarg, "io_uring") == 0) {
|
|
|
|
__event_type = FIBER_EVENT_IO_URING;
|
|
|
|
}
|
|
|
|
break;
|
2019-03-18 17:03:23 +08:00
|
|
|
case 't':
|
|
|
|
nthreads = atoi(optarg);
|
|
|
|
break;
|
2022-05-22 13:10:56 +08:00
|
|
|
case 'z':
|
|
|
|
__stack_size = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
__shared_stack = 1;
|
|
|
|
break;
|
2018-11-30 14:38:22 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sstream = acl_vstream_listen(addr, 128);
|
|
|
|
if (sstream == NULL) {
|
|
|
|
printf("acl_vstream_listen error %s\r\n", acl_last_serror());
|
|
|
|
return 1;
|
|
|
|
}
|
2022-05-22 13:10:56 +08:00
|
|
|
|
|
|
|
printf("listen %s ok, ACL_VSTREAM's size=%zd\r\n", addr, sizeof(ACL_VSTREAM));
|
2018-11-30 14:38:22 +08:00
|
|
|
|
2019-03-18 17:03:23 +08:00
|
|
|
for (i = 0; i < nthreads; i++) {
|
|
|
|
acl_pthread_t* tid = acl_mymalloc(sizeof(acl_pthread_t));
|
|
|
|
if (acl_pthread_create(tid, NULL, thread_main, sstream) != 0) {
|
|
|
|
printf("create thread failed!\r\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2018-11-30 14:38:22 +08:00
|
|
|
|
2019-03-18 17:03:23 +08:00
|
|
|
acl_array_append(a, tid);
|
|
|
|
}
|
|
|
|
|
|
|
|
acl_foreach(iter, a) {
|
|
|
|
acl_pthread_t *tid = (acl_pthread_t *) iter.data;
|
|
|
|
if (acl_pthread_join(*tid, NULL) != 0) {
|
|
|
|
printf("pthread join failed!\r\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
acl_myfree(tid);
|
|
|
|
}
|
2018-11-30 14:38:22 +08:00
|
|
|
|
2019-03-18 17:03:23 +08:00
|
|
|
acl_array_free(a, NULL);
|
2018-11-30 14:38:22 +08:00
|
|
|
return 0;
|
|
|
|
}
|