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:
Vadim Zeitlin
2021-01-16 23:48:15 +01:00
parent 4c19572d9d
commit 970ab0a1ae
3 changed files with 24 additions and 19 deletions

View File

@@ -257,7 +257,7 @@ public:
const wxWebResponse& response = wxWebResponse(),
const wxString& errorDesc = wxString())
: wxEvent(id, type),
m_state(state), m_response(response), m_data(NULL), m_dataSize(0),
m_state(state), m_response(response),
m_errorDescription(errorDesc)
{ }
@@ -271,12 +271,11 @@ public:
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)
{ m_data = buffer; m_dataSize = size; }
void SetDataBuffer(const wxMemoryBuffer& dataBuf) { m_dataBuf = dataBuf; }
wxEvent* Clone() const wxOVERRIDE { return new wxWebRequestEvent(*this); }
@@ -284,8 +283,7 @@ private:
wxWebRequest::State m_state;
const wxWebResponse m_response; // may be invalid
wxString m_dataFile;
const void* m_data;
size_t m_dataSize;
wxMemoryBuffer m_dataBuf;
wxString m_errorDescription;
};