relocated the standard implementation std.c to libmoox
This commit is contained in:
@ -25,12 +25,9 @@ endif
|
||||
|
||||
bin_PROGRAMS = moo
|
||||
moo_SOURCES = main.c
|
||||
if WIN32
|
||||
moo_SOURCES += poll-msw.c poll-msw.h
|
||||
endif
|
||||
moo_CPPFLAGS = $(CPPFLAGS_ALL_COMMON)
|
||||
moo_LDFLAGS = $(LDFLAGS_ALL_COMMON)
|
||||
moo_LDADD = $(LIBADD_LIB_COMMON) -lmoo $(PTHREAD_LIBS)
|
||||
moo_LDADD = $(LIBADD_LIB_COMMON) -lmoox $(PTHREAD_LIBS)
|
||||
if WIN32
|
||||
moo_LDADD += $(SOCKET_LIBS)
|
||||
endif
|
||||
|
@ -91,8 +91,7 @@ host_triplet = @host@
|
||||
@ENABLE_LIBLTDL_TRUE@am__append_1 = $(LTDL_LIBS)
|
||||
@ENABLE_LIBLTDL_FALSE@am__append_2 = $(DL_LIBS)
|
||||
bin_PROGRAMS = moo$(EXEEXT)
|
||||
@WIN32_TRUE@am__append_3 = poll-msw.c poll-msw.h
|
||||
@WIN32_TRUE@am__append_4 = $(SOCKET_LIBS)
|
||||
@WIN32_TRUE@am__append_3 = $(SOCKET_LIBS)
|
||||
@ENABLE_STATIC_MODULE_FALSE@moo_DEPENDENCIES = $(am__DEPENDENCIES_4) \
|
||||
@ENABLE_STATIC_MODULE_FALSE@ $(am__DEPENDENCIES_1) \
|
||||
@ENABLE_STATIC_MODULE_FALSE@ $(am__DEPENDENCIES_5)
|
||||
@ -113,9 +112,7 @@ CONFIG_CLEAN_FILES =
|
||||
CONFIG_CLEAN_VPATH_FILES =
|
||||
am__installdirs = "$(DESTDIR)$(bindir)"
|
||||
PROGRAMS = $(bin_PROGRAMS)
|
||||
am__moo_SOURCES_DIST = main.c poll-msw.c poll-msw.h
|
||||
@WIN32_TRUE@am__objects_1 = moo-poll-msw.$(OBJEXT)
|
||||
am_moo_OBJECTS = moo-main.$(OBJEXT) $(am__objects_1)
|
||||
am_moo_OBJECTS = moo-main.$(OBJEXT)
|
||||
moo_OBJECTS = $(am_moo_OBJECTS)
|
||||
am__DEPENDENCIES_1 =
|
||||
@ENABLE_LIBLTDL_TRUE@am__DEPENDENCIES_2 = $(am__DEPENDENCIES_1)
|
||||
@ -165,7 +162,7 @@ am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@)
|
||||
am__v_CCLD_0 = @echo " CCLD " $@;
|
||||
am__v_CCLD_1 =
|
||||
SOURCES = $(moo_SOURCES)
|
||||
DIST_SOURCES = $(am__moo_SOURCES_DIST)
|
||||
DIST_SOURCES = $(moo_SOURCES)
|
||||
am__can_run_installinfo = \
|
||||
case $$AM_UPDATE_INFO_DIR in \
|
||||
n|no|NO) false;; \
|
||||
@ -337,6 +334,7 @@ pdfdir = @pdfdir@
|
||||
prefix = @prefix@
|
||||
program_transform_name = @program_transform_name@
|
||||
psdir = @psdir@
|
||||
runstatedir = @runstatedir@
|
||||
sbindir = @sbindir@
|
||||
sharedstatedir = @sharedstatedir@
|
||||
srcdir = @srcdir@
|
||||
@ -361,10 +359,11 @@ LDFLAGS_ALL_COMMON = -L$(abs_builddir) -L$(abs_builddir)/../lib -L$(libdir)
|
||||
CPPFLAGS_LIB_COMMON = $(CPPFLAGS_ALL_COMMON) $(LTDLINCL)
|
||||
LDFLAGS_LIB_COMMON = $(LDFLAGS_ALL_COMMON) -version-info 1:0:0 -no-undefined
|
||||
LIBADD_LIB_COMMON = $(LIBM) $(am__append_1) $(am__append_2)
|
||||
moo_SOURCES = main.c $(am__append_3)
|
||||
moo_SOURCES = main.c
|
||||
moo_CPPFLAGS = $(CPPFLAGS_ALL_COMMON)
|
||||
moo_LDFLAGS = $(LDFLAGS_ALL_COMMON)
|
||||
moo_LDADD = $(LIBADD_LIB_COMMON) -lmoo $(PTHREAD_LIBS) $(am__append_4)
|
||||
moo_LDADD = $(LIBADD_LIB_COMMON) -lmoox $(PTHREAD_LIBS) \
|
||||
$(am__append_3)
|
||||
@ENABLE_STATIC_MODULE_TRUE@moo_DEPENDENCIES = ../lib/libmoo.la
|
||||
all: all-am
|
||||
|
||||
@ -460,7 +459,6 @@ distclean-compile:
|
||||
-rm -f *.tab.c
|
||||
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/moo-main.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/moo-poll-msw.Po@am__quote@
|
||||
|
||||
.c.o:
|
||||
@am__fastdepCC_TRUE@ $(AM_V_CC)depbase=`echo $@ | sed 's|[^/]*$$|$(DEPDIR)/&|;s|\.o$$||'`;\
|
||||
@ -500,20 +498,6 @@ moo-main.obj: main.c
|
||||
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(moo_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o moo-main.obj `if test -f 'main.c'; then $(CYGPATH_W) 'main.c'; else $(CYGPATH_W) '$(srcdir)/main.c'; fi`
|
||||
|
||||
moo-poll-msw.o: poll-msw.c
|
||||
@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(moo_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT moo-poll-msw.o -MD -MP -MF $(DEPDIR)/moo-poll-msw.Tpo -c -o moo-poll-msw.o `test -f 'poll-msw.c' || echo '$(srcdir)/'`poll-msw.c
|
||||
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/moo-poll-msw.Tpo $(DEPDIR)/moo-poll-msw.Po
|
||||
@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='poll-msw.c' object='moo-poll-msw.o' libtool=no @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(moo_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o moo-poll-msw.o `test -f 'poll-msw.c' || echo '$(srcdir)/'`poll-msw.c
|
||||
|
||||
moo-poll-msw.obj: poll-msw.c
|
||||
@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(moo_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT moo-poll-msw.obj -MD -MP -MF $(DEPDIR)/moo-poll-msw.Tpo -c -o moo-poll-msw.obj `if test -f 'poll-msw.c'; then $(CYGPATH_W) 'poll-msw.c'; else $(CYGPATH_W) '$(srcdir)/poll-msw.c'; fi`
|
||||
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/moo-poll-msw.Tpo $(DEPDIR)/moo-poll-msw.Po
|
||||
@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='poll-msw.c' object='moo-poll-msw.obj' libtool=no @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(moo_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o moo-poll-msw.obj `if test -f 'poll-msw.c'; then $(CYGPATH_W) 'poll-msw.c'; else $(CYGPATH_W) '$(srcdir)/poll-msw.c'; fi`
|
||||
|
||||
mostlyclean-libtool:
|
||||
-rm -f *.lo
|
||||
|
||||
|
@ -1,411 +0,0 @@
|
||||
/* Emulation for poll(2)
|
||||
Contributed by Paolo Bonzini.
|
||||
|
||||
Copyright 2001-2003, 2006-2016 Free Software Foundation, Inc.
|
||||
|
||||
This file is part of gnulib.
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2, or (at your option)
|
||||
any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along
|
||||
with this program; if not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
/* Tell gcc not to warn about the (nfd < 0) tests, below. */
|
||||
#if (__GNUC__ == 4 && 3 <= __GNUC_MINOR__) || 4 < __GNUC__
|
||||
# pragma GCC diagnostic ignored "-Wtype-limits"
|
||||
#endif
|
||||
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
|
||||
#include "poll-msw.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <time.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
|
||||
#if defined(__BORLANDC__)
|
||||
# include <malloc.h>
|
||||
# define _alloca alloca
|
||||
#endif
|
||||
|
||||
#ifndef INFTIM
|
||||
# define INFTIM (-1)
|
||||
#endif
|
||||
|
||||
/* BeOS does not have MSG_PEEK. */
|
||||
#ifndef MSG_PEEK
|
||||
# define MSG_PEEK 0
|
||||
#endif
|
||||
|
||||
#ifndef POLLRDNORM
|
||||
# define POLLRDNORM 0
|
||||
# define POLLRDBAND 0
|
||||
# define POLLWRNORM 0
|
||||
# define POLLWRBAND 0
|
||||
#endif
|
||||
|
||||
static BOOL IsConsoleHandle (HANDLE h)
|
||||
{
|
||||
DWORD mode;
|
||||
return GetConsoleMode (h, &mode) != 0;
|
||||
}
|
||||
|
||||
static BOOL IsSocketHandle (HANDLE h)
|
||||
{
|
||||
WSANETWORKEVENTS ev;
|
||||
|
||||
if (IsConsoleHandle (h)) return FALSE;
|
||||
|
||||
/* Under Wine, it seems that getsockopt returns 0 for pipes too.
|
||||
WSAEnumNetworkEvents instead distinguishes the two correctly. */
|
||||
ev.lNetworkEvents = 0xDEADBEEF;
|
||||
WSAEnumNetworkEvents ((SOCKET) h, NULL, &ev);
|
||||
return ev.lNetworkEvents != 0xDEADBEEF;
|
||||
}
|
||||
|
||||
/* Declare data structures for ntdll functions. */
|
||||
typedef struct _FILE_PIPE_LOCAL_INFORMATION
|
||||
{
|
||||
ULONG NamedPipeType;
|
||||
ULONG NamedPipeConfiguration;
|
||||
ULONG MaximumInstances;
|
||||
ULONG CurrentInstances;
|
||||
ULONG InboundQuota;
|
||||
ULONG ReadDataAvailable;
|
||||
ULONG OutboundQuota;
|
||||
ULONG WriteQuotaAvailable;
|
||||
ULONG NamedPipeState;
|
||||
ULONG NamedPipeEnd;
|
||||
} FILE_PIPE_LOCAL_INFORMATION, *PFILE_PIPE_LOCAL_INFORMATION;
|
||||
|
||||
typedef struct _IO_STATUS_BLOCK
|
||||
{
|
||||
union
|
||||
{
|
||||
DWORD Status;
|
||||
PVOID Pointer;
|
||||
} u;
|
||||
/*ULONG_PTR Information;*/
|
||||
ULONG* Information;
|
||||
} IO_STATUS_BLOCK, *PIO_STATUS_BLOCK;
|
||||
|
||||
typedef enum _FILE_INFORMATION_CLASS
|
||||
{
|
||||
FilePipeLocalInformation = 24
|
||||
} FILE_INFORMATION_CLASS, *PFILE_INFORMATION_CLASS;
|
||||
|
||||
typedef DWORD (WINAPI *PNtQueryInformationFile) (HANDLE, IO_STATUS_BLOCK *, VOID *, ULONG, FILE_INFORMATION_CLASS);
|
||||
|
||||
# ifndef PIPE_BUF
|
||||
# define PIPE_BUF 512
|
||||
# endif
|
||||
|
||||
/* Compute revents values for file handle H. If some events cannot happen
|
||||
for the handle, eliminate them from *P_SOUGHT. */
|
||||
|
||||
static int windows_compute_revents (HANDLE h, int *p_sought)
|
||||
{
|
||||
int i, ret, happened;
|
||||
INPUT_RECORD *irbuffer;
|
||||
DWORD avail, nbuffer;
|
||||
BOOL bRet;
|
||||
IO_STATUS_BLOCK iosb;
|
||||
FILE_PIPE_LOCAL_INFORMATION fpli;
|
||||
static PNtQueryInformationFile NtQueryInformationFile;
|
||||
static BOOL once_only;
|
||||
|
||||
switch (GetFileType (h))
|
||||
{
|
||||
case FILE_TYPE_PIPE:
|
||||
if (!once_only)
|
||||
{
|
||||
NtQueryInformationFile = (PNtQueryInformationFile)
|
||||
GetProcAddress (GetModuleHandle(TEXT("ntdll.dll")), "NtQueryInformationFile");
|
||||
once_only = TRUE;
|
||||
}
|
||||
|
||||
happened = 0;
|
||||
if (PeekNamedPipe (h, NULL, 0, NULL, &avail, NULL) != 0)
|
||||
{
|
||||
if (avail)
|
||||
happened |= *p_sought & (POLLIN | POLLRDNORM);
|
||||
}
|
||||
else if (GetLastError () == ERROR_BROKEN_PIPE)
|
||||
happened |= POLLHUP;
|
||||
|
||||
else
|
||||
{
|
||||
/* It was the write-end of the pipe. Check if it is writable.
|
||||
If NtQueryInformationFile fails, optimistically assume the pipe is
|
||||
writable. This could happen on Windows 9x, where
|
||||
NtQueryInformationFile is not available, or if we inherit a pipe
|
||||
that doesn't permit FILE_READ_ATTRIBUTES access on the write end
|
||||
(I think this should not happen since Windows XP SP2; WINE seems
|
||||
fine too). Otherwise, ensure that enough space is available for
|
||||
atomic writes. */
|
||||
memset (&iosb, 0, sizeof (iosb));
|
||||
memset (&fpli, 0, sizeof (fpli));
|
||||
|
||||
if (!NtQueryInformationFile
|
||||
|| NtQueryInformationFile (h, &iosb, &fpli, sizeof (fpli), FilePipeLocalInformation)
|
||||
|| fpli.WriteQuotaAvailable >= PIPE_BUF
|
||||
|| (fpli.OutboundQuota < PIPE_BUF && fpli.WriteQuotaAvailable == fpli.OutboundQuota))
|
||||
{
|
||||
happened |= *p_sought & (POLLOUT | POLLWRNORM | POLLWRBAND);
|
||||
}
|
||||
}
|
||||
return happened;
|
||||
|
||||
case FILE_TYPE_CHAR:
|
||||
ret = WaitForSingleObject (h, 0);
|
||||
if (!IsConsoleHandle (h))
|
||||
return ret == WAIT_OBJECT_0 ? *p_sought & ~(POLLPRI | POLLRDBAND) : 0;
|
||||
|
||||
nbuffer = avail = 0;
|
||||
bRet = GetNumberOfConsoleInputEvents (h, &nbuffer);
|
||||
if (bRet)
|
||||
{
|
||||
/* Input buffer. */
|
||||
*p_sought &= POLLIN | POLLRDNORM;
|
||||
if (nbuffer == 0)
|
||||
return POLLHUP;
|
||||
if (!*p_sought)
|
||||
return 0;
|
||||
|
||||
irbuffer = (INPUT_RECORD *)_alloca(nbuffer * sizeof (INPUT_RECORD));
|
||||
bRet = PeekConsoleInput(h, irbuffer, nbuffer, &avail);
|
||||
if (!bRet || avail == 0)
|
||||
return POLLHUP;
|
||||
|
||||
for (i = 0; i < avail; i++)
|
||||
if (irbuffer[i].EventType == KEY_EVENT)
|
||||
return *p_sought;
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Screen buffer. */
|
||||
*p_sought &= POLLOUT | POLLWRNORM | POLLWRBAND;
|
||||
return *p_sought;
|
||||
}
|
||||
|
||||
default:
|
||||
ret = WaitForSingleObject (h, 0);
|
||||
if (ret == WAIT_OBJECT_0)
|
||||
return *p_sought & ~(POLLPRI | POLLRDBAND);
|
||||
|
||||
return *p_sought & (POLLOUT | POLLWRNORM | POLLWRBAND);
|
||||
}
|
||||
}
|
||||
|
||||
/* Convert fd_sets returned by select into revents values. */
|
||||
|
||||
static int windows_compute_revents_socket (SOCKET h, int sought, long lNetworkEvents)
|
||||
{
|
||||
int happened = 0;
|
||||
|
||||
if ((lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE)) == FD_ACCEPT)
|
||||
happened |= (POLLIN | POLLRDNORM) & sought;
|
||||
|
||||
else if (lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE))
|
||||
{
|
||||
int r, error;
|
||||
|
||||
char data[64];
|
||||
WSASetLastError (0);
|
||||
r = recv (h, data, sizeof (data), MSG_PEEK);
|
||||
error = WSAGetLastError ();
|
||||
WSASetLastError (0);
|
||||
|
||||
if (r > 0 || error == WSAENOTCONN)
|
||||
happened |= (POLLIN | POLLRDNORM) & sought;
|
||||
|
||||
/* Distinguish hung-up sockets from other errors. */
|
||||
else if (r == 0 || error == WSAESHUTDOWN || error == WSAECONNRESET
|
||||
|| error == WSAECONNABORTED || error == WSAENETRESET)
|
||||
happened |= POLLHUP;
|
||||
|
||||
else
|
||||
happened |= POLLERR;
|
||||
}
|
||||
|
||||
if (lNetworkEvents & (FD_WRITE | FD_CONNECT))
|
||||
happened |= (POLLOUT | POLLWRNORM | POLLWRBAND) & sought;
|
||||
|
||||
if (lNetworkEvents & FD_OOB)
|
||||
happened |= (POLLPRI | POLLRDBAND) & sought;
|
||||
|
||||
return happened;
|
||||
}
|
||||
|
||||
|
||||
int poll (struct pollfd *pfd, nfds_t nfd, int timeout)
|
||||
{
|
||||
static struct timeval tv0;
|
||||
static HANDLE hEvent;
|
||||
WSANETWORKEVENTS ev;
|
||||
HANDLE h, handle_array[FD_SETSIZE + 2];
|
||||
DWORD ret, wait_timeout, nhandles;
|
||||
fd_set rfds, wfds, xfds;
|
||||
BOOL poll_again;
|
||||
MSG msg;
|
||||
int rc = 0;
|
||||
nfds_t i;
|
||||
|
||||
if (nfd < 0 || timeout < -1)
|
||||
{
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!hEvent)
|
||||
hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
|
||||
|
||||
restart:
|
||||
handle_array[0] = hEvent;
|
||||
nhandles = 1;
|
||||
FD_ZERO (&rfds);
|
||||
FD_ZERO (&wfds);
|
||||
FD_ZERO (&xfds);
|
||||
|
||||
/* Classify socket handles and create fd sets. */
|
||||
for (i = 0; i < nfd; i++)
|
||||
{
|
||||
int sought = pfd[i].events;
|
||||
pfd[i].revents = 0;
|
||||
if (pfd[i].fd < 0) continue;
|
||||
if (!(sought & (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM | POLLWRBAND | POLLPRI | POLLRDBAND))) continue;
|
||||
|
||||
h = (HANDLE)_get_osfhandle(pfd[i].fd);
|
||||
if (IsSocketHandle (h))
|
||||
{
|
||||
int requested = FD_CLOSE;
|
||||
|
||||
/* see above; socket handles are mapped onto select. */
|
||||
if (sought & (POLLIN | POLLRDNORM))
|
||||
{
|
||||
requested |= FD_READ | FD_ACCEPT;
|
||||
FD_SET ((SOCKET) h, &rfds);
|
||||
}
|
||||
if (sought & (POLLOUT | POLLWRNORM | POLLWRBAND))
|
||||
{
|
||||
requested |= FD_WRITE | FD_CONNECT;
|
||||
FD_SET ((SOCKET) h, &wfds);
|
||||
}
|
||||
if (sought & (POLLPRI | POLLRDBAND))
|
||||
{
|
||||
requested |= FD_OOB;
|
||||
FD_SET ((SOCKET) h, &xfds);
|
||||
}
|
||||
|
||||
if (requested)
|
||||
WSAEventSelect ((SOCKET) h, hEvent, requested);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Poll now. If we get an event, do not poll again. Also,
|
||||
screen buffer handles are waitable, and they'll block until
|
||||
a character is available. windows_compute_revents eliminates
|
||||
bits for the "wrong" direction. */
|
||||
pfd[i].revents = windows_compute_revents (h, &sought);
|
||||
if (sought) handle_array[nhandles++] = h;
|
||||
if (pfd[i].revents) timeout = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (select(0, &rfds, &wfds, &xfds, &tv0) > 0)
|
||||
{
|
||||
/* Do MsgWaitForMultipleObjects anyway to dispatch messages, but
|
||||
no need to call select again. */
|
||||
poll_again = FALSE;
|
||||
wait_timeout = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
poll_again = TRUE;
|
||||
if (timeout == INFTIM)
|
||||
wait_timeout = INFINITE;
|
||||
else
|
||||
wait_timeout = timeout;
|
||||
}
|
||||
|
||||
for (;;)
|
||||
{
|
||||
ret = MsgWaitForMultipleObjects(nhandles, handle_array, FALSE, wait_timeout, QS_ALLINPUT);
|
||||
|
||||
if (ret == WAIT_OBJECT_0 + nhandles)
|
||||
{
|
||||
/* new input of some other kind */
|
||||
BOOL bRet;
|
||||
while ((bRet = PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) != 0)
|
||||
{
|
||||
TranslateMessage (&msg);
|
||||
DispatchMessage (&msg);
|
||||
}
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
if (poll_again)
|
||||
select (0, &rfds, &wfds, &xfds, &tv0);
|
||||
|
||||
/* Place a sentinel at the end of the array. */
|
||||
handle_array[nhandles] = NULL;
|
||||
nhandles = 1;
|
||||
for (i = 0; i < nfd; i++)
|
||||
{
|
||||
int happened;
|
||||
|
||||
if (pfd[i].fd < 0)
|
||||
continue;
|
||||
if (!(pfd[i].events & (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM | POLLWRBAND)))
|
||||
continue;
|
||||
|
||||
h = (HANDLE) _get_osfhandle (pfd[i].fd);
|
||||
if (h != handle_array[nhandles])
|
||||
{
|
||||
/* It's a socket. */
|
||||
WSAEnumNetworkEvents ((SOCKET) h, NULL, &ev);
|
||||
WSAEventSelect ((SOCKET) h, 0, 0);
|
||||
|
||||
/* If we're lucky, WSAEnumNetworkEvents already provided a way
|
||||
to distinguish FD_READ and FD_ACCEPT; this saves a recv later. */
|
||||
if (FD_ISSET ((SOCKET) h, &rfds) && !(ev.lNetworkEvents & (FD_READ | FD_ACCEPT)))
|
||||
ev.lNetworkEvents |= FD_READ | FD_ACCEPT;
|
||||
if (FD_ISSET ((SOCKET) h, &wfds))
|
||||
ev.lNetworkEvents |= FD_WRITE | FD_CONNECT;
|
||||
if (FD_ISSET ((SOCKET) h, &xfds))
|
||||
ev.lNetworkEvents |= FD_OOB;
|
||||
|
||||
happened = windows_compute_revents_socket ((SOCKET) h, pfd[i].events, ev.lNetworkEvents);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Not a socket. */
|
||||
int sought = pfd[i].events;
|
||||
happened = windows_compute_revents (h, &sought);
|
||||
nhandles++;
|
||||
}
|
||||
|
||||
if ((pfd[i].revents |= happened) != 0) rc++;
|
||||
}
|
||||
|
||||
if (!rc && timeout == INFTIM)
|
||||
{
|
||||
SleepEx (1, TRUE);
|
||||
goto restart;
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
#ifndef _POLL_MSW_H_
|
||||
#define _POLL_MSW_H_
|
||||
|
||||
#include "moo.h"
|
||||
|
||||
#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0600)
|
||||
/* Event types that can be polled for. These bits may be set in `events'
|
||||
to indicate the interesting event types; they will appear in `revents'
|
||||
to indicate the status of the file descriptor. */
|
||||
#define POLLIN 0x001 /* There is data to read. */
|
||||
#define POLLPRI 0x002 /* There is urgent data to read. */
|
||||
#define POLLOUT 0x004 /* Writing now will not block. */
|
||||
|
||||
/* Event types always implicitly polled for. These bits need not be set in
|
||||
`events', but they will appear in `revents' to indicate the status of
|
||||
the file descriptor. */
|
||||
#define POLLERR 0x008 /* Error condition. */
|
||||
#define POLLHUP 0x010 /* Hung up. */
|
||||
#define POLLNVAL 0x020 /* Invalid polling request. */
|
||||
|
||||
/* Data structure describing a polling request. */
|
||||
struct pollfd
|
||||
{
|
||||
int fd; /* File descriptor to poll. */
|
||||
short int events; /* Types of events poller cares about. */
|
||||
short int revents; /* Types of events that actually occurred. */
|
||||
};
|
||||
|
||||
/* Poll the file descriptors described by the NFDS structures starting at
|
||||
FDS. If TIMEOUT is nonzero and not -1, allow TIMEOUT milliseconds for
|
||||
an event to occur; if TIMEOUT is -1, block until an event occurs.
|
||||
Returns the number of file descriptors with events, zero if timed out,
|
||||
or -1 for errors. */
|
||||
#endif
|
||||
|
||||
typedef unsigned long nfds_t;
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int poll (struct pollfd *pfd, nfds_t nfd, int timeout);
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user