Make sure wxEVT_WEBREQUEST_DATA is processed in the main thread
This event was processed in a worker thread, which was different from all the other events and also almost surely not thread-safe, so change this and queue it for processing in the main thread instead. Use wxMemoryBuffer instead of non-owning pointer in wxWebRequestEvent and reset the buffer used internally every time to ensure the data is still available by the time the event is processed. Also increase the amount of data downloaded in the "advanced" page of the sample as it has to be greater than wxWEBREQUEST_BUFFER_SIZE, which is currently 64KiB, to have a chance of seeing the value actually change, otherwise all the data arrives in a single event. As it is, using the maximal size supported by the httpbin service, we only get 2 events.
This commit is contained in:
@@ -257,7 +257,7 @@ public:
|
|||||||
const wxWebResponse& response = wxWebResponse(),
|
const wxWebResponse& response = wxWebResponse(),
|
||||||
const wxString& errorDesc = wxString())
|
const wxString& errorDesc = wxString())
|
||||||
: wxEvent(id, type),
|
: wxEvent(id, type),
|
||||||
m_state(state), m_response(response), m_data(NULL), m_dataSize(0),
|
m_state(state), m_response(response),
|
||||||
m_errorDescription(errorDesc)
|
m_errorDescription(errorDesc)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
@@ -271,12 +271,11 @@ public:
|
|||||||
|
|
||||||
void SetDataFile(const wxString& dataFile) { m_dataFile = dataFile; }
|
void SetDataFile(const wxString& dataFile) { m_dataFile = dataFile; }
|
||||||
|
|
||||||
const void* GetDataBuffer() const { return m_data; }
|
const void* GetDataBuffer() const { return m_dataBuf.GetData(); }
|
||||||
|
|
||||||
size_t GetDataSize() const { return m_dataSize; }
|
size_t GetDataSize() const { return m_dataBuf.GetDataLen(); }
|
||||||
|
|
||||||
void SetDataBuffer(const void* buffer, size_t size)
|
void SetDataBuffer(const wxMemoryBuffer& dataBuf) { m_dataBuf = dataBuf; }
|
||||||
{ m_data = buffer; m_dataSize = size; }
|
|
||||||
|
|
||||||
wxEvent* Clone() const wxOVERRIDE { return new wxWebRequestEvent(*this); }
|
wxEvent* Clone() const wxOVERRIDE { return new wxWebRequestEvent(*this); }
|
||||||
|
|
||||||
@@ -284,8 +283,7 @@ private:
|
|||||||
wxWebRequest::State m_state;
|
wxWebRequest::State m_state;
|
||||||
const wxWebResponse m_response; // may be invalid
|
const wxWebResponse m_response; // may be invalid
|
||||||
wxString m_dataFile;
|
wxString m_dataFile;
|
||||||
const void* m_data;
|
wxMemoryBuffer m_dataBuf;
|
||||||
size_t m_dataSize;
|
|
||||||
wxString m_errorDescription;
|
wxString m_errorDescription;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -364,22 +364,19 @@ public:
|
|||||||
void OnRequestData(wxWebRequestEvent& evt)
|
void OnRequestData(wxWebRequestEvent& evt)
|
||||||
{
|
{
|
||||||
// Count zero bytes in data buffer
|
// Count zero bytes in data buffer
|
||||||
bool notify = false;
|
|
||||||
|
|
||||||
const char* p = (const char*) evt.GetDataBuffer();
|
const char* p = (const char*) evt.GetDataBuffer();
|
||||||
for ( size_t i = 0; i < evt.GetDataSize(); i++ )
|
for ( size_t i = 0; i < evt.GetDataSize(); i++ )
|
||||||
{
|
{
|
||||||
if ( *p == 0 )
|
if ( *p == 0 )
|
||||||
{
|
|
||||||
m_advCount++;
|
m_advCount++;
|
||||||
notify = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
p++;
|
p++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( notify )
|
UpdateAdvCount();
|
||||||
CallAfter(&WebRequestFrame::UpdateAdvCount);
|
|
||||||
|
// Make sure the new text is immediately visible.
|
||||||
|
m_advCountStaticText->Update();
|
||||||
}
|
}
|
||||||
|
|
||||||
void UpdateAdvCount()
|
void UpdateAdvCount()
|
||||||
@@ -425,7 +422,7 @@ public:
|
|||||||
defaultURL = "https://github.com/wxWidgets/wxWidgets/releases/download/v3.1.1/wxWidgets-3.1.1.7z";
|
defaultURL = "https://github.com/wxWidgets/wxWidgets/releases/download/v3.1.1/wxWidgets-3.1.1.7z";
|
||||||
break;
|
break;
|
||||||
case Page_Advanced:
|
case Page_Advanced:
|
||||||
defaultURL = "https://httpbin.org/bytes/64000";
|
defaultURL = "https://httpbin.org/bytes/100000";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
m_urlTextCtrl->SetValue(defaultURL);
|
m_urlTextCtrl->SetValue(defaultURL);
|
||||||
|
@@ -692,10 +692,20 @@ void wxWebResponseImpl::ReportDataReceived(size_t sizeReceived)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case wxWebRequest::Storage_None:
|
case wxWebRequest::Storage_None:
|
||||||
wxWebRequestEvent evt(wxEVT_WEBREQUEST_DATA, m_request.GetId(), wxWebRequest::State_Active);
|
wxWebRequestEvent* const evt = new wxWebRequestEvent
|
||||||
evt.SetDataBuffer(m_readBuffer.GetData(), m_readBuffer.GetDataLen());
|
(
|
||||||
m_request.GetHandler()->ProcessEvent(evt);
|
wxEVT_WEBREQUEST_DATA,
|
||||||
m_readBuffer.Clear();
|
m_request.GetId(),
|
||||||
|
wxWebRequest::State_Active
|
||||||
|
);
|
||||||
|
evt->SetDataBuffer(m_readBuffer);
|
||||||
|
|
||||||
|
m_request.GetHandler()->QueueEvent(evt);
|
||||||
|
|
||||||
|
// Make sure we switch to a different buffer instead of just
|
||||||
|
// clearing the current one, which will be needed by the event
|
||||||
|
// handler when it's finally called in the main thread.
|
||||||
|
m_readBuffer = wxMemoryBuffer();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user