diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2016-10-11 15:20:44 +0000 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2016-10-11 15:20:44 +0000 |
commit | 90e0124b6caa6e5a126bd0bb1d491ebd7aa4e31c (patch) | |
tree | 89efefcba38942c6352bc22f730c4eaf6b711bea | |
parent | a8948d201be986112a2a616b2ff30e58e8a8f08f (diff) | |
download | libmicrohttpd-90e0124b6caa6e5a126bd0bb1d491ebd7aa4e31c.tar.gz libmicrohttpd-90e0124b6caa6e5a126bd0bb1d491ebd7aa4e31c.zip |
Added socketpair creation in non-blocking mode to save system calls where supported
-rw-r--r-- | src/microhttpd/mhd_sockets.c | 27 | ||||
-rw-r--r-- | src/microhttpd/mhd_sockets.h | 13 |
2 files changed, 26 insertions, 14 deletions
diff --git a/src/microhttpd/mhd_sockets.c b/src/microhttpd/mhd_sockets.c index aca08e08..3bd48f58 100644 --- a/src/microhttpd/mhd_sockets.c +++ b/src/microhttpd/mhd_sockets.c | |||
@@ -241,10 +241,12 @@ const char* MHD_W32_strerror_winsock_(int err) | |||
241 | /** | 241 | /** |
242 | * Create pair of mutually connected TCP/IP sockets on loopback address | 242 | * Create pair of mutually connected TCP/IP sockets on loopback address |
243 | * @param sockets_pair array to receive resulted sockets | 243 | * @param sockets_pair array to receive resulted sockets |
244 | * @param non_blk if set to non-zero value, sockets created in non-blocking mode | ||
245 | * otherwise sockets will be in blocking mode | ||
244 | * @return non-zero if succeeded, zero otherwise | 246 | * @return non-zero if succeeded, zero otherwise |
245 | */ | 247 | */ |
246 | int | 248 | int |
247 | MHD_W32_socket_pair_(SOCKET sockets_pair[2]) | 249 | MHD_W32_socket_pair_(SOCKET sockets_pair[2], int non_blk) |
248 | { | 250 | { |
249 | int i; | 251 | int i; |
250 | 252 | ||
@@ -261,7 +263,8 @@ MHD_W32_socket_pair_(SOCKET sockets_pair[2]) | |||
261 | SOCKET listen_s; | 263 | SOCKET listen_s; |
262 | static const int c_addinlen = sizeof(struct sockaddr_in); /* help compiler to optimize */ | 264 | static const int c_addinlen = sizeof(struct sockaddr_in); /* help compiler to optimize */ |
263 | int addr_len = c_addinlen; | 265 | int addr_len = c_addinlen; |
264 | int opt = 1; | 266 | unsigned long on_val = 1; |
267 | unsigned long off_val = 0; | ||
265 | 268 | ||
266 | listen_s = socket (AF_INET, | 269 | listen_s = socket (AF_INET, |
267 | SOCK_STREAM, | 270 | SOCK_STREAM, |
@@ -297,7 +300,7 @@ MHD_W32_socket_pair_(SOCKET sockets_pair[2]) | |||
297 | 300 | ||
298 | if ( (0 != ioctlsocket (client_s, | 301 | if ( (0 != ioctlsocket (client_s, |
299 | FIONBIO, | 302 | FIONBIO, |
300 | (u_long*) &opt)) || | 303 | &on_val)) || |
301 | ( (0 != connect (client_s, | 304 | ( (0 != connect (client_s, |
302 | (struct sockaddr*) &listen_addr, | 305 | (struct sockaddr*) &listen_addr, |
303 | c_addinlen)) && | 306 | c_addinlen)) && |
@@ -322,23 +325,23 @@ MHD_W32_socket_pair_(SOCKET sockets_pair[2]) | |||
322 | } | 325 | } |
323 | 326 | ||
324 | addr_len = c_addinlen; | 327 | addr_len = c_addinlen; |
325 | opt = 0; | ||
326 | if ( (0 == getsockname (client_s, | 328 | if ( (0 == getsockname (client_s, |
327 | (struct sockaddr*) &client_addr, | 329 | (struct sockaddr*) &client_addr, |
328 | &addr_len)) && | 330 | &addr_len)) && |
329 | (accepted_from_addr.sin_family == client_addr.sin_family) && | 331 | (accepted_from_addr.sin_family == client_addr.sin_family) && |
330 | (accepted_from_addr.sin_port == client_addr.sin_port) && | 332 | (accepted_from_addr.sin_port == client_addr.sin_port) && |
331 | (accepted_from_addr.sin_addr.s_addr == client_addr.sin_addr.s_addr) && | 333 | (accepted_from_addr.sin_addr.s_addr == client_addr.sin_addr.s_addr) && |
332 | (0 == ioctlsocket(client_s, | 334 | ( (0 != non_blk) ? |
333 | FIONBIO, | 335 | (0 == ioctlsocket(server_s, |
334 | (u_long*) &opt)) && | 336 | FIONBIO, |
335 | (0 == ioctlsocket(server_s, | 337 | &on_val)) : |
336 | FIONBIO, | 338 | (0 == ioctlsocket(client_s, |
337 | (u_long*) &opt)) ) | 339 | FIONBIO, |
340 | &off_val)) ) ) | ||
338 | { | 341 | { |
339 | closesocket (listen_s); | 342 | closesocket (listen_s); |
340 | sockets_pair[0] = client_s; | 343 | sockets_pair[0] = server_s; |
341 | sockets_pair[1] = server_s; | 344 | sockets_pair[1] = client_s; |
342 | return !0; | 345 | return !0; |
343 | } | 346 | } |
344 | closesocket (server_s); | 347 | closesocket (server_s); |
diff --git a/src/microhttpd/mhd_sockets.h b/src/microhttpd/mhd_sockets.h index 236e93f2..f33d85aa 100644 --- a/src/microhttpd/mhd_sockets.h +++ b/src/microhttpd/mhd_sockets.h | |||
@@ -594,17 +594,26 @@ | |||
594 | 594 | ||
595 | #if defined(MHD_POSIX_SOCKETS) && defined(AF_LOCAL) | 595 | #if defined(MHD_POSIX_SOCKETS) && defined(AF_LOCAL) |
596 | # define MHD_socket_pair_(fdarr) (!socketpair(AF_LOCAL, SOCK_STREAM, 0, (fdarr))) | 596 | # define MHD_socket_pair_(fdarr) (!socketpair(AF_LOCAL, SOCK_STREAM, 0, (fdarr))) |
597 | # if defined(HAVE_SOCK_NONBLOCK) | ||
598 | # define MHD_socket_pair_nblk_(fdarr) (!socketpair(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK, 0, (fdarr))) | ||
599 | # endif /* HAVE_SOCK_NONBLOCK*/ | ||
597 | #elif defined(MHD_POSIX_SOCKETS) && defined(AF_UNIX) | 600 | #elif defined(MHD_POSIX_SOCKETS) && defined(AF_UNIX) |
598 | # define MHD_socket_pair_(fdarr) (!socketpair(AF_UNIX, SOCK_STREAM, 0, (fdarr))) | 601 | # define MHD_socket_pair_(fdarr) (!socketpair(AF_UNIX, SOCK_STREAM, 0, (fdarr))) |
602 | # if defined(HAVE_SOCK_NONBLOCK) | ||
603 | # define MHD_socket_pair_nblk_(fdarr) (!socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0, (fdarr))) | ||
604 | # endif /* HAVE_SOCK_NONBLOCK*/ | ||
599 | #elif defined(MHD_WINSOCK_SOCKETS) | 605 | #elif defined(MHD_WINSOCK_SOCKETS) |
600 | /** | 606 | /** |
601 | * Create pair of mutually connected TCP/IP sockets on loopback address | 607 | * Create pair of mutually connected TCP/IP sockets on loopback address |
602 | * @param sockets_pair array to receive resulted sockets | 608 | * @param sockets_pair array to receive resulted sockets |
609 | * @param non_blk if set to non-zero value, sockets created in non-blocking mode | ||
610 | * otherwise sockets will be in blocking mode | ||
603 | * @return non-zero if succeeded, zero otherwise | 611 | * @return non-zero if succeeded, zero otherwise |
604 | */ | 612 | */ |
605 | int MHD_W32_socket_pair_(SOCKET sockets_pair[2]); | 613 | int MHD_W32_socket_pair_(SOCKET sockets_pair[2], int non_blk); |
606 | 614 | ||
607 | # define MHD_socket_pair_(fdarr) MHD_W32_socket_pair_((fdarr)) | 615 | # define MHD_socket_pair_(fdarr) MHD_W32_socket_pair_((fdarr), 0) |
616 | # define MHD_socket_pair_nblk_(fdarr) MHD_W32_socket_pair_((fdarr), 1) | ||
608 | #endif | 617 | #endif |
609 | 618 | ||
610 | /** | 619 | /** |