/*
* Empire - A multi-player, client/server Internet based war game.
- * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ * Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
*
* This would be one of the main functions used within gen\io.c
*/
-void
-empth_select(int fd, int flags)
+int
+empth_select(int fd, int flags, struct timeval *timeout)
{
int handle;
WSAEVENT hEventObject[2];
+ long events;
+ DWORD result, msec;
empth_t *pThread = TlsGetValue(dwTLSIndex);
+ int res;
loc_debug("%s select on %d",
flags == EMPTH_FD_READ ? "read" : "write", fd);
handle = posix_fd2socket(fd);
CANT_HAPPEN(handle < 0);
- if (flags == EMPTH_FD_READ)
- WSAEventSelect(handle, hEventObject[0], FD_READ | FD_ACCEPT | FD_CLOSE);
- else if (flags == EMPTH_FD_WRITE)
- WSAEventSelect(handle, hEventObject[0], FD_WRITE | FD_CLOSE);
- else {
- logerror("bad flag %d passed to empth_select", flags);
- empth_exit();
+ events = 0;
+ if (flags & EMPTH_FD_READ)
+ events |= FD_READ | FD_ACCEPT | FD_CLOSE;
+ if (flags & EMPTH_FD_WRITE)
+ events |= FD_WRITE | FD_CLOSE;
+ WSAEventSelect(handle, hEventObject[0], events);
+
+ if (timeout)
+ msec = timeout->tv_sec * 1000L + timeout->tv_usec / 1000L;
+ else
+ msec = WSA_INFINITE;
+ result = WSAWaitForMultipleEvents(2, hEventObject, FALSE, msec, FALSE);
+
+ switch (result) {
+ case WSA_WAIT_TIMEOUT:
+ res = 0;
+ break;
+ case WSA_WAIT_FAILED:
+ errno = WSAGetLastError();
+ res = -1;
+ break;
+ default:
+ res = 1;
}
- WSAWaitForMultipleEvents(2, hEventObject, FALSE, WSA_INFINITE, FALSE);
-
WSAEventSelect(handle, hEventObject[0], 0);
WSACloseEvent(hEventObject[0]);
loc_RunThisThread(NULL);
+ return res;
}
/************************