acl/lib_acl_cpp/samples/http_request_pool/main.cpp

180 lines
3.7 KiB
C++
Raw Normal View History

#include "stdafx.h"
2014-11-19 00:25:21 +08:00
using namespace acl;
static int __loop_count = 10;
static connect_pool* __conn_pool = NULL;
static acl_pthread_pool_t* __thr_pool = NULL;
static bool __unzip = false;
static bool __debug = false;
static acl::string __host;
2014-11-19 00:25:21 +08:00
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
static void init(const char* addr, int count)
{
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳض<D3B3><D8B6><EFBFBD>
2014-11-19 00:25:21 +08:00
__conn_pool = new http_request_pool(addr, count);
// <20><><EFBFBD><EFBFBD><EFBFBD>̳߳<DFB3>
2014-11-19 00:25:21 +08:00
__thr_pool = acl_thread_pool_create(count, 60);
}
// <20><><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
2014-11-19 00:25:21 +08:00
static void end(void)
{
// <20><><EFBFBD><EFBFBD><EFBFBD>̳߳<DFB3>
2014-11-19 00:25:21 +08:00
acl_pthread_pool_destroy(__thr_pool);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>
2014-11-19 00:25:21 +08:00
delete __conn_pool;
}
// HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̣<EFBFBD><CCA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
static bool http_get(http_request* conn, int n)
{
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
http_header& header = conn->request_header();
header.set_url("/")
2014-11-19 00:25:21 +08:00
.set_keep_alive(true)
.set_method(HTTP_METHOD_GET)
.accept_gzip(__unzip);
if (!__host.empty()) {
header.set_host(__host);
}
2014-11-19 00:25:21 +08:00
if (__debug) {
printf("%lu--%d: begin send request\r\n",
(unsigned long) acl_pthread_self(), n);
}
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬʱ<CDAC><CAB1><EFBFBD><EFBFBD> HTTP <20><>Ӧͷ
if (conn->request(NULL, 0) == false) {
2014-11-19 00:25:21 +08:00
printf("%lu--%d: send GET request error\r\n",
(unsigned long) acl_pthread_self(), n);
return false;
}
char buf[8192];
int ret, length = 0;
// <20><><EFBFBD><EFBFBD> HTTP <20><>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
while (true) {
2014-11-19 00:25:21 +08:00
ret = conn->read_body(buf, sizeof(buf));
if (ret == 0) {
2014-11-19 00:25:21 +08:00
break;
} else if (ret < 0) {
2014-11-19 00:25:21 +08:00
printf("%lu--%d: error, length: %d\r\n",
(unsigned long) acl_pthread_self(), n, length);
return false;
}
length += ret;
if (__debug) {
2014-11-19 00:25:21 +08:00
printf("%lu--%d: read length: %d, %d\r\n",
(unsigned long) acl_pthread_self(), n, length, ret);
}
}
if (__debug) {
printf("%lu--%d: read body over, length: %d\r\n",
(unsigned long) acl_pthread_self(), n, length);
2014-11-19 00:25:21 +08:00
}
return true;
}
// <20>̴߳<DFB3><CCB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
static void thread_main(void*)
{
for (int i = 0; i < __loop_count; i++) {
// <20><><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD><D3B3>л<EFBFBD>ȡһ<C8A1><D2BB> HTTP <20><><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
http_request* conn = (http_request*) __conn_pool->peek();
if (conn == NULL) {
2014-11-19 00:25:21 +08:00
printf("peek connect failed\r\n");
break;
}
// <20><>Ҫ<EFBFBD>Ի<EFBFBD><D4BB>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̵<EFBFBD><CCB5><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
else {
2014-11-19 00:25:21 +08:00
conn->reset();
}
// <20><>ʼ<EFBFBD>µ<EFBFBD> HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (http_get(conn, i) == false) {
2014-11-19 00:25:21 +08:00
printf("one request failed, close connection\r\n");
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ر<EFBFBD>
2014-11-19 00:25:21 +08:00
__conn_pool->put(conn, false);
} else {
2014-11-19 00:25:21 +08:00
__conn_pool->put(conn, true);
}
2014-11-19 00:25:21 +08:00
}
}
static void run(int cocurrent)
{
// <20><><EFBFBD>̳߳<DFB3><CCB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (int i = 0; i < cocurrent; i++) {
2014-11-19 00:25:21 +08:00
acl_pthread_pool_add(__thr_pool, thread_main, NULL);
}
2014-11-19 00:25:21 +08:00
}
static void usage(const char* procname)
{
printf("usage: %s -h [help]\r\n"
" -s http_server_addr [www.sina.com.cn:80]\r\n"
" -H host\r\n"
2014-11-19 00:25:21 +08:00
" -z [unzip response body, default: false]\r\n"
" -c cocurrent [default: 10]\r\n"
" -D [if in debug mode]\r\n"
2014-11-19 00:25:21 +08:00
" -n loop_count[default: 10]\r\n", procname);
}
int main(int argc, char* argv[])
{
int ch, cocurrent = 1;
2014-11-19 00:25:21 +08:00
string addr("www.sina.com.cn:80");
// <20><>ʼ<EFBFBD><CABC> acl <20><>
2014-11-19 00:25:21 +08:00
acl::acl_cpp_init();
acl::log::stdout_open(true);
2014-11-19 00:25:21 +08:00
while ((ch = getopt(argc, argv, "hs:H:n:c:zD")) > 0) {
switch (ch) {
2014-11-19 00:25:21 +08:00
case 'h':
usage(argv[0]);
return 0;
case 's':
addr = optarg;
break;
case 'H':
__host = optarg;
break;
2014-11-19 00:25:21 +08:00
case 'c':
cocurrent = atoi(optarg);
break;
case 'n':
__loop_count = atoi(optarg);
break;
case 'z':
__unzip = true;
break;
case 'D':
__debug = true;
break;
2014-11-19 00:25:21 +08:00
default:
usage(argv[0]);
return 0;
}
}
if (__host.empty()) {
__host = addr;
}
2014-11-19 00:25:21 +08:00
init(addr, cocurrent);
run(cocurrent);
end();
#ifdef WIN32
printf("enter any key to exit...\r\n");
getchar();
#endif
return 0;
}