-
-// --------------------------------------------------------------
-// wxSocketBase Wait functions
-// --------------------------------------------------------------
-
-// GRG: I have completely rewritten this family of functions
-// so that they don't depend on event notifications; instead,
-// they poll the socket, using GSocket_Select(), to check for
-// the specified combination of event flags, until an event
-// occurs or until the timeout ellapses. The polling loop
-// calls wxYield(), so this won't block the GUI.
-
-bool wxSocketBase::_Wait(long seconds, long milliseconds, wxSocketEventFlags flags)
-{
- GSocketEventFlags result;
- _wxSocketInternalTimer timer;
- long timeout;
- int state = -1;
-
- // 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
- timer.m_state = &state;
- timer.m_new_val = 0;
- timer.Start(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.
- //
- while (state == -1)
- {
- result = GSocket_Select(m_socket, flags | GSOCK_LOST_FLAG);
-
- // Connection lost
- if (result & GSOCK_LOST_FLAG)
- {
- timer.Stop();
- m_defer_buffer = NULL;
- Close();
- return TRUE;
- }
-
- // Incoming connection (server) or connection established (client)
- if (result & GSOCK_CONNECTION_FLAG)
- {
- timer.Stop();
- m_connected = TRUE;
- m_establishing = FALSE;
- return TRUE;
- }
-
- // If we are in the middle of a deferred R/W, ignore these.
- if ((result & GSOCK_INPUT_FLAG) || (result & GSOCK_OUTPUT_FLAG))
- {
- if (m_defer_buffer == NULL)
- {
- timer.Stop();
- return TRUE;
- }
- }
-
- wxYield();
- }
-
- timer.Stop();
- return FALSE;
-}
-
-bool wxSocketBase::Wait(long seconds, long milliseconds)
-{
- return _Wait(seconds, milliseconds, GSOCK_INPUT_FLAG |
- GSOCK_OUTPUT_FLAG |
- GSOCK_CONNECTION_FLAG |
- GSOCK_LOST_FLAG);
-}
-
-bool wxSocketBase::WaitForRead(long seconds, long milliseconds)
-{
- return _Wait(seconds, milliseconds, GSOCK_INPUT_FLAG);
-}
-
-bool wxSocketBase::WaitForWrite(long seconds, long milliseconds)
-{
- return _Wait(seconds, milliseconds, GSOCK_OUTPUT_FLAG);
-}
-
-bool wxSocketBase::WaitForLost(long seconds, long milliseconds)
-{
- return _Wait(seconds, milliseconds, GSOCK_LOST_FLAG);
-}