Update snapshot(2017-12-14)
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / tcp_adapter / catcpserver.c
index 16ed703..8551650 100644 (file)
@@ -612,13 +612,12 @@ static void CAReceiveMessage(int fd)
     //disconnect session and clean-up data if any error occurs
     if (res != CA_STATUS_OK)
     {
-#ifdef __WITH_TLS__
-        if (CA_STATUS_OK != CAcloseSslConnection(&svritem->sep.endpoint))
+        if (CA_STATUS_OK != CATCPDisconnectSession(&svritem->sep.endpoint))
         {
-            OIC_LOG(ERROR, TAG, "Failed to close TLS session");
+            CASearchAndDeleteTCPSession(&svritem->sep.endpoint);
+            OIC_LOG(ERROR, TAG, "Failed to disconnect TCP session");
         }
-#endif
-        CASearchAndDeleteTCPSession(&(svritem->sep.endpoint));
+
         return;
     }
 }
@@ -1066,6 +1065,7 @@ size_t CACheckPayloadLengthFromHeader(const void *data, size_t dlen)
     if (!pdu)
     {
         OIC_LOG(ERROR, TAG, "outpdu is null");
+        OIC_LOG_V(ERROR, TAG, "data length: %zu", dlen);
         return 0;
     }
 
@@ -1111,22 +1111,29 @@ static ssize_t sendData(const CAEndpoint_t *endpoint, const void *data,
 
     // #2. send data to remote device.
     ssize_t remainLen = dlen;
+    size_t sendCounter = 0;
     do
     {
 #ifdef MSG_NOSIGNAL
-        ssize_t len = send(sockFd, data, remainLen, MSG_NOSIGNAL);
+        ssize_t len = send(sockFd, data, remainLen, MSG_DONTWAIT | MSG_NOSIGNAL);
 #else
-        ssize_t len = send(sockFd, data, remainLen, 0);
+        ssize_t len = send(sockFd, data, remainLen, MSG_DONTWAIT);
 #endif
         if (-1 == len)
         {
-            if (EWOULDBLOCK != errno)
+            if (EWOULDBLOCK != errno && EAGAIN != errno)
             {
                 OIC_LOG_V(ERROR, TAG, "unicast ipv4tcp sendTo failed: %s", strerror(errno));
                 CALogSendStateInfo(endpoint->adapter, endpoint->addr, endpoint->port,
                                    len, false, strerror(errno));
                 return len;
             }
+            sendCounter++;
+            OIC_LOG_V(WARNING, TAG, "send blocked. trying %zu attempt from 25", sendCounter);
+            if(sendCounter >= 25)
+            {
+                return len;
+            }
             continue;
         }
         data += len;
@@ -1260,6 +1267,8 @@ CAResult_t CADisconnectTCPSession(size_t index)
 
 void CATCPDisconnectAll()
 {
+    OIC_LOG(DEBUG, TAG, "IN - CATCPDisconnectAll");
+
     oc_mutex_lock(g_mutexObjectList);
 
     uint32_t length = u_arraylist_length(caglobals.tcp.svrlist);
@@ -1278,6 +1287,7 @@ void CATCPDisconnectAll()
     CAcloseSslConnectionAll(CA_ADAPTER_TCP);
 #endif
 
+    OIC_LOG(DEBUG, TAG, "OUT - CATCPDisconnectAll");
 }
 
 CATCPSessionInfo_t *CAGetTCPSessionInfoFromEndpoint(const CAEndpoint_t *endpoint, size_t *index)
@@ -1391,6 +1401,37 @@ CAResult_t CASearchAndDeleteTCPSession(const CAEndpoint_t *endpoint)
     return result;
 }
 
+void CATCPCloseInProgressConnections()
+{
+    OIC_LOG(INFO, TAG, "IN - CATCPCloseInProgressConnections");
+
+    oc_mutex_lock(g_mutexObjectList);
+
+    uint32_t length = u_arraylist_length(caglobals.tcp.svrlist);
+    for (size_t index = 0; index < length; index++)
+    {
+        CATCPSessionInfo_t *svritem = (CATCPSessionInfo_t *) u_arraylist_get(
+                caglobals.tcp.svrlist, index);
+        if (!svritem)
+        {
+            continue;
+        }
+
+        // Session which are connecting state
+        if (svritem->fd >= 0 && svritem->state == CONNECTING)
+        {
+            shutdown(svritem->fd, SHUT_RDWR);
+            close(svritem->fd);
+            svritem->fd = -1;
+            svritem->state = DISCONNECTED;
+        }
+    }
+
+    oc_mutex_unlock(g_mutexObjectList);
+
+    OIC_LOG(INFO, TAG, "OUT - CATCPCloseInProgressConnections");
+}
+
 size_t CAGetTotalLengthFromHeader(const unsigned char *recvBuffer)
 {
     OIC_LOG(DEBUG, TAG, "IN - CAGetTotalLengthFromHeader");