Fix 64bit build error for tizen dlog print format
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / tcp_adapter / catcpserver.c
index f40c533..ba1d224 100644 (file)
@@ -245,8 +245,15 @@ static void CAReceiveHandler(void *data)
     (void)data;
     OIC_LOG(DEBUG, TAG, "IN - CAReceiveHandler");
 
-    while (!caglobals.tcp.terminate)
+    while (true)
     {
+        oc_mutex_lock(g_mutexObjectList);
+        if (caglobals.tcp.terminate)
+        {
+            oc_mutex_unlock(g_mutexObjectList);
+            break;
+        }
+        oc_mutex_unlock(g_mutexObjectList);
         CAFindReadyMessage();
     }
 
@@ -291,11 +298,14 @@ static void CAFindReadyMessage()
 
     int ret = select(caglobals.tcp.maxfd + 1, &readFds, NULL, NULL, &timeout);
 
+    oc_mutex_lock(g_mutexObjectList);
     if (caglobals.tcp.terminate)
     {
+        oc_mutex_unlock(g_mutexObjectList);
         OIC_LOG_V(INFO, TAG, "Packet receiver Stop request received.");
         return;
     }
+    oc_mutex_unlock(g_mutexObjectList);
     if (0 >= ret)
     {
         if (0 > ret)
@@ -502,7 +512,7 @@ CAResult_t CAConstructCoAP(CATCPSessionInfo_t *svritem, unsigned char **data,
 
     unsigned char *inBuffer = *data;
     size_t inLen = *dataLength;
-    OIC_LOG_V(DEBUG, TAG, "before-datalength : %u", *dataLength);
+    OIC_LOG_V(DEBUG, TAG, "before-datalength : %zd", *dataLength);
 
     if (NULL == svritem->data && inLen > 0)
     {
@@ -606,7 +616,7 @@ CAResult_t CAConstructCoAP(CATCPSessionInfo_t *svritem, unsigned char **data,
     *data = inBuffer;
     *dataLength = inLen;
 
-    OIC_LOG_V(DEBUG, TAG, "after-datalength : %u", *dataLength);
+    OIC_LOG_V(DEBUG, TAG, "after-datalength : %zd", *dataLength);
     OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
     return CA_STATUS_OK;
 }
@@ -646,7 +656,7 @@ static void CAReceiveMessage(int fd)
             //[3][4] bytes in tls header are tls payload length
             tlsLength = TLS_HEADER_SIZE +
                             (size_t)((svritem->tlsdata[3] << 8) | svritem->tlsdata[4]);
-            OIC_LOG_V(DEBUG, TAG, "total tls length = %u", tlsLength);
+            OIC_LOG_V(DEBUG, TAG, "total tls length = %zd", tlsLength);
             if (tlsLength > TLS_DATA_MAX_SIZE)
             {
                 OIC_LOG_V(ERROR, TAG, "total tls length is too big (buffer size : %u)",
@@ -673,7 +683,7 @@ static void CAReceiveMessage(int fd)
         else
         {
             svritem->tlsLen += len;
-            OIC_LOG_V(DEBUG, TAG, "nb_read : %u bytes , recv() : %d bytes, svritem->tlsLen : %u bytes",
+            OIC_LOG_V(DEBUG, TAG, "nb_read : %zd bytes , recv() : %d bytes, svritem->tlsLen : %zd bytes",
                                 nbRead, len, svritem->tlsLen);
             if (tlsLength > 0 && tlsLength == svritem->tlsLen)
             {
@@ -1019,6 +1029,7 @@ static void CAInitializePipe(int *fds)
 #endif
 }
 
+#ifndef DISABLE_TCP_SERVER
 #define NEWSOCKET(FAMILY, NAME) \
     caglobals.tcp.NAME.fd = CACreateAcceptSocket(FAMILY, &caglobals.tcp.NAME); \
     if (caglobals.tcp.NAME.fd == -1) \
@@ -1056,6 +1067,7 @@ void CATCPInitializeSocket()
                   caglobals.tcp.ipv6s.fd, caglobals.tcp.ipv6s.port);
 #endif
 }
+#endif // DISABLE_TCP_SERVER
 
 CAResult_t CATCPStartServer(const ca_thread_pool_t threadPool)
 {
@@ -1084,10 +1096,13 @@ CAResult_t CATCPStartServer(const ca_thread_pool_t threadPool)
         caglobals.tcp.svrlist = u_arraylist_create();
     }
 
+#ifndef DISABLE_TCP_SERVER
     if (caglobals.server)
     {
         CATCPInitializeSocket();
     }
+#endif
+
 #ifndef __TIZENRT__
     // create pipe for fast shutdown
     CAInitializePipe(caglobals.tcp.shutdownFds);
@@ -1155,10 +1170,16 @@ void CATCPStopServer()
     CLOSE_SOCKET(ipv6s);
 #endif
 
-    close(caglobals.tcp.connectionFds[1]);
-    close(caglobals.tcp.connectionFds[0]);
-    caglobals.tcp.connectionFds[1] = OC_INVALID_SOCKET;
-    caglobals.tcp.connectionFds[0] = OC_INVALID_SOCKET;
+    if (caglobals.tcp.connectionFds[1] != OC_INVALID_SOCKET)
+    {
+       close(caglobals.tcp.connectionFds[1]);
+       caglobals.tcp.connectionFds[1] = OC_INVALID_SOCKET;
+    }
+    if (caglobals.tcp.connectionFds[0] != OC_INVALID_SOCKET)
+    {
+       close(caglobals.tcp.connectionFds[0]);
+       caglobals.tcp.connectionFds[0] = OC_INVALID_SOCKET;
+    }
 #ifndef __TIZENRT__
     if (caglobals.tcp.shutdownFds[1] != OC_INVALID_SOCKET)
     {
@@ -1283,10 +1304,13 @@ static ssize_t sendData(const CAEndpoint_t *endpoint, const void *data,
             oc_cond_wait_for(g_condSend, g_mutexSend, waitTime);
             oc_mutex_unlock(g_mutexSend);
 
+            oc_mutex_lock(g_mutexObjectList);
             if (caglobals.tcp.terminate)
             {
+                oc_mutex_unlock(g_mutexObjectList);
                 return len;
             }
+            oc_mutex_unlock(g_mutexObjectList);
 
             sendRetryTime = (sendRetryTime << 1);
 
@@ -1428,10 +1452,13 @@ CAResult_t CADisconnectTCPSession(size_t index)
 
     OIC_LOG(DEBUG, TAG, "data is removed from session list");
 
+#ifndef DISABLE_TCP_SERVER
     if (caglobals.server && MAX_CONNECTION_COUNTS == u_arraylist_length(caglobals.tcp.svrlist) + 1)
     {
         CATCPInitializeSocket();
     }
+#endif
+
     return CA_STATUS_OK;
 }