Set error code to 0 for normal TCP disconnection. 85/217885/1
authorSenthil Kumar G S <senthil.gs@samsung.com>
Thu, 31 Oct 2019 05:04:42 +0000 (10:34 +0530)
committerSudipto Bal <sudipto.bal@samsung.com>
Fri, 15 Nov 2019 07:49:01 +0000 (16:49 +0900)
g_lastErrorCode is set to 0 to indicate normal session disconnections(recv() returns 0).

Background;
"g_lastErrorCode" is being set as errno when send() returns -1 or recv() returns under 0.
For instance, in case of recv(),
    - normal TCP session disconnect happens when recv() returns 0 len.
    - abnormal TCP session disconnect happens when recv() returns under 0. ( "g_lastErrorCode" is set as errno. )
Let's guess this scenario,
    - abnormal TCP session disconnect => "g_lastErrorCode" is set as X => ...... => retry TCP session connect => ...... => normal TCP session disconnect
From above scenario, then, is "g_lastErrorCode" not changed to default value 0?
I think, it is needed to set "g_lastErrorCode" as default value 0 when normal TCP session connect/disconnect happens.

https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/pull/595/commits/db79794eab8a1dbbe93d2da4a5deeb4826e22ccb
(cherry-picked from db79794eab8a1dbbe93d2da4a5deeb4826e22ccb)

Change-Id: Iea0d2ed780b9dedee6870a8946c3c5c65d15757c
Signed-off-by: Senthil Kumar G S <senthil.gs@samsung.com>
Signed-off-by: Sudipto Bal <sudipto.bal@samsung.com>
resource/csdk/connectivity/src/tcp_adapter/catcpserver.c

index 1a1702a19bea316bd85a300b335e7411afb65fc7..f0ca652d807abaac5916d628944840a5f2f16374 100755 (executable)
@@ -131,6 +131,7 @@ static CATCPConnectionHandleCallback g_connectionCallback = NULL;
 /**
  * Error code to hold the errno(Timeout, Destination unreachable, etc) when send/recv fails.
  * Main purpose is to hold the reason for connection drop when keep-alive is in use.
+ * To indicate normal disconnection, 0 will be assigned.
  */
 static int g_lastErrorCode;
 
@@ -750,11 +751,13 @@ static void CAReceiveMessage(int fd)
         {
             g_lastErrorCode = errno;
             OIC_LOG_V(DEBUG, TAG, "Set g_lastErrorCode to %s", strerror(g_lastErrorCode));
-            OIC_LOG_V(ERROR, TAG, "recv failed %s", strerror(errno));
+            OIC_LOG_V(ERROR, TAG, "recv failed %s", strerror(g_lastErrorCode));
             res = CA_RECEIVE_FAILED;
         }
         else if (0 == len)
         {
+            g_lastErrorCode = 0;
+            OIC_LOG(DEBUG, TAG, "Set g_lastErrorCode to 0");
             OIC_LOG(INFO, TAG, "Received disconnect from peer. Close connection");
             svritem->state = DISCONNECTED;
             res = CA_DESTINATION_DISCONNECTED;
@@ -804,11 +807,13 @@ static void CAReceiveMessage(int fd)
         {
             g_lastErrorCode = errno;
             OIC_LOG_V(DEBUG, TAG, "Set g_lastErrorCode to %s", strerror(g_lastErrorCode));
-            OIC_LOG_V(ERROR, TAG, "recv failed %s", strerror(errno));
+            OIC_LOG_V(ERROR, TAG, "recv failed %s", strerror(g_lastErrorCode));
             res = CA_RECEIVE_FAILED;
         }
         else if (0 == len)
         {
+            g_lastErrorCode = 0;
+            OIC_LOG_V(DEBUG, TAG, "Set g_lastErrorCode to 0");
             OIC_LOG(INFO, TAG, "Received disconnect from peer. Close connection");
             res = CA_DESTINATION_DISCONNECTED;
         }
@@ -1511,11 +1516,11 @@ static ssize_t sendData(const CAEndpoint_t *endpoint, const void *data,
         {
             g_lastErrorCode = errno;
             OIC_LOG_V(DEBUG, TAG, "Set g_lastErrorCode to %s", strerror(g_lastErrorCode));
-            if (EWOULDBLOCK != errno && EAGAIN != errno)
+            if (EWOULDBLOCK != g_lastErrorCode && EAGAIN != g_lastErrorCode)
             {
-                OIC_LOG_V(ERROR, TAG, "unicast ipv4tcp sendTo failed: %s", strerror(errno));
+                OIC_LOG_V(ERROR, TAG, "unicast ipv4tcp sendTo failed: %s", strerror(g_lastErrorCode));
                 CALogSendStateInfo(endpoint->adapter, endpoint->addr, endpoint->port,
-                                   len, false, strerror(errno));
+                                   len, false, strerror(g_lastErrorCode));
                 return len;
             }