Skip to content

Commit

Permalink
fix win32 index handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sreimers committed Aug 17, 2023
1 parent 8e510bb commit 37e09d8
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions src/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ enum {

/** File descriptor handler struct */
struct re_fhs {
int index;
re_sock_t fd; /**< File Descriptor */
int flags; /**< Polling flags (Read, Write, etc.) */
fd_h* fh; /**< Event handler */
Expand Down Expand Up @@ -286,39 +287,34 @@ static void fd_handler(struct re_fhs *fhs, int flags)
#ifdef HAVE_SELECT
static int set_select_fds(struct re *re, struct re_fhs *fhs)
{
int index;
int i;

if (!re || !fhs)
return EINVAL;

#ifdef WIN32
int i;
for (i = 0; i < re->nfds; i++) {
if (re->fhsl[i] == fhs)
goto idx;
if (fhs->index != -1) {
i = fhs->index;
goto update;
}

/* if nothing is found a linear search for the first
* zeroed handler */
for (i = 0; i < re->maxfds; i++) {
if (!re->fhsl[i])
goto idx;
goto update;
}

return ERANGE;
idx:
index = i;
#else
index = fhs->fd;
#endif


if (fhs->flags)
re->fhsl[index] = fhs;
else
re->fhsl[index] = NULL;

re->nfds = max(re->nfds, (int)fhs->fd + 1);
update:
if (fhs->flags) {
re->fhsl[i] = fhs;
fhs->index = i;
}
else {
re->fhsl[i] = NULL;
fhs->index = -1;
}

return 0;
}
Expand Down Expand Up @@ -610,6 +606,7 @@ int fd_listen(struct re_fhs **fhsp, re_sock_t fd, int flags, fd_h fh,
return ENOMEM;

fhs->fd = RE_BAD_SOCK;
fhs->index = -1;

*fhsp = fhs;
}
Expand Down Expand Up @@ -667,7 +664,6 @@ int fd_listen(struct re_fhs **fhsp, re_sock_t fd, int flags, fd_h fh,
++re->nfds;
}


/* Update listening flags */
fhs->flags = flags;

Expand Down Expand Up @@ -729,6 +725,7 @@ static int fd_poll(struct re *re)
{
const uint64_t to = tmr_next_timeout(re->tmrl);
int i, n;
int nfds = re->nfds;
struct re_fhs *fhs = NULL;
#ifdef HAVE_SELECT
fd_set rfds, wfds, efds;
Expand All @@ -742,34 +739,44 @@ static int fd_poll(struct re *re)
#ifdef HAVE_SELECT
case METHOD_SELECT: {
struct timeval tv;
int max_fd = 0;
int cfds = 0;

/* Clear and update fd sets */
FD_ZERO(&rfds);
FD_ZERO(&wfds);
FD_ZERO(&efds);

for (i=0; i<re->nfds; i++) {
for (i = 0; cfds < nfds; i++) {
fhs = re->fhsl[i];

if (!fhs || !fhs->fh)
continue;

++cfds;

re_sock_t fd = fhs->fd;
if (fhs->flags & FD_READ)
FD_SET(fd, &rfds);
if (fhs->flags & FD_WRITE)
FD_SET(fd, &wfds);
if (fhs->flags & FD_EXCEPT)
FD_SET(fd, &efds);
#if !defined(WIN32)
max_fd = max(max_fd, fd + 1);
#endif
}

#ifdef WIN32
tv.tv_sec = (long) to / 1000;
#else
tv.tv_sec = (time_t) to / 1000;
nfds = max_fd;
#endif
tv.tv_usec = (uint32_t) (to % 1000) * 1000;

re_unlock(re);
n = select(re->nfds, &rfds, &wfds, &efds, to ? &tv : NULL);
n = select(max_fd, &rfds, &wfds, &efds, to ? &tv : NULL);
re_lock(re);
}
break;
Expand Down Expand Up @@ -808,7 +815,7 @@ static int fd_poll(struct re *re)
return RE_ERRNO_SOCK;

/* Check for events */
for (i=0; (n > 0) && (i < re->nfds); i++) {
for (i=0; (n > 0) && (i < nfds); i++) {
re_sock_t fd;
int flags = 0;

Expand Down

0 comments on commit 37e09d8

Please sign in to comment.