Detect any attempt to use non-blocking socket from worker threads

This doesn't work, as non-blocking sockets implementation requires
dispatching events generated by them, which is only possible from the
main thread event loop, and it's better to be upfront about it rather
than failing mysteriously later.
This commit is contained in:
Vadim Zeitlin 2017-08-15 13:53:15 +02:00
parent 8d66bfd7ef
commit 8a29f958a1
2 changed files with 15 additions and 1 deletions

View File

@ -289,6 +289,12 @@ public:
/**
Constructor.
Notice that if the object is created from a worker thread or if it is
created from the main thread but the event loop is not running, @a
flags parameter @em must include ::wxSOCKET_BLOCK as non-blocking
sockets require dispatching events, which can only be done in the main
thread.
@param flags
Socket flags (See wxSocketBase::SetFlags())
*/

View File

@ -25,7 +25,6 @@
#include "wx/socket.h"
#ifndef WX_PRECOMP
#include "wx/app.h"
#include "wx/object.h"
#include "wx/string.h"
#include "wx/intl.h"
@ -1975,6 +1974,15 @@ bool wxSocketBase::SetLocal(const wxIPV4address& local)
wxSocketClient::wxSocketClient(wxSocketFlags flags)
: wxSocketBase(flags, wxSOCKET_CLIENT)
{
// Notice that we don't check for a running event loop here, unlike in
// GetBlockingFlagIfNeeded() because it is common to create the sockets
// before the event loop is entered and we shouldn't break existing code
// doing this as it can still work correctly if it only uses non-blocking
// sockets once the event loop is running.
wxASSERT_MSG( (flags & wxSOCKET_BLOCK) || wxIsMainThread(),
wxS("Non-blocking sockets may only be created ")
wxS("in the main thread") );
m_initialRecvBufferSize =
m_initialSendBufferSize = -1;
}