IRSOL
C++ code implementing socket server for interacting with Baumer camera.
irsol::protocol::Serializer Class Reference

Serializes outgoing protocol messages and primitive values. More...

#include <serializer.hpp>

Classes

struct  SpecialBytes
 Special byte constants used in the Simple Communication Protocol. More...
 

Static Public Member Functions

static internal::SerializedMessage serialize (OutMessage &&msg)
 Serialize an irsol::protocol::OutMessage variant into a serialized protocol message.
 
template<typename T , std::enable_if_t< irsol::traits::is_type_in_variant_v< T, OutMessage >, int > = 0>
static internal::SerializedMessage serialize (T &&msg)
 Serialize a specific irsol::protocol::OutMessage type into a serialized protocol message.
 
static std::string serializeValue (irsol::types::protocol_value_t &&value)
 Serialize a protocol primitive value to a string.
 
template<typename T , std::enable_if_t< irsol::traits::is_type_in_variant< T, irsol::types::protocol_value_t >::value, int > = 0>
static std::string serializeValue (T &&value)
 Serialize a specific primitive type to a string.
 
static std::string serializeBinaryDataAttribute (irsol::protocol::BinaryDataAttribute &&att)
 Serialize a irsol::protocol::BinaryDataAttribute to a string.
 

Static Public Attributes

static constexpr const char * message_termination = "\n"
 Message line termination sequence (newline character) used to "close" a serialized message.
 

Private Member Functions

 Serializer ()=delete
 This class cannot be instantiated.
 

Static Private Member Functions

static internal::SerializedMessage serializeSuccess (Success &&msg)
 Serializes a irsol::protocol::Success message to a binary protocol message.
 
static internal::SerializedMessage serializeBinaryDataBuffer (BinaryDataBuffer &&msg)
 Serializes a irsol::protocol::BinaryDataBuffer message to a binary protocol message.
 
static internal::SerializedMessage serializeImageBinaryData (ImageBinaryData &&msg)
 Serializes a irsol::protocol::ImageBinaryData message to a binary protocol message.
 
static internal::SerializedMessage serializeColorImageBinaryData (ColorImageBinaryData &&msg)
 Serializes a irsol::protocol::ColorImageBinaryData message to a binary protocol message.
 
static internal::SerializedMessage serializeError (Error &&msg)
 Serializes a irsol::protocol::Error message to a binary protocol message.
 

Detailed Description

Serializes outgoing protocol messages and primitive values.

This class offers a collection of static methods to serialize irsol::protocol::OutMessage variant types, as well as protocol primitive values such as integers, doubles, and strings (irsol::types::protocol_value_t), into irsol::protocol::internal::SerializedMessage instances or plain strings.

The class uses compile-time checks to ensure that only valid message types are serialized. It also provides utility constants for the special byte markers used in the protocol, following the Simple Communication Protocol specification used by irsol.

The serialization consumes the input message or value (via rvalue references), leaving the original object in a valid but unspecified state.

Definition at line 45 of file serializer.hpp.

Constructor & Destructor Documentation

◆ Serializer()

irsol::protocol::Serializer::Serializer ( )
privatedelete

This class cannot be instantiated.

Member Function Documentation

◆ serialize() [1/2]

internal::SerializedMessage irsol::protocol::Serializer::serialize ( OutMessage &&  msg)
static

Serialize an irsol::protocol::OutMessage variant into a serialized protocol message.

The message is consumed by this operation.

Parameters
msgThe irsol::protocol::OutMessage to serialize.
Returns
A irsol::protocol::internal::SerializedMessage containing the serialized message.

Definition at line 14 of file serializer.cpp.

15{
16
17 return std::visit(
18 [](auto&& msg) -> internal::SerializedMessage {
19 using T = std::decay_t<decltype(msg)>;
20 return serialize<T>(std::move(msg));
21 },
22 msg);
23}
auto msg

◆ serialize() [2/2]

template<typename T , std::enable_if_t< irsol::traits::is_type_in_variant_v< T, OutMessage >, int > = 0>
static internal::SerializedMessage irsol::protocol::Serializer::serialize ( T &&  msg)
inlinestatic

Serialize a specific irsol::protocol::OutMessage type into a serialized protocol message.

Enabled only for types contained in the irsol::protocol::OutMessage variant. The method dispatches to specialized serialization functions based on message type.

Template Parameters
TThe specific irsol::protocol::OutMessage type to serialize.
Parameters
msgThe message to serialize.
Returns
A irsol::protocol::internal::SerializedMessage containing the serialized message.
Warning
The input msg is consumed by this function and is left in a valid but unspecified state after the call.

Definition at line 95 of file serializer.hpp.

96 {
97 if constexpr(std::is_same_v<T, Success>) {
98 return serializeSuccess(std::move(msg));
99 } else if constexpr(std::is_same_v<T, BinaryDataBuffer>) {
100 return serializeBinaryDataBuffer(std::move(msg));
101 } else if constexpr(std::is_same_v<T, ImageBinaryData>) {
102 return serializeImageBinaryData(std::move(msg));
103 } else if constexpr(std::is_same_v<T, ColorImageBinaryData>) {
104 return serializeColorImageBinaryData(std::move(msg));
105 } else if constexpr(std::is_same_v<T, Error>) {
106 return serializeError(std::move(msg));
107 } else
108 IRSOL_MISSING_TEMPLATE_SPECIALIZATION(T, "Serializer::serialize()");
109 }
static internal::SerializedMessage serializeBinaryDataBuffer(BinaryDataBuffer &&msg)
Serializes a irsol::protocol::BinaryDataBuffer message to a binary protocol message.
static internal::SerializedMessage serializeImageBinaryData(ImageBinaryData &&msg)
Serializes a irsol::protocol::ImageBinaryData message to a binary protocol message.
static internal::SerializedMessage serializeColorImageBinaryData(ColorImageBinaryData &&msg)
Serializes a irsol::protocol::ColorImageBinaryData message to a binary protocol message.
static internal::SerializedMessage serializeError(Error &&msg)
Serializes a irsol::protocol::Error message to a binary protocol message.
static internal::SerializedMessage serializeSuccess(Success &&msg)
Serializes a irsol::protocol::Success message to a binary protocol message.
#define IRSOL_MISSING_TEMPLATE_SPECIALIZATION(T, funcNameLiteral)
Emits a compile-time error when no template specialization is available.
Definition macros.hpp:173

◆ serializeBinaryDataAttribute()

std::string irsol::protocol::Serializer::serializeBinaryDataAttribute ( irsol::protocol::BinaryDataAttribute &&  att)
static

Serialize a irsol::protocol::BinaryDataAttribute to a string.

Parameters
attThe irsol::protocol::BinaryDataAttribute to serialize.
Returns
A string representation of the serialized attribute.

Definition at line 131 of file serializer.cpp.

132{
133 std::stringstream ss;
134 ss << att.identifier << "=" << serializeValue(std::move(att.value));
135 return ss.str();
136}
static std::string serializeValue(irsol::types::protocol_value_t &&value)
Serialize a protocol primitive value to a string.
std::string identifier
Identifier of the binary data attribute.
Definition binary.hpp:44
irsol::types::protocol_value_t value
Value associated with the attribute.
Definition binary.hpp:47

◆ serializeBinaryDataBuffer()

internal::SerializedMessage irsol::protocol::Serializer::serializeBinaryDataBuffer ( BinaryDataBuffer &&  msg)
staticprivate

Serializes a irsol::protocol::BinaryDataBuffer message to a binary protocol message.

Parameters
msgThe BinaryDataBuffer message to serialize.
Returns
A SerializedMessage containing the serialized binary data buffer.
Warning
The input msg is consumed by this function and is left in a valid but unspecified state after the call.

Definition at line 63 of file serializer.cpp.

64{
65 IRSOL_LOG_TRACE("Serializing binary buffer: {}", msg.toString());
66 // TODO: implement serialization
67 throw std::runtime_error("Binary data serialization not supported");
68}
#define IRSOL_LOG_TRACE(...)
Logs a trace-level message using the default logger.
Definition logging.hpp:90

◆ serializeColorImageBinaryData()

internal::SerializedMessage irsol::protocol::Serializer::serializeColorImageBinaryData ( ColorImageBinaryData &&  msg)
staticprivate

Serializes a irsol::protocol::ColorImageBinaryData message to a binary protocol message.

Parameters
msgThe ColorImageBinaryData message to serialize.
Returns
A SerializedMessage containing the serialized color image data.
Warning
The input msg is consumed by this function and is left in a valid but unspecified state after the call.

Definition at line 116 of file serializer.cpp.

117{
118 IRSOL_LOG_TRACE("Serializing color image binary data: {}", msg.toString());
119 // TODO: implement serialization
120 throw std::runtime_error("Binary data serialization not supported");
121}

◆ serializeError()

internal::SerializedMessage irsol::protocol::Serializer::serializeError ( Error &&  msg)
staticprivate

Serializes a irsol::protocol::Error message to a binary protocol message.

Parameters
msgThe Error message to serialize.
Returns
A SerializedMessage containing the serialized error message.
Warning
The input msg is consumed by this function and is left in a valid but unspecified state after the call.

Definition at line 124 of file serializer.cpp.

125{
126 IRSOL_LOG_TRACE("Serializing error message: {}", msg.toString());
127 return {msg.identifier + ": Error: " + msg.description + Serializer::message_termination, {}};
128}
static constexpr const char * message_termination
Message line termination sequence (newline character) used to "close" a serialized message.

◆ serializeImageBinaryData()

internal::SerializedMessage irsol::protocol::Serializer::serializeImageBinaryData ( ImageBinaryData &&  msg)
staticprivate

Serializes a irsol::protocol::ImageBinaryData message to a binary protocol message.

This method serializes the image data into a binary buffer suitable for transmission. The image data is assumed to be in unpacked Mono12 format (2 bytes per pixel). During serialization, the bytes for each pixel are swapped (i.e., the order of the two bytes for each pixel is reversed) to match the IRSOL server protocol requirements. The resulting buffer contains the image data with swapped bytes, ready for network transfer.

Parameters
msgThe ImageBinaryData message to serialize.
Returns
A SerializedMessage containing the serialized image data with swapped bytes.
Note
The byte swapping is required so that the client can reconstruct the image correctly using irsol::camera::PixelByteSwapper and related utilities.
Warning
The input msg is consumed by this function and is left in a valid but unspecified state after the call.

Definition at line 71 of file serializer.cpp.

72{
73 IRSOL_LOG_TRACE("Serializing image binary data: {}", msg.toString());
74
75 std::vector<irsol::types::byte_t> payload;
76 payload.reserve(msg.data.size() + 128); // Reserve extra for header/meta
77
78 {
79 std::string imagePrefix = "img=";
80 auto imagePrefixAsBytes = irsol::utils::stringToBytes(imagePrefix);
81 std::move(imagePrefixAsBytes.begin(), imagePrefixAsBytes.end(), std::back_inserter(payload));
82 }
83 payload.emplace_back(Serializer::SpecialBytes::SOH);
84 {
85 std::stringstream ss;
86 ss << "u" << msg.BYTES_PER_ELEMENT * 8 << "[" << msg.shape[0] << "," << msg.shape[1] << "]";
87 for(auto& att : msg.attributes) {
88 ss << " " << serializeBinaryDataAttribute(std::move(att));
89 }
90 auto attributesString = ss.str();
92 "Attributes for message '{}' serialized to {}", msg.toString(), attributesString);
93 auto attributesStringAsBytes = irsol::utils::stringToBytes(attributesString);
94
95 payload.insert(payload.end(), attributesStringAsBytes.begin(), attributesStringAsBytes.end());
96 }
97 payload.emplace_back(Serializer::SpecialBytes::STX);
98
99 // Copy image data to the end of the payload buffer
100 size_t dataOffset = payload.size();
101 payload.resize(payload.size() + msg.data.size());
102 std::memcpy(&payload[dataOffset], msg.data.data(), msg.data.size());
103
104 // Swap bytes in-place for 16-bit data (assume always 16-bit)
105 // This swaps each pair of bytes in the image data region of the payload.
107 payload.begin() + static_cast<std::ptrdiff_t>(dataOffset), payload.end());
108
109 payload.emplace_back(Serializer::SpecialBytes::ETX);
110
111 auto message = internal::SerializedMessage("", std::move(payload));
112 return std::move(message);
113}
static std::string serializeBinaryDataAttribute(irsol::protocol::BinaryDataAttribute &&att)
Serialize a irsol::protocol::BinaryDataAttribute to a string.
#define IRSOL_LOG_DEBUG(...)
Logs a debug-level message using the default logger.
Definition logging.hpp:91
std::vector< irsol::types::byte_t > stringToBytes(const std::string &s)
Converts a std::string to a std::vector of irsol::types::byte_t.
Definition utils.cpp:182
Utility for swapping pixel bytes in a buffer.
static constexpr irsol::types::byte_t SOH
Start of Header (SOH) byte: 0x01.
static constexpr irsol::types::byte_t STX
Start of Text (STX) byte: 0x02.
static constexpr irsol::types::byte_t ETX
End of Text (ETX) byte: 0x03.

◆ serializeSuccess()

internal::SerializedMessage irsol::protocol::Serializer::serializeSuccess ( Success &&  msg)
staticprivate

Serializes a irsol::protocol::Success message to a binary protocol message.

Parameters
msgThe Success message to serialize.
Returns
A SerializedMessage containing the serialized Success message.
Warning
The input msg is consumed by this function and is left in a valid but unspecified state after the call.

Definition at line 37 of file serializer.cpp.

38{
39 IRSOL_LOG_TRACE("Serializing Success message: {}", msg.toString());
40 std::string result = msg.identifier;
41 if(msg.source == InMessageKind::ASSIGNMENT) {
43 msg.hasBody(),
44 "Body is not present in 'Success' message, created from 'Assignment'. This should "
45 "never happen, as a successful assignment should always provide a body for the "
46 "associated Success message.");
47 auto body = *msg.body;
48 result += "=" + serializeValue(std::move(body)) + Serializer::message_termination;
49 } else if(msg.source == InMessageKind::INQUIRY) {
50 if(msg.hasBody()) {
51 auto body = *msg.body;
52 result += "=" + serializeValue(std::move(body)) + Serializer::message_termination;
53 } else {
55 }
56 } else if(msg.source == InMessageKind::COMMAND) {
57 result += ";\n";
58 }
59 return {result, {}};
60}
#define IRSOL_ASSERT_ERROR
Error-level assertion macro.
Definition assert.hpp:134
auto result

◆ serializeValue() [1/2]

std::string irsol::protocol::Serializer::serializeValue ( irsol::types::protocol_value_t &&  value)
static

Serialize a protocol primitive value to a string.

Supported types include int, double, and std::string (see irsol::types::protocol_value_t).

Parameters
valueThe protocol value to serialize.
Returns
A string representation of the serialized value.

Definition at line 26 of file serializer.cpp.

27{
28 return std::visit(
29 [](auto&& val) -> std::string {
30 using T = std::decay_t<decltype(val)>;
31 return serializeValue<T>(std::move(val));
32 },
33 value);
34}
auto value

◆ serializeValue() [2/2]

template<typename T , std::enable_if_t< irsol::traits::is_type_in_variant< T, irsol::types::protocol_value_t >::value, int > = 0>
static std::string irsol::protocol::Serializer::serializeValue ( T &&  value)
inlinestatic

Serialize a specific primitive type to a string.

Enabled only for types contained in irsol::types::protocol_value_t.

Template Parameters
TThe primitive type to serialize (int, double, or std::string).
Parameters
valueThe value to serialize.
Returns
A string representation of the serialized value.

Definition at line 136 of file serializer.hpp.

137 {
138 if constexpr(std::is_same_v<T, int>) {
139 return std::to_string(value);
140 } else if constexpr(std::is_same_v<T, double>) {
141 return std::to_string(value);
142 } else if constexpr(std::is_same_v<T, std::string>) {
143 return "{" + value + "}";
144 } else
145 IRSOL_MISSING_TEMPLATE_SPECIALIZATION(T, "Serializer::serializeValue()");
146 }

Member Data Documentation

◆ message_termination

constexpr const char* irsol::protocol::Serializer::message_termination = "\n"
staticconstexpr

Message line termination sequence (newline character) used to "close" a serialized message.

Definition at line 65 of file serializer.hpp.


The documentation for this class was generated from the following files: