2019-07-28 10:31:56 +08:00
|
|
|
#include "stdafx.h"
|
2018-11-30 14:38:22 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2022-03-26 14:18:31 +08:00
|
|
|
#include <string.h>
|
2018-11-30 14:38:22 +08:00
|
|
|
|
|
|
|
static void fiber_client(acl::socket_stream* conn)
|
|
|
|
{
|
2022-03-26 14:18:31 +08:00
|
|
|
printf("fiber-%d running, sock fd=%d\r\n",
|
|
|
|
acl::fiber::self(), conn->sock_handle());
|
2018-11-30 14:38:22 +08:00
|
|
|
|
2022-03-26 14:18:31 +08:00
|
|
|
bool stop = false;
|
2018-11-30 14:38:22 +08:00
|
|
|
char buf[8192];
|
2022-03-26 14:18:31 +08:00
|
|
|
while (true) {
|
2018-11-30 14:38:22 +08:00
|
|
|
int ret = conn->read(buf, sizeof(buf), false);
|
2022-03-26 14:18:31 +08:00
|
|
|
if (ret == -1) {
|
2018-11-30 14:38:22 +08:00
|
|
|
break;
|
2022-03-26 14:18:31 +08:00
|
|
|
}
|
|
|
|
buf[ret] = 0;
|
|
|
|
if (strncasecmp(buf, "stop", 4) == 0) {
|
|
|
|
stop = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (conn->write(buf, ret) == -1) {
|
2018-11-30 14:38:22 +08:00
|
|
|
break;
|
2022-03-26 14:18:31 +08:00
|
|
|
}
|
2018-11-30 14:38:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
delete conn;
|
2022-03-26 14:18:31 +08:00
|
|
|
|
|
|
|
if (stop) {
|
|
|
|
printf("Stop fiber schedule now\r\n");
|
|
|
|
acl::fiber::schedule_stop();
|
|
|
|
}
|
2018-11-30 14:38:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void fiber_server(acl::server_socket& ss)
|
|
|
|
{
|
2022-03-26 14:18:31 +08:00
|
|
|
while (true) {
|
2018-11-30 14:38:22 +08:00
|
|
|
acl::socket_stream* conn = ss.accept();
|
2022-03-26 14:18:31 +08:00
|
|
|
if (conn == NULL) {
|
2018-11-30 14:38:22 +08:00
|
|
|
printf("accept error %s\r\n", acl::last_serror());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("accept ok, fd: %d\r\n", conn->sock_handle());
|
|
|
|
|
|
|
|
go[=] {
|
|
|
|
fiber_client(conn);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(const char* procname)
|
|
|
|
{
|
|
|
|
printf("usage: %s -h [help] -s listen_addr\r\n", procname);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
acl::acl_cpp_init();
|
|
|
|
acl::string addr("127.0.0.1:9006");
|
|
|
|
acl::log::stdout_open(true);
|
|
|
|
|
2022-03-26 14:18:31 +08:00
|
|
|
while ((ch = getopt(argc, argv, "hs:")) > 0) {
|
|
|
|
switch (ch) {
|
2018-11-30 14:38:22 +08:00
|
|
|
case 'h':
|
|
|
|
usage(argv[0]);
|
|
|
|
return 0;
|
|
|
|
case 's':
|
|
|
|
addr = optarg;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
acl::server_socket ss;
|
2022-03-26 14:18:31 +08:00
|
|
|
if (ss.open(addr) == false) {
|
2018-11-30 14:38:22 +08:00
|
|
|
printf("listen %s error %s\r\n", addr.c_str(), acl::last_serror());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
printf("listen %s ok\r\n", addr.c_str());
|
|
|
|
|
|
|
|
go[&] {
|
|
|
|
fiber_server(ss);
|
|
|
|
};
|
|
|
|
|
|
|
|
acl::fiber::schedule(); // start fiber schedule
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|