Message¶
-
class Message¶
Message class.
The message class will hold the header and payload of a message: the default mechanism to communicate in Melo.
Public Types
Public Functions
-
Message() = default¶
Create a new empty message.
An empty message is invalid and to fill the message, the function get_buffer() and parse_buffer() should be used.
-
Message(Type type, node::Address src, node::Address dst, uint32_t uid, std::string payload)¶
Create a message.
Note
The unique ID is only applicable for Type::kRequest, Type::kCancel, Type::kResponse or Type::kError, and it should be >= 1 to be valid.
- Parameters:
type – [in] The type of the message
src – [in] The address of sending Node
dst – [in] The address of the receiving Node
uid – [in] The unique ID of the message (only for Type::kRequest, Type::kCancel, Type::kResponse, Type::kError
payload – [in] The payload of the message
-
inline Message(Type type, node::Address src, node::Address dst, std::string payload)¶
Create a message.
-
Message(Type type, node::Address src, node::Address dst, uint32_t uid, const google::protobuf::Message &msg)¶
Create a message from a protobuf.
- Parameters:
type – [in] The type of the message
src – [in] The address of sending Node
dst – [in] The address of the receiving Node
uid – [in] The unique ID of the message (only for Type::kRequest, Type::kCancel, Type::kResponse, Type::kError
msg – [in] The protobuf message to serialize
-
inline Message(Type type, node::Address src, node::Address dst, const google::protobuf::Message &msg)¶
Create a message from a protobuf.
-
inline Message &operator=(Message &&msg)¶
Assign a message by move.
- Parameters:
msg – [in] The message to move
-
std::span<char> get_buffer(size_t chunk_size = kDefaultHeaderSize)¶
Get parsing buffer.
This function is used in conjunction with parse_buffer() and is used to get a buffer to fill with data to parse.
- Parameters:
chunk_size – [in] The optional chunk size in bytes (clipped to maximal header size)
- Returns:
a span pointing the buffer to fill.
-
size_t parse_buffer(size_t size, std::string &extra)¶
Parse from internal buffer with extra buffer.
This function is used to tell the parser that new data has been written in the internal buffer fetched with get_buffer(). The amount of data is provided by
size.If too many data have been provided, the
extraparameter will be filled with the extra data not used for this message. Thus, the extra data can be used with the other parse_buffer() implementation.When the parsing is finished, a call to is_valid() will return
true.- Parameters:
size – [in] The size of data to parse
extra – [out] The buffer to store extra data (out of this message)
- Returns:
the length in bytes of the data parsed or 0 if failed.
-
size_t parse_buffer(std::string &buffer)¶
Parse from a buffer.
This function should be used when the extra parameter of parse_buffer() has been filled with remaining data not used during parsing.
If the buffer was containing enough data for a new message, a call to is_valid() will return
trueand thebufferwill be updated with the extra unused data. In such case, this function should called again until thebufferis empty after the function returned.- Parameters:
buffer – [inout] The buffer to parse and where to store extra data (out of this message)
- Returns:
the length in bytes of the data parsed or 0 if failed.
-
inline bool is_valid() const¶
Check if the message is valid.
This function should be used with parse_buffer(): when the message is fully parsed, this function will return
true.- Returns:
trueis the message is fully parse and valid.
-
inline const node::Address &get_source() const¶
Get source address.
- Returns:
a reference to the source address.
-
inline const node::Address &get_destination() const¶
Get destination address.
- Returns:
a reference to the destination address.
-
inline uint32_t get_uid() const¶
Get message unique ID.
Note
The unique ID is only applicable for Type::kRequest, Type::kCancel, Type::kResponse or Type::kError, and it should be >= 1 to be valid.
- Returns:
the unique ID of the current message or 0 if not applicable.
-
inline const std::string &get_header() const¶
Get message header.
- Returns:
a reference to the message header.
-
inline const std::string &get_payload() const¶
Get message payload.
- Returns:
a reference to the message payload.
-
inline bool get_payload(google::protobuf::Message &msg) const¶
Get message payload as protobuf.
This function will deserialize the payload to a protobuf.
- Parameters:
msg – [out] The message to fill with data from payload
- Returns:
trueif the payload has been deserialized,falseotherwise.
-
template<class T>
inline T get_payload() const¶ Get message payload as protobuf.
This function will return a deserialization of the payload.
- Returns:
a reference to the message payload.
-
bool convert_to_response(std::string payload)¶
Convert the message into a response.
- Parameters:
payload – [in] The serialized payload
- Returns:
trueif the message has been converted,falseotherwise.
-
bool convert_to_response(const google::protobuf::Message &msg)¶
Convert the message into a response.
- Parameters:
msg – [in] The message to serialize as payload
- Returns:
trueif the message has been converted,falseotherwise.
-
bool convert_to_error(uint32_t code, std::string reason)¶
Convert the message into an error.
- Parameters:
code – [in] The error code
reason – [in] The reason of the error
- Returns:
trueif the message has been converted,falseotherwise.
Public Static Functions
Create a shared message.
This function can be useful to create a shared pointer of a Message in order to be shared by multiple parts of the code, such as event messages.
Public Static Attributes
-
static size_t kMaxAddressSize = 256¶
Maximal address size.
-
static size_t kMinHeaderSize = 12¶
Minimal header size.
-
static size_t kMaxHeaderSize = 512 + kMinHeaderSize¶
Maximal header size.
-
static size_t kDefaultHeaderSize = 64¶
Default header size.
-
Message() = default¶