Refactor mg_socketpair and document MG_ENABLE_NATIVE_SOCKETPAIR

This commit is contained in:
Sergey Lyubka 2021-08-06 10:23:08 +01:00
parent 2407509ce0
commit 86f43cd8d6
5 changed files with 23 additions and 12 deletions

View File

@ -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()` |

View File

@ -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) {

View File

@ -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

View File

@ -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

View File

@ -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) {