Commit 5163b1a6 authored by Max Kellermann's avatar Max Kellermann

lib/curl/Request: require the caller to explicitly register the request

This allows constructing an instance in any thread, and register it inside the IOThread later.
parent 860aa9d6
...@@ -373,6 +373,8 @@ CurlInputStream::InitEasy() ...@@ -373,6 +373,8 @@ CurlInputStream::InitEasy()
request_headers.Clear(); request_headers.Clear();
request_headers.Append("Icy-Metadata: 1"); request_headers.Append("Icy-Metadata: 1");
request->SetOption(CURLOPT_HTTPHEADER, request_headers.Get()); request->SetOption(CURLOPT_HTTPHEADER, request_headers.Get());
request->Start();
} }
void void
......
...@@ -62,8 +62,6 @@ CurlRequest::CurlRequest(CurlGlobal &_global, const char *url, ...@@ -62,8 +62,6 @@ CurlRequest::CurlRequest(CurlGlobal &_global, const char *url,
easy.SetOption(CURLOPT_NOSIGNAL, 1l); easy.SetOption(CURLOPT_NOSIGNAL, 1l);
easy.SetOption(CURLOPT_CONNECTTIMEOUT, 10l); easy.SetOption(CURLOPT_CONNECTTIMEOUT, 10l);
easy.SetOption(CURLOPT_URL, url); easy.SetOption(CURLOPT_URL, url);
global.Add(easy.Get(), *this);
} }
CurlRequest::~CurlRequest() CurlRequest::~CurlRequest()
...@@ -72,18 +70,39 @@ CurlRequest::~CurlRequest() ...@@ -72,18 +70,39 @@ CurlRequest::~CurlRequest()
} }
void void
CurlRequest::Start()
{
assert(!registered);
global.Add(easy.Get(), *this);
registered = true;
}
void
CurlRequest::Stop()
{
assert(registered);
global.Remove(easy.Get());
registered = false;
}
void
CurlRequest::FreeEasy() CurlRequest::FreeEasy()
{ {
if (!easy) if (!easy)
return; return;
global.Remove(easy.Get()); if (registered)
Stop();
easy = nullptr; easy = nullptr;
} }
void void
CurlRequest::Resume() CurlRequest::Resume()
{ {
assert(registered);
curl_easy_pause(easy.Get(), CURLPAUSE_CONT); curl_easy_pause(easy.Get(), CURLPAUSE_CONT);
if (IsCurlOlderThan(0x072000)) if (IsCurlOlderThan(0x072000))
......
...@@ -58,7 +58,12 @@ class CurlRequest { ...@@ -58,7 +58,12 @@ class CurlRequest {
/** error message provided by libcurl */ /** error message provided by libcurl */
char error_buffer[CURL_ERROR_SIZE]; char error_buffer[CURL_ERROR_SIZE];
bool registered = false;
public: public:
/**
* To start sending the request, call Start().
*/
CurlRequest(CurlGlobal &_global, const char *url, CurlRequest(CurlGlobal &_global, const char *url,
CurlResponseHandler &_handler); CurlResponseHandler &_handler);
~CurlRequest(); ~CurlRequest();
...@@ -66,6 +71,21 @@ public: ...@@ -66,6 +71,21 @@ public:
CurlRequest(const CurlRequest &) = delete; CurlRequest(const CurlRequest &) = delete;
CurlRequest &operator=(const CurlRequest &) = delete; CurlRequest &operator=(const CurlRequest &) = delete;
/**
* Register this request via CurlGlobal::Add(), which starts
* the request.
*
* This method must be called in the event loop thread.
*/
void Start();
/**
* Unregister this request via CurlGlobal::Remove().
*
* This method must be called in the event loop thread.
*/
void Stop();
CURL *Get() { CURL *Get() {
return easy.Get(); return easy.Get();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment