Revamp server's Windows POSIX compatibility code

Unlike POSIX sockets, Windows sockets are not file descriptors, but
"OS handles", with a completely separate set of functions.

However, Windows can create a file descriptor for a socket, and return
a file descriptor's underlying handle.  Use that instead of wrapping
our own file descriptors around Windows file descriptors and sockets.

Remove the wrapping machinery: MAX_FDS, enum fdmap_io_type, struct
fdmap, fdmap[], nfd, get_fd(), free_fd(), set_fd(), lookup_handle(),
lookup_fd().

Rewrite SOCKET_FUNCTION(), posix_accept(), posix_socket(),
posix_close(), ftruncate(), posix_open(), posix_read(), posix_write(),
fcntl().

Remove FILE_FUNCTION(), posix_fstat(), posix_lseek(),
SHARED_FUNCTION(), and fileno(), because the system's functions now
work fine.

posix_fsync() is used only #ifdef _WIN32, remove it, and call
_commit() directly.

The old code stuffed WSA error codes into errno, which doesn't work.
Use new w32_set_winsock_errno() to retrieve, convert & stuff into
errno.  Adapt inet_ntop() to set the WSA error code instead of errno,
so it can use w32_set_winsock_errno().

Move EWOULDBLOCK from sys/socket.h to w32misc.h, and drop unused
ENOTSOCK, EAFNOSUPPORT.

Use SOCKET rather than int in Windows-specific code.
This commit is contained in:
Markus Armbruster 2009-04-13 15:35:40 +02:00
parent 798af5b45b
commit 1c08ccf25b
6 changed files with 156 additions and 397 deletions

View file

@ -56,10 +56,6 @@
#undef NS_ALL
#include <windows.h>
#include <process.h>
/* Note: unistd.h(posixio.c) is not thread-safe.
* It may be used *only* while holding hThreadMutex.
*/
#include "unistd.h"
#include "misc.h"
#include "empthread.h"
#include "prototypes.h"
@ -537,7 +533,7 @@ empth_yield(void)
int
empth_select(int fd, int flags, struct timeval *timeout)
{
int handle;
SOCKET sock;
WSAEVENT hEventObject[2];
long events;
DWORD result, msec;
@ -551,15 +547,15 @@ empth_select(int fd, int flags, struct timeval *timeout)
hEventObject[0] = WSACreateEvent();
hEventObject[1] = pThread->hThreadEvent;
handle = posix_fd2socket(fd);
CANT_HAPPEN(handle < 0);
sock = posix_fd2socket(fd);
CANT_HAPPEN(sock == (SOCKET)-1);
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);
WSAEventSelect(sock, hEventObject[0], events);
if (timeout)
msec = timeout->tv_sec * 1000L + timeout->tv_usec / 1000L;
@ -576,7 +572,7 @@ empth_select(int fd, int flags, struct timeval *timeout)
res = 0;
break;
case WSA_WAIT_FAILED:
errno = WSAGetLastError();
w32_set_winsock_errno();
res = -1;
break;
default:
@ -585,7 +581,7 @@ empth_select(int fd, int flags, struct timeval *timeout)
res = -1;
}
WSAEventSelect(handle, hEventObject[0], 0);
WSAEventSelect(sock, hEventObject[0], 0);
WSACloseEvent(hEventObject[0]);