IOT-2126 IOT-2349 CA: Windows: shutdown improvements 35/20335/4
authorDan Mihai <Daniel.Mihai@microsoft.com>
Tue, 23 May 2017 16:52:21 +0000 (09:52 -0700)
committerDan Mihai <Daniel.Mihai@microsoft.com>
Wed, 7 Jun 2017 17:35:12 +0000 (17:35 +0000)
Continuing to use WSA after shutting it down might have unpredictable
side effects.

Change-Id: I738097cd83baacc24c673eb751fb363a05b67ebd
Signed-off-by: Dan Mihai <Daniel.Mihai@microsoft.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/20335
Reviewed-by: Way Vadhanasin <wayvad@microsoft.com>
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
resource/csdk/connectivity/src/ip_adapter/caipadapter.c
resource/csdk/connectivity/src/ip_adapter/caipserver.c
resource/csdk/connectivity/src/tcp_adapter/catcpadapter.c
resource/csdk/connectivity/src/tcp_adapter/catcpserver.c

index 3e0b4dd..9ab4a3e 100644 (file)
@@ -37,6 +37,7 @@
 #include "logger.h"
 #include "oic_malloc.h"
 #include "oic_string.h"
+#include "iotivity_debug.h"
 
 /**
  * Logging tag for module name.
@@ -331,6 +332,19 @@ CAResult_t CAInitializeIP(CARegisterConnectivityCallback registerCallback,
     VERIFY_NON_NULL(handle, TAG, "thread pool handle");
 #endif
 
+#ifdef WSA_WAIT_EVENT_0
+    // Windows-specific initialization.
+    WORD wVersionRequested = MAKEWORD(2, 2);
+    WSADATA wsaData = {.wVersion = 0};
+    int err = WSAStartup(wVersionRequested, &wsaData);
+    if (0 != err)
+    {
+        OIC_LOG_V(ERROR, TAG, "%s: WSAStartup failed: %i", __func__, err);
+        return CA_STATUS_FAILED;
+    }
+    OIC_LOG(DEBUG, TAG, "WSAStartup Succeeded");
+#endif
+
     g_networkChangeCallback = netCallback;
     g_networkPacketCallback = networkPacketCallback;
     g_errorCallback = errorCallback;
@@ -534,6 +548,11 @@ void CATerminateIP()
     CADeInitializeIPGlobals();
     CAIPDeinitializeQueueHandles();
 #endif
+
+#ifdef WSA_WAIT_EVENT_0
+    // Windows-specific clean-up.
+    OC_VERIFY(0 == WSACleanup());
+#endif
 }
 
 #ifndef SINGLE_THREAD
index fba7876..d5d8f2e 100644 (file)
@@ -472,7 +472,6 @@ static void CAFindReadyMessage()
     if (caglobals.ip.terminate)
     {
         caglobals.ip.shutdownEvent = WSA_INVALID_EVENT;
-        WSACleanup();
     }
 }
 
@@ -920,17 +919,6 @@ CAResult_t CAIPStartServer(const ca_thread_pool_t threadPool)
     {
         return res;
     }
-#if defined (_WIN32)
-    WORD wVersionRequested = MAKEWORD(2, 2);
-    WSADATA wsaData ={.wVersion = 0};
-    int err = WSAStartup(wVersionRequested, &wsaData);
-    if (err != 0)
-    {
-        OIC_LOG_V(ERROR, TAG, "WSAStartup failed: %i", err);
-        return CA_STATUS_FAILED;
-    }
-    OIC_LOG(DEBUG, TAG, "WSAStartup Succeeded");
-#endif
     if (!IPv4MulticastAddress.s_addr)
     {
         (void)inet_pton(AF_INET, IPv4_MULTICAST, &IPv4MulticastAddress);
@@ -980,7 +968,7 @@ CAResult_t CAIPStartServer(const ca_thread_pool_t threadPool)
     caglobals.ip.wsaRecvMsg = NULL;
     GUID GuidWSARecvMsg = WSAID_WSARECVMSG;
     DWORD copied = 0;
-    err = WSAIoctl(caglobals.ip.u4.fd, SIO_GET_EXTENSION_FUNCTION_POINTER, &GuidWSARecvMsg, sizeof(GuidWSARecvMsg), &(caglobals.ip.wsaRecvMsg), sizeof(caglobals.ip.wsaRecvMsg), &copied, 0, 0);
+    int err = WSAIoctl(caglobals.ip.u4.fd, SIO_GET_EXTENSION_FUNCTION_POINTER, &GuidWSARecvMsg, sizeof(GuidWSARecvMsg), &(caglobals.ip.wsaRecvMsg), sizeof(caglobals.ip.wsaRecvMsg), &copied, 0, 0);
     if (0 != err)
     {
         OIC_LOG_V(ERROR, TAG, "WSAIoctl failed %i", WSAGetLastError());
index 1121e1e..31cc6c8 100755 (executable)
@@ -42,6 +42,7 @@
 #ifdef __WITH_TLS__
 #include "ca_adapter_net_ssl.h"
 #endif
+#include "iotivity_debug.h"
 
 /**
  * Logging tag for module name.
@@ -344,6 +345,19 @@ CAResult_t CAInitializeTCP(CARegisterConnectivityCallback registerCallback,
     VERIFY_NON_NULL(handle, TAG, "thread pool handle");
 #endif
 
+#ifdef WSA_WAIT_EVENT_0
+    // Windows-specific initialization.
+    WORD wVersionRequested = MAKEWORD(2, 2);
+    WSADATA wsaData = {.wVersion = 0};
+    int err = WSAStartup(wVersionRequested, &wsaData);
+    if (0 != err)
+    {
+        OIC_LOG_V(ERROR, TAG, "%s: WSAStartup failed: %i", __func__, err);
+        return CA_STATUS_FAILED;
+    }
+    OIC_LOG(DEBUG, TAG, "WSAStartup Succeeded");
+#endif
+
     g_networkChangeCallback = netCallback;
     g_connectionChangeCallback = connCallback;
     g_networkPacketCallback = networkPacketCallback;
@@ -550,6 +564,11 @@ void CATerminateTCP()
 {
     CAStopTCP();
     CATCPSetPacketReceiveCallback(NULL);
+
+#ifdef WSA_WAIT_EVENT_0
+    // Windows-specific clean-up.
+    OC_VERIFY(0 == WSACleanup());
+#endif
 }
 
 void CATCPSendDataThread(void *threadData)
index 54b6ab8..ce10eea 100644 (file)
@@ -502,7 +502,6 @@ static void CAFindReadyMessage()
     if (caglobals.tcp.terminate)
     {
         caglobals.tcp.updateEvent = WSA_INVALID_EVENT;
-        WSACleanup();
     }
 }