From 86f43cd8d60f1b64bee987432bf037edc925988e Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Fri, 6 Aug 2021 10:23:08 +0100 Subject: [PATCH] Refactor mg_socketpair and document MG_ENABLE_NATIVE_SOCKETPAIR --- docs/README.md | 1 + mongoose.c | 13 +++++++------ mongoose.h | 4 ++++ src/config.h | 4 ++++ src/sock.c | 13 +++++++------ 5 files changed, 23 insertions(+), 12 deletions(-) diff --git a/docs/README.md b/docs/README.md index f6dc17b6..c708c5b7 100644 --- a/docs/README.md +++ b/docs/README.md @@ -241,6 +241,7 @@ Here is a list of build constants and their default values: |MG_ENABLE_LOG | 1 | Enable `LOG()` macro | |MG_ENABLE_MD5 | 0 | Use native MD5 implementation | |MG_ENABLE_SOCKETPAIR | 0 | Enable `mg_socketpair()` for multi-threading | +|MG_ENABLE_NATIVE_SOCKETPAIR | 0 | Use native `socketpair()` syscall for `mg_socketpair()`| |MG_ENABLE_SSI | 1 | Enable serving SSI files by `mg_http_serve_dir()` | |MG_ENABLE_DIRLIST | 0 | Enable directory listing | |MG_ENABLE_CUSTOM_RANDOM | 0 | Provide custom RNG function `mg_random()` | diff --git a/mongoose.c b/mongoose.c index 65cf7076..84deb1ad 100644 --- a/mongoose.c +++ b/mongoose.c @@ -2855,8 +2855,8 @@ typedef Socket_t SOCKET; typedef int SOCKET; #endif -#define FD(c_) ((SOCKET)(size_t)(c_)->fd) -#define S2PTR(s_) ((void *) (size_t)(s_)) +#define FD(c_) ((SOCKET) (size_t) (c_)->fd) +#define S2PTR(s_) ((void *) (size_t) (s_)) #ifndef MSG_NONBLOCKING #define MSG_NONBLOCKING 0 @@ -3231,9 +3231,8 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) { } } -#if MG_ENABLE_SOCKETPAIR bool mg_socketpair(int *s1, int *s2) { -#ifdef MG_ENABLE_NATIVE_SOCKETPAIR +#if MG_ENABLE_NATIVE_SOCKETPAIR // For some reason, native socketpair() call fails on Macos // Enable this codepath only when MG_ENABLE_NATIVE_SOCKETPAIR is defined int sp[2], ret = 0; @@ -3242,7 +3241,7 @@ bool mg_socketpair(int *s1, int *s2) { } LOG(LL_INFO, ("errno %d", errno)); return ret; -#else +#elif MG_ENABLE_SOCKETPAIR union usa sa, sa2; SOCKET sp[2] = {INVALID_SOCKET, INVALID_SOCKET}; socklen_t len = sizeof(sa.sin); @@ -3269,9 +3268,11 @@ bool mg_socketpair(int *s1, int *s2) { } return ret; +#else + *s1 = *s2 = INVALID_SOCKET; + return false; #endif } -#endif struct mg_connection *mg_listen(struct mg_mgr *mgr, const char *url, mg_event_handler_t fn, void *fn_data) { diff --git a/mongoose.h b/mongoose.h index 0fe9e93a..e25c10b5 100644 --- a/mongoose.h +++ b/mongoose.h @@ -66,6 +66,10 @@ extern "C" { #define MG_ENABLE_SOCKETPAIR 0 #endif +#ifndef MG_ENABLE_NATIVE_SOCKETPAIR +#define MG_ENABLE_NATIVE_SOCKETPAIR 0 +#endif + #ifndef MG_ENABLE_CUSTOM_RANDOM #define MG_ENABLE_CUSTOM_RANDOM 0 #endif diff --git a/src/config.h b/src/config.h index d1f52ed4..32f06f82 100644 --- a/src/config.h +++ b/src/config.h @@ -41,6 +41,10 @@ #define MG_ENABLE_SOCKETPAIR 0 #endif +#ifndef MG_ENABLE_NATIVE_SOCKETPAIR +#define MG_ENABLE_NATIVE_SOCKETPAIR 0 +#endif + #ifndef MG_ENABLE_CUSTOM_RANDOM #define MG_ENABLE_CUSTOM_RANDOM 0 #endif diff --git a/src/sock.c b/src/sock.c index 4fda912d..678e3a2c 100644 --- a/src/sock.c +++ b/src/sock.c @@ -28,8 +28,8 @@ typedef Socket_t SOCKET; typedef int SOCKET; #endif -#define FD(c_) ((SOCKET)(size_t)(c_)->fd) -#define S2PTR(s_) ((void *) (size_t)(s_)) +#define FD(c_) ((SOCKET) (size_t) (c_)->fd) +#define S2PTR(s_) ((void *) (size_t) (s_)) #ifndef MSG_NONBLOCKING #define MSG_NONBLOCKING 0 @@ -404,9 +404,8 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) { } } -#if MG_ENABLE_SOCKETPAIR bool mg_socketpair(int *s1, int *s2) { -#ifdef MG_ENABLE_NATIVE_SOCKETPAIR +#if MG_ENABLE_NATIVE_SOCKETPAIR // For some reason, native socketpair() call fails on Macos // Enable this codepath only when MG_ENABLE_NATIVE_SOCKETPAIR is defined int sp[2], ret = 0; @@ -415,7 +414,7 @@ bool mg_socketpair(int *s1, int *s2) { } LOG(LL_INFO, ("errno %d", errno)); return ret; -#else +#elif MG_ENABLE_SOCKETPAIR union usa sa, sa2; SOCKET sp[2] = {INVALID_SOCKET, INVALID_SOCKET}; socklen_t len = sizeof(sa.sin); @@ -442,9 +441,11 @@ bool mg_socketpair(int *s1, int *s2) { } return ret; +#else + *s1 = *s2 = INVALID_SOCKET; + return false; #endif } -#endif struct mg_connection *mg_listen(struct mg_mgr *mgr, const char *url, mg_event_handler_t fn, void *fn_data) {