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) CurlGlobal::CurlGlobal(EventLoop &_loop)
:DeferredMonitor(_loop), :defer_read_info(_loop, BIND_THIS_METHOD(ReadInfo)),
timeout_event(_loop, BIND_THIS_METHOD(OnTimeout)) timeout_event(_loop, BIND_THIS_METHOD(OnTimeout))
{ {
multi.SetOption(CURLMOPT_SOCKETFUNCTION, CurlSocket::SocketFunction); 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_socket_action() failed: %s",
curl_multi_strerror(mcode)); curl_multi_strerror(mcode));
DeferredMonitor::Schedule(); defer_read_info.Schedule();
}
void
CurlGlobal::RunDeferred()
{
ReadInfo();
} }

View File

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

View File

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

View File

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