acl/lib_fiber/samples-c++1x/client/client.cpp

161 lines
3.7 KiB
C++
Raw Normal View History

2021-10-12 08:56:56 +08:00
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
//////////////////////////////////////////////////////////////////////////////
2021-10-12 21:25:08 +08:00
static size_t do_echo(acl::socket_stream& conn, int count, bool readable) {
2022-01-26 18:08:55 +08:00
acl::string data("hello world\r\n");
2021-10-12 08:56:56 +08:00
acl::string buf;
size_t i;
for (i = 0; i < (size_t) count; i++) {
if (conn.write(data) == -1) {
printf("client write error %s\r\n", acl::last_serror());
break;
}
2021-10-12 21:25:08 +08:00
if (readable) {
struct timeval begin, end;
gettimeofday(&begin, NULL);
int ret = acl_readable(conn.sock_handle());
gettimeofday(&end, NULL);
double cost = acl::stamp_sub(end, begin);
if (ret == 0) {
printf("not readable, cost=%.2f\r\n", cost);
} else if (ret == 1) {
printf("readable, cost=%.2f\r\n", cost);
} else {
printf("readable error\r\n");
}
2021-10-12 08:56:56 +08:00
}
2021-10-12 21:25:08 +08:00
2021-10-12 08:56:56 +08:00
if (!conn.gets(buf, false)) {
printf("client read error %s\r\n", acl::last_serror());
break;
}
if (i == 0) {
printf("gets: %s\r\n", buf.c_str());
}
2021-10-12 08:56:56 +08:00
}
2021-10-12 11:39:41 +08:00
printf(">>>count=%zd<<<\r\n", i);
2021-10-12 08:56:56 +08:00
return i;
}
static size_t start(const char* addr, int count, bool readable,
int conn_timeout, int rw_timeout) {
if (conn_timeout > 0) {
acl::fiber::set_non_blocking(true);
2022-01-24 09:02:42 +08:00
} else {
acl::fiber::set_non_blocking(false);
}
2021-10-12 08:56:56 +08:00
acl::socket_stream conn;
if (!conn.open(addr, conn_timeout, rw_timeout)) {
2021-10-12 08:56:56 +08:00
printf("connect %s error %s\r\n", addr, acl::last_serror());
return 0;
}
printf(">>>connect %s ok, conn_timeout=%d, rw_timeout=%d\r\n",
addr, conn_timeout, rw_timeout);
struct sockaddr_in in;
int len = sizeof(in);
2022-01-23 23:56:48 +08:00
#if defined(_WIN32) || defined(_WIN64)
if (getpeername(conn.sock_handle(), (struct sockaddr*) &in, &len) < 0) {
2022-01-23 23:56:48 +08:00
#else
if (getpeername(conn.sock_handle(), (struct sockaddr*) &in,
(socklen_t*) &len) < 0) {
#endif
printf(">>>getpeername error: %s\r\n", acl::last_serror());
}
2021-10-12 21:25:08 +08:00
return do_echo(conn, count, readable);
2021-10-12 08:56:56 +08:00
}
//////////////////////////////////////////////////////////////////////////////
static void usage(const char* procname) {
printf("usage: %s -h [help]\r\n"
" -e event_type[kernel|select|poll, default: kernel]\r\n"
" -s server_addr[default: 127.0.0.1:9000]\r\n"
" -c fiber_count[default: 1]\r\n"
" -n count[default: 100]\r\n"
" -R [if call readable? default: false]\r\n"
" -t connect_timeout[default: 10 seconds]\r\n"
" -r rw_timeout[default: 10 seconds]\r\n"
2021-10-12 08:56:56 +08:00
, procname);
}
int main(int argc, char *argv[]) {
int ch, nfiber = 1, count = 100, conn_timeout = 10, rw_timeout = 10;
2021-10-12 21:25:08 +08:00
bool readable = false;
2021-10-12 08:56:56 +08:00
acl::string addr = "127.0.0.1:9000", event_type("kernel");
acl::acl_cpp_init();
acl::log::stdout_open(true);
acl::fiber::stdout_open(true);
2021-10-12 08:56:56 +08:00
while ((ch = getopt(argc, argv, "he:s:c:n:Rt:r:")) > 0) {
2021-10-12 08:56:56 +08:00
switch (ch) {
case 'h':
usage(argv[0]);
return 0;
case 'e':
event_type = optarg;
break;
case 's':
addr = optarg;
break;
case 'c':
nfiber = atoi(optarg);
break;
case 'n':
count = atoi(optarg);
break;
case 'R':
2021-10-12 21:25:08 +08:00
readable = true;
break;
case 't':
conn_timeout = atoi(optarg);
break;
case 'r':
rw_timeout = atoi(optarg);
2021-10-13 20:40:22 +08:00
break;
2021-10-12 08:56:56 +08:00
default:
break;
}
}
acl::fiber_event_t type;
if (event_type == "select") {
type = acl::FIBER_EVENT_T_SELECT;
} else if (event_type == "poll") {
type = acl::FIBER_EVENT_T_POLL;
} else {
type = acl::FIBER_EVENT_T_KERNEL;
}
2021-10-12 20:54:54 +08:00
long long total = 0;
2021-10-12 08:56:56 +08:00
struct timeval begin;
gettimeofday(&begin, NULL);
for (int i = 0; i < nfiber; i++) {
go[&] {
total += start(addr, count, readable, conn_timeout, rw_timeout);
2021-10-12 08:56:56 +08:00
};
}
acl::fiber::schedule_with(type);
struct timeval end;
gettimeofday(&end, NULL);
double cost = acl::stamp_sub(end, begin);
2021-10-12 20:54:54 +08:00
printf("Total count=%lld, cost=%.2f ms, speed=%.2f\r\n",
2021-10-12 08:56:56 +08:00
total, cost, (total * 1000) / (cost > 0 ? cost : 0.1));
return 0;
}