+ {
+ Sleep (ms_total);
+ return 0;
+ }
+
+ if (select_thread == NULL)
+ {
+ SOCKET select_listening_socket = -1;
+ struct sockaddr_in s_in;
+ int alen;
+ int res;
+ unsigned long p;
+
+ select_standby_event = CreateEvent (NULL, TRUE, FALSE, NULL);
+ select_finished_event = CreateEvent (NULL, TRUE, FALSE, NULL);
+
+ select_wakeup_socket = WSASocket (AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
+
+ select_listening_socket = WSASocket (AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
+
+ p = 1;
+ res = ioctlsocket (select_wakeup_socket, FIONBIO, &p);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Select thread initialization: ioctlsocket() returns %d\n", res);
+
+ alen = sizeof (s_in);
+ s_in.sin_family = AF_INET;
+ s_in.sin_port = 0;
+ s_in.sin_addr.S_un.S_un_b.s_b1 = 127;
+ s_in.sin_addr.S_un.S_un_b.s_b2 = 0;
+ s_in.sin_addr.S_un.S_un_b.s_b3 = 0;
+ s_in.sin_addr.S_un.S_un_b.s_b4 = 1;
+ res = bind (select_listening_socket, (const struct sockaddr *) &s_in, sizeof (s_in));
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Select thread initialization: bind() returns %d\n", res);
+
+ res = getsockname (select_listening_socket, (struct sockaddr *) &s_in, &alen);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Select thread initialization: getsockname() returns %d\n", res);
+
+ res = listen (select_listening_socket, SOMAXCONN);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Select thread initialization: listen() returns %d\n", res);
+
+ res = connect (select_wakeup_socket, (const struct sockaddr *) &s_in, sizeof (s_in));
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Select thread initialization: connect() returns %d\n", res);
+
+ select_send_socket = accept (select_listening_socket, (struct sockaddr *) &s_in, &alen);
+
+ closesocket (select_listening_socket);
+
+ sp.wakeup = select_finished_event;
+ sp.standby = select_standby_event;
+ sp.wakeup_socket = select_wakeup_socket;
+
+ select_thread = CreateThread (NULL, 0, _selector, &sp, 0, NULL);
+ }
+
+
+ handles_read = GNUNET_CONTAINER_slist_create ();
+ handles_write = GNUNET_CONTAINER_slist_create ();
+ handles_except = GNUNET_CONTAINER_slist_create ();
+ FD_ZERO (&aread);
+ FD_ZERO (&awrite);
+ FD_ZERO (&aexcept);
+#if DEBUG_NETWORK
+ FD_ZERO (&bread);
+ FD_ZERO (&bwrite);
+ FD_ZERO (&bexcept);
+#endif
+ if (rfds)
+ {
+ FD_COPY (&rfds->sds, &aread);
+#if DEBUG_NETWORK
+ FD_COPY (&rfds->sds, &bread);
+#endif
+ }
+ if (wfds)
+ {
+ FD_COPY (&wfds->sds, &awrite);
+#if DEBUG_NETWORK
+ FD_COPY (&wfds->sds, &bwrite);
+#endif
+ }
+ if (efds)
+ {
+ FD_COPY (&efds->sds, &aexcept);
+#if DEBUG_NETWORK
+ FD_COPY (&efds->sds, &bexcept);
+#endif
+ }
+
+ /* Start by doing a fast check on sockets and pipes (without waiting). It is cheap, and is sufficient most of the time.
+ By profiling we detected that to be true in 90% of the cases.
+ */
+
+ /* Do the select now */
+ select_timeout.tv_sec = 0;
+ select_timeout.tv_usec = 0;
+
+ /* Copy all the writes to the except, so we can detect connect() errors */
+ for (i = 0; i < awrite.fd_count; i++)
+ FD_SET (awrite.fd_array[i], &aexcept);
+ if (aread.fd_count > 0 || awrite.fd_count > 0 || aexcept.fd_count > 0)
+ selectret = select (1, (rfds != NULL) ? &aread : NULL,
+ (wfds != NULL) ? &awrite : NULL, &aexcept, &select_timeout);
+ else
+ selectret = 0;
+ if (selectret == -1)
+ {
+ /* Throw an error early on, while we still have the context. */
+ LOG (GNUNET_ERROR_TYPE_ERROR, "W32 select(%d, %d, %d) failed: %lu\n",
+ rfds ? aread.fd_count : 0, wfds ? awrite.fd_count : 0, aexcept.fd_count, GetLastError ());
+ GNUNET_abort ();
+ }
+
+ /* Check aexcept, add its contents to awrite
+ This is technically wrong (aexcept might have its own descriptors), we should
+ have checked that descriptors were in awrite originally before re-adding them from
+ aexcept. Luckily, GNUnet never uses aexcept for anything, so this does not become a problem (yet). */
+ for (i = 0; i < aexcept.fd_count; i++)
+ FD_SET (aexcept.fd_array[i], &awrite);
+
+ /* If our select returned something or is a 0-timed request, then also check the pipes and get out of here! */
+ /* Sadly, it means code duplication :( */
+ if ((selectret > 0) || (ms_total == 0))
+ {
+ /* Read Pipes */
+ if (rfds && read_handles)
+ {
+ struct GNUNET_CONTAINER_SList_Iterator i;
+ int c;
+
+ for (c = 0, i = GNUNET_CONTAINER_slist_begin (rfds->handles);
+ GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES;
+ GNUNET_CONTAINER_slist_next (&i), c++)
+ {
+ struct GNUNET_DISK_FileHandle *fh;
+
+ fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&i,NULL);
+ if (fh->type == GNUNET_DISK_HANLDE_TYPE_PIPE)
+ {
+ DWORD error;
+ BOOL bret;
+
+ SetLastError (0);
+ DWORD waitstatus = 0;
+ bret = PeekNamedPipe (fh->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",
+ c, fh->h, bret, waitstatus, error);
+ if (bret == 0)
+ {
+ /* TODO: either add more errors to this condition, or eliminate it
+ * entirely (failed to peek -> pipe is in serious trouble, should
+ * be selected as readable).
+ */
+ if (error != ERROR_BROKEN_PIPE && error != ERROR_INVALID_HANDLE)
+ continue;
+ }
+ else if (waitstatus <= 0)
+ continue;
+ GNUNET_CONTAINER_slist_add (handles_read, GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
+ fh, sizeof (struct GNUNET_DISK_FileHandle));
+ retcode++;
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe 0x%x (0x%x)\n",
+ fh, fh->h);
+ }
+ else
+ {
+ GNUNET_CONTAINER_slist_add (handles_read, GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
+ fh, sizeof (struct GNUNET_DISK_FileHandle));
+ retcode++;
+ }
+ }
+ }
+ if (wfds && write_handles)