From 915c5b792496c9e928283b6d395e9fbf026117d4 Mon Sep 17 00:00:00 2001 From: Uwe Seimet Date: Fri, 16 Jul 2021 14:09:43 +0100 Subject: [PATCH] Updated deserialization --- src/raspberrypi/rascsi.cpp | 5 +++-- src/raspberrypi/rasctl.cpp | 16 +++++++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/raspberrypi/rascsi.cpp b/src/raspberrypi/rascsi.cpp index f0c77910..124c7260 100644 --- a/src/raspberrypi/rascsi.cpp +++ b/src/raspberrypi/rascsi.cpp @@ -475,10 +475,11 @@ bool ReturnStatus(FILE *fp, bool status = true, const char* msg = "") { command_result.set_msg(msg); command_result.set_status(status); - // Serialize the result in binary format + // Serialize the result in binary format: Length followed by the actual data string data; command_result.SerializeToString(&data); - fprintf(fp, "%d", (unsigned int)data.length()); + int len = data.length(); + fwrite(&len, sizeof(len), 1, fp); void* buf = malloc(data.length()); memcpy(buf, (void*)data.data(), data.length()); fwrite(buf, data.length(), 1, fp); diff --git a/src/raspberrypi/rasctl.cpp b/src/raspberrypi/rasctl.cpp index 43c6599c..a1aa6c77 100644 --- a/src/raspberrypi/rasctl.cpp +++ b/src/raspberrypi/rasctl.cpp @@ -14,6 +14,8 @@ #include "rasctl.h" #include "rasctl_interface.pb.h" +using namespace std; + //--------------------------------------------------------------------------- // // Send Command @@ -46,18 +48,22 @@ BOOL SendCommand(char *buf) // Receive the message while (true) { - size_t len = fread(buf, sizeof(unsigned int), 1, fp); - if (len != sizeof(unsigned int)) { + int len; + size_t res = fread(&len, sizeof(int), 1, fp); + if (res != 1) { break; } - if (fread(buf, len, 1, fp) != len) { + res = fread(buf, len, 1, fp); + if (res != 1) { break; } + string msg(buf, len); rasctl_interface::CommandResult command_result; - command_result.ParseFromString(buf); + command_result.ParseFromString(msg); - std::cout << command_result.msg(); + cout << "Command terminated with status " << (command_result.status() ? "true" : "false") << ":" << endl; + cout << command_result.msg(); if (command_result.status()) { fclose(fp);