Skip to content

Commit fb69016

Browse files
authored
Merge pull request #3096 from cesanta/polldoze
allow other stack than WinSock in Windows
2 parents 169cbc0 + 08091d0 commit fb69016

File tree

3 files changed

+64
-18
lines changed

3 files changed

+64
-18
lines changed

mongoose.h

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,18 @@ static inline int mg_mkdir(const char *path, mode_t mode) {
424424

425425
#if MG_ARCH == MG_ARCH_WIN32
426426

427+
// Avoid name clashing; (macro expansion producing 'defined' has undefined
428+
// behaviour). See config.h for user options
429+
#ifndef MG_ENABLE_WINSOCK
430+
#if (!defined(MG_ENABLE_TCPIP) || !MG_ENABLE_TCPIP) && \
431+
(!defined(MG_ENABLE_LWIP) || !MG_ENABLE_LWIP) && \
432+
(!defined(MG_ENABLE_FREERTOS_TCP) || !MG_ENABLE_FREERTOS_TCP)
433+
#define MG_ENABLE_WINSOCK 1
434+
#else
435+
#define MG_ENABLE_WINSOCK 0
436+
#endif
437+
#endif
438+
427439
#ifndef _CRT_RAND_S
428440
#define _CRT_RAND_S
429441
#endif
@@ -469,17 +481,19 @@ typedef enum { false = 0, true = 1 } bool;
469481
#else
470482
#include <stdbool.h>
471483
#include <stdint.h>
484+
#if MG_ENABLE_WINSOCK
472485
#include <ws2tcpip.h>
473486
#endif
487+
#endif
474488

475489
#include <process.h>
476490
#include <winerror.h>
477-
#include <winsock2.h>
478491

479492
// For mg_random()
480493
#if defined(_MSC_VER) && _MSC_VER < 1700
481494
#ifndef _WIN32_WINNT
482495
#define _WIN32_WINNT 0x400 // Let vc98 pick up wincrypt.h
496+
#include <winsock2.h> // and fix missing macros
483497
#endif
484498
#include <wincrypt.h>
485499
#pragma comment(lib, "advapi32.lib")
@@ -495,25 +509,31 @@ typedef enum { false = 0, true = 1 } bool;
495509
#endif
496510
#endif
497511

498-
#define MG_INVALID_SOCKET INVALID_SOCKET
499-
#define MG_SOCKET_TYPE SOCKET
500512
typedef unsigned long nfds_t;
501513
#if defined(_MSC_VER)
514+
#if MG_ENABLE_WINSOCK
502515
#pragma comment(lib, "ws2_32.lib")
516+
#endif
503517
#ifndef alloca
504518
#define alloca(a) _alloca(a)
505519
#endif
506520
#endif
507-
#define poll(a, b, c) WSAPoll((a), (b), (c))
508-
#define closesocket(x) closesocket(x)
509521

510-
typedef int socklen_t;
511522
#define MG_DIRSEP '\\'
512523

513524
#ifndef MG_PATH_MAX
514525
#define MG_PATH_MAX FILENAME_MAX
515526
#endif
516527

528+
#if MG_ENABLE_WINSOCK
529+
#include <winsock2.h>
530+
531+
#define MG_INVALID_SOCKET INVALID_SOCKET
532+
#define MG_SOCKET_TYPE SOCKET
533+
#define poll(a, b, c) WSAPoll((a), (b), (c))
534+
#define closesocket(x) closesocket(x)
535+
typedef int socklen_t;
536+
517537
#ifndef SO_EXCLUSIVEADDRUSE
518538
#define SO_EXCLUSIVEADDRUSE ((int) (~SO_REUSEADDR))
519539
#endif
@@ -528,13 +548,15 @@ typedef int socklen_t;
528548
#define MG_SOCK_RESET(errcode) \
529549
(((errcode) < 0) && (WSAGetLastError() == WSAECONNRESET))
530550

551+
#endif // MG_ENABLE_WINSOCK
552+
531553
#define realpath(a, b) _fullpath((b), (a), MG_PATH_MAX)
532-
#define sleep(x) Sleep((x) * 1000)
554+
#define sleep(x) Sleep((x) *1000)
533555
#define mkdir(a, b) _mkdir(a)
534556
#define timegm(x) _mkgmtime(x)
535557

536558
#ifndef S_ISDIR
537-
#define S_ISDIR(x) (((x) & _S_IFMT) == _S_IFDIR)
559+
#define S_ISDIR(x) (((x) &_S_IFMT) == _S_IFDIR)
538560
#endif
539561

540562
#ifndef MG_ENABLE_DIRLIST
@@ -774,7 +796,8 @@ struct timeval {
774796
#define MG_ENABLE_MD5 1
775797
#endif
776798

777-
// Set MG_ENABLE_WINSOCK=0 for Win32 builds with external IP stack (like LWIP)
799+
// Set MG_ENABLE_WINSOCK=0 for Win32 builds with other external IP stack not
800+
// mentioned in arch_win32.h
778801
#ifndef MG_ENABLE_WINSOCK
779802
#define MG_ENABLE_WINSOCK 1
780803
#endif

src/arch_win32.h

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
#if MG_ARCH == MG_ARCH_WIN32
44

5+
// Avoid name clashing; (macro expansion producing 'defined' has undefined
6+
// behaviour). See config.h for user options
7+
#ifndef MG_ENABLE_WINSOCK
8+
#if (!defined(MG_ENABLE_TCPIP) || !MG_ENABLE_TCPIP) && \
9+
(!defined(MG_ENABLE_LWIP) || !MG_ENABLE_LWIP) && \
10+
(!defined(MG_ENABLE_FREERTOS_TCP) || !MG_ENABLE_FREERTOS_TCP)
11+
#define MG_ENABLE_WINSOCK 1
12+
#else
13+
#define MG_ENABLE_WINSOCK 0
14+
#endif
15+
#endif
16+
517
#ifndef _CRT_RAND_S
618
#define _CRT_RAND_S
719
#endif
@@ -47,17 +59,19 @@ typedef enum { false = 0, true = 1 } bool;
4759
#else
4860
#include <stdbool.h>
4961
#include <stdint.h>
62+
#if MG_ENABLE_WINSOCK
5063
#include <ws2tcpip.h>
5164
#endif
65+
#endif
5266

5367
#include <process.h>
5468
#include <winerror.h>
55-
#include <winsock2.h>
5669

5770
// For mg_random()
5871
#if defined(_MSC_VER) && _MSC_VER < 1700
5972
#ifndef _WIN32_WINNT
6073
#define _WIN32_WINNT 0x400 // Let vc98 pick up wincrypt.h
74+
#include <winsock2.h> // and fix missing macros
6175
#endif
6276
#include <wincrypt.h>
6377
#pragma comment(lib, "advapi32.lib")
@@ -73,25 +87,31 @@ typedef enum { false = 0, true = 1 } bool;
7387
#endif
7488
#endif
7589

76-
#define MG_INVALID_SOCKET INVALID_SOCKET
77-
#define MG_SOCKET_TYPE SOCKET
7890
typedef unsigned long nfds_t;
7991
#if defined(_MSC_VER)
92+
#if MG_ENABLE_WINSOCK
8093
#pragma comment(lib, "ws2_32.lib")
94+
#endif
8195
#ifndef alloca
8296
#define alloca(a) _alloca(a)
8397
#endif
8498
#endif
85-
#define poll(a, b, c) WSAPoll((a), (b), (c))
86-
#define closesocket(x) closesocket(x)
8799

88-
typedef int socklen_t;
89100
#define MG_DIRSEP '\\'
90101

91102
#ifndef MG_PATH_MAX
92103
#define MG_PATH_MAX FILENAME_MAX
93104
#endif
94105

106+
#if MG_ENABLE_WINSOCK
107+
#include <winsock2.h>
108+
109+
#define MG_INVALID_SOCKET INVALID_SOCKET
110+
#define MG_SOCKET_TYPE SOCKET
111+
#define poll(a, b, c) WSAPoll((a), (b), (c))
112+
#define closesocket(x) closesocket(x)
113+
typedef int socklen_t;
114+
95115
#ifndef SO_EXCLUSIVEADDRUSE
96116
#define SO_EXCLUSIVEADDRUSE ((int) (~SO_REUSEADDR))
97117
#endif
@@ -106,13 +126,15 @@ typedef int socklen_t;
106126
#define MG_SOCK_RESET(errcode) \
107127
(((errcode) < 0) && (WSAGetLastError() == WSAECONNRESET))
108128

129+
#endif // MG_ENABLE_WINSOCK
130+
109131
#define realpath(a, b) _fullpath((b), (a), MG_PATH_MAX)
110-
#define sleep(x) Sleep((x) * 1000)
132+
#define sleep(x) Sleep((x) *1000)
111133
#define mkdir(a, b) _mkdir(a)
112134
#define timegm(x) _mkgmtime(x)
113135

114136
#ifndef S_ISDIR
115-
#define S_ISDIR(x) (((x) & _S_IFMT) == _S_IFDIR)
137+
#define S_ISDIR(x) (((x) &_S_IFMT) == _S_IFDIR)
116138
#endif
117139

118140
#ifndef MG_ENABLE_DIRLIST

src/config.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
#define MG_ENABLE_MD5 1
5757
#endif
5858

59-
// Set MG_ENABLE_WINSOCK=0 for Win32 builds with external IP stack (like LWIP)
59+
// Set MG_ENABLE_WINSOCK=0 for Win32 builds with other external IP stack not
60+
// mentioned in arch_win32.h
6061
#ifndef MG_ENABLE_WINSOCK
6162
#define MG_ENABLE_WINSOCK 1
6263
#endif

0 commit comments

Comments
 (0)