完善httpclientpool
This commit is contained in:
parent
803b1812d0
commit
3e8beea319
@ -29,8 +29,6 @@ namespace f8
|
|||||||
long long context_id = 0;
|
long long context_id = 0;
|
||||||
a8::XParams param;
|
a8::XParams param;
|
||||||
time_t add_time = 0;
|
time_t add_time = 0;
|
||||||
std::string url;
|
|
||||||
std::string url_params;
|
|
||||||
AsyncHttpOnOkFunc on_ok = nullptr;
|
AsyncHttpOnOkFunc on_ok = nullptr;
|
||||||
AsyncHttpOnErrorFunc on_error = nullptr;
|
AsyncHttpOnErrorFunc on_error = nullptr;
|
||||||
a8::TimerAttacher timer_attacher;
|
a8::TimerAttacher timer_attacher;
|
||||||
@ -40,8 +38,11 @@ namespace f8
|
|||||||
{
|
{
|
||||||
int socket_handle = 0;
|
int socket_handle = 0;
|
||||||
long long context_id = 0;
|
long long context_id = 0;
|
||||||
|
int method = 0;
|
||||||
std::string url;
|
std::string url;
|
||||||
std::string url_params;
|
std::string url_params;
|
||||||
|
std::string content;
|
||||||
|
a8::XObject headers;
|
||||||
AsyncHttpNode* nextnode = nullptr;
|
AsyncHttpNode* nextnode = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -130,7 +131,22 @@ namespace f8
|
|||||||
finally_url = node->url + "?" + node->url_params;
|
finally_url = node->url + "?" + node->url_params;
|
||||||
}
|
}
|
||||||
std::string response;
|
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();
|
a8::XObject* xobj = new a8::XObject();
|
||||||
if (xobj->ReadFromJsonString(response)) {
|
if (xobj->ReadFromJsonString(response)) {
|
||||||
f8::MsgQueue::Instance()->PostMsg_r(exec_async_http_msgid,
|
f8::MsgQueue::Instance()->PostMsg_r(exec_async_http_msgid,
|
||||||
@ -224,8 +240,10 @@ namespace f8
|
|||||||
delete request;
|
delete request;
|
||||||
}
|
}
|
||||||
|
|
||||||
void InternalExecAsyncHttp(const char* url, a8::XObject& url_params,
|
void InternalExecAsyncHttp(int method, const char* url, a8::XObject& url_params,
|
||||||
a8::XParams param, AsyncHttpOnOkFunc on_ok, AsyncHttpOnErrorFunc on_error, long long hash_code)
|
const char* content, a8::XObject* headers,
|
||||||
|
a8::XParams param, AsyncHttpOnOkFunc on_ok, AsyncHttpOnErrorFunc on_error,
|
||||||
|
long long hash_code)
|
||||||
|
|
||||||
{
|
{
|
||||||
AsyncHttpRequest* p = new AsyncHttpRequest();
|
AsyncHttpRequest* p = new AsyncHttpRequest();
|
||||||
@ -233,8 +251,6 @@ namespace f8
|
|||||||
p->context_id = ++curr_seqid;
|
p->context_id = ++curr_seqid;
|
||||||
p->param = param;
|
p->param = param;
|
||||||
p->add_time = time(nullptr);
|
p->add_time = time(nullptr);
|
||||||
p->url = url;
|
|
||||||
url_params.ToUrlEncodeStr(p->url_params);
|
|
||||||
p->on_ok = on_ok;
|
p->on_ok = on_ok;
|
||||||
p->on_error = on_error;
|
p->on_error = on_error;
|
||||||
async_http_hash[p->context_id] = p;
|
async_http_hash[p->context_id] = p;
|
||||||
@ -248,8 +264,13 @@ namespace f8
|
|||||||
AsyncHttpNode* node = new AsyncHttpNode();
|
AsyncHttpNode* node = new AsyncHttpNode();
|
||||||
node->socket_handle = 0;
|
node->socket_handle = 0;
|
||||||
node->context_id = p->context_id;
|
node->context_id = p->context_id;
|
||||||
|
node->method = method;
|
||||||
node->url = url;
|
node->url = url;
|
||||||
url_params.ToUrlEncodeStr(node->url_params);
|
url_params.ToUrlEncodeStr(node->url_params);
|
||||||
|
node->content = std::string(content);
|
||||||
|
if (headers) {
|
||||||
|
headers->DeepCopy(node->headers);
|
||||||
|
}
|
||||||
http_thread->AddAsyncHttp(node);
|
http_thread->AddAsyncHttp(node);
|
||||||
}
|
}
|
||||||
a8::Timer::Instance()->AddDeadLineTimerAndAttach(1000 * 10,
|
a8::Timer::Instance()->AddDeadLineTimerAndAttach(1000 * 10,
|
||||||
@ -309,10 +330,19 @@ namespace f8
|
|||||||
impl_->SetThreadNum(thread_num);
|
impl_->SetThreadNum(thread_num);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpClientPool::HttpGet(const char* url, a8::XObject url_params,
|
void HttpClientPool::HttpGet(a8::XParams param, AsyncHttpOnOkFunc on_ok, AsyncHttpOnErrorFunc on_error,
|
||||||
a8::XParams param, AsyncHttpOnOkFunc on_ok, AsyncHttpOnErrorFunc on_error, long long hash_code)
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -19,8 +19,13 @@ namespace f8
|
|||||||
void SetThreadNum(int thread_num);
|
void SetThreadNum(int thread_num);
|
||||||
|
|
||||||
//执行异步http get
|
//执行异步http get
|
||||||
void HttpGet(const char* url, a8::XObject url_params,
|
void HttpGet(a8::XParams param, AsyncHttpOnOkFunc on_ok, AsyncHttpOnErrorFunc on_error,
|
||||||
a8::XParams param, AsyncHttpOnOkFunc on_ok, AsyncHttpOnErrorFunc on_error, long long hash_code = 0);
|
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:
|
private:
|
||||||
HttpClientPoolImpl* impl_ = nullptr;
|
HttpClientPoolImpl* impl_ = nullptr;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user