]> git.meshlink.io Git - catta/commitdiff
wrap lifetimes of CattaServer and CattaSimplePoll in WSAStartup/WSACleanup
authorSven M. Hallberg <pesco@khjk.org>
Wed, 3 Sep 2014 14:36:30 +0000 (16:36 +0200)
committerSven M. Hallberg <pesco@khjk.org>
Wed, 3 Sep 2014 14:43:03 +0000 (16:43 +0200)
src/compat/windows/wincompat.c
src/compat/windows/wincompat.h
src/internal.h
src/server.c
src/simple-watch.c

index 4433b5b54648be93ff1fec50c7baa2674a74d11f..4e3145e6e672c73bee5d40d9af5bdaf4c6f4f86b 100644 (file)
@@ -4,6 +4,8 @@
 #include <assert.h>
 #include <stdint.h>
 
+#include <catta/log.h>
+
 // helper: convert WSAGetLastError() to an errno constant
 static int wsa_errno(void)
 {
@@ -29,6 +31,21 @@ static int wsa_errno(void)
     }
 }
 
+void winsock_init(void)
+{
+    WSADATA wsa;
+    int error;
+
+    if((error = WSAStartup(MAKEWORD(2,2), &wsa)) != 0)
+        catta_log_error("WSAStartup() failed: %d", error);
+}
+
+void winsock_exit(void)
+{
+    if(WSACleanup() == SOCKET_ERROR)
+        catta_log_warn("WSACleanup() failed: %d", WSAGetLastError());
+}
+
 ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags)
 {
     LPFN_WSARECVMSG WSARecvMsg = NULL;
index c736f75f938dc03868c75accbad1354239e21d37..de3396c2a62bc08665c9212812a1305d51636710 100644 (file)
 #include <mswsock.h>
 
 
+// wrappers around WSAStartup/WSACleanup to avoid clutter
+void winsock_init(void);
+void winsock_exit(void);
+
+
 // Winsock doesn't have recvmsg/sendmsg but offers the same functionality
 // with WSARecvMsg/WSASendMsg, so we implement the former in terms of the
 // latter.
index b74ce25d3418e2c2a86189554a20b161e07c79a2..789f3634a209e1e9601e267c3082a1b45e4175d7 100644 (file)
@@ -50,6 +50,8 @@ typedef struct CattaEntry CattaEntry;
 
 #ifndef _WIN32
 #define closesocket close
+#define winsock_init()
+#define winsock_exit()
 #endif
 
 typedef struct CattaLegacyUnicastReflectSlot CattaLegacyUnicastReflectSlot;
index 1dda8b4545edfe6481557873588365b878fe00bb..7f84e5aaffd5f5a68f402bacc055528986586d4b 100644 (file)
@@ -1381,12 +1381,14 @@ CattaServer *catta_server_new(const CattaPoll *poll_api, const CattaServerConfig
     else
         catta_server_config_init(&s->config);
 
+    winsock_init();  // on Windows, call WSAStartup; no-op on other platforms
     if ((e = setup_sockets(s)) < 0) {
         if (error)
             *error = e;
 
         catta_server_config_free(&s->config);
         catta_free(s);
+        winsock_exit();
 
         return NULL;
     }
@@ -1533,6 +1535,7 @@ void catta_server_free(CattaServer* s) {
     catta_server_config_free(&s->config);
 
     catta_free(s);
+    winsock_exit();  // on Windows, call WSACleanup(); no-op on other platforms
 }
 
 const char* catta_server_get_domain_name(CattaServer *s) {
index 77da9f84e0e3a099e71ee0f56b27bc13cd475a27..7a0052d5e915b9eb0166f32061d04b2bf3818ada 100644 (file)
@@ -309,8 +309,10 @@ CattaSimplePoll *catta_simple_poll_new(void) {
     if (!(s = catta_new(CattaSimplePoll, 1)))
         return NULL;
 
+    winsock_init();  // on Windows, pipe uses sockets; no-op on other platforms
     if (pipe(s->wakeup_pipe) < 0) {
         catta_free(s);
+        winsock_exit();
         return NULL;
     }
 
@@ -368,6 +370,7 @@ void catta_simple_poll_free(CattaSimplePoll *s) {
         closesocket(s->wakeup_pipe[1]);
 
     catta_free(s);
+    winsock_exit();  // match the winsock_init in catta_simple_poll_new
 }
 
 static int rebuild(CattaSimplePoll *s) {