diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2016-08-23 20:13:19 +0000 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2016-08-23 20:13:19 +0000 |
commit | 16a4b0b527e56e9b7268bfd921646bfd722e56bb (patch) | |
tree | 9b0e4f2a6529b913d60594c2f00e799d02ed8785 | |
parent | 58ce3ae66ee79602a381803a4dab1fb3a148634f (diff) |
mhd_sockets.h: added fd_set macros to use less '#ifdef' in code
-rw-r--r-- | src/microhttpd/daemon.c | 40 | ||||
-rw-r--r-- | src/microhttpd/mhd_sockets.h | 47 |
2 files changed, 58 insertions, 29 deletions
diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c index 076a7ada..eef9eca8 100644 --- a/src/microhttpd/daemon.c +++ b/src/microhttpd/daemon.c @@ -650,22 +650,12 @@ add_to_fd_set (MHD_socket fd, MHD_socket *max_fd, unsigned int fd_setsize) { - if (NULL == set) + if (NULL == set || MHD_INVALID_SOCKET == fd) return MHD_NO; -#ifdef MHD_WINSOCK_SOCKETS - if (set->fd_count >= fd_setsize) - { - if (FD_ISSET(fd, set)) - return MHD_YES; - else - return MHD_NO; - } -#else /* ! MHD_WINSOCK_SOCKETS */ - if (fd >= (MHD_socket)fd_setsize) + if (!MHD_SCKT_FD_FITS_FDSET_SETSIZE_(fd, set, fd_setsize)) return MHD_NO; -#endif /* ! MHD_WINSOCK_SOCKETS */ - FD_SET (fd, set); - if ( (NULL != max_fd) && (MHD_INVALID_SOCKET != fd) && + MHD_SCKT_ADD_FD_TO_FDSET_SETSIZE_(fd, set, fd_setsize); + if ( (NULL != max_fd) && ((fd > *max_fd) || (MHD_INVALID_SOCKET == *max_fd)) ) *max_fd = fd; @@ -1325,15 +1315,14 @@ internal_add_connection (struct MHD_Daemon *daemon, return MHD_NO; } -#ifndef MHD_WINSOCK_SOCKETS - if ( (client_socket >= FD_SETSIZE) && + if ( (!MHD_SCKT_FD_FITS_FDSET_(client_socket, NULL)) && (0 == (daemon->options & (MHD_USE_POLL | MHD_USE_EPOLL_LINUX_ONLY))) ) { #ifdef HAVE_MESSAGES MHD_DLOG (daemon, "Socket descriptor larger than FD_SETSIZE: %d > %d\n", - client_socket, - FD_SETSIZE); + (int)client_socket, + (int)FD_SETSIZE); #endif if (0 != MHD_socket_close_ (client_socket)) MHD_PANIC ("close failed\n"); @@ -1342,7 +1331,6 @@ internal_add_connection (struct MHD_Daemon *daemon, #endif return MHD_NO; } -#endif #ifdef HAVE_MESSAGES @@ -2267,7 +2255,7 @@ MHD_run_from_select (struct MHD_Daemon *daemon, { /* we're in epoll mode, the epoll FD stands for the entire event set! */ - if (daemon->epoll_fd >= FD_SETSIZE) + if (!MHD_SCKT_FD_FITS_FDSET_(daemon->epoll_fd, NULL)) return MHD_NO; /* poll fd too big, fail hard */ if (FD_ISSET (daemon->epoll_fd, read_fd_set)) return MHD_run (daemon); @@ -3772,10 +3760,9 @@ MHD_start_daemon_va (unsigned int flags, } make_nonblocking (daemon, daemon->wpipe[1]); } -#ifndef MHD_WINSOCK_SOCKETS if ( (0 == (flags & (MHD_USE_POLL | MHD_USE_EPOLL_LINUX_ONLY))) && (1 == use_pipe) && - (daemon->wpipe[0] >= FD_SETSIZE) ) + (!MHD_SCKT_FD_FITS_FDSET_(daemon->wpipe[0], NULL)) ) { #ifdef HAVE_MESSAGES MHD_DLOG (daemon, @@ -3788,7 +3775,6 @@ MHD_start_daemon_va (unsigned int flags, free (daemon); return NULL; } -#endif #ifdef DAUTH_SUPPORT daemon->digest_auth_rand_size = 0; daemon->digest_auth_random = NULL; @@ -4131,8 +4117,7 @@ MHD_start_daemon_va (unsigned int flags, goto free_and_fail; } } -#ifndef MHD_WINSOCK_SOCKETS - if ( (socket_fd >= FD_SETSIZE) && + if ( (!MHD_SCKT_FD_FITS_FDSET_(socket_fd, NULL)) && (0 == (flags & (MHD_USE_POLL | MHD_USE_EPOLL_LINUX_ONLY)) ) ) { #ifdef HAVE_MESSAGES @@ -4145,7 +4130,6 @@ MHD_start_daemon_va (unsigned int flags, MHD_PANIC ("close failed\n"); goto free_and_fail; } -#endif #if EPOLL_SUPPORT if ( (0 != (flags & MHD_USE_EPOLL_LINUX_ONLY)) && @@ -4293,9 +4277,8 @@ MHD_start_daemon_va (unsigned int flags, } make_nonblocking (d, d->wpipe[1]); } -#ifndef MHD_WINSOCK_SOCKETS if ( (0 == (flags & (MHD_USE_POLL | MHD_USE_EPOLL_LINUX_ONLY))) && - (d->wpipe[0] >= FD_SETSIZE) ) + (!MHD_SCKT_FD_FITS_FDSET_(d->wpipe[0], NULL)) ) { #ifdef HAVE_MESSAGES MHD_DLOG (daemon, @@ -4307,7 +4290,6 @@ MHD_start_daemon_va (unsigned int flags, MHD_PANIC ("close failed\n"); goto thread_failed; } -#endif /* Divide available connections evenly amongst the threads. * Thread indexes in [0, leftover_conns) each get one of the diff --git a/src/microhttpd/mhd_sockets.h b/src/microhttpd/mhd_sockets.h index acffaa57..13b90d2e 100644 --- a/src/microhttpd/mhd_sockets.h +++ b/src/microhttpd/mhd_sockets.h @@ -204,6 +204,53 @@ # define MHD_socket_close_(fd) closesocket((fd)) #endif +/** + * Check whether FD can be added to fd_set with specified FD_SETSIZE. + * @param fd the fd to check + * @param pset the pointer to fd_set to check or NULL to check + * whether FD can be used with fd_sets. + * @param setsize the value of FD_SETSIZE. + * @return boolean true if FD can be added to fd_set, + * boolean false otherwise. + */ +#if defined(MHD_POSIX_SOCKETS) +# define MHD_SCKT_FD_FITS_FDSET_SETSIZE_(fd,pset,setsize) ((fd) < (setsize)) +#elif defined(MHD_WINSOCK_SOCKETS) +# define MHD_SCKT_FD_FITS_FDSET_SETSIZE_(fd,pset,setsize) ( ((void*)(pset)==(void*)0) || \ + (((fd_set*)(pset))->fd_count < (setsize)) || \ + (FD_ISSET((fd),(pset))) ) +#endif + +/** + * Check whether FD can be added to fd_set with current FD_SETSIZE. + * @param fd the fd to check + * @param pset the pointer to fd_set to check or NULL to check + * whether FD can be used with fd_sets. + * @return boolean true if FD can be added to fd_set, + * boolean false otherwise. + */ +#define MHD_SCKT_FD_FITS_FDSET_(fd,pset) MHD_SCKT_FD_FITS_FDSET_SETSIZE_((fd),(pset),FD_SETSIZE) + +/** + * Add FD to fd_set with specified FD_SETSIZE. + * @param fd the fd to add + * @param pset the valid pointer to fd_set. + * @param setsize the value of FD_SETSIZE. + * @note To work on W32 with value of FD_SETSIZE different from currently defined value, + * system definition of FD_SET() is not used. + */ +#if defined(MHD_POSIX_SOCKETS) +# define MHD_SCKT_ADD_FD_TO_FDSET_SETSIZE_(fd,pset,setsize) FD_SET((fd),(pset)) +#elif defined(MHD_WINSOCK_SOCKETS) +# define MHD_SCKT_ADD_FD_TO_FDSET_SETSIZE_(fd,pset,setsize) \ + do { \ + u_int _i_ = 0; \ + fd_set* const _s_ = (fd_set*)(pset); \ + while((_i_ < _s_->fd_count) && ((fd) != _s_->fd_array[_i_])) {++_i_;} \ + if ((_i_ == _s_->fd_count)) {_s_->fd_array[_s_->fd_count++] = (fd);} \ + } while(0) +#endif + /* MHD_SYS_select_ is wrapper macro for system select() function */ #if !defined(MHD_WINSOCK_SOCKETS) # define MHD_SYS_select_(n,r,w,e,t) select((n),(r),(w),(e),(t)) |