Skip to content

Commit

Permalink
feat(el): Check for closed connections during select/epoll processing
Browse files Browse the repository at this point in the history
  • Loading branch information
jpfr committed Oct 10, 2023
1 parent 34e060c commit 188e712
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
9 changes: 9 additions & 0 deletions arch/eventloop_posix_epoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ UA_EventLoopPOSIX_pollFDs(UA_EventLoopPOSIX *el, UA_DateTime listenTimeout) {
/* Process all received events */
for(int i = 0; i < events; i++) {
UA_RegisteredFD *rfd = (UA_RegisteredFD*)epoll_events[i].data.ptr;

/* The rfd is already registered for removal. Don't process incoming
* events any longer. */
if(rfd->dc.callback)
continue;

/* Get the event */
short revent = 0;
if((epoll_events[i].events & EPOLLIN) == EPOLLIN) {
revent = UA_FDEVENT_IN;
Expand All @@ -111,6 +118,8 @@ UA_EventLoopPOSIX_pollFDs(UA_EventLoopPOSIX *el, UA_DateTime listenTimeout) {
} else {
revent = UA_FDEVENT_ERR;
}

/* Call the EventSource callback */
rfd->eventSourceCB(rfd->es, rfd, revent);
}
return UA_STATUSCODE_GOOD;
Expand Down
20 changes: 13 additions & 7 deletions arch/eventloop_posix_select.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,26 +135,32 @@ UA_EventLoopPOSIX_pollFDs(UA_EventLoopPOSIX *el, UA_DateTime listenTimeout) {
}

/* Loop over all registered FD to see if an event arrived. Yes, this is why
* select is slow for many open sockets. */
* select is slow for many open sockets. */
for(size_t i = 0; i < el->fdsSize; i++) {
UA_RegisteredFD *rfd = el->fds[i];
UA_FD fd = rfd->fd;

/* Error Event */
/* The rfd is already registered for removal. Don't process incoming
* events any longer. */
if(rfd->dc.callback)
continue;

/* Event signaled for the fd? */
short event = 0;
if(FD_ISSET(fd, &readset)) {
if(FD_ISSET(rfd->fd, &readset)) {
event = UA_FDEVENT_IN;
} else if(FD_ISSET(fd, &writeset)) {
} else if(FD_ISSET(rfd->fd, &writeset)) {
event = UA_FDEVENT_OUT;
} else if(FD_ISSET(fd, &errset)) {
} else if(FD_ISSET(rfd->fd, &errset)) {
event = UA_FDEVENT_ERR;
} else {
continue;
}

UA_LOG_DEBUG(el->eventLoop.logger, UA_LOGCATEGORY_EVENTLOOP,
"Processing event %u on fd %u", (unsigned)event, (unsigned)fd);
"Processing event %u on fd %u", (unsigned)event,
(unsigned)rfd->fd);

/* Call the EventSource callback */
rfd->eventSourceCB(rfd->es, rfd, event);

/* The fd has removed itself */
Expand Down

0 comments on commit 188e712

Please sign in to comment.