-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_header.hpp
83 lines (69 loc) · 2.03 KB
/
data_header.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#pragma once
#include <cstddef>
#include <cstdint>
#include <optional>
#include <ostream>
#include "data_type.hpp"
namespace ipc {
/**
* Object containing metadata for the actual message.
*/
class DataHeader {
public:
/**
* Create a new data header object containing metadata information.
*
* @param id Id of the message.
* @param type Type of the message.
* @param body_size Size of the actual message.
* @param timestamp Timestamp when the message/header was created.
*/
DataHeader(std::uint32_t id, DataType type, std::uint16_t body_size, std::int64_t timestamp);
/**
* Serialize the header into a buffer.
*
* @param buffer Buffer to serialize the header into.
* @param size Size of the buffer.
*
* @return Total number of bytes written into the buffer or -1 if an error occurred.
*/
int serialize(std::byte *buffer, unsigned int size) const;
/**
* Check if this header is valid.
*
* @return True, if header is valid.
*/
constexpr bool is_valid() const { return type_ != DataType::INVALID; }
/**
* Id of the message.
*/
std::uint32_t get_id() const { return id_; }
/**
* Type of the message.
*/
DataType get_type() const { return type_; }
/**
* Size of the actual message.
*/
std::uint16_t get_body_size() const { return body_size_; }
/**
* Timestamp when the message/header was created.
*/
std::int64_t get_timestamp() const { return timestamp_; }
/**
* Deserialize the header from a buffer.
*
* @param buffer Buffer to deserialize the header from.
* @param size Size of the buffer.
*
* @return Deserialized header from buffer.
*/
static std::optional<DataHeader> deserialize(const std::byte *buffer, unsigned int size);
private:
std::uint32_t id_;
DataType type_;
std::uint16_t body_size_;
std::int64_t timestamp_;
};
std::ostream &operator<<(std::ostream &outs, const DataHeader &header);
}