diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2016-08-23 20:13:23 +0000 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2016-08-23 20:13:23 +0000 |
commit | b6d29e0cb35aeb2df21f2e959a93ca0552951009 (patch) | |
tree | 02db574a6e238a042c1a935f8be4b355c4853c4b | |
parent | 16a4b0b527e56e9b7268bfd921646bfd722e56bb (diff) | |
download | libmicrohttpd-b6d29e0cb35aeb2df21f2e959a93ca0552951009.tar.gz libmicrohttpd-b6d29e0cb35aeb2df21f2e959a93ca0552951009.zip |
Moved add_to_fd_set() to mhd_sockets.c, simplified return value
-rw-r--r-- | src/microhttpd/daemon.c | 79 | ||||
-rw-r--r-- | src/microhttpd/mhd_sockets.c | 29 | ||||
-rw-r--r-- | src/microhttpd/mhd_sockets.h | 17 |
3 files changed, 69 insertions, 56 deletions
diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c index eef9eca8..105f3083 100644 --- a/src/microhttpd/daemon.c +++ b/src/microhttpd/daemon.c | |||
@@ -634,34 +634,6 @@ MHD_TLS_init (struct MHD_Daemon *daemon) | |||
634 | #endif | 634 | #endif |
635 | 635 | ||
636 | 636 | ||
637 | /** | ||
638 | * Add @a fd to the @a set. If @a fd is | ||
639 | * greater than @a max_fd, set @a max_fd to @a fd. | ||
640 | * | ||
641 | * @param fd file descriptor to add to the @a set | ||
642 | * @param set set to modify | ||
643 | * @param max_fd maximum value to potentially update | ||
644 | * @param fd_setsize value of FD_SETSIZE | ||
645 | * @return #MHD_YES on success, #MHD_NO otherwise | ||
646 | */ | ||
647 | static int | ||
648 | add_to_fd_set (MHD_socket fd, | ||
649 | fd_set *set, | ||
650 | MHD_socket *max_fd, | ||
651 | unsigned int fd_setsize) | ||
652 | { | ||
653 | if (NULL == set || MHD_INVALID_SOCKET == fd) | ||
654 | return MHD_NO; | ||
655 | if (!MHD_SCKT_FD_FITS_FDSET_SETSIZE_(fd, set, fd_setsize)) | ||
656 | return MHD_NO; | ||
657 | MHD_SCKT_ADD_FD_TO_FDSET_SETSIZE_(fd, set, fd_setsize); | ||
658 | if ( (NULL != max_fd) && | ||
659 | ((fd > *max_fd) || (MHD_INVALID_SOCKET == *max_fd)) ) | ||
660 | *max_fd = fd; | ||
661 | |||
662 | return MHD_YES; | ||
663 | } | ||
664 | |||
665 | #undef MHD_get_fdset | 637 | #undef MHD_get_fdset |
666 | 638 | ||
667 | /** | 639 | /** |
@@ -740,11 +712,11 @@ MHD_get_fdset2 (struct MHD_Daemon *daemon, | |||
740 | /* we're in epoll mode, use the epoll FD as a stand-in for | 712 | /* we're in epoll mode, use the epoll FD as a stand-in for |
741 | the entire event set */ | 713 | the entire event set */ |
742 | 714 | ||
743 | return add_to_fd_set (daemon->epoll_fd, read_fd_set, max_fd, fd_setsize); | 715 | return MHD_add_to_fd_set_ (daemon->epoll_fd, read_fd_set, max_fd, fd_setsize) ? MHD_YES : MHD_NO; |
744 | } | 716 | } |
745 | #endif | 717 | #endif |
746 | if (MHD_INVALID_SOCKET != daemon->socket_fd && | 718 | if (MHD_INVALID_SOCKET != daemon->socket_fd && |
747 | MHD_YES != add_to_fd_set (daemon->socket_fd, read_fd_set, max_fd, fd_setsize)) | 719 | !MHD_add_to_fd_set_ (daemon->socket_fd, read_fd_set, max_fd, fd_setsize)) |
748 | result = MHD_NO; | 720 | result = MHD_NO; |
749 | 721 | ||
750 | for (pos = daemon->connections_head; NULL != pos; pos = pos->next) | 722 | for (pos = daemon->connections_head; NULL != pos; pos = pos->next) |
@@ -752,19 +724,19 @@ MHD_get_fdset2 (struct MHD_Daemon *daemon, | |||
752 | switch (pos->event_loop_info) | 724 | switch (pos->event_loop_info) |
753 | { | 725 | { |
754 | case MHD_EVENT_LOOP_INFO_READ: | 726 | case MHD_EVENT_LOOP_INFO_READ: |
755 | if (MHD_YES != add_to_fd_set (pos->socket_fd, read_fd_set, max_fd, fd_setsize)) | 727 | if (!MHD_add_to_fd_set_ (pos->socket_fd, read_fd_set, max_fd, fd_setsize)) |
756 | result = MHD_NO; | 728 | result = MHD_NO; |
757 | break; | 729 | break; |
758 | case MHD_EVENT_LOOP_INFO_WRITE: | 730 | case MHD_EVENT_LOOP_INFO_WRITE: |
759 | if (MHD_YES != add_to_fd_set (pos->socket_fd, write_fd_set, max_fd, fd_setsize)) | 731 | if (!MHD_add_to_fd_set_ (pos->socket_fd, write_fd_set, max_fd, fd_setsize)) |
760 | result = MHD_NO; | 732 | result = MHD_NO; |
761 | if (pos->read_buffer_size > pos->read_buffer_offset && | 733 | if (pos->read_buffer_size > pos->read_buffer_offset && |
762 | MHD_YES != add_to_fd_set (pos->socket_fd, read_fd_set, max_fd, fd_setsize)) | 734 | !MHD_add_to_fd_set_ (pos->socket_fd, read_fd_set, max_fd, fd_setsize)) |
763 | result = MHD_NO; | 735 | result = MHD_NO; |
764 | break; | 736 | break; |
765 | case MHD_EVENT_LOOP_INFO_BLOCK: | 737 | case MHD_EVENT_LOOP_INFO_BLOCK: |
766 | if (pos->read_buffer_size > pos->read_buffer_offset && | 738 | if (pos->read_buffer_size > pos->read_buffer_offset && |
767 | MHD_YES != add_to_fd_set (pos->socket_fd, read_fd_set, max_fd, fd_setsize)) | 739 | !MHD_add_to_fd_set_ (pos->socket_fd, read_fd_set, max_fd, fd_setsize)) |
768 | result = MHD_NO; | 740 | result = MHD_NO; |
769 | break; | 741 | break; |
770 | case MHD_EVENT_LOOP_INFO_CLEANUP: | 742 | case MHD_EVENT_LOOP_INFO_CLEANUP: |
@@ -915,23 +887,19 @@ MHD_handle_connection (void *data) | |||
915 | switch (con->event_loop_info) | 887 | switch (con->event_loop_info) |
916 | { | 888 | { |
917 | case MHD_EVENT_LOOP_INFO_READ: | 889 | case MHD_EVENT_LOOP_INFO_READ: |
918 | if (MHD_YES != | 890 | if (!MHD_add_to_fd_set_ (con->socket_fd, &rs, &maxsock, FD_SETSIZE)) |
919 | add_to_fd_set (con->socket_fd, &rs, &maxsock, FD_SETSIZE)) | ||
920 | err_state = 1; | 891 | err_state = 1; |
921 | break; | 892 | break; |
922 | case MHD_EVENT_LOOP_INFO_WRITE: | 893 | case MHD_EVENT_LOOP_INFO_WRITE: |
923 | if (MHD_YES != | 894 | if (!MHD_add_to_fd_set_ (con->socket_fd, &ws, &maxsock, FD_SETSIZE)) |
924 | add_to_fd_set (con->socket_fd, &ws, &maxsock, FD_SETSIZE)) | ||
925 | err_state = 1; | 895 | err_state = 1; |
926 | if ( (con->read_buffer_size > con->read_buffer_offset) && | 896 | if ( (con->read_buffer_size > con->read_buffer_offset) && |
927 | (MHD_YES != | 897 | (!MHD_add_to_fd_set_ (con->socket_fd, &rs, &maxsock, FD_SETSIZE)) ) |
928 | add_to_fd_set (con->socket_fd, &rs, &maxsock, FD_SETSIZE)) ) | ||
929 | err_state = 1; | 898 | err_state = 1; |
930 | break; | 899 | break; |
931 | case MHD_EVENT_LOOP_INFO_BLOCK: | 900 | case MHD_EVENT_LOOP_INFO_BLOCK: |
932 | if ( (con->read_buffer_size > con->read_buffer_offset) && | 901 | if ( (con->read_buffer_size > con->read_buffer_offset) && |
933 | (MHD_YES != | 902 | (!MHD_add_to_fd_set_ (con->socket_fd, &rs, &maxsock, FD_SETSIZE)) ) |
934 | add_to_fd_set (con->socket_fd, &rs, &maxsock, FD_SETSIZE)) ) | ||
935 | err_state = 1; | 903 | err_state = 1; |
936 | tv.tv_sec = 0; | 904 | tv.tv_sec = 0; |
937 | tv.tv_usec = 0; | 905 | tv.tv_usec = 0; |
@@ -944,8 +912,7 @@ MHD_handle_connection (void *data) | |||
944 | #if WINDOWS | 912 | #if WINDOWS |
945 | if (MHD_INVALID_PIPE_ != spipe) | 913 | if (MHD_INVALID_PIPE_ != spipe) |
946 | { | 914 | { |
947 | if (MHD_YES != | 915 | if (!MHD_add_to_fd_set_ (spipe, &rs, &maxsock, FD_SETSIZE)) |
948 | add_to_fd_set (spipe, &rs, &maxsock, FD_SETSIZE)) | ||
949 | err_state = 1; | 916 | err_state = 1; |
950 | } | 917 | } |
951 | #endif | 918 | #endif |
@@ -2339,10 +2306,10 @@ MHD_select (struct MHD_Daemon *daemon, | |||
2339 | { | 2306 | { |
2340 | /* accept only, have one thread per connection */ | 2307 | /* accept only, have one thread per connection */ |
2341 | if ( (MHD_INVALID_SOCKET != daemon->socket_fd) && | 2308 | if ( (MHD_INVALID_SOCKET != daemon->socket_fd) && |
2342 | (MHD_YES != add_to_fd_set (daemon->socket_fd, | 2309 | (!MHD_add_to_fd_set_ (daemon->socket_fd, |
2343 | &rs, | 2310 | &rs, |
2344 | &maxsock, | 2311 | &maxsock, |
2345 | FD_SETSIZE)) ) | 2312 | FD_SETSIZE)) ) |
2346 | { | 2313 | { |
2347 | #ifdef HAVE_MESSAGES | 2314 | #ifdef HAVE_MESSAGES |
2348 | MHD_DLOG (daemon, "Could not add listen socket to fdset"); | 2315 | MHD_DLOG (daemon, "Could not add listen socket to fdset"); |
@@ -2351,10 +2318,10 @@ MHD_select (struct MHD_Daemon *daemon, | |||
2351 | } | 2318 | } |
2352 | } | 2319 | } |
2353 | if ( (MHD_INVALID_PIPE_ != daemon->wpipe[0]) && | 2320 | if ( (MHD_INVALID_PIPE_ != daemon->wpipe[0]) && |
2354 | (MHD_YES != add_to_fd_set (daemon->wpipe[0], | 2321 | (!MHD_add_to_fd_set_ (daemon->wpipe[0], |
2355 | &rs, | 2322 | &rs, |
2356 | &maxsock, | 2323 | &maxsock, |
2357 | FD_SETSIZE)) ) | 2324 | FD_SETSIZE)) ) |
2358 | { | 2325 | { |
2359 | #if defined(MHD_WINSOCK_SOCKETS) | 2326 | #if defined(MHD_WINSOCK_SOCKETS) |
2360 | /* fdset limit reached, new connections | 2327 | /* fdset limit reached, new connections |
@@ -2363,10 +2330,10 @@ MHD_select (struct MHD_Daemon *daemon, | |||
2363 | if (MHD_INVALID_SOCKET != daemon->socket_fd) | 2330 | if (MHD_INVALID_SOCKET != daemon->socket_fd) |
2364 | { | 2331 | { |
2365 | FD_CLR (daemon->socket_fd, &rs); | 2332 | FD_CLR (daemon->socket_fd, &rs); |
2366 | if (MHD_YES != add_to_fd_set (daemon->wpipe[0], | 2333 | if (!MHD_add_to_fd_set_ (daemon->wpipe[0], |
2367 | &rs, | 2334 | &rs, |
2368 | &maxsock, | 2335 | &maxsock, |
2369 | FD_SETSIZE)) | 2336 | FD_SETSIZE)) |
2370 | { | 2337 | { |
2371 | #endif /* MHD_WINSOCK_SOCKETS */ | 2338 | #endif /* MHD_WINSOCK_SOCKETS */ |
2372 | #ifdef HAVE_MESSAGES | 2339 | #ifdef HAVE_MESSAGES |
diff --git a/src/microhttpd/mhd_sockets.c b/src/microhttpd/mhd_sockets.c index 643ed849..640c59e3 100644 --- a/src/microhttpd/mhd_sockets.c +++ b/src/microhttpd/mhd_sockets.c | |||
@@ -235,3 +235,32 @@ const char* MHD_W32_strerror_winsock_(int err) | |||
235 | 235 | ||
236 | 236 | ||
237 | #endif /* MHD_WINSOCK_SOCKETS */ | 237 | #endif /* MHD_WINSOCK_SOCKETS */ |
238 | |||
239 | |||
240 | /** | ||
241 | * Add @a fd to the @a set. If @a fd is | ||
242 | * greater than @a max_fd, set @a max_fd to @a fd. | ||
243 | * | ||
244 | * @param fd file descriptor to add to the @a set | ||
245 | * @param set set to modify | ||
246 | * @param max_fd maximum value to potentially update | ||
247 | * @param fd_setsize value of FD_SETSIZE | ||
248 | * @return non-zero if succeeded, zero otherwise | ||
249 | */ | ||
250 | int | ||
251 | MHD_add_to_fd_set_ (MHD_socket fd, | ||
252 | fd_set *set, | ||
253 | MHD_socket *max_fd, | ||
254 | unsigned int fd_setsize) | ||
255 | { | ||
256 | if (NULL == set || MHD_INVALID_SOCKET == fd) | ||
257 | return 0; | ||
258 | if (!MHD_SCKT_FD_FITS_FDSET_SETSIZE_(fd, set, fd_setsize)) | ||
259 | return 0; | ||
260 | MHD_SCKT_ADD_FD_TO_FDSET_SETSIZE_(fd, set, fd_setsize); | ||
261 | if ( (NULL != max_fd) && | ||
262 | ((fd > *max_fd) || (MHD_INVALID_SOCKET == *max_fd)) ) | ||
263 | *max_fd = fd; | ||
264 | |||
265 | return !0; | ||
266 | } | ||
diff --git a/src/microhttpd/mhd_sockets.h b/src/microhttpd/mhd_sockets.h index 13b90d2e..4d8444ca 100644 --- a/src/microhttpd/mhd_sockets.h +++ b/src/microhttpd/mhd_sockets.h | |||
@@ -569,4 +569,21 @@ | |||
569 | # define MHD_socket_set_error_to_ENOMEM() MHD_socket_set_error_(MHD_SCKT_ENOMEM_) | 569 | # define MHD_socket_set_error_to_ENOMEM() MHD_socket_set_error_(MHD_SCKT_ENOMEM_) |
570 | #endif | 570 | #endif |
571 | 571 | ||
572 | /* Socket functions */ | ||
573 | |||
574 | /** | ||
575 | * Add @a fd to the @a set. If @a fd is | ||
576 | * greater than @a max_fd, set @a max_fd to @a fd. | ||
577 | * | ||
578 | * @param fd file descriptor to add to the @a set | ||
579 | * @param set set to modify | ||
580 | * @param max_fd maximum value to potentially update | ||
581 | * @param fd_setsize value of FD_SETSIZE | ||
582 | * @return non-zero if succeeded, zero otherwise | ||
583 | */ | ||
584 | int | ||
585 | MHD_add_to_fd_set_ (MHD_socket fd, | ||
586 | fd_set *set, | ||
587 | MHD_socket *max_fd, | ||
588 | unsigned int fd_setsize); | ||
572 | #endif /* ! MHD_SOCKETS_H */ | 589 | #endif /* ! MHD_SOCKETS_H */ |