- GSocketEventFlags result;
- _wxSocketInternalTimer timer;
- long timeout;
- int state = -1;
-
- // Set this to TRUE to interrupt ongoing waits
- m_interrupt = FALSE;
-
- // Check for valid socket
- if (!m_socket)
- return FALSE;
-
- // If it is not a server, it must be connected or establishing connection
- if ((m_type != SOCK_SERVER) && (!m_connected && !m_establishing))
- return FALSE;
-
- // Check for valid timeout value
- if (seconds != -1)
- timeout = seconds * 1000 + milliseconds;
- else
- timeout = m_timeout * 1000;
-
- // Activate timer
- if (timeout)
- {
- timer.m_state = &state;
- timer.m_new_val = 0;
- timer.Start((int)timeout, TRUE);
- }
-
- // Active polling (without using events)
- //
- // NOTE: this duplicates some of the code in OnRequest (lost
- // connection and connection establishment handling) but this
- // doesn't hurt. It has to be here because the event might
- // be a bit delayed, and it has to be in OnRequest as well
- // because maybe the WaitXXX functions are not being used.
- //
- // Do this at least once (important if timeout == 0, when
- // we are just polling). Also, if just polling, do not yield.
- //
- while (state == -1)
- {
- result = GSocket_Select(m_socket, flags | GSOCK_LOST_FLAG);
-
- // The order in which we check events is important; in particular,
- // GSOCKET_LOST events should be checked last (there might be still
- // data to read in the incoming queue even after the peer has closed
- // the connection).
-
- // Incoming connection (server) or connection established (client)
- if (result & GSOCK_CONNECTION_FLAG)
+ wxCHECK_MSG( m_socket, false, "can't wait on invalid socket" );
+
+ // This can be set to true from Interrupt() to exit this function a.s.a.p.
+ m_interrupt = false;
+
+
+ // Use either the provided timeout or the default timeout value associated
+ // with this socket.
+ //
+ // TODO: allow waiting forever, see #9443
+ const long timeout = seconds == -1 ? m_timeout * 1000
+ : seconds * 1000 + milliseconds;
+ const wxMilliClock_t timeEnd = wxGetLocalTimeMillis() + timeout;
+
+ // Get the active event loop which we'll use for the message dispatching
+ // when running in the main thread
+ wxEventLoopBase *eventLoop;
+ if ( wxIsMainThread() )