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_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);
|
||||||
|
@ -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);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user