PIHTTP::MessageConst code categories
PIHTTPClient migrated to curl_multi_api
This commit is contained in:
@@ -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,30 +103,64 @@ 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();
|
||||
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);
|
||||
// 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);
|
||||
} 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;
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user