From: Maxim Mamontov Date: Fri, 6 Sep 2013 16:57:05 +0000 (+0300) Subject: [NY Flight] Improved socket reading for sgconf. X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/ca1d7360658f557bafb67c87a003f52cc0d02198 [NY Flight] Improved socket reading for sgconf. --- diff --git a/stglibs/srvconf.lib/netunit.cpp b/stglibs/srvconf.lib/netunit.cpp index 63ea2228..670c4522 100644 --- a/stglibs/srvconf.lib/netunit.cpp +++ b/stglibs/srvconf.lib/netunit.cpp @@ -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; } } }