From ba8bab2282b0fa24c0ccea24e6dd77f1e556a791 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 15 Aug 2017 19:26:38 +0200 Subject: [PATCH] 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. --- include/wx/socket.h | 10 ++++++++++ src/common/protocol.cpp | 4 +--- src/common/socket.cpp | 9 +++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/include/wx/socket.h b/include/wx/socket.h index daded4bf05..4a66716984 100644 --- a/include/wx/socket.h +++ b/include/wx/socket.h @@ -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; diff --git a/src/common/protocol.cpp b/src/common/protocol.cpp index f6250252c3..ffe0e0f086 100644 --- a/src/common/protocol.cpp +++ b/src/common/protocol.cpp @@ -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; diff --git a/src/common/socket.cpp b/src/common/socket.cpp index 24214b1859..37cd53783a 100644 --- a/src/common/socket.cpp +++ b/src/common/socket.cpp @@ -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 // --------------------------------------------------------------------------