Implement wxWebRequestCURL::Cancel()

This commit is contained in:
Tobias Taschner
2018-11-05 22:12:41 +01:00
parent fe4a5343f7
commit d2420a064c
2 changed files with 28 additions and 2 deletions

View File

@@ -13,6 +13,7 @@
#if wxUSE_WEBREQUEST_CURL #if wxUSE_WEBREQUEST_CURL
#include "wx/thread.h" #include "wx/thread.h"
#include "wx/vector.h"
#include "curl/curl.h" #include "curl/curl.h"
@@ -117,6 +118,8 @@ public:
bool StartRequest(wxWebRequestCURL& request); bool StartRequest(wxWebRequestCURL& request);
void CancelRequest(wxWebRequestCURL* request);
protected: protected:
wxThread::ExitCode Entry() wxOVERRIDE; wxThread::ExitCode Entry() wxOVERRIDE;
@@ -125,6 +128,8 @@ private:
wxCondition m_condition; wxCondition m_condition;
wxMutex m_mutex; wxMutex m_mutex;
bool m_shuttingDown; bool m_shuttingDown;
wxMutex m_cancelledMutex;
wxVector< wxObjectDataPtr<wxWebRequestCURL> > m_cancelledRequests;
void Initialize(); void Initialize();

View File

@@ -243,7 +243,7 @@ bool wxWebRequestCURL::StartRequest()
void wxWebRequestCURL::Cancel() void wxWebRequestCURL::Cancel()
{ {
wxFAIL_MSG("not implemented"); static_cast<wxWebSessionCURL&>(GetSession()).CancelRequest(this);
} }
void wxWebRequestCURL::HandleCompletion() void wxWebRequestCURL::HandleCompletion()
@@ -393,6 +393,18 @@ wxThread::ExitCode wxWebSessionCURL::Entry()
while ( activeRequests ) while ( activeRequests )
{ {
// Handle cancelled requests
{
wxMutexLocker lock(m_cancelledMutex);
while ( !m_cancelledRequests.empty() )
{
wxObjectDataPtr<wxWebRequestCURL> request(m_cancelledRequests.back());
m_cancelledRequests.pop_back();
curl_multi_remove_handle(m_handle, request->GetHandle());
request->SetState(wxWebRequest::State_Cancelled);
}
}
// Instruct CURL to work on requests // Instruct CURL to work on requests
curl_multi_perform(m_handle, &activeRequests); curl_multi_perform(m_handle, &activeRequests);
@@ -413,7 +425,7 @@ wxThread::ExitCode wxWebSessionCURL::Entry()
{ {
// Wait for CURL work to finish // Wait for CURL work to finish
int numfds; int numfds;
curl_multi_wait(m_handle, NULL, 0, 1000, &numfds); curl_multi_wait(m_handle, NULL, 0, 500, &numfds);
if ( !numfds ) if ( !numfds )
{ {
@@ -458,6 +470,15 @@ bool wxWebSessionCURL::StartRequest(wxWebRequestCURL & request)
return true; return true;
} }
void wxWebSessionCURL::CancelRequest(wxWebRequestCURL* request)
{
// Add the request to a list of threads that will be removed from the curl
// multi handle in the worker thread
wxMutexLocker lock(m_cancelledMutex);
request->IncRef();
m_cancelledRequests.push_back(wxObjectDataPtr<wxWebRequestCURL>(request));
}
// static // static
void wxWebSessionCURL::InitializeCURL() void wxWebSessionCURL::InitializeCURL()
{ {