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

338 lines
7.3 KiB
C++
Raw Normal View History

2019-08-08 17:40:16 +08:00
#include <iostream>
#include <assert.h>
#include <getopt.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "lib_acl.h"
#include "acl_cpp/lib_acl.hpp"
static acl::atomic_long __aio_refer = 0;
2019-08-23 15:12:19 +08:00
static int __success = 0, __destroy = 0, __disconnect = 0, __ns_failed = 0;
static int __connect_ok = 0, __connect_timeout = 0, __connect_failed = 0;
static int __header_ok = 0, __read_timeout = 0;
2019-08-08 17:40:16 +08:00
//////////////////////////////////////////////////////////////////////////////
class http_client : public acl::http_aclient
{
public:
http_client(acl::aio_handle& handle, const char* host)
: http_aclient(handle, NULL)
, host_(host)
, debug_(false)
, compressed_(false)
{
++__aio_refer;
}
~http_client(void)
{
printf("delete http_client!\r\n");
if (--__aio_refer == 0) {
printf("%s: stop aio engine now!\r\n", __FUNCTION__);
handle_.stop();
}
}
http_client& enable_debug(bool on)
{
debug_ = on;
return *this;
}
protected:
// @override
void destroy(void)
{
printf("http_client will be deleted!\r\n");
fflush(stdout);
2019-08-23 15:12:19 +08:00
__destroy++;
2019-08-08 17:40:16 +08:00
delete this;
}
// @override
bool on_connect(void)
{
printf("--------------- connect server ok ------------\r\n");
2019-08-14 15:46:08 +08:00
acl::string addr;
if (this->get_ns_addr(addr)) {
printf(">>>ns server: %s\r\n", addr.c_str());
}
2019-08-08 17:40:16 +08:00
fflush(stdout);
printf(">>> begin send_request\r\n");
//this->ws_handshake();
this->send_request(NULL, 0);
2019-08-08 17:57:45 +08:00
2019-08-23 15:12:19 +08:00
__connect_ok++;
2019-08-08 17:40:16 +08:00
return true;
}
// @override
void on_disconnect(void)
{
printf("disconnect from server\r\n");
fflush(stdout);
2019-08-23 15:12:19 +08:00
__disconnect++;
2019-08-08 17:40:16 +08:00
}
// @override
void on_ns_failed(void)
{
printf("dns lookup failed\r\n");
fflush(stdout);
2019-08-23 15:12:19 +08:00
__ns_failed++;
}
2019-08-08 17:40:16 +08:00
// @override
void on_connect_timeout(void)
{
printf("connect timeout\r\n");
fflush(stdout);
2019-08-23 15:12:19 +08:00
__connect_timeout++;
2019-08-08 17:40:16 +08:00
}
// @override
void on_connect_failed(void)
{
printf("connect failed\r\n");
fflush(stdout);
2019-08-23 15:12:19 +08:00
__connect_failed++;
2019-08-08 17:40:16 +08:00
}
// @override
bool on_read_timeout(void)
{
printf("read timeout\r\n");
2019-08-23 15:12:19 +08:00
__read_timeout++;
2019-08-08 18:03:33 +08:00
return false;
2019-08-08 17:40:16 +08:00
}
protected:
2019-08-08 17:40:16 +08:00
// @override
bool on_http_res_hdr(const acl::http_header& header)
{
acl::string buf;
header.build_response(buf);
compressed_ = header.is_transfer_gzip();
2019-08-23 15:12:19 +08:00
__header_ok++;
2019-08-08 17:40:16 +08:00
2019-08-08 17:57:45 +08:00
if (!debug_) {
return true;
}
2019-08-08 17:40:16 +08:00
printf("-----------%s: response header----\r\n", __FUNCTION__);
printf("[%s]\r\n", buf.c_str());
fflush(stdout);
return true;
}
// @override
bool on_http_res_body(char* data, size_t dlen)
{
2019-08-08 17:57:45 +08:00
if (!debug_) {
return true;
}
if (!compressed_ || this->is_unzip_body()) {
2019-08-08 17:40:16 +08:00
(void) write(1, data, dlen);
} else {
printf(">>>read body: %ld\r\n", dlen);
}
return true;
}
// @override
bool on_http_res_finish(bool success)
{
printf("---------------response over-------------------\r\n");
printf("http finish: keep_alive=%s, success=%s\r\n",
keep_alive_ ? "true" : "false",
success ? "ok" : "failed");
fflush(stdout);
2019-08-08 17:57:45 +08:00
__success++;
2019-08-08 17:40:16 +08:00
return keep_alive_;
}
private:
acl::string host_;
bool debug_;
bool compressed_;
};
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-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[])
{
int ch, 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-09 13:26:56 +08:00
while ((ch = getopt(argc, argv, "he:Kc:s:N:U:H:t:i:D")) > 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;
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) {
handle.set_dns(*cit, 5);
}
//////////////////////////////////////////////////////////////////////
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, host);
if (!conn->open(addr, conn_timeout, rw_timeout)) {
printf("connect %s error\r\n", addr.c_str());
fflush(stdout);
delete conn;
return 1;
}
(*conn).enable_debug(debug); // <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>õ<EFBFBD><C3B5>Է<EFBFBD>ʽ
conn->unzip_body(true); // <20><><EFBFBD><EFBFBD> HTTP <20>Զ<EFBFBD><D4B6><EFBFBD>ѹ
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7>Ҳ<EFBFBD>ɽ<EFBFBD><C9BD>˹<EFBFBD><CBB9>̷<EFBFBD><CCB7><EFBFBD> conn->on_connect() <20><>
acl::http_header& head = conn->request_header();
head.set_url(url)
.set_host(host)
.accept_gzip(true)
2019-08-08 17:57:45 +08:00
.set_keep_alive(keep_alive);
2019-08-08 17:40:16 +08:00
if (i > 0) {
continue;
}
acl::string buf;
head.build_request(buf);
printf("---------------request header-----------------\r\n");
printf("[%s]\r\n", buf.c_str());
fflush(stdout);
}
time_t last = time(NULL), now;
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
}
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;
}