Updated deserialization

This commit is contained in:
Uwe Seimet 2021-07-16 14:09:43 +01:00
parent e50cc334c5
commit 915c5b7924
2 changed files with 14 additions and 7 deletions

View File

@ -475,10 +475,11 @@ bool ReturnStatus(FILE *fp, bool status = true, const char* msg = "") {
command_result.set_msg(msg); command_result.set_msg(msg);
command_result.set_status(status); 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; string data;
command_result.SerializeToString(&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()); void* buf = malloc(data.length());
memcpy(buf, (void*)data.data(), data.length()); memcpy(buf, (void*)data.data(), data.length());
fwrite(buf, data.length(), 1, fp); fwrite(buf, data.length(), 1, fp);

View File

@ -14,6 +14,8 @@
#include "rasctl.h" #include "rasctl.h"
#include "rasctl_interface.pb.h" #include "rasctl_interface.pb.h"
using namespace std;
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// //
// Send Command // Send Command
@ -46,18 +48,22 @@ BOOL SendCommand(char *buf)
// Receive the message // Receive the message
while (true) { while (true) {
size_t len = fread(buf, sizeof(unsigned int), 1, fp); int len;
if (len != sizeof(unsigned int)) { size_t res = fread(&len, sizeof(int), 1, fp);
if (res != 1) {
break; break;
} }
if (fread(buf, len, 1, fp) != len) { res = fread(buf, len, 1, fp);
if (res != 1) {
break; break;
} }
string msg(buf, len);
rasctl_interface::CommandResult command_result; 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()) { if (command_result.status()) {
fclose(fp); fclose(fp);