PIHTTP::MessageConst code categories
PIHTTPClient migrated to curl_multi_api
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include <curl/curl.h>
|
||||
|
||||
CurlThreadPool::CurlThreadPool() {
|
||||
curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||
piForTimes(10) {
|
||||
auto * t = new PIThread([this]() { threadFunc(); }, true);
|
||||
threads << t;
|
||||
@@ -15,27 +16,10 @@ CurlThreadPool::CurlThreadPool() {
|
||||
|
||||
|
||||
CurlThreadPool::~CurlThreadPool() {
|
||||
exiting = true;
|
||||
for (auto * t: threads)
|
||||
t->stop();
|
||||
{
|
||||
auto cr = clients.getRef();
|
||||
for (auto c: *cr)
|
||||
c->abort();
|
||||
}
|
||||
sem.release(threads.size());
|
||||
for (auto * t: threads) {
|
||||
t->waitForFinish();
|
||||
t->setDebug(false);
|
||||
t->terminate();
|
||||
}
|
||||
piDeleteAllAndClear(threads);
|
||||
{
|
||||
auto cr = clients.getRef();
|
||||
for (auto c: *cr)
|
||||
delete c;
|
||||
}
|
||||
destroy();
|
||||
// piCout << "~CurlThreadPool cleanup ...";
|
||||
curl_global_cleanup();
|
||||
// piCout << "~CurlThreadPool cleanup ok";
|
||||
}
|
||||
|
||||
|
||||
@@ -75,3 +59,30 @@ CurlThreadPool * CurlThreadPool::instance() {
|
||||
static CurlThreadPool ret;
|
||||
return &ret;
|
||||
}
|
||||
|
||||
void CurlThreadPool::destroy() {
|
||||
// piCout << "~CurlThreadPool";
|
||||
exiting = true;
|
||||
for (auto * t: threads)
|
||||
t->stop();
|
||||
{
|
||||
auto cr = clients.getRef();
|
||||
for (auto c: *cr)
|
||||
c->abort();
|
||||
}
|
||||
// piCout << "~CurlThreadPool release ...";
|
||||
sem.release(threads.size());
|
||||
for (auto * t: threads) {
|
||||
t->waitForFinish();
|
||||
t->setDebug(false);
|
||||
t->terminate();
|
||||
}
|
||||
// piCout << "~CurlThreadPool delete ...";
|
||||
piDeleteAllAndClear(threads);
|
||||
{
|
||||
auto cr = clients.getRef();
|
||||
for (auto c: *cr)
|
||||
delete c;
|
||||
}
|
||||
// piCout << "~CurlThreadPool ok";
|
||||
}
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
#ifndef curl_thread_pool_p_H
|
||||
#define curl_thread_pool_p_H
|
||||
|
||||
#include "pip_http_client_export.h"
|
||||
#include "piprotectedvariable.h"
|
||||
#include "pisemaphore.h"
|
||||
#include "pithread.h"
|
||||
|
||||
class PIHTTPClient;
|
||||
|
||||
class CurlThreadPool {
|
||||
class PIP_HTTP_CLIENT_EXPORT CurlThreadPool {
|
||||
public:
|
||||
void registerClient(PIHTTPClient * c);
|
||||
|
||||
@@ -18,6 +19,7 @@ private:
|
||||
CurlThreadPool();
|
||||
~CurlThreadPool();
|
||||
|
||||
void destroy();
|
||||
void threadFunc();
|
||||
void procClient(PIHTTPClient * c);
|
||||
|
||||
|
||||
@@ -22,8 +22,30 @@ int debug_callback(CURL * handle, curl_infotype type, char * data, size_t size,
|
||||
|
||||
|
||||
PRIVATE_DEFINITION_START(PIHTTPClient)
|
||||
CURLM * multi = nullptr;
|
||||
CURL * handle = nullptr;
|
||||
curl_slist * header_list = nullptr;
|
||||
bool isInit() const {
|
||||
return multi && handle;
|
||||
}
|
||||
bool init() {
|
||||
multi = curl_multi_init();
|
||||
handle = curl_easy_init();
|
||||
if (!multi || !handle) {
|
||||
destroy();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void destroy() {
|
||||
if (multi && handle) curl_multi_remove_handle(multi, handle);
|
||||
if (header_list) curl_slist_free_all(header_list);
|
||||
if (handle) curl_easy_cleanup(handle);
|
||||
if (multi) curl_multi_cleanup(multi);
|
||||
multi = nullptr;
|
||||
handle = nullptr;
|
||||
header_list = nullptr;
|
||||
}
|
||||
PRIVATE_DEFINITION_END(PIHTTPClient)
|
||||
|
||||
|
||||
@@ -35,8 +57,8 @@ PIHTTPClient::~PIHTTPClient() {}
|
||||
|
||||
bool PIHTTPClient::init() {
|
||||
if (is_cancel) return false;
|
||||
PRIVATE->handle = curl_easy_init();
|
||||
if (!PRIVATE->handle) return false;
|
||||
CurlThreadPool::instance();
|
||||
if (!PRIVATE->init()) return false;
|
||||
auto ait = request.arguments().makeIterator();
|
||||
while (ait.next()) {
|
||||
if (!url.contains('?'))
|
||||
@@ -81,12 +103,39 @@ bool PIHTTPClient::init() {
|
||||
|
||||
|
||||
void PIHTTPClient::perform() {
|
||||
if (!PRIVATE->handle) return;
|
||||
if (!PRIVATE->isInit()) return;
|
||||
if (!is_cancel) {
|
||||
// piCout << "perform ...";
|
||||
PITimeMeasurer tm;
|
||||
CURLcode res = curl_easy_perform(PRIVATE->handle);
|
||||
// piCout << "done" << res << "in" << tm.elapsed_m() << ", bytes" << buffer_out.size();
|
||||
// CURLcode res = curl_easy_perform(PRIVATE->handle);
|
||||
|
||||
curl_multi_add_handle(PRIVATE->multi, PRIVATE->handle);
|
||||
int running_cnt = 1;
|
||||
do {
|
||||
CURLMcode mc = curl_multi_perform(PRIVATE->multi, &running_cnt);
|
||||
// piCout << "curl_multi_perform" << mc << running_cnt;
|
||||
if (!mc) /* wait for activity, timeout or "nothing" */
|
||||
mc = curl_multi_poll(PRIVATE->multi, nullptr, 0, 50, nullptr);
|
||||
if (mc || is_cancel) {
|
||||
// piCout << "curl_multi_poll() failed";
|
||||
break;
|
||||
}
|
||||
|
||||
} while (running_cnt > 0 && !is_cancel);
|
||||
|
||||
if (is_cancel) {
|
||||
if (on_abort) on_abort(reply);
|
||||
PRIVATE->destroy();
|
||||
return;
|
||||
}
|
||||
|
||||
CURLMsg * m = nullptr;
|
||||
CURLcode res = (CURLcode)-1;
|
||||
do {
|
||||
int msgq = 0;
|
||||
m = curl_multi_info_read(PRIVATE->multi, &msgq);
|
||||
if (m && (m->msg == CURLMSG_DONE)) {
|
||||
res = m->data.result;
|
||||
if (res == CURLE_OK) {
|
||||
reply.setBody(std::move(buffer_out));
|
||||
if (on_finish) on_finish(reply);
|
||||
@@ -99,12 +148,19 @@ void PIHTTPClient::perform() {
|
||||
if (on_error) on_error(reply);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
} while (m && !is_cancel);
|
||||
|
||||
if (res == (CURLcode)-1) {
|
||||
last_error = "CURL error";
|
||||
if (on_error) on_error(reply);
|
||||
}
|
||||
|
||||
// piCout << "done" << (int)res << "in" << tm.elapsed_m() << ", bytes" << buffer_out.size();
|
||||
}
|
||||
// piCout << last_error;
|
||||
if (PRIVATE->header_list) curl_slist_free_all(PRIVATE->header_list);
|
||||
curl_easy_cleanup(PRIVATE->handle);
|
||||
PRIVATE->handle = nullptr;
|
||||
PRIVATE->header_list = nullptr;
|
||||
PRIVATE->destroy();
|
||||
}
|
||||
|
||||
|
||||
@@ -224,6 +280,7 @@ void PIHTTPClient::start() {
|
||||
|
||||
void PIHTTPClient::abort() {
|
||||
is_cancel = true;
|
||||
if (PRIVATE->isInit()) curl_multi_wakeup(PRIVATE->multi);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ enum class Method {
|
||||
};
|
||||
|
||||
enum class Code {
|
||||
Unknown,
|
||||
Unknown = -1,
|
||||
Continue = 100,
|
||||
SwitchingProtocols = 101,
|
||||
Processing = 102,
|
||||
|
||||
@@ -18,6 +18,35 @@ const char * PIHTTP::methodName(Method m) {
|
||||
}
|
||||
|
||||
|
||||
// PIHTTP::MessageConst
|
||||
|
||||
bool PIHTTP::MessageConst::isCodeInformational() const {
|
||||
return (int)code() >= 100 && (int)code() < 200;
|
||||
}
|
||||
|
||||
|
||||
bool PIHTTP::MessageConst::isCodeSuccess() const {
|
||||
return (int)code() >= 200 && (int)code() < 300;
|
||||
}
|
||||
|
||||
|
||||
bool PIHTTP::MessageConst::isCodeRedirection() const {
|
||||
return (int)code() >= 300 && (int)code() < 400;
|
||||
}
|
||||
|
||||
|
||||
bool PIHTTP::MessageConst::isCodeClientError() const {
|
||||
return (int)code() >= 400 && (int)code() < 500;
|
||||
}
|
||||
|
||||
|
||||
bool PIHTTP::MessageConst::isCodeServerError() const {
|
||||
return (int)code() >= 500 && (int)code() < 600;
|
||||
}
|
||||
|
||||
|
||||
// PIHTTP::MessageMutable
|
||||
|
||||
PIHTTP::MessageMutable & PIHTTP::MessageMutable::addHeader(const PIString & header, const PIString & value) {
|
||||
m_headers[header] = value;
|
||||
return *this;
|
||||
|
||||
@@ -13,6 +13,12 @@ class PIP_EXPORT MessageConst {
|
||||
public:
|
||||
PIHTTP::Method method() const { return m_method; }
|
||||
PIHTTP::Code code() const { return m_code; }
|
||||
bool isCodeInformational() const;
|
||||
bool isCodeSuccess() const;
|
||||
bool isCodeRedirection() const;
|
||||
bool isCodeClientError() const;
|
||||
bool isCodeServerError() const;
|
||||
bool isCodeError() const { return isCodeClientError() || isCodeServerError(); }
|
||||
const PIString & path() const { return m_path; }
|
||||
PIStringList pathList() const { return m_path.split('/').removeAll({}); }
|
||||
const PIByteArray & body() const { return m_body; }
|
||||
|
||||
Reference in New Issue
Block a user