add http_client library, using libcurl
take out common http entities to http_common dir
This commit is contained in:
75
libs/http_client/curl_thread_pool_p.cpp
Normal file
75
libs/http_client/curl_thread_pool_p.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
|
||||
#include "curl_thread_pool_p.h"
|
||||
|
||||
#include "pihttpclient.h"
|
||||
#include "pitime.h"
|
||||
|
||||
|
||||
CurlThreadPool::CurlThreadPool() {
|
||||
piForTimes(10) {
|
||||
auto * t = new PIThread([this]() { threadFunc(); }, true);
|
||||
threads << t;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void CurlThreadPool::threadFunc() {
|
||||
if (exiting) return;
|
||||
// piCout << "threadFuncl w ...";
|
||||
sem.acquire();
|
||||
// piCout << "threadFuncl wdone";
|
||||
if (exiting) return;
|
||||
PIHTTPClient * c = nullptr;
|
||||
{
|
||||
auto cr = clients.getRef();
|
||||
if (cr->isEmpty()) return;
|
||||
c = cr->dequeue();
|
||||
// piCout << "threadFuncl get c";
|
||||
}
|
||||
// piCout << "threadFuncl proc c";
|
||||
procClient(c);
|
||||
// piCout << "threadFuncl end";
|
||||
}
|
||||
|
||||
|
||||
void CurlThreadPool::procClient(PIHTTPClient * c) {
|
||||
if (c->init()) c->perform();
|
||||
delete c;
|
||||
}
|
||||
|
||||
|
||||
void CurlThreadPool::registerClient(PIHTTPClient * c) {
|
||||
clients.getRef()->enqueue(c);
|
||||
sem.release();
|
||||
// piCout << "registerClient";
|
||||
}
|
||||
|
||||
|
||||
CurlThreadPool * CurlThreadPool::instance() {
|
||||
static CurlThreadPool ret;
|
||||
return &ret;
|
||||
}
|
||||
Reference in New Issue
Block a user