# HG changeset patch # User Matti Hamalainen # Date 1241643877 -10800 # Node ID acfc4b4bc18088d1e89b763008ba496aaa9eb5f1 # Parent 1e0bf7b4fd4113798af7f3936d66bcd5cc9de72d Create network initialization functions in libnnchat and move Win32/WinSock code there. diff -r 1e0bf7b4fd41 -r acfc4b4bc180 libnnchat.c --- 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; diff -r 1e0bf7b4fd41 -r acfc4b4bc180 libnnchat.h --- 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); diff -r 1e0bf7b4fd41 -r acfc4b4bc180 nnchat.c --- 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");