mirror of
https://github.com/akuker/RASCSI.git
synced 2025-01-11 09:29:53 +00:00
Updated deserialization
This commit is contained in:
parent
e50cc334c5
commit
915c5b7924
@ -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);
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user