完善httpclientpool

This commit is contained in:
aozhiwei 2019-01-12 14:30:13 +08:00
parent 803b1812d0
commit 3e8beea319
2 changed files with 47 additions and 12 deletions

View File

@ -29,8 +29,6 @@ namespace f8
long long context_id = 0;
a8::XParams param;
time_t add_time = 0;
std::string url;
std::string url_params;
AsyncHttpOnOkFunc on_ok = nullptr;
AsyncHttpOnErrorFunc on_error = nullptr;
a8::TimerAttacher timer_attacher;
@ -40,8 +38,11 @@ namespace f8
{
int socket_handle = 0;
long long context_id = 0;
int method = 0;
std::string url;
std::string url_params;
std::string content;
a8::XObject headers;
AsyncHttpNode* nextnode = nullptr;
};
@ -130,7 +131,22 @@ namespace f8
finally_url = node->url + "?" + node->url_params;
}
std::string response;
if (a8::http::Get(finally_url, response, 10)) {
bool ret = false;
switch (node->method) {
case 1:
{
ret = a8::http::Get(finally_url, response, &node->headers, 10);
};
case 2:
{
ret = a8::http::Post(finally_url.c_str(), node->content, response, &node->headers, 10);
}
default:
{
}
}
if (ret) {
a8::XObject* xobj = new a8::XObject();
if (xobj->ReadFromJsonString(response)) {
f8::MsgQueue::Instance()->PostMsg_r(exec_async_http_msgid,
@ -224,8 +240,10 @@ namespace f8
delete request;
}
void InternalExecAsyncHttp(const char* url, a8::XObject& url_params,
a8::XParams param, AsyncHttpOnOkFunc on_ok, AsyncHttpOnErrorFunc on_error, long long hash_code)
void InternalExecAsyncHttp(int method, const char* url, a8::XObject& url_params,
const char* content, a8::XObject* headers,
a8::XParams param, AsyncHttpOnOkFunc on_ok, AsyncHttpOnErrorFunc on_error,
long long hash_code)
{
AsyncHttpRequest* p = new AsyncHttpRequest();
@ -233,8 +251,6 @@ namespace f8
p->context_id = ++curr_seqid;
p->param = param;
p->add_time = time(nullptr);
p->url = url;
url_params.ToUrlEncodeStr(p->url_params);
p->on_ok = on_ok;
p->on_error = on_error;
async_http_hash[p->context_id] = p;
@ -248,8 +264,13 @@ namespace f8
AsyncHttpNode* node = new AsyncHttpNode();
node->socket_handle = 0;
node->context_id = p->context_id;
node->method = method;
node->url = url;
url_params.ToUrlEncodeStr(node->url_params);
node->content = std::string(content);
if (headers) {
headers->DeepCopy(node->headers);
}
http_thread->AddAsyncHttp(node);
}
a8::Timer::Instance()->AddDeadLineTimerAndAttach(1000 * 10,
@ -309,10 +330,19 @@ namespace f8
impl_->SetThreadNum(thread_num);
}
void HttpClientPool::HttpGet(const char* url, a8::XObject url_params,
a8::XParams param, AsyncHttpOnOkFunc on_ok, AsyncHttpOnErrorFunc on_error, long long hash_code)
void HttpClientPool::HttpGet(a8::XParams param, AsyncHttpOnOkFunc on_ok, AsyncHttpOnErrorFunc on_error,
const char* url, a8::XObject url_params,
long long hash_code, a8::XObject* headers)
{
impl_->InternalExecAsyncHttp(url, url_params, param, on_ok, on_error, hash_code);
impl_->InternalExecAsyncHttp(1, url, url_params, "", headers, param, on_ok, on_error, hash_code);
}
void HttpClientPool::HttpPost(a8::XParams param, AsyncHttpOnOkFunc on_ok, AsyncHttpOnErrorFunc on_error,
const char* url, a8::XObject url_params, const std::string& content,
long long hash_code, a8::XObject* headers)
{
impl_->InternalExecAsyncHttp(2, url, url_params, content.c_str(), headers, param, on_ok, on_error, hash_code);
}
}

View File

@ -19,8 +19,13 @@ namespace f8
void SetThreadNum(int thread_num);
//执行异步http get
void HttpGet(const char* url, a8::XObject url_params,
a8::XParams param, AsyncHttpOnOkFunc on_ok, AsyncHttpOnErrorFunc on_error, long long hash_code = 0);
void HttpGet(a8::XParams param, AsyncHttpOnOkFunc on_ok, AsyncHttpOnErrorFunc on_error,
const char* url, a8::XObject url_params,
long long hash_code, a8::XObject* headers = nullptr);
//执行异步http post
void HttpPost(a8::XParams param, AsyncHttpOnOkFunc on_ok, AsyncHttpOnErrorFunc on_error,
const char* url, a8::XObject url_params, const std::string& content,
long long hash_code, a8::XObject* headers = nullptr);
private:
HttpClientPoolImpl* impl_ = nullptr;