SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
Header.h
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 #ifndef swcdb_core_comm_Header_h
7 #define swcdb_core_comm_Header_h
8 
9 
12 #include "swcdb/core/Checksum.h"
13 
14 
15 namespace SWC { namespace Comm {
16 
17 
18 struct Header final {
19 
20  static const uint8_t PROTOCOL_VERSION = 1;
21  static const uint8_t PREFIX_LENGTH = 2;
22  static const uint8_t FIXED_LENGTH = PREFIX_LENGTH + 16;
23  static const uint8_t MAX_LENGTH = FIXED_LENGTH + 15 * 2;
24 
25  static const uint8_t FLAG_REQUEST_BIT = 0x01;
26  static const uint8_t FLAG_REQUEST_MASK = 0xFE;
27 
28  static const uint8_t FLAG_RESPONSE_IGNORE_BIT = 0x02;
29  static const uint8_t FLAG_RESPONSE_PARTIAL_BIT = 0x04;
30  static const uint8_t FLAG_RESPONSE_PARTIAL_MASK = 0xFB;
31 
32 
33  Header(uint64_t cmd=0, uint32_t timeout=0) noexcept
34  : version(1), header_len(0), flags(0), buffers(0),
35  id(0), timeout_ms(timeout), checksum(0), command(cmd),
36  data(), data_ext() {
37  }
38 
39  explicit Header(const Header& init_from_req_header) noexcept
40  : version(1), header_len(0),
41  flags(init_from_req_header.flags), buffers(0),
42  id(init_from_req_header.id),
43  timeout_ms(0), checksum(0),
44  command(init_from_req_header.command),
45  data(), data_ext() {
46  }
47 
48  ~Header() noexcept { }
49 
50  void reset() noexcept;
51 
52  void set(uint64_t cmd=0, uint32_t timeout=0) noexcept {
53  command = cmd;
54  timeout_ms = timeout;
55  }
56 
57  uint8_t encoded_length() noexcept;
58 
59  void encode(uint8_t** bufp) const;
60 
61  void decode_prefix(const uint8_t** bufp, size_t* remainp);
62 
63  void decode(const uint8_t** bufp, size_t* remainp);
64 
65  void initialize_from_response(const Header& header);
66 
67  void initialize_from_request(const Header& header);
68 
69  void print(std::ostream& out) const;
70 
71  uint8_t version;
72  uint8_t header_len;
73 
74  uint8_t flags;
75  uint8_t buffers;
76  uint32_t id;
77  uint32_t timeout_ms;
78  uint32_t checksum;
79  uint16_t command;
80 
83 
84 } __attribute__((packed));
85 
86 
87 
89 void Header::reset() noexcept {
90  version = 0;
91  header_len = 0;
92  flags = 0;
93  id = 0;
94  timeout_ms = 0;
95  command = 0;
96  buffers = 0;
97  data.reset();
98  data_ext.reset();
99  checksum = 0;
100 }
101 
103 uint8_t Header::encoded_length() noexcept {
105  buffers = 0;
106  if(data.size) {
107  ++buffers;
109  if(data_ext.size) {
110  ++buffers;
112  }
113  }
114  return header_len;
115 }
116 
118 void Header::encode(uint8_t** bufp) const {
119  uint8_t *base = *bufp;
123  Serialization::encode_i32(bufp, id);
127 
128  if(data.size) {
129  data.encode(bufp);
130  if(data_ext.size)
131  data_ext.encode(bufp);
132  }
133 
134  Core::checksum_i32(base, header_len-4, bufp);
135 }
136 
138 void Header::decode_prefix(const uint8_t** bufp, size_t* remainp) {
139  if (*remainp < PREFIX_LENGTH)
141  "Header size " SWC_FMT_LU " is less than the fixed length %d",
142  *remainp, PREFIX_LENGTH);
143 
144  version = Serialization::decode_i8(bufp, remainp);
145  header_len = Serialization::decode_i8(bufp, remainp);
146 }
147 
149 void Header::decode(const uint8_t** bufp, size_t* remainp) {
150  const uint8_t *base = *bufp;
151  *bufp += PREFIX_LENGTH;
152 
153  flags = Serialization::decode_i8(bufp, remainp);
154  id = Serialization::decode_i32(bufp, remainp);
155  timeout_ms = Serialization::decode_i32(bufp, remainp);
156  command = Serialization::decode_i16(bufp, remainp);
157 
158  if((buffers = Serialization::decode_i8(bufp, remainp))) {
159  data.decode(bufp, remainp);
160  if(buffers > 1)
161  data_ext.decode(bufp, remainp);
162  }
163 
164  checksum = Serialization::decode_i32(bufp, remainp);
167  "header-checksum decoded-len=" SWC_FMT_LD,
168  *bufp-base);
169 }
170 
173  flags = header.flags;
174  id = header.id;
175  command = header.command;
176  buffers = 0;
177  data.reset();
178  data_ext.reset();
179 }
180 
183  flags = header.flags;
184  id = header.id;
185  command = header.command;
186  buffers = 0;
187  data.reset();
188  data_ext.reset();
189 }
190 
191 
192 
193 }} //namespace SWC::Comm
194 
195 
196 
197 #ifdef SWC_IMPL_SOURCE
198 #include "swcdb/core/comm/Header.cc"
199 #endif
200 
201 #endif // swcdb_core_comm_Header_h
SWC::Comm::Header::print
void print(std::ostream &out) const
Definition: Header.cc:13
SWC::Comm::BufferInfo
Definition: HeaderBufferInfo.h:19
SWC::Comm::BufferInfo::encode
SWC_CAN_INLINE void encode(uint8_t **bufp) const
Definition: HeaderBufferInfo.h:44
SWC::Comm::Header::FLAG_REQUEST_BIT
static const uint8_t FLAG_REQUEST_BIT
Definition: Header.h:25
SWC::Comm::Header::command
uint16_t command
Request command number.
Definition: Header.h:79
SWC::Comm::Header::id
uint32_t id
Request ID.
Definition: Header.h:76
SWC::Comm::Header::~Header
~Header() noexcept
Definition: Header.h:48
SWC::Comm::Header::FLAG_RESPONSE_PARTIAL_MASK
static const uint8_t FLAG_RESPONSE_PARTIAL_MASK
Definition: Header.h:30
SWC::Serialization::encode_i32
SWC_CAN_INLINE void encode_i32(uint8_t **bufp, uint32_t val) noexcept
Definition: Serialization.h:138
SWC::Core::checksum_i32_chk
SWC_CAN_INLINE bool checksum_i32_chk(uint32_t checksum, const uint8_t *base, uint32_t len)
Definition: Checksum.h:94
SWC::Comm::Header::PROTOCOL_VERSION
static const uint8_t PROTOCOL_VERSION
Definition: Header.h:20
SWC::Comm::__attribute__
struct SWC::Comm::Header __attribute__((packed))
SWC::Comm::Header::data
BufferInfo data
Data Buffer.
Definition: Header.h:81
SWC::Serialization::encode_i16
SWC_CAN_INLINE void encode_i16(uint8_t **bufp, uint16_t val) noexcept
Definition: Serialization.h:112
SWC::Comm::Header::MAX_LENGTH
static const uint8_t MAX_LENGTH
Definition: Header.h:23
SWC::Serialization::encode_i8
constexpr SWC_CAN_INLINE void encode_i8(uint8_t **bufp, uint8_t val) noexcept
Definition: Serialization.h:85
SWC::Comm::Header::decode_prefix
void decode_prefix(const uint8_t **bufp, size_t *remainp)
Definition: Header.h:138
SWC::Comm::Header::FLAG_RESPONSE_PARTIAL_BIT
static const uint8_t FLAG_RESPONSE_PARTIAL_BIT
Definition: Header.h:29
SWC::Comm::BufferInfo::reset
SWC_CAN_INLINE void reset() noexcept
Definition: HeaderBufferInfo.h:30
Header.cc
SWC::Comm::Header::Header
Header(const Header &init_from_req_header) noexcept
Definition: Header.h:39
SWC::Comm::Header::data_ext
BufferInfo data_ext
Data Extended Buffer.
Definition: Header.h:82
SWC::Comm::BufferInfo::size
uint32_t size
Buffer size.
Definition: HeaderBufferInfo.h:67
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC::Serialization::decode_i16
SWC_CAN_INLINE uint16_t decode_i16(const uint8_t **bufp, size_t *remainp)
Definition: Serialization.h:117
SWC::Comm::Header::checksum
uint32_t checksum
Header checksum (excl. it self)
Definition: Header.h:78
SWC::Comm::Header::FIXED_LENGTH
static const uint8_t FIXED_LENGTH
Definition: Header.h:22
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Comm::Header::encode
void encode(uint8_t **bufp) const
Definition: Header.h:118
Serialization.h
SWC::Comm::Header::encoded_length
uint8_t encoded_length() noexcept
Definition: Header.h:103
SWC_THROWF
#define SWC_THROWF(_code_, _fmt_,...)
Definition: Exception.h:136
SWC::Comm::BufferInfo::decode
SWC_CAN_INLINE void decode(const uint8_t **bufp, size_t *remainp)
Definition: HeaderBufferInfo.h:53
SWC::Comm::Header::flags
uint8_t flags
Flags.
Definition: Header.h:74
SWC_FMT_LU
#define SWC_FMT_LU
Definition: Compat.h:98
HeaderBufferInfo.h
SWC::Comm::Header::reset
void reset() noexcept
Definition: Header.h:89
SWC::Comm::Header::initialize_from_request
void initialize_from_request(const Header &header)
Definition: Header.h:182
Checksum.h
SWC::Comm::Header::PREFIX_LENGTH
static const uint8_t PREFIX_LENGTH
Definition: Header.h:21
SWC::Comm::BufferInfo::encoded_length
SWC_CAN_INLINE uint8_t encoded_length() const noexcept
Definition: HeaderBufferInfo.h:36
SWC::Comm::Header::FLAG_RESPONSE_IGNORE_BIT
static const uint8_t FLAG_RESPONSE_IGNORE_BIT
Definition: Header.h:28
SWC::Comm::Header::buffers
uint8_t buffers
number of buffers from 0 to 2 (data+data_ext)
Definition: Header.h:75
SWC::Error::COMM_BAD_HEADER
@ COMM_BAD_HEADER
Definition: Error.h:68
SWC::Comm::Header::version
uint8_t version
Protocol version.
Definition: Header.h:71
SWC::Comm::Header::timeout_ms
uint32_t timeout_ms
Request timeout.
Definition: Header.h:77
SWC::Comm::Header::FLAG_REQUEST_MASK
static const uint8_t FLAG_REQUEST_MASK
Definition: Header.h:26
SWC::Comm::Header::set
void set(uint64_t cmd=0, uint32_t timeout=0) noexcept
Definition: Header.h:52
SWC::Comm::Header::header_len
uint8_t header_len
Length of header.
Definition: Header.h:72
SWC::Core::checksum_i32
SWC_CAN_INLINE void checksum_i32(const uint8_t *start, size_t len, uint8_t **ptr) noexcept
Definition: Checksum.h:62
SWC::Comm::Header::Header
Header(uint64_t cmd=0, uint32_t timeout=0) noexcept
Definition: Header.h:33
SWC::Comm::Header::initialize_from_response
void initialize_from_response(const Header &header)
Definition: Header.h:172
SWC::Comm::Header
Definition: Header.h:18
SWC::Error::COMM_HEADER_CHECKSUM_MISMATCH
@ COMM_HEADER_CHECKSUM_MISMATCH
Definition: Error.h:67
SWC_FMT_LD
#define SWC_FMT_LD
Definition: Compat.h:99
SWC::Comm::Header::decode
void decode(const uint8_t **bufp, size_t *remainp)
Definition: Header.h:149
SWC::Serialization::decode_i8
constexpr SWC_CAN_INLINE uint8_t decode_i8(const uint8_t **bufp, size_t *remainp)
Definition: Serialization.h:91
SWC::Serialization::decode_i32
SWC_CAN_INLINE uint32_t decode_i32(const uint8_t **bufp, size_t *remainp)
Definition: Serialization.h:143