+ }
+ }
+ }
+
+ sp.status = 0;
+
+ if (nfds > 0)
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Adding the socket event to the array as %d\n", nhandles);
+ handle_array[nhandles++] = select_finished_event;
+ if (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
+ sp.tv = NULL;
+ else
+ {
+ select_timeout.tv_sec = timeout.rel_value / GNUNET_TIME_UNIT_SECONDS.rel_value;
+ select_timeout.tv_usec = 1000 * (timeout.rel_value -
+ (select_timeout.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value));
+ sp.tv = &select_timeout;
+ }
+ FD_SET (select_wakeup_socket, &aread);
+ sp.r = &aread;
+ sp.w = &awrite;
+ sp.e = &aexcept;
+ /* Failed connections cause sockets to be set in errorfds on W32,
+ * but on POSIX it should set them in writefds.
+ * First copy all awrite sockets to aexcept, later we'll
+ * check aexcept and set its contents in awrite as well
+ * Sockets are also set in errorfds when OOB data is available,
+ * but we don't use OOB data.
+ */
+ for (i = 0; i < awrite.fd_count; i++)
+ FD_SET (awrite.fd_array[i], &aexcept);
+ ResetEvent (select_finished_event);
+ SetEvent (select_standby_event);
+ }
+
+ handle_array[nhandles] = NULL;
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "nfds: %d, handles: %d, will wait: %llu ms\n",
+ nfds, nhandles, (unsigned long long) ms_total);
+ if (nhandles)
+ {
+ returncode =
+ WaitForMultipleObjects (nhandles, handle_array, FALSE, ms_total);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "WaitForMultipleObjects Returned : %d\n",
+ returncode);
+ }
+ else if (nfds > 0)
+ {
+ i = (int) WaitForSingleObject (select_finished_event, INFINITE);
+ returncode = WAIT_TIMEOUT;
+ }
+ else
+ {
+ /* Shouldn't come this far. If it does - investigate. */
+ GNUNET_assert (0);
+ }
+
+ if (nfds > 0)
+ {
+ /* Don't wake up select-thread when delay is 0, it should return immediately
+ * and wake up by itself.
+ */
+ if (ms_total != 0)
+ i = send (select_send_socket, (const char *) &returnedpos, 1, 0);
+ i = (int) WaitForSingleObject (select_finished_event, INFINITE);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished waiting for the select thread: %d %d\n", i, sp.status);
+ if (ms_total != 0)
+ {
+ do
+ {
+ i = recv (select_wakeup_socket, (char *) &returnedpos, 1, 0);
+ } while (i == 1);
+ }
+ /* Check aexcept, add its contents to awrite */
+ for (i = 0; i < aexcept.fd_count; i++)
+ FD_SET (aexcept.fd_array[i], &awrite);
+ }