2022-09-25 21:49:24 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2022-12-05 17:58:23 +00:00
|
|
|
// SCSI Target Emulator PiSCSI
|
2022-09-25 21:49:24 +00:00
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
|
|
|
// Copyright (C) 2022 Uwe Seimet
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2022-11-10 06:44:06 +00:00
|
|
|
#include "shared/protobuf_serializer.h"
|
2022-12-05 17:58:23 +00:00
|
|
|
#include "shared/piscsi_exceptions.h"
|
|
|
|
#include "generated/piscsi_interface.pb.h"
|
2022-09-25 21:49:24 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
using namespace std;
|
2022-12-05 17:58:23 +00:00
|
|
|
using namespace piscsi_interface;
|
2022-09-25 21:49:24 +00:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2022-10-04 15:23:42 +00:00
|
|
|
// Serialize/Deserialize protobuf message: Length followed by the actual data.
|
|
|
|
// A little endian platform is assumed.
|
2022-09-25 21:49:24 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2022-10-04 15:23:42 +00:00
|
|
|
void ProtobufSerializer::SerializeMessage(int fd, const google::protobuf::Message& message) const
|
2022-09-25 21:49:24 +00:00
|
|
|
{
|
|
|
|
string data;
|
|
|
|
message.SerializeToString(&data);
|
|
|
|
|
|
|
|
// Write the size of the protobuf data as a header
|
2022-11-02 06:36:25 +00:00
|
|
|
auto size = static_cast<int32_t>(data.length());
|
2022-09-25 21:49:24 +00:00
|
|
|
if (write(fd, &size, sizeof(size)) != sizeof(size)) {
|
|
|
|
throw io_exception("Can't write protobuf message header");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the actual protobuf data
|
|
|
|
if (write(fd, data.data(), size) != size) {
|
|
|
|
throw io_exception("Can't write protobuf message data");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 15:23:42 +00:00
|
|
|
void ProtobufSerializer::DeserializeMessage(int fd, google::protobuf::Message& message) const
|
2022-09-25 21:49:24 +00:00
|
|
|
{
|
|
|
|
// Read the header with the size of the protobuf data
|
|
|
|
vector<byte> header_buf(4);
|
|
|
|
if (ReadBytes(fd, header_buf) < header_buf.size()) {
|
2022-10-08 17:26:04 +00:00
|
|
|
throw io_exception("Invalid protobuf message header");
|
2022-09-25 21:49:24 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
const int size = (static_cast<int>(header_buf[3]) << 24) + (static_cast<int>(header_buf[2]) << 16)
|
|
|
|
+ (static_cast<int>(header_buf[1]) << 8) + static_cast<int>(header_buf[0]);
|
2022-10-23 19:51:39 +00:00
|
|
|
if (size < 0) {
|
2022-09-25 21:49:24 +00:00
|
|
|
throw io_exception("Invalid protobuf message header");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the binary protobuf data
|
|
|
|
vector<byte> data_buf(size);
|
|
|
|
if (ReadBytes(fd, data_buf) < data_buf.size()) {
|
|
|
|
throw io_exception("Missing protobuf message data");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create protobuf message
|
|
|
|
string data((const char *)data_buf.data(), size);
|
|
|
|
message.ParseFromString(data);
|
|
|
|
}
|
|
|
|
|
2022-10-04 15:23:42 +00:00
|
|
|
size_t ProtobufSerializer::ReadBytes(int fd, vector<byte>& buf) const
|
2022-09-25 21:49:24 +00:00
|
|
|
{
|
|
|
|
size_t offset = 0;
|
|
|
|
while (offset < buf.size()) {
|
2022-10-08 17:26:04 +00:00
|
|
|
const ssize_t len = read(fd, &buf.data()[offset], buf.size() - offset);
|
2022-09-25 21:49:24 +00:00
|
|
|
if (len <= 0) {
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|