- wxNode *node;
- wxSocketState *state;
-
- node = m_states.Last();
- if (!node)
- return;
-
- state = (wxSocketState *)node->Data();
-
- SetFlags(state->socket_flags);
- m_neededreq = state->evt_notify_state;
- m_cbk = state->c_callback;
- m_cdata = state->c_callback_data;
- Notify(state->notify_state);
-
- delete node;
- delete state;
-}
-
-
-// --------------------------------------------------------------
-// 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 PROCESS_EVENTS(), 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
- if (timeout)
- {
- 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.
- //
- // Do this at least once (important if timeout == 0, when
- // we are just polling)
- do
- {
- result = GSocket_Select(m_socket, flags | GSOCK_LOST_FLAG);
-
- // Connection lost
- if (result & GSOCK_LOST_FLAG)
+ if ( m_fd == INVALID_SOCKET )
+ return (wxSOCKET_LOST_FLAG & flags);
+
+ struct timeval tv;
+ if ( timeout )
+ tv = *timeout;
+ else
+ tv.tv_sec = tv.tv_usec = 0;
+
+ // prepare the FD sets, passing NULL for the one(s) we don't use
+ fd_set
+ readfds, *preadfds = NULL,
+ writefds, *pwritefds = NULL,
+ exceptfds; // always want to know about errors
+
+ if ( flags & wxSOCKET_INPUT_FLAG )
+ {
+ preadfds = &readfds;
+ wxFD_ZERO(preadfds);
+ wxFD_SET(m_fd, preadfds);
+ }
+
+ // when using non-blocking connect() the socket becomes connected
+ // (successfully or not) when it becomes writable
+ if ( flags & (wxSOCKET_OUTPUT_FLAG | wxSOCKET_CONNECTION_FLAG) )
+ {
+ pwritefds = &writefds;
+ wxFD_ZERO(pwritefds);
+ wxFD_SET(m_fd, pwritefds);
+ }
+
+ wxFD_ZERO(&exceptfds);
+ wxFD_SET(m_fd, &exceptfds);
+
+ const int rc = select(m_fd + 1, preadfds, pwritefds, &exceptfds, &tv);
+
+ // check for errors first
+ if ( rc == -1 || wxFD_ISSET(m_fd, &exceptfds) )