commit 90e0124b6caa6e5a126bd0bb1d491ebd7aa4e31c
parent a8948d201be986112a2a616b2ff30e58e8a8f08f
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date: Tue, 11 Oct 2016 15:20:44 +0000
Added socketpair creation in non-blocking mode to save system calls where supported
Diffstat:
2 files changed, 26 insertions(+), 14 deletions(-)
diff --git a/src/microhttpd/mhd_sockets.c b/src/microhttpd/mhd_sockets.c
@@ -241,10 +241,12 @@ const char* MHD_W32_strerror_winsock_(int err)
/**
* Create pair of mutually connected TCP/IP sockets on loopback address
* @param sockets_pair array to receive resulted sockets
+ * @param non_blk if set to non-zero value, sockets created in non-blocking mode
+ * otherwise sockets will be in blocking mode
* @return non-zero if succeeded, zero otherwise
*/
int
-MHD_W32_socket_pair_(SOCKET sockets_pair[2])
+MHD_W32_socket_pair_(SOCKET sockets_pair[2], int non_blk)
{
int i;
@@ -261,7 +263,8 @@ MHD_W32_socket_pair_(SOCKET sockets_pair[2])
SOCKET listen_s;
static const int c_addinlen = sizeof(struct sockaddr_in); /* help compiler to optimize */
int addr_len = c_addinlen;
- int opt = 1;
+ unsigned long on_val = 1;
+ unsigned long off_val = 0;
listen_s = socket (AF_INET,
SOCK_STREAM,
@@ -297,7 +300,7 @@ MHD_W32_socket_pair_(SOCKET sockets_pair[2])
if ( (0 != ioctlsocket (client_s,
FIONBIO,
- (u_long*) &opt)) ||
+ &on_val)) ||
( (0 != connect (client_s,
(struct sockaddr*) &listen_addr,
c_addinlen)) &&
@@ -322,23 +325,23 @@ MHD_W32_socket_pair_(SOCKET sockets_pair[2])
}
addr_len = c_addinlen;
- opt = 0;
if ( (0 == getsockname (client_s,
(struct sockaddr*) &client_addr,
&addr_len)) &&
(accepted_from_addr.sin_family == client_addr.sin_family) &&
(accepted_from_addr.sin_port == client_addr.sin_port) &&
(accepted_from_addr.sin_addr.s_addr == client_addr.sin_addr.s_addr) &&
- (0 == ioctlsocket(client_s,
- FIONBIO,
- (u_long*) &opt)) &&
- (0 == ioctlsocket(server_s,
- FIONBIO,
- (u_long*) &opt)) )
+ ( (0 != non_blk) ?
+ (0 == ioctlsocket(server_s,
+ FIONBIO,
+ &on_val)) :
+ (0 == ioctlsocket(client_s,
+ FIONBIO,
+ &off_val)) ) )
{
closesocket (listen_s);
- sockets_pair[0] = client_s;
- sockets_pair[1] = server_s;
+ sockets_pair[0] = server_s;
+ sockets_pair[1] = client_s;
return !0;
}
closesocket (server_s);
diff --git a/src/microhttpd/mhd_sockets.h b/src/microhttpd/mhd_sockets.h
@@ -594,17 +594,26 @@
#if defined(MHD_POSIX_SOCKETS) && defined(AF_LOCAL)
# define MHD_socket_pair_(fdarr) (!socketpair(AF_LOCAL, SOCK_STREAM, 0, (fdarr)))
+# if defined(HAVE_SOCK_NONBLOCK)
+# define MHD_socket_pair_nblk_(fdarr) (!socketpair(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK, 0, (fdarr)))
+# endif /* HAVE_SOCK_NONBLOCK*/
#elif defined(MHD_POSIX_SOCKETS) && defined(AF_UNIX)
# define MHD_socket_pair_(fdarr) (!socketpair(AF_UNIX, SOCK_STREAM, 0, (fdarr)))
+# if defined(HAVE_SOCK_NONBLOCK)
+# define MHD_socket_pair_nblk_(fdarr) (!socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0, (fdarr)))
+# endif /* HAVE_SOCK_NONBLOCK*/
#elif defined(MHD_WINSOCK_SOCKETS)
/**
* Create pair of mutually connected TCP/IP sockets on loopback address
* @param sockets_pair array to receive resulted sockets
+ * @param non_blk if set to non-zero value, sockets created in non-blocking mode
+ * otherwise sockets will be in blocking mode
* @return non-zero if succeeded, zero otherwise
*/
- int MHD_W32_socket_pair_(SOCKET sockets_pair[2]);
+ int MHD_W32_socket_pair_(SOCKET sockets_pair[2], int non_blk);
-# define MHD_socket_pair_(fdarr) MHD_W32_socket_pair_((fdarr))
+# define MHD_socket_pair_(fdarr) MHD_W32_socket_pair_((fdarr), 0)
+# define MHD_socket_pair_nblk_(fdarr) MHD_W32_socket_pair_((fdarr), 1)
#endif
/**