From 3e8beea3190fe4bf53573c25b7ea7a29106bf325 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sat, 12 Jan 2019 14:30:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84httpclientpool?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cpp/httpclientpool.cc | 50 ++++++++++++++++++++++++++++++++++--------- cpp/httpclientpool.h | 9 ++++++-- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/cpp/httpclientpool.cc b/cpp/httpclientpool.cc index 839188b..a3b4944 100644 --- a/cpp/httpclientpool.cc +++ b/cpp/httpclientpool.cc @@ -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); } } diff --git a/cpp/httpclientpool.h b/cpp/httpclientpool.h index dad94d8..96c13ad 100644 --- a/cpp/httpclientpool.h +++ b/cpp/httpclientpool.h @@ -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;