Better reuseaddr handling on Win32

This commit is contained in:
cpq 2021-01-13 03:08:11 +00:00
parent 83c106a857
commit 6c93446a1f
3 changed files with 9 additions and 1 deletions

View File

@ -2458,6 +2458,9 @@ struct mg_connection *mg_sntp_connect(struct mg_mgr *mgr, const char *url,
#if MG_ENABLE_SOCKET
#if defined(_WIN32)
#ifndef SO_EXCLUSIVEADDRUSE
#define SO_EXCLUSIVEADDRUSE ((int) (~SO_REUSEADDR))
#endif
#define MG_SOCK_ERRNO WSAGetLastError()
#define FD(C_) ((SOCKET)(C_)->fd)
#elif MG_ARCH == MG_ARCH_FREERTOS
@ -2654,6 +2657,7 @@ SOCKET mg_open_listener(const char *url) {
// "Using SO_REUSEADDR and SO_EXCLUSIVEADDRUSE"
!setsockopt(fd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (char *) &on,
sizeof(on)) &&
!setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char *) &on, sizeof(on)) &&
#endif
bind(fd, &usa.sa, slen) == 0 &&
// NOTE(lsm): FreeRTOS uses backlog value as a connection limit

View File

@ -16,7 +16,7 @@
// license, as set out in <https://www.cesanta.com/license>.
#pragma once
#define MG_VERSION "7.0"
#define MG_VERSION "7.1"
#define MG_ARCH_CUSTOM 0

View File

@ -11,6 +11,9 @@
#if MG_ENABLE_SOCKET
#if defined(_WIN32)
#ifndef SO_EXCLUSIVEADDRUSE
#define SO_EXCLUSIVEADDRUSE ((int) (~SO_REUSEADDR))
#endif
#define MG_SOCK_ERRNO WSAGetLastError()
#define FD(C_) ((SOCKET)(C_)->fd)
#elif MG_ARCH == MG_ARCH_FREERTOS
@ -207,6 +210,7 @@ SOCKET mg_open_listener(const char *url) {
// "Using SO_REUSEADDR and SO_EXCLUSIVEADDRUSE"
!setsockopt(fd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (char *) &on,
sizeof(on)) &&
!setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char *) &on, sizeof(on)) &&
#endif
bind(fd, &usa.sa, slen) == 0 &&
// NOTE(lsm): FreeRTOS uses backlog value as a connection limit