-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
245 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
#include "../eventloop_posix.h" | ||
#include "mbed.h" | ||
|
||
UA_StatusCode | ||
UA_EventLoopPOSIX_setNonBlocking(UA_FD sockfd) { | ||
((Socket*)sockfd)->set_blocking(false); | ||
return UA_STATUSCODE_GOOD; | ||
} | ||
|
||
UA_StatusCode | ||
UA_EventLoopPOSIX_setNoSigPipe(UA_FD sockfd) { | ||
return UA_STATUSCODE_GOOD; | ||
} | ||
|
||
UA_StatusCode | ||
UA_EventLoopPOSIX_setReusable(UA_FD sockfd) { | ||
return UA_STATUSCODE_GOOD; | ||
} | ||
|
||
UA_StatusCode | ||
UA_EventLoopPOSIX_registerFD(UA_EventLoopPOSIX *el, UA_RegisteredFD *rfd) { | ||
UA_LOCK_ASSERT(&el->elMutex, 1); | ||
UA_LOG_DEBUG(el->eventLoop.logger, UA_LOGCATEGORY_EVENTLOOP, | ||
"Registering fd: %u", (unsigned)rfd->fd); | ||
|
||
/* Realloc */ | ||
UA_RegisteredFD **fds_tmp = (UA_RegisteredFD**) | ||
UA_realloc(el->fds, sizeof(UA_RegisteredFD*) * (el->fdsSize + 1)); | ||
if(!fds_tmp) { | ||
return UA_STATUSCODE_BADOUTOFMEMORY; | ||
} | ||
el->fds = fds_tmp; | ||
|
||
/* Add to the last entry */ | ||
el->fds[el->fdsSize] = rfd; | ||
el->fdsSize++; | ||
return UA_STATUSCODE_GOOD; | ||
} | ||
|
||
UA_StatusCode | ||
UA_EventLoopPOSIX_modifyFD(UA_EventLoopPOSIX *el, UA_RegisteredFD *rfd) { | ||
/* Do nothing, it is enough if the data was changed in the rfd */ | ||
UA_LOCK_ASSERT(&el->elMutex, 1); | ||
return UA_STATUSCODE_GOOD; | ||
} | ||
|
||
void | ||
UA_EventLoopPOSIX_deregisterFD(UA_EventLoopPOSIX *el, UA_RegisteredFD *rfd) { | ||
UA_LOCK_ASSERT(&el->elMutex, 1); | ||
UA_LOG_DEBUG(el->eventLoop.logger, UA_LOGCATEGORY_EVENTLOOP, | ||
"Unregistering fd: %u", (unsigned)rfd->fd); | ||
|
||
/* Find the entry */ | ||
size_t i = 0; | ||
for(; i < el->fdsSize; i++) { | ||
if(el->fds[i] == rfd) | ||
break; | ||
} | ||
|
||
/* Not found? */ | ||
if(i == el->fdsSize) | ||
return; | ||
|
||
if(el->fdsSize > 1) { | ||
/* Move the last entry in the ith slot and realloc. */ | ||
el->fdsSize--; | ||
el->fds[i] = el->fds[el->fdsSize]; | ||
UA_RegisteredFD **fds_tmp = (UA_RegisteredFD**) | ||
UA_realloc(el->fds, sizeof(UA_RegisteredFD*) * el->fdsSize); | ||
/* if realloc fails the fds are still in a correct state with | ||
* possibly lost memory, so failing silently here is ok */ | ||
if(fds_tmp) | ||
el->fds = fds_tmp; | ||
} else { | ||
/* Remove the last entry */ | ||
UA_free(el->fds); | ||
el->fds = NULL; | ||
el->fdsSize = 0; | ||
} | ||
} | ||
|
||
rtos::EventFlags _events; | ||
|
||
UA_StatusCode | ||
UA_EventLoopPOSIX_pollFDs(UA_EventLoopPOSIX *el, UA_DateTime listenTimeout) { | ||
UA_assert(listenTimeout >= 0); | ||
UA_LOCK_ASSERT(&el->elMutex, 1); | ||
|
||
|
||
auto flag = _events.wait_any(0x7fffffff, 10); | ||
if (flag & 0x80000000) { | ||
yield(); | ||
return UA_STATUSCODE_GOOD; | ||
} | ||
UA_FD ev_sock = flag; | ||
|
||
/* Loop over all registered FD to see if an event arrived. Yes, this is why | ||
* select is slow for many open sockets. */ | ||
for(size_t i = 0; i < el->fdsSize; i++) { | ||
UA_RegisteredFD *rfd = el->fds[i]; | ||
|
||
if (el->fds[i]->fd != (UA_FD)ev_sock) { | ||
// TODO: find out why we don't receive the first accepted socket event | ||
//continue; | ||
} | ||
|
||
/* 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 = rfd->listenEvents; | ||
|
||
UA_LOG_INFO(el->eventLoop.logger, UA_LOGCATEGORY_EVENTLOOP, | ||
"Processing event %u on fd %u", (unsigned)event, | ||
(unsigned)rfd->fd); | ||
|
||
/* Call the EventSource callback */ | ||
rfd->eventSourceCB(rfd->es, rfd, event); | ||
} | ||
return UA_STATUSCODE_GOOD; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters