diff options
Diffstat (limited to 'stacks/lwip_stack/lwip_src/socket')
-rw-r--r-- | stacks/lwip_stack/lwip_src/socket/stackx_event.c | 117 |
1 files changed, 108 insertions, 9 deletions
diff --git a/stacks/lwip_stack/lwip_src/socket/stackx_event.c b/stacks/lwip_stack/lwip_src/socket/stackx_event.c index 93e47a3..1967ecc 100644 --- a/stacks/lwip_stack/lwip_src/socket/stackx_event.c +++ b/stacks/lwip_stack/lwip_src/socket/stackx_event.c @@ -17,13 +17,112 @@ #include "stackx_spl_share.h" #include "common_pal_bitwide_adjust.h" #include "stackx_event.h" -#include <netinet/in.h> - -#define FREE_FD_SET(readfd, writefd, exceptfd) {\ - if(readfd)\ - free(readfd);\ - if(writefd)\ - free(writefd);\ - if(exceptfd)\ - free(exceptfd);\ + +#define FREE_FD_SET(readfd, writefd, exceptfd) {\ +if(readfd)\ +free(readfd);\ +if(writefd)\ +free(writefd);\ +if(exceptfd)\ +free(exceptfd);\ +} + +int +lwip_try_select (int fdsize, fd_set * fdread, fd_set * fdwrite, + fd_set * fderr, struct timeval *timeout) +{ + int i; + int nready = 0; + nstack_fd_set *read_out; + nstack_fd_set *write_out; + nstack_fd_set *err_out; + nstack_fd_set *readfd = (nstack_fd_set *) fdread; + nstack_fd_set *writefd = (nstack_fd_set *) fdwrite; + nstack_fd_set *exceptfd = (nstack_fd_set *) fderr; + sbr_socket_t *sock; + spl_netconn_t *conn; + + if ((fdsize >= NSTACK_SETSIZE) || (fdsize < 0)) + { + return 0; + } + read_out = malloc (sizeof (nstack_fd_set)); + write_out = malloc (sizeof (nstack_fd_set)); + err_out = malloc (sizeof (nstack_fd_set)); + if ((!read_out) || (!write_out) || (!err_out)) + { + NSPOL_LOGERR ("malloc nstack_fd_set fail"); + FREE_FD_SET (read_out, write_out, err_out); + return -1; + } + int ret = NSTACK_FD_ZERO (read_out); + int ret1 = NSTACK_FD_ZERO (write_out); + int ret2 = NSTACK_FD_ZERO (err_out); + + if ((EOK != ret) || (EOK != ret1) || (EOK != ret2)) + { + NSPOL_LOGERR ("NSTACK_FD_ZERO fail"); + FREE_FD_SET (read_out, write_out, err_out); + return -1; + } + + for (i = 0; i < fdsize; i++) + { + if (!((readfd && NSTACK_FD_ISSET (i, readfd)) + || (writefd && NSTACK_FD_ISSET (i, writefd)) + || (exceptfd && NSTACK_FD_ISSET (i, exceptfd)))) + { + continue; + } + sock = sbr_lookup_sk (i); + if (sock == NULL) + { + errno = EBADF; + FREE_FD_SET (read_out, write_out, err_out); + return -1; + } + conn = sbr_get_conn (sock); + if (!conn) + { + errno = EBADF; + FREE_FD_SET (read_out, write_out, err_out); + return -1; + } + if (readfd && NSTACK_FD_ISSET (i, readfd) && + ((sbr_get_fd_share (sock)->lastdata != NULL) + || (conn->rcvevent > 0))) + { + NSTACK_FD_SET (i, read_out); + nready++; + } + if (writefd && NSTACK_FD_ISSET (i, writefd) && (conn->sendevent != 0)) + { + NSTACK_FD_SET (i, write_out); + nready++; + } + if (exceptfd && NSTACK_FD_ISSET (i, exceptfd) && (conn->errevent != 0)) + { + NSTACK_FD_SET (i, write_out); + nready++; + } + } + + //TODO: need to handle fd_set and nstack_fd_set memory issue + if (readfd) + { + *readfd = *read_out; + } + + if (writefd) + { + *writefd = *write_out; + } + + if (exceptfd) + { + *exceptfd = *err_out; + } + + FREE_FD_SET (read_out, write_out, err_out); + return nready; } |