Quellcode durchsuchen

WSAStartup => WSAInit

ithewei vor 4 Jahren
Ursprung
Commit
8a84c3d7d3
8 geänderte Dateien mit 33 neuen und 36 gelöschten Zeilen
  1. 23 11
      base/hsocket.c
  2. 3 0
      base/hsocket.h
  3. 3 3
      cpputil/RAII.cpp
  4. 1 6
      event/hloop.c
  5. 3 0
      protocol/dns.c
  6. 0 6
      unittest/nslookup_test.c
  7. 0 5
      unittest/ping_test.c
  8. 0 5
      unittest/socketpair_test.c

+ 23 - 11
base/hsocket.c

@@ -3,7 +3,21 @@
 #include "hdef.h"
 
 #ifdef OS_WIN
-static int s_wsa_initialized = 0;
+#include "hatomic.h"
+static hatomic_flag s_wsa_initialized = HATOMIC_FLAG_INIT;
+void WSAInit() {
+    if (!hatomic_flag_test_and_set(&s_wsa_initialized)) {
+        WSADATA wsadata;
+        WSAStartup(MAKEWORD(2, 2), &wsadata);
+    }
+}
+
+void WSADeinit() {
+    if (hatomic_flag_test_and_set(&s_wsa_initialized)) {
+        hatomic_flag_clear(&s_wsa_initialized);
+        WSACleanup();
+    }
+}
 #endif
 
 static inline int socket_errno_negative() {
@@ -37,6 +51,9 @@ bool is_ipv6(const char* host) {
 }
 
 int ResolveAddr(const char* host, sockaddr_u* addr) {
+#ifdef OS_WIN
+    WSAInit();
+#endif
     if (inet_pton(AF_INET, host, &addr->sin.sin_addr) == 1) {
         addr->sa.sa_family = AF_INET; // host is ipv4, so easy ;)
         return 0;
@@ -267,11 +284,7 @@ error:
 
 int Bind(int port, const char* host, int type) {
 #ifdef OS_WIN
-    if (s_wsa_initialized == 0) {
-        s_wsa_initialized = 1;
-        WSADATA wsadata;
-        WSAStartup(MAKEWORD(2,2), &wsadata);
-    }
+    WSAInit();
 #endif
     sockaddr_u localaddr;
     memset(&localaddr, 0, sizeof(localaddr));
@@ -290,11 +303,7 @@ int Listen(int port, const char* host) {
 
 int Connect(const char* host, int port, int nonblock) {
 #ifdef OS_WIN
-    if (s_wsa_initialized == 0) {
-        s_wsa_initialized = 1;
-        WSADATA wsadata;
-        WSAStartup(MAKEWORD(2,2), &wsadata);
-    }
+    WSAInit();
 #endif
     sockaddr_u peeraddr;
     memset(&peeraddr, 0, sizeof(peeraddr));
@@ -356,6 +365,9 @@ int Socketpair(int family, int type, int protocol, int sv[2]) {
     if (family != AF_INET || type != SOCK_STREAM) {
         return -1;
     }
+#ifdef OS_WIN
+    WSAInit();
+#endif
     int listenfd, connfd, acceptfd;
     listenfd = connfd = acceptfd = INVALID_SOCKET;
     struct sockaddr_in localaddr;

+ 3 - 0
base/hsocket.h

@@ -30,6 +30,9 @@ HV_EXPORT const char* socket_strerror(int err);
 
 typedef int socklen_t;
 
+void WSAInit();
+void WSADeinit();
+
 HV_INLINE int blocking(int sockfd) {
     unsigned long nb = 0;
     return ioctlsocket(sockfd, FIONBIO, &nb);

+ 3 - 3
cpputil/RAII.cpp

@@ -28,17 +28,17 @@ static LONG UnhandledException(EXCEPTION_POINTERS *pException) {
 }
 #endif
 
+#include "hsocket.h"
 class WsaRAII {
 public:
     WsaRAII() {
-        WSADATA wsadata;
-        WSAStartup(MAKEWORD(2,2), &wsadata);
+        WSAInit();
 #ifdef ENABLE_WINDUMP
         SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)UnhandledException);
 #endif
     }
     ~WsaRAII() {
-        WSACleanup();
+        WSADeinit();
     }
 };
 static WsaRAII s_wsa;

+ 1 - 6
event/hloop.c

@@ -255,12 +255,7 @@ unlock:
 
 static void hloop_init(hloop_t* loop) {
 #ifdef OS_WIN
-    static int s_wsa_initialized = 0;
-    if (s_wsa_initialized == 0) {
-        s_wsa_initialized = 1;
-        WSADATA wsadata;
-        WSAStartup(MAKEWORD(2,2), &wsadata);
-    }
+    WSAInit();
 #endif
 #ifdef SIGPIPE
     // NOTE: if not ignore SIGPIPE, write twice when peer close will lead to exit process by SIGPIPE.

+ 3 - 0
protocol/dns.c

@@ -235,6 +235,9 @@ int dns_query(dns_t* query, dns_t* response, const char* nameserver) {
     if (buflen < 0) {
         return buflen;
     }
+#ifdef OS_WIN
+    WSAInit();
+#endif
     int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
     if (sockfd < 0) {
         perror("socket");

+ 0 - 6
unittest/nslookup_test.c

@@ -11,12 +11,6 @@ int main(int argc, char* argv[]) {
 
     const char* domain = argv[1];
     const char* nameserver = "127.0.1.1";
-
-#ifdef OS_WIN
-    WSADATA wsadata;
-    WSAStartup(MAKEWORD(2,2), &wsadata);
-#endif
-
 #ifndef OS_LINUX
     nameserver = "114.114.114.114";
     // nameserver = "8.8.8.8";

+ 0 - 5
unittest/ping_test.c

@@ -8,11 +8,6 @@ int main(int argc, char* argv[]) {
         return -10;
     }
 
-#ifdef OS_WIN
-    WSADATA wsadata;
-    WSAStartup(MAKEWORD(2,2), &wsadata);
-#endif
-
     char* host = argv[1];
     int ping_cnt = 4;
     int ok_cnt = ping(host, ping_cnt);

+ 0 - 5
unittest/socketpair_test.c

@@ -3,11 +3,6 @@
 #include "hsocket.h"
 
 int main(int argc, char* argv[]) {
-#ifdef OS_WIN
-    WSADATA wsadata;
-    WSAStartup(MAKEWORD(2,2), &wsadata);
-#endif
-
     int sockfds[2];
     if (Socketpair(AF_INET, SOCK_STREAM, 0, sockfds) != 0) {
         printf("socketpair failed!\n");