Use blocking sockets from non-main threads in wxProtocol.

Non-blocking sockets can't work in worker threads without additional locking
as they generate events that can be dispatched from the main thread after the
socket object, created in the worker thread, is already destroyed, so don't
even attempt to use them if wxProtocol object is created from non-main thread.

Also simplify the code by removing the calls to SetFlags(), Notify() and
{Save,Restore}State() and simply put the socket from the beginning in
blocking, wait all mode that it needs to be in.

This, with the fixes in the previous commit, allows wxHTTP and wxFTP to work
from worker threads too.

Test using wxHTTP from a worker thread in the socket client sample.

Closes #17031.
This commit is contained in:
Vadim Zeitlin
2015-07-05 16:18:58 +02:00
parent e18c8fd29a
commit d421373c2e
4 changed files with 57 additions and 39 deletions

View File

@@ -48,8 +48,6 @@ wxHTTP::wxHTTP()
m_read = false;
m_proxy_mode = false;
m_http_response = 0;
SetNotify(wxSOCKET_LOST_FLAG);
}
wxHTTP::~wxHTTP()
@@ -370,16 +368,6 @@ bool wxHTTP::BuildRequest(const wxString& path, const wxString& method)
SetHeader(wxT("Authorization"), GenerateAuthString(m_username, m_password));
}
SaveState();
// we may use non blocking sockets only if we can dispatch events from them
int flags = wxIsMainThread() && wxApp::IsMainLoopRunning() ? wxSOCKET_NONE
: wxSOCKET_BLOCK;
// and we must use wxSOCKET_WAITALL to ensure that all data is sent
flags |= wxSOCKET_WAITALL;
SetFlags(flags);
Notify(false);
wxString buf;
buf.Printf(wxT("%s %s HTTP/1.0\r\n"), method, path);
const wxWX2MBbuf pathbuf = buf.mb_str();
@@ -395,10 +383,8 @@ bool wxHTTP::BuildRequest(const wxString& path, const wxString& method)
wxString tmp_str;
m_lastError = ReadLine(this, tmp_str);
if (m_lastError != wxPROTO_NOERR) {
RestoreState();
if (m_lastError != wxPROTO_NOERR)
return false;
}
if (!tmp_str.Contains(wxT("HTTP/"))) {
// TODO: support HTTP v0.9 which can have no header.
@@ -441,7 +427,7 @@ bool wxHTTP::BuildRequest(const wxString& path, const wxString& method)
m_lastError = wxPROTO_NOERR;
ret_value = ParseHeaders();
RestoreState();
return ret_value;
}
@@ -540,9 +526,6 @@ wxInputStream *wxHTTP::GetInputStream(const wxString& path)
inp_stream->m_read_bytes = 0;
Notify(false);
SetFlags(wxSOCKET_BLOCK | wxSOCKET_WAITALL);
// no error; reset m_lastError
m_lastError = wxPROTO_NOERR;
return inp_stream;