PIHTTP::MessageConst code categories
PIHTTPClient migrated to curl_multi_api
This commit is contained in:
@@ -7,6 +7,7 @@
|
|||||||
#include <curl/curl.h>
|
#include <curl/curl.h>
|
||||||
|
|
||||||
CurlThreadPool::CurlThreadPool() {
|
CurlThreadPool::CurlThreadPool() {
|
||||||
|
curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||||
piForTimes(10) {
|
piForTimes(10) {
|
||||||
auto * t = new PIThread([this]() { threadFunc(); }, true);
|
auto * t = new PIThread([this]() { threadFunc(); }, true);
|
||||||
threads << t;
|
threads << t;
|
||||||
@@ -15,27 +16,10 @@ CurlThreadPool::CurlThreadPool() {
|
|||||||
|
|
||||||
|
|
||||||
CurlThreadPool::~CurlThreadPool() {
|
CurlThreadPool::~CurlThreadPool() {
|
||||||
exiting = true;
|
destroy();
|
||||||
for (auto * t: threads)
|
// piCout << "~CurlThreadPool cleanup ...";
|
||||||
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;
|
|
||||||
}
|
|
||||||
curl_global_cleanup();
|
curl_global_cleanup();
|
||||||
|
// piCout << "~CurlThreadPool cleanup ok";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -75,3 +59,30 @@ CurlThreadPool * CurlThreadPool::instance() {
|
|||||||
static CurlThreadPool ret;
|
static CurlThreadPool ret;
|
||||||
return &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
|
#ifndef curl_thread_pool_p_H
|
||||||
#define curl_thread_pool_p_H
|
#define curl_thread_pool_p_H
|
||||||
|
|
||||||
|
#include "pip_http_client_export.h"
|
||||||
#include "piprotectedvariable.h"
|
#include "piprotectedvariable.h"
|
||||||
#include "pisemaphore.h"
|
#include "pisemaphore.h"
|
||||||
#include "pithread.h"
|
#include "pithread.h"
|
||||||
|
|
||||||
class PIHTTPClient;
|
class PIHTTPClient;
|
||||||
|
|
||||||
class CurlThreadPool {
|
class PIP_HTTP_CLIENT_EXPORT CurlThreadPool {
|
||||||
public:
|
public:
|
||||||
void registerClient(PIHTTPClient * c);
|
void registerClient(PIHTTPClient * c);
|
||||||
|
|
||||||
@@ -18,6 +19,7 @@ private:
|
|||||||
CurlThreadPool();
|
CurlThreadPool();
|
||||||
~CurlThreadPool();
|
~CurlThreadPool();
|
||||||
|
|
||||||
|
void destroy();
|
||||||
void threadFunc();
|
void threadFunc();
|
||||||
void procClient(PIHTTPClient * c);
|
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)
|
PRIVATE_DEFINITION_START(PIHTTPClient)
|
||||||
|
CURLM * multi = nullptr;
|
||||||
CURL * handle = nullptr;
|
CURL * handle = nullptr;
|
||||||
curl_slist * header_list = 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)
|
PRIVATE_DEFINITION_END(PIHTTPClient)
|
||||||
|
|
||||||
|
|
||||||
@@ -35,8 +57,8 @@ PIHTTPClient::~PIHTTPClient() {}
|
|||||||
|
|
||||||
bool PIHTTPClient::init() {
|
bool PIHTTPClient::init() {
|
||||||
if (is_cancel) return false;
|
if (is_cancel) return false;
|
||||||
PRIVATE->handle = curl_easy_init();
|
CurlThreadPool::instance();
|
||||||
if (!PRIVATE->handle) return false;
|
if (!PRIVATE->init()) return false;
|
||||||
auto ait = request.arguments().makeIterator();
|
auto ait = request.arguments().makeIterator();
|
||||||
while (ait.next()) {
|
while (ait.next()) {
|
||||||
if (!url.contains('?'))
|
if (!url.contains('?'))
|
||||||
@@ -81,30 +103,64 @@ bool PIHTTPClient::init() {
|
|||||||
|
|
||||||
|
|
||||||
void PIHTTPClient::perform() {
|
void PIHTTPClient::perform() {
|
||||||
if (!PRIVATE->handle) return;
|
if (!PRIVATE->isInit()) return;
|
||||||
if (!is_cancel) {
|
if (!is_cancel) {
|
||||||
// piCout << "perform ...";
|
// piCout << "perform ...";
|
||||||
PITimeMeasurer tm;
|
PITimeMeasurer tm;
|
||||||
CURLcode res = curl_easy_perform(PRIVATE->handle);
|
// CURLcode res = curl_easy_perform(PRIVATE->handle);
|
||||||
// piCout << "done" << res << "in" << tm.elapsed_m() << ", bytes" << buffer_out.size();
|
|
||||||
if (res == CURLE_OK) {
|
curl_multi_add_handle(PRIVATE->multi, PRIVATE->handle);
|
||||||
reply.setBody(std::move(buffer_out));
|
int running_cnt = 1;
|
||||||
if (on_finish) on_finish(reply);
|
do {
|
||||||
} else {
|
CURLMcode mc = curl_multi_perform(PRIVATE->multi, &running_cnt);
|
||||||
if (res == CURLE_ABORTED_BY_CALLBACK || is_cancel) {
|
// piCout << "curl_multi_perform" << mc << running_cnt;
|
||||||
// piCerr << "curl_easy_perform() failed:" << curl_easy_strerror(res);
|
if (!mc) /* wait for activity, timeout or "nothing" */
|
||||||
if (on_abort) on_abort(reply);
|
mc = curl_multi_poll(PRIVATE->multi, nullptr, 0, 50, nullptr);
|
||||||
} else {
|
if (mc || is_cancel) {
|
||||||
last_error = curl_easy_strerror(res);
|
// piCout << "curl_multi_poll() failed";
|
||||||
if (on_error) on_error(reply);
|
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);
|
||||||
|
} else {
|
||||||
|
if (res == CURLE_ABORTED_BY_CALLBACK || is_cancel) {
|
||||||
|
// piCerr << "curl_easy_perform() failed:" << curl_easy_strerror(res);
|
||||||
|
if (on_abort) on_abort(reply);
|
||||||
|
} else {
|
||||||
|
last_error = curl_easy_strerror(res);
|
||||||
|
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;
|
// piCout << last_error;
|
||||||
if (PRIVATE->header_list) curl_slist_free_all(PRIVATE->header_list);
|
PRIVATE->destroy();
|
||||||
curl_easy_cleanup(PRIVATE->handle);
|
|
||||||
PRIVATE->handle = nullptr;
|
|
||||||
PRIVATE->header_list = nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -224,6 +280,7 @@ void PIHTTPClient::start() {
|
|||||||
|
|
||||||
void PIHTTPClient::abort() {
|
void PIHTTPClient::abort() {
|
||||||
is_cancel = true;
|
is_cancel = true;
|
||||||
|
if (PRIVATE->isInit()) curl_multi_wakeup(PRIVATE->multi);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ enum class Method {
|
|||||||
};
|
};
|
||||||
|
|
||||||
enum class Code {
|
enum class Code {
|
||||||
Unknown,
|
Unknown = -1,
|
||||||
Continue = 100,
|
Continue = 100,
|
||||||
SwitchingProtocols = 101,
|
SwitchingProtocols = 101,
|
||||||
Processing = 102,
|
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) {
|
PIHTTP::MessageMutable & PIHTTP::MessageMutable::addHeader(const PIString & header, const PIString & value) {
|
||||||
m_headers[header] = value;
|
m_headers[header] = value;
|
||||||
return *this;
|
return *this;
|
||||||
|
|||||||
@@ -13,6 +13,12 @@ class PIP_EXPORT MessageConst {
|
|||||||
public:
|
public:
|
||||||
PIHTTP::Method method() const { return m_method; }
|
PIHTTP::Method method() const { return m_method; }
|
||||||
PIHTTP::Code code() const { return m_code; }
|
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; }
|
const PIString & path() const { return m_path; }
|
||||||
PIStringList pathList() const { return m_path.split('/').removeAll({}); }
|
PIStringList pathList() const { return m_path.split('/').removeAll({}); }
|
||||||
const PIByteArray & body() const { return m_body; }
|
const PIByteArray & body() const { return m_body; }
|
||||||
|
|||||||
Reference in New Issue
Block a user