Deserialization of the UDP binary data

The class srs::ProtoMsgReader is responsible for the deserialization of the UDP binary data, which converts the Protobuf binary data sent by a UDP socket to a C++ data structure. All memory allocation of this class is done in its constructor. If the conversion are repeated many times, the object should be kept alive to reduce the memory allocation.

The full example can be found in this script .

Minimum example:

 1#include <srs/srs.hpp>
 2
 3auto main() -> int
 4{
 5  auto msg_reader = srs::ProtoMsgReader{};
 6
 7  std::string_view proto_binary = get_proto_msg();
 8
 9  const auto& data_struct = msg_reader.convert(proto_binary);
10
11  return 0;
12}

Details of srs::ProtoMsgReader

class ProtoMsgReader

Public Functions

ProtoMsgReader()

Constructor allocates the memeory for member variables

ProtoMsgReader(const ProtoMsgReader&) = delete

Deleted copy constructor.

ProtoMsgReader &operator=(const ProtoMsgReader&) = delete

Deleted copy assignment.

ProtoMsgReader(ProtoMsgReader&&) = default

Default move constructor.

ProtoMsgReader &operator=(ProtoMsgReader&&) = default

Default move assignment.

~ProtoMsgReader()

Default destructor.

auto convert(std::string_view msg, StructData &struct_data)

Convert binary data to a struct (inout).

Parameters:
  • msg – The input binary data.

  • struct_data – The struct data to store the deserialized binary data.

const StructData &convert(std::string_view msg)

Convert binary data to a struct, owned by ProtoMsgReader.

Description.

Parameters:

msg – The input binary data.

Returns:

The const reference to the internal struct data.

Private Members

std::unique_ptr<Proto2StructConverter> proto_to_struct_converter_

A converter to transform Protobuf struct to native C++ struct.

std::unique_ptr<ProtoDeserializer> proto_deserializer_

A converter to transform binary data to Protobuf struct.

Reading delimited binary data from the binary output file

The class srs::RawFrameReader is used to extract frames of binary data separately and consecutively from a binary file (or a FIFO file). Frames in the binary file are divided by a value in the type RawDelimSizeType located at the beginning of each frame, which stores the total byte size of the current frame.

This class provides both a public method and a static public method for the users to extract the frames from the binary file. The static public method should be used if the user wants to use the self-provided file handler and data buffer. In such case, the class should be constructed from its default constructor (RawFrameReader::RawFrameReader()) and no memory is allocated internally. On the other hand, the normal public method can be used if the user wants to rely on this class for the file opening and the data buffer. In this case, the constructor with a string input should be used instead and the object should be kept alive throughout the whole reading.

Minimum example:

 1#include <srs/srs.hpp>
 2#include <string>
 3#include <fstream>
 4
 5auto main() -> int
 6{
 7  const auto filename = std::string{ "output.bin" };
 8
 9  // Using external buffer
10  auto raw_frame_reader1 = srs::RawFrameReader{};
11
12  auto input_file = std::ifstream{ filename, std::ios::binary };
13  auto external_buffer = std::vector<char>{};
14
15  raw_rame_reader1.read_one_frame(external_buffer, input_file);
16
17  // Using internal buffer
18  auto raw_frame_reader2 = srs::RawFrameReader{ filename };
19
20  auto binary_data = raw_frame_reader2.read_one_frame();
21
22  return 0;
23}

Details of srs::RawFrameReader

class RawFrameReader

Public Functions

explicit RawFrameReader() = default

Default Constructor. No memory allocation.

explicit RawFrameReader(const std::string &filename)

Constructor that opens a file with the given filename.

Parameters:

filename – The file name of the input binary

std::string_view read_one_frame()

Read one frame of the binary data from a file specified by the constructor RawFrameReader(conststd::string&).

The binary data frame is written to the internal member variable input_buffer_; srs::LARGE_READ_MSG_BUFFER_SIZE bytes of memory will be reserved for the vector.

Returns:

Non-owning binary data.

inline const auto &get_binary_data() const

Getter to the internal binary data used by the member function read_one_frame().

Public Static Functions

static std::size_t read_one_frame(std::vector<char> &binary_data, std::ifstream &input_file)

Read one frame of the bianry data to a vector.

The input vector is first reserved with srs::LARGE_READ_MSG_BUFFER_SIZE elements and then resized with the size value, which is read from the binary file in the beginning. The vector is then set with size bytes of data from the file.

Parameters:
  • binary_data – The vector of chars to be filled.

  • input_file – Input file handler.

Returns:

The amount of bytes read from the binary file. If the binary input file is not open, return 0.

Private Members

std::string input_filename_

Input binary file name.

std::ifstream input_file_

Input binary file handler.

std::vector<char> input_buffer_

internal binary data buffer