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))
306 return INVALID_SOCKET;
346 rs = WSAAccept(s, addr, addrlen, NULL, 0);
347 if (rs == INVALID_SOCKET)
350 return INVALID_SOCKET;
362 r = WSAConnect(s, addr, addrlen, NULL, NULL, NULL, NULL);
366 if (WSAGetLastError() != WSAEWOULDBLOCK)
395 r = WSARecv(s, &wbuf, 1, &
b, &flags, NULL, NULL);
396 if (r != SOCKET_ERROR)
399 if (WSAGetLastError() != WSAEWOULDBLOCK)
417 for (n = 0; n < 5; n++)
423 r = WSARecv(s, &wbuf, 1, &
b, &flags, NULL, NULL);
424 if (r != SOCKET_ERROR)
426 if (WSAGetLastError() != WSAEWOULDBLOCK)
442 (
errmsg_internal(
"could not read from ready socket (after retries)")));
468 wbuf.buf = (
char *)
buf;
476 r = WSASend(s, &wbuf, 1, &
b, flags, NULL, NULL);
477 if (r != SOCKET_ERROR &&
b > 0)
481 if (r == SOCKET_ERROR &&
482 WSAGetLastError() != WSAEWOULDBLOCK)
516 pgwin32_select(
int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
const struct timeval *timeout)
518 WSAEVENT events[FD_SETSIZE * 2];
521 SOCKET sockets[FD_SETSIZE * 2];
525 DWORD timeoutval = WSA_INFINITE;
530 Assert(exceptfds == NULL);
535 FD_ZERO(&outreadfds);
536 FD_ZERO(&outwritefds);
548 if (writefds != NULL)
550 for (
i = 0;
i < writefds->fd_count;
i++)
559 r = WSASend(writefds->fd_array[
i], &
buf, 1, &sent, 0, NULL, NULL);
560 if (r == 0 || WSAGetLastError() != WSAEWOULDBLOCK)
561 FD_SET(writefds->fd_array[
i], &outwritefds);
565 if (outwritefds.fd_count > 0)
567 memcpy(writefds, &outwritefds,
sizeof(fd_set));
570 return outwritefds.fd_count;
580 timeoutval = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
585 for (
i = 0;
i < readfds->fd_count;
i++)
587 events[numevents] = WSACreateEvent();
588 sockets[numevents] = readfds->fd_array[
i];
592 if (writefds != NULL)
594 for (
i = 0;
i < writefds->fd_count;
i++)
597 !FD_ISSET(writefds->fd_array[
i], readfds))
600 events[numevents] = WSACreateEvent();
601 sockets[numevents] = writefds->fd_array[
i];
607 for (
i = 0;
i < numevents;
i++)
611 if (readfds && FD_ISSET(sockets[
i], readfds))
612 flags |= FD_READ | FD_ACCEPT | FD_CLOSE;
614 if (writefds && FD_ISSET(sockets[
i], writefds))
615 flags |= FD_WRITE | FD_CLOSE;
617 if (WSAEventSelect(sockets[
i], events[
i], flags) != 0)
622 WSAEventSelect(sockets[
i], NULL, 0);
623 for (
i = 0;
i < numevents;
i++)
624 WSACloseEvent(events[
i]);
630 r = WaitForMultipleObjectsEx(numevents + 1, events, FALSE, timeoutval, TRUE);
631 if (r != WAIT_TIMEOUT && r != WAIT_IO_COMPLETION && r != (WAIT_OBJECT_0 + numevents))
637 WSANETWORKEVENTS resEvents;
639 for (
i = 0;
i < numevents;
i++)
641 ZeroMemory(&resEvents,
sizeof(resEvents));
642 if (WSAEnumNetworkEvents(sockets[
i], events[
i], &resEvents) != 0)
643 elog(
ERROR,
"failed to enumerate network events: error code %d",
646 if (readfds && FD_ISSET(sockets[
i], readfds))
648 if ((resEvents.lNetworkEvents & FD_READ) ||
649 (resEvents.lNetworkEvents & FD_ACCEPT) ||
650 (resEvents.lNetworkEvents & FD_CLOSE))
652 FD_SET(sockets[
i], &outreadfds);
658 if (writefds && FD_ISSET(sockets[
i], writefds))
660 if ((resEvents.lNetworkEvents & FD_WRITE) ||
661 (resEvents.lNetworkEvents & FD_CLOSE))
663 FD_SET(sockets[
i], &outwritefds);
672 for (
i = 0;
i < numevents;
i++)
674 WSAEventSelect(sockets[
i], NULL, 0);
675 WSACloseEvent(events[
i]);
678 if (r == WSA_WAIT_TIMEOUT)
688 if (r == WAIT_OBJECT_0 + numevents || r == WAIT_IO_COMPLETION)
701 memcpy(readfds, &outreadfds,
sizeof(fd_set));
703 memcpy(writefds, &outwritefds,
sizeof(fd_set));
static void PGresult * res
elog(ERROR, "%s: %s", p2, msg)
int errmsg_internal(const char *fmt,...)
#define ereport(elevel,...)
Assert(fmt[strlen(fmt) - 1] !='\n')
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)