58 switch (WSAGetLastError())
61 case WSANOTINITIALISED:
62 case WSAEINVALIDPROVIDER:
63 case WSAEINVALIDPROCTABLE:
88 case WSAEPROTONOSUPPORT:
90 case WSAESOCKTNOSUPPORT:
120 case WSAEADDRNOTAVAIL:
126 case WSAEHOSTUNREACH:
127 case WSAHOST_NOT_FOUND:
150 WSAGetLastError())));
172 int typelen =
sizeof(
type);
174 if (getsockopt(s, SOL_SOCKET, SO_TYPE, (
char *) &
type, &typelen))
177 return (
type == SOCK_DGRAM) ? 1 : 0;
183 static HANDLE waitevent = INVALID_HANDLE_VALUE;
184 static SOCKET current_socket = INVALID_SOCKET;
185 static int isUDP = 0;
190 if (waitevent == INVALID_HANDLE_VALUE)
192 waitevent = CreateEvent(NULL, TRUE, FALSE, NULL);
194 if (waitevent == INVALID_HANDLE_VALUE)
196 (
errmsg_internal(
"could not create socket waiting event: error code %lu", GetLastError())));
198 else if (!ResetEvent(waitevent))
200 (
errmsg_internal(
"could not reset socket waiting event: error code %lu", GetLastError())));
207 if (current_socket != s)
216 if (WSAEventSelect(s, waitevent, what) != 0)
223 events[1] = waitevent;
232 if ((what & FD_WRITE) && isUDP)
236 r = WaitForMultipleObjectsEx(2, events, FALSE, 100, TRUE);
238 if (r == WAIT_TIMEOUT)
247 r = WSASend(s, &
buf, 1, &sent, 0, NULL, NULL);
250 WSAEventSelect(s, NULL, 0);
253 else if (WSAGetLastError() != WSAEWOULDBLOCK)
256 WSAEventSelect(s, NULL, 0);
265 r = WaitForMultipleObjectsEx(2, events, FALSE, timeout, TRUE);
267 WSAEventSelect(s, NULL, 0);
269 if (r == WAIT_OBJECT_0 || r == WAIT_IO_COMPLETION)
275 if (r == WAIT_OBJECT_0 + 1)
277 if (r == WAIT_TIMEOUT)
283 (
errmsg_internal(
"unrecognized return value from WaitForMultipleObjects: %d (error code %lu)", r, GetLastError())));
294 unsigned long on = 1;
296 s = WSASocket(af,
type, protocol, NULL, 0, WSA_FLAG_OVERLAPPED);
297 if (s == INVALID_SOCKET)
300 return INVALID_SOCKET;
303 if (ioctlsocket(s, FIONBIO, &on))
307 return INVALID_SOCKET;
347 rs = WSAAccept(s, addr, addrlen, NULL, 0);
348 if (rs == INVALID_SOCKET)
351 return INVALID_SOCKET;
363 r = WSAConnect(s, addr, addrlen, NULL, NULL, NULL, NULL);
367 if (WSAGetLastError() != WSAEWOULDBLOCK)
396 r = WSARecv(s, &wbuf, 1, &
b, &flags, NULL, NULL);
397 if (r != SOCKET_ERROR)
400 if (WSAGetLastError() != WSAEWOULDBLOCK)
418 for (n = 0; n < 5; n++)
424 r = WSARecv(s, &wbuf, 1, &
b, &flags, NULL, NULL);
425 if (r != SOCKET_ERROR)
427 if (WSAGetLastError() != WSAEWOULDBLOCK)
443 (
errmsg_internal(
"could not read from ready socket (after retries)")));
469 wbuf.buf = (
char *)
buf;
477 r = WSASend(s, &wbuf, 1, &
b, flags, NULL, NULL);
478 if (r != SOCKET_ERROR &&
b > 0)
482 if (r == SOCKET_ERROR &&
483 WSAGetLastError() != WSAEWOULDBLOCK)
517 pgwin32_select(
int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
const struct timeval *timeout)
519 WSAEVENT events[FD_SETSIZE * 2];
522 SOCKET sockets[FD_SETSIZE * 2];
526 DWORD timeoutval = WSA_INFINITE;
531 Assert(exceptfds == NULL);
536 FD_ZERO(&outreadfds);
537 FD_ZERO(&outwritefds);
549 if (writefds != NULL)
551 for (
i = 0;
i < writefds->fd_count;
i++)
560 r = WSASend(writefds->fd_array[
i], &
buf, 1, &sent, 0, NULL, NULL);
561 if (r == 0 || WSAGetLastError() != WSAEWOULDBLOCK)
562 FD_SET(writefds->fd_array[
i], &outwritefds);
566 if (outwritefds.fd_count > 0)
568 memcpy(writefds, &outwritefds,
sizeof(fd_set));
571 return outwritefds.fd_count;
581 timeoutval = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
586 for (
i = 0;
i < readfds->fd_count;
i++)
588 events[numevents] = WSACreateEvent();
589 sockets[numevents] = readfds->fd_array[
i];
593 if (writefds != NULL)
595 for (
i = 0;
i < writefds->fd_count;
i++)
598 !FD_ISSET(writefds->fd_array[
i], readfds))
601 events[numevents] = WSACreateEvent();
602 sockets[numevents] = writefds->fd_array[
i];
608 for (
i = 0;
i < numevents;
i++)
612 if (readfds && FD_ISSET(sockets[
i], readfds))
613 flags |= FD_READ | FD_ACCEPT | FD_CLOSE;
615 if (writefds && FD_ISSET(sockets[
i], writefds))
616 flags |= FD_WRITE | FD_CLOSE;
618 if (WSAEventSelect(sockets[
i], events[
i], flags) != 0)
623 WSAEventSelect(sockets[
i], NULL, 0);
624 for (
i = 0;
i < numevents;
i++)
625 WSACloseEvent(events[
i]);
631 r = WaitForMultipleObjectsEx(numevents + 1, events, FALSE, timeoutval, TRUE);
632 if (r != WAIT_TIMEOUT && r != WAIT_IO_COMPLETION && r != (WAIT_OBJECT_0 + numevents))
638 WSANETWORKEVENTS resEvents;
640 for (
i = 0;
i < numevents;
i++)
642 ZeroMemory(&resEvents,
sizeof(resEvents));
643 if (WSAEnumNetworkEvents(sockets[
i], events[
i], &resEvents) != 0)
644 elog(
ERROR,
"failed to enumerate network events: error code %d",
647 if (readfds && FD_ISSET(sockets[
i], readfds))
649 if ((resEvents.lNetworkEvents & FD_READ) ||
650 (resEvents.lNetworkEvents & FD_ACCEPT) ||
651 (resEvents.lNetworkEvents & FD_CLOSE))
653 FD_SET(sockets[
i], &outreadfds);
659 if (writefds && FD_ISSET(sockets[
i], writefds))
661 if ((resEvents.lNetworkEvents & FD_WRITE) ||
662 (resEvents.lNetworkEvents & FD_CLOSE))
664 FD_SET(sockets[
i], &outwritefds);
673 for (
i = 0;
i < numevents;
i++)
675 WSAEventSelect(sockets[
i], NULL, 0);
676 WSACloseEvent(events[
i]);
679 if (r == WSA_WAIT_TIMEOUT)
689 if (r == WAIT_OBJECT_0 + numevents || r == WAIT_IO_COMPLETION)
702 memcpy(readfds, &outreadfds,
sizeof(fd_set));
704 memcpy(writefds, &outwritefds,
sizeof(fd_set));
#define Assert(condition)
static void PGresult * res
int errmsg_internal(const char *fmt,...)
#define ereport(elevel,...)
void pg_usleep(long microsec)
void pgwin32_dispatch_queued_signals(void)
HANDLE pgwin32_signal_event
int pgwin32_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout)
int pgwin32_recv(SOCKET s, char *buf, int len, int f)
int pgwin32_send(SOCKET s, const void *buf, int len, int flags)
int pgwin32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
int pgwin32_waitforsinglesocket(SOCKET s, int what, int timeout)
static int pgwin32_poll_signals(void)
SOCKET pgwin32_socket(int af, int type, int protocol)
static int isDataGram(SOCKET s)
static void TranslateSocketError(void)
SOCKET pgwin32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
int pgwin32_bind(SOCKET s, struct sockaddr *addr, int addrlen)
int pgwin32_listen(SOCKET s, int backlog)
#define UNBLOCKED_SIGNAL_QUEUE()
#define bind(s, addr, addrlen)
#define listen(s, backlog)