Use socketpair syscall when available

This commit is contained in:
cpq 2020-12-07 10:25:28 +00:00
parent 7ea3293aba
commit d1d8a77bf6
2 changed files with 16 additions and 0 deletions

View File

@ -2593,6 +2593,13 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) {
#if MG_ENABLE_SOCKETPAIR
bool mg_socketpair(int *s1, int *s2) {
#if MG_ARCH == MG_ARCH_UNIX
int sp[2], ret = 0;
if (socketpair(AF_INET, SOCK_DGRAM, 0) == 0) {
*s1 = sp[0], *s2 = sp[1], ret = 1;
}
return ret;
#else
union usa sa, sa2;
SOCKET sp[2] = {INVALID_SOCKET, INVALID_SOCKET};
socklen_t len = sizeof(sa.sin);
@ -2619,6 +2626,7 @@ bool mg_socketpair(int *s1, int *s2) {
}
return ret;
#endif
}
#endif

View File

@ -356,6 +356,13 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) {
#if MG_ENABLE_SOCKETPAIR
bool mg_socketpair(int *s1, int *s2) {
#if MG_ARCH == MG_ARCH_UNIX
int sp[2], ret = 0;
if (socketpair(AF_INET, SOCK_DGRAM, 0) == 0) {
*s1 = sp[0], *s2 = sp[1], ret = 1;
}
return ret;
#else
union usa sa, sa2;
SOCKET sp[2] = {INVALID_SOCKET, INVALID_SOCKET};
socklen_t len = sizeof(sa.sin);
@ -382,6 +389,7 @@ bool mg_socketpair(int *s1, int *s2) {
}
return ret;
#endif
}
#endif