acl/lib_acl_cpp/samples/aio/http_client/main.cpp

181 lines
4.4 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include "http_client.h"
acl::atomic_long __aio_refer = 0;
int __success = 0;
int __destroy = 0;
int __disconnect = 0;
int __ns_failed = 0;
int __connect_ok = 0;
int __connect_timeout = 0;
int __connect_failed = 0;
int __header_ok = 0;
int __read_timeout = 0;
2019-08-08 17:40:16 +08:00
//////////////////////////////////////////////////////////////////////////////
static void usage(const char* procname)
{
printf("usage: %s -h[help]\r\n"
" -s server_addr\r\n"
2019-08-09 13:26:56 +08:00
" -e event_type[kernel|select|poll, default: kernel]\r\n"
2019-08-08 17:40:16 +08:00
" -D [if in debug mode, default: false]\r\n"
" -c cocorrent\r\n"
" -t connect_timeout[default: 5]\r\n"
" -i rw_timeout[default: 5]\r\n"
" -U url\r\n"
" -H host\r\n"
2019-08-08 17:57:45 +08:00
" -K [http keep_alive true]\r\n"
2019-08-14 16:36:23 +08:00
" -N name_server_list[default: 8.8.8.8:53]\r\n"
2019-08-23 16:28:00 +08:00
" -T ns_lookup_timeout[default: 5]\r\n"
2019-08-08 17:40:16 +08:00
, procname);
}
2019-08-14 16:36:23 +08:00
static void add_name_servers(std::vector<acl::string>& addrs, const char* s)
{
acl::string buf(s);
const std::vector<acl::string>& tokens = buf.split2(",; \t");
for (std::vector<acl::string>::const_iterator cit = tokens.begin();
cit != tokens.end(); ++cit) {
addrs.push_back(*cit);
}
}
2019-08-08 17:40:16 +08:00
int main(int argc, char* argv[])
{
2019-08-23 16:28:00 +08:00
int ch, ns_timeout = 5, conn_timeout = 5, rw_timeout = 5, cocurrent = 1;
2019-08-14 16:36:23 +08:00
acl::string addr("pvwu8bubc.bkt.clouddn.com:80");
2019-08-08 18:03:33 +08:00
acl::string host("pvwu8bubc.bkt.clouddn.com"), url("/20160528212429_c2HAm.jpeg");
2019-08-14 16:36:23 +08:00
std::vector<acl::string> name_servers;
2019-08-09 13:26:56 +08:00
bool debug = false, keep_alive = false;
acl::string event("kernel");
acl::aio_handle_type event_type;
2019-08-08 17:40:16 +08:00
2019-08-23 16:28:00 +08:00
while ((ch = getopt(argc, argv, "he:Kc:s:N:U:H:t:i:DT:")) > 0) {
2019-08-08 17:40:16 +08:00
switch (ch) {
case 'h':
usage(argv[0]);
return (0);
2019-08-09 13:26:56 +08:00
case 'e':
event = optarg;
2019-08-08 17:57:45 +08:00
break;
case 'K':
keep_alive = true;
break;
2019-08-08 17:40:16 +08:00
case 'c':
cocurrent = atoi(optarg);
break;
case 's':
addr = optarg;
break;
case 'N':
2019-08-14 16:36:23 +08:00
add_name_servers(name_servers, optarg);
2019-08-08 17:40:16 +08:00
break;
case 'U':
url = optarg;
break;
case 'H':
host = optarg;
break;
case 't':
conn_timeout = atoi(optarg);
break;
case 'i':
rw_timeout = atoi(optarg);
break;
case 'D':
debug = true;
break;
2019-08-23 16:28:00 +08:00
case 'T':
ns_timeout = atoi(optarg);
break;
2019-08-08 17:40:16 +08:00
default:
break;
}
}
2019-08-14 16:36:23 +08:00
if (name_servers.empty()) {
name_servers.push_back("8.8.8.8:53");
}
2019-08-08 17:40:16 +08:00
acl::acl_cpp_init();
acl::log::stdout_open(true);
2019-08-09 13:26:56 +08:00
if (event == "select") {
event_type = acl::ENGINE_SELECT;
printf("use select event\r\n");
} else if (event == "poll") {
event_type = acl::ENGINE_POLL;
printf("use poll event\r\n");
} else {
event_type = acl::ENGINE_KERNEL;
printf("use kernel event\r\n");
}
2019-08-08 17:40:16 +08:00
// <20><><EFBFBD><EFBFBD> AIO <20>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD>
2019-08-09 13:26:56 +08:00
acl::aio_handle handle(event_type);
2019-08-08 17:40:16 +08:00
handle.set_delay_sec(0);
handle.set_delay_usec(1000000);
2019-08-08 17:40:16 +08:00
// <20><><EFBFBD><EFBFBD> DNS <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
2019-08-14 16:36:23 +08:00
for (std::vector<acl::string>::const_iterator cit = name_servers.begin();
cit != name_servers.end(); ++cit) {
2019-08-23 16:28:00 +08:00
handle.set_dns(*cit, ns_timeout);
2019-08-14 16:36:23 +08:00
}
//////////////////////////////////////////////////////////////////////
2019-08-08 17:40:16 +08:00
// <20><>ʼ<EFBFBD><EFBFBD><ECB2BD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6> WEB <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (int i = 0; i < cocurrent; i++) {
http_client* conn = new http_client(handle);
(*conn).set_addr(addr)
.set_timeout(conn_timeout, rw_timeout)
.set_url(url)
.set_debug(debug)
2019-08-08 17:40:16 +08:00
.set_host(host)
2019-08-08 17:57:45 +08:00
.set_keep_alive(keep_alive);
2019-08-08 17:40:16 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><D8B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ 0 <20><><EFBFBD><EFBFBD>ֹ<EFBFBD>ض<EFBFBD><D8B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
conn->set_redirect_limit(3);
2019-08-08 17:40:16 +08:00
if (!conn->start()) {
printf("connect %s error\r\n", addr.c_str());
return 1;
}
2019-08-08 17:40:16 +08:00
}
2019-08-23 16:28:00 +08:00
time_t last = time(NULL), now, begin = last;
2019-08-08 17:40:16 +08:00
// <20><>ʼ AIO <20>¼<EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
while (true) {
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> false <20><><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD>ټ<EFBFBD><D9BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>˳<EFBFBD>
if (!handle.check()) {
break;
}
(void) time(&now);
if (now - last > 0) {
printf("continue check %ld seconds...\r\n", now - last);
}
last = now;
2019-08-08 17:40:16 +08:00
}
2019-08-23 16:28:00 +08:00
(void) time(&now);
printf("\r\ntime spent: %ld seconds\r\n", now - begin);
2019-08-08 17:40:16 +08:00
handle.check();
2019-08-09 13:26:56 +08:00
2019-08-08 17:57:45 +08:00
printf("\r\n---------------------------------------------------\r\n");
2019-08-23 15:12:19 +08:00
printf("all over, destroy=%d\r\n\r\n", __destroy);
printf("ns_failed=%d, connect_ok=%d, disconnect=%d, connect_timeout=%d,"
" connect_faile=%d\r\n\r\n", __ns_failed, __connect_ok,
__disconnect, __connect_timeout, __connect_failed);
printf("success=%d, header_ok=%d, read_timeout=%d\r\n",
__success, __header_ok, __read_timeout);
printf("---------------------------------------------------\r\n");
2019-08-08 17:40:16 +08:00
return 0;
}