2021-06-06 10:05:02 +08:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static void client_echo(acl::socket_stream* conn) {
|
|
|
|
acl::string buf;
|
|
|
|
while (true) {
|
2021-10-07 23:13:21 +08:00
|
|
|
#if 1
|
2021-06-06 10:05:02 +08:00
|
|
|
struct timeval begin, end;
|
|
|
|
gettimeofday(&begin, NULL);
|
2021-09-17 00:11:06 +08:00
|
|
|
int ret = acl_readable(conn->sock_handle());
|
2021-06-06 10:05:02 +08:00
|
|
|
gettimeofday(&end, NULL);
|
|
|
|
double cost = acl::stamp_sub(end, begin);
|
|
|
|
|
|
|
|
if (ret == 0) {
|
2021-10-07 23:13:21 +08:00
|
|
|
//printf("not readable, cost=%.2f\r\n", cost);
|
2021-06-06 10:05:02 +08:00
|
|
|
} else if (ret == 1) {
|
|
|
|
printf("readable, cost=%.2f\r\n", cost);
|
|
|
|
} else {
|
|
|
|
printf("readable error\r\n");
|
|
|
|
}
|
2021-10-07 23:13:21 +08:00
|
|
|
#endif
|
2021-06-06 10:05:02 +08:00
|
|
|
|
|
|
|
if (!conn->gets(buf, false)) {
|
|
|
|
printf("client read error %s\r\n", acl::last_serror());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (conn->write(buf) == -1) {
|
|
|
|
printf("client write error %s\r\n", acl::last_serror());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void server_listen(acl::server_socket& ss) {
|
|
|
|
while (true) {
|
|
|
|
acl::socket_stream* conn = ss.accept();
|
|
|
|
if (conn == NULL) {
|
|
|
|
printf("accept error %s\r\n", acl::last_serror());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
go[=] {
|
|
|
|
client_echo(conn);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
acl::fiber::schedule_stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static void usage(const char* procname) {
|
|
|
|
printf("usage: %s -h [help] -s server_addr\r\n", procname);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
int ch;
|
2021-10-07 23:13:21 +08:00
|
|
|
acl::string addr = "0.0.0.0:9000";
|
2021-06-06 10:05:02 +08:00
|
|
|
|
|
|
|
acl::acl_cpp_init();
|
|
|
|
acl::log::stdout_open(true);
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "hs:")) > 0) {
|
|
|
|
switch (ch) {
|
|
|
|
case 'h':
|
|
|
|
usage(argv[0]);
|
|
|
|
return 0;
|
|
|
|
case 's':
|
|
|
|
addr = optarg;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
acl::server_socket ss;
|
|
|
|
if (!ss.open(addr)) {
|
|
|
|
printf("open %s error %s\r\n", addr.c_str(), acl::last_serror());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
go[&] {
|
|
|
|
server_listen(ss);
|
|
|
|
};
|
|
|
|
|
|
|
|
acl::fiber::schedule();
|
2021-09-17 00:11:06 +08:00
|
|
|
|
2021-06-06 10:05:02 +08:00
|
|
|
return 0;
|
|
|
|
}
|