]> git.stg.codes - stg.git/commitdiff
[NY Flight] Improved socket reading for sgconf.
authorMaxim Mamontov <faust.madf@gmail.com>
Fri, 6 Sep 2013 16:57:05 +0000 (19:57 +0300)
committerMaxim Mamontov <faust.madf@gmail.com>
Fri, 6 Sep 2013 16:57:05 +0000 (19:57 +0300)
stglibs/srvconf.lib/netunit.cpp

index 63ea22286189c98a8c69168099b054a7ef44968f..670c452242a00d565b57c5a3636a7ec813224c81 100644 (file)
@@ -397,45 +397,40 @@ return 0;
 //---------------------------------------------------------------------------
 int NETTRANSACT::RxDataAnswer()
 {
-int n = 0;
-int ret;
-char bufferS[ENC_MSG_LEN];
-char buffer[ENC_MSG_LEN + 1];
-
 BLOWFISH_CTX ctx;
 EnDecodeInit(password.c_str(), PASSWD_LEN, &ctx);
 
-while (1)
+while (true)
     {
-    ret = recv(outerSocket, &bufferS[n++], 1, 0);
-    if (ret <= 0)
+    char bufferS[ENC_MSG_LEN];
+    size_t toRead = ENC_MSG_LEN;
+    while (toRead > 0)
         {
-        printf("Receive data error: '%s'\n", strerror(errno));
-        close(outerSocket);
-        errorMsg = RECV_DATA_ANSWER_ERROR;
-        return st_recv_fail;
+        int ret = recv(outerSocket, &bufferS[ENC_MSG_LEN - toRead], toRead, 0);
+        if (ret <= 0)
+            {
+            printf("Receive data error: '%s'\n", strerror(errno));
+            close(outerSocket);
+            errorMsg = RECV_DATA_ANSWER_ERROR;
+            return st_recv_fail;
+            }
+        toRead -= ret;
         }
 
-    if (n == ENC_MSG_LEN)
-        {
-        n = 0;
-        DecodeString(buffer, bufferS, &ctx);
-        buffer[ENC_MSG_LEN] = 0;
-
-        printf("%s", buffer);
+    char buffer[ENC_MSG_LEN + 1];
+    DecodeString(buffer, bufferS, &ctx);
+    buffer[ENC_MSG_LEN] = 0;
 
-        answerList.push_back(buffer);
+    answerList.push_back(buffer);
 
-        for (int j = 0; j < ENC_MSG_LEN; j++)
+    for (size_t i = 0; i < ENC_MSG_LEN; i++)
+        {
+        if (buffer[i] == 0)
             {
-            if (buffer[j] == 0)
-                {
-                printf("\n");
-                if (RxCallBack)
-                    if (st_ok != RxCallBack(dataRxCallBack, &answerList))
-                        return st_xml_parse_error;
-                return st_ok;
-                }
+            if (RxCallBack)
+                if (st_ok != RxCallBack(dataRxCallBack, &answerList))
+                    return st_xml_parse_error;
+            return st_ok;
             }
         }
     }