changeset 91:acfc4b4bc180

Create network initialization functions in libnnchat and move Win32/WinSock code there.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 07 May 2009 00:04:37 +0300
parents 1e0bf7b4fd41
children 638f88374e3e
files libnnchat.c libnnchat.h nnchat.c
diffstat 3 files changed, 30 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/libnnchat.c	Wed May 06 23:55:21 2009 +0300
+++ b/libnnchat.c	Thu May 07 00:04:37 2009 +0300
@@ -92,6 +92,29 @@
 }
 
 
+BOOL initNetwork(void)
+{
+#ifdef __WIN32
+    /* Initialize WinSock, if needed */
+    WSADATA wsaData;
+    int err = WSAStartup(0x0101, &wsaData);
+    if (err != 0) {
+        THERR("Could not initialize WinSock library (err=%d).\n", err);
+        return FALSE;
+    }
+#endif
+    return TRUE;
+}
+
+
+void closeNetwork(void)
+{
+#ifdef __WIN32
+    WSACleanup();
+#endif
+}
+
+
 BOOL sendToSocket(const int sock, char *buf, const size_t bufLen)
 {
     size_t bufLeft = bufLen;
--- a/libnnchat.h	Wed May 06 23:55:21 2009 +0300
+++ b/libnnchat.h	Thu May 07 00:04:37 2009 +0300
@@ -45,6 +45,8 @@
 #endif
 int         getSocketErrno(void);
 const char *getSocketErrStr(int err);
+BOOL        initNetwork(void);
+void        closeNetwork(void);
 
 int         openConnection(struct in_addr *addr, const int port);
 void        closeConnection(const int sock);
--- a/nnchat.c	Wed May 06 23:55:21 2009 +0300
+++ b/nnchat.c	Thu May 07 00:04:37 2009 +0300
@@ -563,9 +563,7 @@
         exitProg = FALSE,
         colorSet = FALSE,
         cursesInit = FALSE,
-#if __WIN32
         networkInit = FALSE,
-#endif
         insertMode = TRUE;
     struct timeval tv;
     fd_set sockfds;
@@ -606,18 +604,11 @@
         }
     }
     
-#ifdef __WIN32
-    {
-        /* Initialize WinSock, if needed */
-        WSADATA wsaData;
-        int err = WSAStartup(0x0101, &wsaData);
-        if (err != 0) {
-            THERR("Could not initialize WinSock DLL: %d\n", err);
-            goto err_exit;
-        }
+    if (!initNetwork()) {
+        THERR("Could not initialize network subsystem.\n");
+        goto err_exit;
+    } else
         networkInit = TRUE;
-    }
-#endif
 
     /* Okay ... */
     THMSG(1, "Trying to resolve host '%s' ...\n", optServer);
@@ -981,10 +972,8 @@
 
     closeConnection(tmpSocket);
     
-#ifdef __WIN32
     if (networkInit)
-        WSACleanup();
-#endif
+        closeNetwork();
 
     THMSG(1, "Connection terminated.\n");