lib/curl/{Global,Request}: migrate from DeferredMonitor to DeferEvent

This commit is contained in:
Max Kellermann 2017-09-21 22:44:44 +02:00
parent 9df4853e23
commit 431eb7bc8c
4 changed files with 21 additions and 24 deletions

View File

@ -96,7 +96,7 @@ private:
};
CurlGlobal::CurlGlobal(EventLoop &_loop)
:DeferredMonitor(_loop),
:defer_read_info(_loop, BIND_THIS_METHOD(ReadInfo)),
timeout_event(_loop, BIND_THIS_METHOD(OnTimeout))
{
multi.SetOption(CURLMOPT_SOCKETFUNCTION, CurlSocket::SocketFunction);
@ -264,11 +264,5 @@ CurlGlobal::SocketAction(curl_socket_t fd, int ev_bitmask)
"curl_multi_socket_action() failed: %s",
curl_multi_strerror(mcode));
DeferredMonitor::Schedule();
}
void
CurlGlobal::RunDeferred()
{
ReadInfo();
defer_read_info.Schedule();
}

View File

@ -32,7 +32,7 @@
#include "Multi.hxx"
#include "event/TimerEvent.hxx"
#include "event/DeferredMonitor.hxx"
#include "event/DeferEvent.hxx"
class CurlSocket;
class CurlRequest;
@ -40,15 +40,19 @@ class CurlRequest;
/**
* Manager for the global CURLM object.
*/
class CurlGlobal final : DeferredMonitor {
class CurlGlobal final {
CurlMulti multi;
DeferEvent defer_read_info;
TimerEvent timeout_event;
public:
explicit CurlGlobal(EventLoop &_loop);
using DeferredMonitor::GetEventLoop;
EventLoop &GetEventLoop() {
return timeout_event.GetEventLoop();
}
void Add(CURL *easy, CurlRequest &request);
void Remove(CURL *easy);
@ -86,9 +90,6 @@ private:
/* callback for #timeout_event */
void OnTimeout();
/* virtual methods from class DeferredMonitor */
void RunDeferred() override;
};
#endif

View File

@ -46,8 +46,9 @@
CurlRequest::CurlRequest(CurlGlobal &_global, const char *url,
CurlResponseHandler &_handler)
:DeferredMonitor(_global.GetEventLoop()),
global(_global), handler(_handler)
:global(_global), handler(_handler),
postpone_error_event(global.GetEventLoop(),
BIND_THIS_METHOD(OnPostponeError))
{
error_buffer[0] = 0;
@ -241,7 +242,7 @@ CurlRequest::DataReceived(const void *ptr, size_t received_size)
/* move the CurlResponseHandler::OnError() call into a
"safe" stack frame */
postponed_error = std::current_exception();
DeferredMonitor::Schedule();
postpone_error_event.Schedule();
return CURL_WRITEFUNC_PAUSE;
}
@ -260,7 +261,7 @@ CurlRequest::WriteFunction(void *ptr, size_t size, size_t nmemb, void *stream)
}
void
CurlRequest::RunDeferred()
CurlRequest::OnPostponeError()
{
assert(postponed_error);

View File

@ -31,7 +31,7 @@
#define CURL_REQUEST_HXX
#include "Easy.hxx"
#include "event/DeferredMonitor.hxx"
#include "event/DeferEvent.hxx"
#include <map>
#include <string>
@ -41,7 +41,7 @@ struct StringView;
class CurlGlobal;
class CurlResponseHandler;
class CurlRequest final : DeferredMonitor {
class CurlRequest final {
CurlGlobal &global;
CurlResponseHandler &handler;
@ -60,13 +60,15 @@ class CurlRequest final : DeferredMonitor {
/**
* An exception caught by DataReceived(), which will be
* forwarded into a "safe" stack frame by
* DeferredMonitor::RunDeferred(). This works around the
* #postpone_error_event. This works around the
* problem that libcurl crashes if you call
* curl_multi_remove_handle() from within the WRITEFUNCTION
* (i.e. DataReceived()).
*/
std::exception_ptr postponed_error;
DeferEvent postpone_error_event;
/** error message provided by libcurl */
char error_buffer[CURL_ERROR_SIZE];
@ -134,6 +136,8 @@ private:
void HeaderFunction(StringView s);
void OnPostponeError();
/** called by curl when new data is available */
static size_t _HeaderFunction(void *ptr, size_t size, size_t nmemb,
void *stream);
@ -141,9 +145,6 @@ private:
/** called by curl when new data is available */
static size_t WriteFunction(void *ptr, size_t size, size_t nmemb,
void *stream);
/* virtual methods from class DeferredMonitor */
void RunDeferred() override;
};
#endif