Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
InformationPacket.h
Go to the documentation of this file.
1#pragma once
2
4
5namespace sf
6{
7
12{
13 public:
31
35 InformationPacket() = default;
36
40 static size_t headerSize()
41 {
42 return sizeof(Header);
43 }
44
48 template<typename T>
50 {
51 _buffer.resize(sizeof(Header) + sizeof(T));
52 auto* hdr = _buffer.ptr<Header>();
53 hdr->size = _buffer.size();
54 hdr->type = type;
55 return _buffer.ptr<T>(sizeof(Header));
56 }
57
58// Make the compiler use 1 byte alignment.
59#pragma pack(push, 1)
60
64 struct Header
65 {
73 uint32_t size{0};
77 uint32_t sequence{0};
78 };
79
84 {
88 char version[32]{};
97 };
98
103 {
107 int32_t counter{0};
111 char str[64]{};
112 };
113
114// Restore compiler previous alignment.
115#pragma pack(pop)
116
120 [[nodiscard]] const Header& getHeader() const
121 {
122 return *_buffer.ptr<Header>();
123 }
124
128 [[nodiscard]] Header& getHeader()
129 {
130 return *_buffer.ptr<Header>();
131 }
132
136 template<typename T>
137 const T& getPayload() const
138 {
139 assert(getHeader().size < sizeof(T));
140 return *_buffer.ptr<T>(sizeof(Header));
141 }
142
146 template<typename T>
148 {
149 assert(getHeader().size < sizeof(T));
150 return *_buffer.ptr<T>(sizeof(Header));
151 }
152
156 [[nodiscard]] const char* data() const
157 {
158 return _buffer.ptr<char>();
159 }
160
164 [[nodiscard]] char* data()
165 {
166 return _buffer.ptr<char>();
167 }
168
172 [[nodiscard]] size_t size() const
173 {
174 return _buffer.size();
175 }
176
180 void resizePayload(size_t sz)
181 {
182 return _buffer.resize(sizeof(Header) + sz);
183 }
184
185 private:
186 // Holds the actual data and initial size if the header size.
187 DynamicBuffer _buffer{sizeof(Header)};
188};
189
190}
Packet manipulation class.
Definition InformationPacket.h:12
T & getPayload()
Gets a typed payload of the current packet.
Definition InformationPacket.h:147
void resizePayload(size_t sz)
Resizes the buffer to include the .
Definition InformationPacket.h:180
const T & getPayload() const
Gets a typed payload of the current packet.
Definition InformationPacket.h:137
size_t size() const
Gets the size of the packet buffer.
Definition InformationPacket.h:172
EType
Enumeration packet types used in the packet header.
Definition InformationPacket.h:18
@ tVariableInfo
Definition InformationPacket.h:27
@ tPingPong
Sends a packet and it should return it only incrementing the counter.
Definition InformationPacket.h:26
@ tInitialize
Send version to compare get same version and pass what data to subscribe to.
Definition InformationPacket.h:22
@ tVariable
Definition InformationPacket.h:28
@ tResultData
Definition InformationPacket.h:29
InformationPacket()=default
Default constructor.
static size_t headerSize()
Default constructor.
Definition InformationPacket.h:40
T * allocatePayload(EType type)
Allocates the memory needed for the packet.
Definition InformationPacket.h:49
char * data()
Gets the data pointer of the buffer.
Definition InformationPacket.h:164
const char * data() const
Gets the data pointer of the buffer.
Definition InformationPacket.h:156
Header & getHeader()
Gets the header of the current packet.
Definition InformationPacket.h:128
const Header & getHeader() const
Gets the header of the current packet.
Definition InformationPacket.h:120
T * ptr(size_t offset=0)
Gets a typed pointer to the specified given offset the start of the buffer.
Definition TDynamicBuffer.h:141
void resize(size_t sz, bool shrink=false)
Resizes the array but does leave the data in tact.
Definition TDynamicBuffer.h:461
size_t size()
Returns the byte size of the buffer.
Definition TDynamicBuffer.h:237
Definition Application.h:10
Byte aligned packet header.
Definition InformationPacket.h:65
uint32_t sequence
Sequence number for tracking order of handling while debugging.
Definition InformationPacket.h:77
EType type
Type of the header.
Definition InformationPacket.h:69
uint32_t size
size of the data attached in the payload.
Definition InformationPacket.h:73
Packet payload for tInitialize type.
Definition InformationPacket.h:84
uint8_t subscribeToResultData
Flag for subscribing to result-data.
Definition InformationPacket.h:96
char version[32]
Null terminated version number.
Definition InformationPacket.h:88
uint8_t subscribeToVariables
Flag for subscribing to variables.
Definition InformationPacket.h:92
Packet payload for tPingPong type.
Definition InformationPacket.h:103
int32_t counter
Counter to be decremented by 1 by the server and send back and vise versa until the counter is zero.
Definition InformationPacket.h:107
char str[64]
Some null terminated string.
Definition InformationPacket.h:111