+ 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);
+ }
+
+ returnedpos = returncode - WAIT_OBJECT_0;
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "return pos is : %d\n", returnedpos);
+
+ if (nhandles && (returnedpos < nhandles))
+ {
+ DWORD waitstatus;
+
+ if (sp.status > 0)
+ retcode += sp.status;
+
+ if ((writePipePos != -1) && (returnedpos < writePipePos))
+ {
+ GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
+ retcode += write_handles;
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Added write pipe\n");
+ }
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "ReadPipes is : %d\n", readPipes);
+ /* We have some pipes ready for read. */
+ if (returnedpos < readPipes)
+ {
+ for (i = 0; i < readPipes; i++)
+ {
+ DWORD error;
+ BOOL bret;
+
+ SetLastError (0);
+ waitstatus = 0;
+ bret =
+ PeekNamedPipe (readArray[i]->h, NULL, 0, NULL, &waitstatus, NULL);
+ error = GetLastError ();
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Peek at read pipe %d (0x%x) returned %d (%d bytes available) GLE %u\n",
+ i, readArray[i]->h, bret, waitstatus, error);
+ if (bret == 0)