acl/lib_acl_cpp/samples/http_request_pool/main.cpp

163 lines
3.3 KiB
C++
Raw Normal View History

2014-11-19 00:25:21 +08:00
#include "stdafx.h"
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;
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
static void init(const char* addr, int count)
{
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳض<D3B3><D8B6><EFBFBD>
__conn_pool = new http_request_pool(addr, count);
// <20><><EFBFBD><EFBFBD><EFBFBD>̳߳<DFB3>
__thr_pool = acl_thread_pool_create(count, 60);
}
// <20><><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
static void end(void)
{
// <20><><EFBFBD><EFBFBD><EFBFBD>̳߳<DFB3>
acl_pthread_pool_destroy(__thr_pool);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>
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>
static bool http_get(http_request* conn, int n)
{
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD>
http_header& header = conn->request_header();
header.set_url("/")
.set_keep_alive(true)
.set_method(HTTP_METHOD_GET)
.accept_gzip(__unzip);
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)
{
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)
{
ret = conn->read_body(buf, sizeof(buf));
if (ret == 0)
break;
else if (ret < 0)
{
printf("%lu--%d: error, length: %d\r\n",
(unsigned long) acl_pthread_self(), n, length);
return false;
}
length += ret;
if (0)
printf("%lu--%d: read length: %d, %d\r\n",
(unsigned long) acl_pthread_self(), n, length, ret);
}
printf("%lu--%d: read body over, length: %d\r\n",
(unsigned long) acl_pthread_self(), n, length);
return true;
}
// <20>̴߳<DFB3><CCB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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>
http_request* conn = (http_request*) __conn_pool->peek();
if (conn == NULL)
{
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
conn->reset();
// <20><>ʼ<EFBFBD>µ<EFBFBD> HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (http_get(conn, i) == false)
{
printf("one request failed, close connection\r\n");
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ر<EFBFBD>
__conn_pool->put(conn, false);
}
else
__conn_pool->put(conn, true);
}
}
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++)
acl_pthread_pool_add(__thr_pool, thread_main, NULL);
}
static void usage(const char* procname)
{
printf("usage: %s -h [help]\r\n"
" -s http_server_addr [www.sina.com.cn:80]\r\n"
" -z [unzip response body, default: false]\r\n"
" -c cocurrent [default: 10]\r\n"
" -n loop_count[default: 10]\r\n", procname);
}
int main(int argc, char* argv[])
{
int ch, cocurrent = 10;
string addr("www.sina.com.cn:80");
// <20><>ʼ<EFBFBD><CABC> acl <20><>
acl::acl_cpp_init();
while ((ch = getopt(argc, argv, "hs:n:c:z")) > 0)
{
switch (ch)
{
case 'h':
usage(argv[0]);
return 0;
case 's':
addr = optarg;
break;
case 'c':
cocurrent = atoi(optarg);
break;
case 'n':
__loop_count = atoi(optarg);
break;
case 'z':
__unzip = true;
break;
default:
usage(argv[0]);
return 0;
}
}
init(addr, cocurrent);
run(cocurrent);
end();
#ifdef WIN32
printf("enter any key to exit...\r\n");
getchar();
#endif
return 0;
}