2021-03-24 20:32:43 +03:00
|
|
|
#ifndef _WIN32
|
2021-03-16 17:15:13 +03:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "pollset.h"
|
|
|
|
#include <winpr/handle.h>
|
2021-03-24 20:32:43 +03:00
|
|
|
#include <winpr/sysinfo.h>
|
2022-11-24 12:24:19 +03:00
|
|
|
#include <winpr/assert.h>
|
2021-03-16 17:15:13 +03:00
|
|
|
#include "../log.h"
|
|
|
|
|
|
|
|
#define TAG WINPR_TAG("sync.pollset")
|
|
|
|
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_POLL_H
|
2021-06-18 10:58:18 +03:00
|
|
|
static INT16 handle_mode_to_pollevent(ULONG mode)
|
2021-03-16 17:15:13 +03:00
|
|
|
{
|
2021-06-18 10:58:18 +03:00
|
|
|
INT16 event = 0;
|
2021-03-16 17:15:13 +03:00
|
|
|
|
|
|
|
if (mode & WINPR_FD_READ)
|
|
|
|
event |= POLLIN;
|
|
|
|
|
|
|
|
if (mode & WINPR_FD_WRITE)
|
|
|
|
event |= POLLOUT;
|
|
|
|
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
BOOL pollset_init(WINPR_POLL_SET* set, size_t nhandles)
|
|
|
|
{
|
2022-11-24 12:24:19 +03:00
|
|
|
WINPR_ASSERT(set);
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_POLL_H
|
2021-03-16 17:15:13 +03:00
|
|
|
if (nhandles > MAXIMUM_WAIT_OBJECTS)
|
|
|
|
{
|
|
|
|
set->isStatic = FALSE;
|
|
|
|
set->pollset = calloc(nhandles, sizeof(*set->pollset));
|
|
|
|
if (!set->pollset)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
set->pollset = set->staticSet;
|
|
|
|
set->isStatic = TRUE;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
set->fdIndex = calloc(nhandles, sizeof(*set->fdIndex));
|
|
|
|
if (!set->fdIndex)
|
|
|
|
return FALSE;
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
FD_ZERO(&set->rset_base);
|
2021-03-16 17:15:13 +03:00
|
|
|
FD_ZERO(&set->rset);
|
2021-03-24 20:32:43 +03:00
|
|
|
FD_ZERO(&set->wset_base);
|
2021-03-16 17:15:13 +03:00
|
|
|
FD_ZERO(&set->wset);
|
|
|
|
set->maxFd = 0;
|
|
|
|
set->nread = set->nwrite = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
set->size = nhandles;
|
|
|
|
set->fillIndex = 0;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pollset_uninit(WINPR_POLL_SET* set)
|
|
|
|
{
|
2022-11-24 12:24:19 +03:00
|
|
|
WINPR_ASSERT(set);
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_POLL_H
|
2021-03-16 17:15:13 +03:00
|
|
|
if (!set->isStatic)
|
|
|
|
free(set->pollset);
|
|
|
|
#else
|
|
|
|
free(set->fdIndex);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void pollset_reset(WINPR_POLL_SET* set)
|
|
|
|
{
|
2022-11-24 12:24:19 +03:00
|
|
|
WINPR_ASSERT(set);
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifndef WINPR_HAVE_POLL_H
|
2021-03-24 20:32:43 +03:00
|
|
|
FD_ZERO(&set->rset_base);
|
|
|
|
FD_ZERO(&set->wset_base);
|
2021-03-16 17:15:13 +03:00
|
|
|
set->maxFd = 0;
|
|
|
|
set->nread = set->nwrite = 0;
|
|
|
|
#endif
|
|
|
|
set->fillIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL pollset_add(WINPR_POLL_SET* set, int fd, ULONG mode)
|
|
|
|
{
|
2022-11-24 12:24:19 +03:00
|
|
|
WINPR_ASSERT(set);
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_POLL_H
|
2024-01-23 18:49:54 +03:00
|
|
|
struct pollfd* item = NULL;
|
2021-03-16 17:15:13 +03:00
|
|
|
if (set->fillIndex == set->size)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
item = &set->pollset[set->fillIndex];
|
|
|
|
item->fd = fd;
|
|
|
|
item->revents = 0;
|
|
|
|
item->events = handle_mode_to_pollevent(mode);
|
|
|
|
#else
|
|
|
|
FdIndex* fdIndex = &set->fdIndex[set->fillIndex];
|
|
|
|
if (mode & WINPR_FD_READ)
|
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
FD_SET(fd, &set->rset_base);
|
2021-03-16 17:15:13 +03:00
|
|
|
set->nread++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode & WINPR_FD_WRITE)
|
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
FD_SET(fd, &set->wset_base);
|
2021-03-16 17:15:13 +03:00
|
|
|
set->nwrite++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fd > set->maxFd)
|
|
|
|
set->maxFd = fd;
|
|
|
|
|
|
|
|
fdIndex->fd = fd;
|
|
|
|
fdIndex->mode = mode;
|
|
|
|
#endif
|
|
|
|
set->fillIndex++;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pollset_poll(WINPR_POLL_SET* set, DWORD dwMilliseconds)
|
|
|
|
{
|
2022-11-24 12:24:19 +03:00
|
|
|
WINPR_ASSERT(set);
|
2021-03-24 20:32:43 +03:00
|
|
|
int ret = 0;
|
2024-01-23 18:49:54 +03:00
|
|
|
UINT64 dueTime = 0;
|
|
|
|
UINT64 now = 0;
|
2021-03-24 20:32:43 +03:00
|
|
|
|
|
|
|
now = GetTickCount64();
|
|
|
|
if (dwMilliseconds == INFINITE)
|
|
|
|
dueTime = 0xFFFFFFFFFFFFFFFF;
|
|
|
|
else
|
|
|
|
dueTime = now + dwMilliseconds;
|
|
|
|
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_POLL_H
|
2024-01-23 18:49:54 +03:00
|
|
|
int timeout = 0;
|
2021-03-24 20:32:43 +03:00
|
|
|
|
2021-03-16 17:15:13 +03:00
|
|
|
do
|
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
if (dwMilliseconds == INFINITE)
|
|
|
|
timeout = -1;
|
|
|
|
else
|
|
|
|
timeout = (int)(dueTime - now);
|
2021-03-16 17:15:13 +03:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
ret = poll(set->pollset, set->fillIndex, timeout);
|
|
|
|
if (ret >= 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (errno != EINTR)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
now = GetTickCount64();
|
|
|
|
} while (now < dueTime);
|
2021-03-16 17:15:13 +03:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
#else
|
2021-03-16 17:15:13 +03:00
|
|
|
do
|
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
struct timeval staticTimeout;
|
|
|
|
struct timeval* timeout;
|
|
|
|
|
|
|
|
fd_set* rset = NULL;
|
|
|
|
fd_set* wset = NULL;
|
|
|
|
|
|
|
|
if (dwMilliseconds == INFINITE)
|
|
|
|
{
|
|
|
|
timeout = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
long waitTime = (long)(dueTime - now);
|
|
|
|
|
|
|
|
timeout = &staticTimeout;
|
|
|
|
timeout->tv_sec = waitTime / 1000;
|
|
|
|
timeout->tv_usec = (waitTime % 1000) * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (set->nread)
|
|
|
|
{
|
|
|
|
rset = &set->rset;
|
|
|
|
memcpy(rset, &set->rset_base, sizeof(*rset));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (set->nwrite)
|
|
|
|
{
|
|
|
|
wset = &set->wset;
|
|
|
|
memcpy(wset, &set->wset_base, sizeof(*wset));
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = select(set->maxFd + 1, rset, wset, NULL, timeout);
|
|
|
|
if (ret >= 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (errno != EINTR)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
now = GetTickCount64();
|
|
|
|
|
|
|
|
} while (now < dueTime);
|
|
|
|
|
|
|
|
FD_ZERO(&set->rset);
|
|
|
|
FD_ZERO(&set->wset);
|
2021-03-16 17:15:13 +03:00
|
|
|
#endif
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
return 0; /* timeout */
|
2021-03-16 17:15:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL pollset_isSignaled(WINPR_POLL_SET* set, size_t idx)
|
|
|
|
{
|
2022-11-24 12:24:19 +03:00
|
|
|
WINPR_ASSERT(set);
|
|
|
|
|
2021-03-16 17:15:13 +03:00
|
|
|
if (idx > set->fillIndex)
|
|
|
|
{
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_ERR(TAG, "index=%d out of pollset(fillIndex=%" PRIuz ")", idx, set->fillIndex);
|
2021-03-16 17:15:13 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_POLL_H
|
2021-03-16 17:15:13 +03:00
|
|
|
return !!(set->pollset[idx].revents & set->pollset[idx].events);
|
|
|
|
#else
|
|
|
|
FdIndex* fdIndex = &set->fdIndex[idx];
|
|
|
|
if (fdIndex->fd < 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if ((fdIndex->mode & WINPR_FD_READ) && FD_ISSET(fdIndex->fd, &set->rset))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if ((fdIndex->mode & WINPR_FD_WRITE) && FD_ISSET(fdIndex->fd, &set->wset))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
2022-11-24 12:24:19 +03:00
|
|
|
|
|
|
|
BOOL pollset_isReadSignaled(WINPR_POLL_SET* set, size_t idx)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(set);
|
|
|
|
|
|
|
|
if (idx > set->fillIndex)
|
|
|
|
{
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_ERR(TAG, "index=%d out of pollset(fillIndex=%" PRIuz ")", idx, set->fillIndex);
|
2022-11-24 12:24:19 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_POLL_H
|
2022-11-24 12:24:19 +03:00
|
|
|
return !!(set->pollset[idx].revents & POLLIN);
|
|
|
|
#else
|
|
|
|
FdIndex* fdIndex = &set->fdIndex[idx];
|
|
|
|
if (fdIndex->fd < 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return FD_ISSET(fdIndex->fd, &set->rset);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL pollset_isWriteSignaled(WINPR_POLL_SET* set, size_t idx)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(set);
|
|
|
|
|
|
|
|
if (idx > set->fillIndex)
|
|
|
|
{
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_ERR(TAG, "index=%d out of pollset(fillIndex=%" PRIuz ")", idx, set->fillIndex);
|
2022-11-24 12:24:19 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_POLL_H
|
2022-11-24 12:24:19 +03:00
|
|
|
return !!(set->pollset[idx].revents & POLLOUT);
|
|
|
|
#else
|
|
|
|
FdIndex* fdIndex = &set->fdIndex[idx];
|
|
|
|
if (fdIndex->fd < 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return FD_ISSET(fdIndex->fd, &set->wset);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
#endif
|