Factor out socket flag selection into GetBlockingFlagIfNeeded()

No real changes, just refactor wxProtocol ctor to use a new function
that can be reused elsewhere too.
This commit is contained in:
Vadim Zeitlin 2017-08-15 19:26:38 +02:00
parent 01fac4b748
commit ba8bab2282
3 changed files with 20 additions and 3 deletions

View File

@ -225,6 +225,16 @@ public:
bool IsNoWait() const { return ((m_flags & wxSOCKET_NOWAIT) != 0); }
wxSocketType GetType() const { return m_type; }
// Helper returning wxSOCKET_NONE if non-blocking sockets can be used, i.e.
// the socket is being created in the main thread and the event loop is
// running, or wxSOCKET_BLOCK otherwise.
//
// This is an internal function used only by wxWidgets itself, user code
// should decide if it wants blocking sockets or not and use the
// appropriate style instead of using it (but wxWidgets has to do it like
// this for compatibility with the original network classes behaviour).
static int GetBlockingFlagIfNeeded();
private:
friend class wxSocketClient;
friend class wxSocketServer;

View File

@ -65,9 +65,7 @@ wxIMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxObject);
wxProtocol::wxProtocol()
#if wxUSE_SOCKETS
// Only use non blocking sockets if we can dispatch events.
: wxSocketClient((wxIsMainThread() && wxApp::IsMainLoopRunning()
? wxSOCKET_NONE
: wxSOCKET_BLOCK) | wxSOCKET_WAITALL)
: wxSocketClient(wxSocketClient::GetBlockingFlagIfNeeded() | wxSOCKET_WAITALL)
#endif
{
m_lastError = wxPROTO_NOERR;

View File

@ -25,6 +25,7 @@
#include "wx/socket.h"
#ifndef WX_PRECOMP
#include "wx/app.h"
#include "wx/object.h"
#include "wx/string.h"
#include "wx/intl.h"
@ -928,6 +929,14 @@ wxSocketError wxSocketBase::LastError() const
return m_impl->GetError();
}
/* static */
int wxSocketBase::GetBlockingFlagIfNeeded()
{
return wxIsMainThread() && wxApp::IsMainLoopRunning()
? wxSOCKET_NONE
: wxSOCKET_BLOCK;
}
// --------------------------------------------------------------------------
// Basic IO calls
// --------------------------------------------------------------------------