Jpp  15.0.1-rc.2-highQE
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ligier_helper.cpp
Go to the documentation of this file.
1 #include "ligier_helper.hpp"
2 
3 #include <stdexcept>
4 
5 #include <stdint.h>
6 #include <cstring>
7 
8 #include <arpa/inet.h>
9 
10 /**
11  * \author cpellegrino
12  */
13 
14 namespace ligier {
15 
16 std::size_t prepare_tag(
17  char* buffer
18  , std::size_t max_size
19  , std::string const& tag
20 ) {
21  static std::string const header = "_Subscri";
22  static std::string const mode = " w ";
23  static std::string const trailer = "_Always";
24 
25  uint32_t const tagsize = ntohl(3 + tag.size());
26 
27  std::size_t const total_size
28  = header.size()
29  + sizeof(tagsize)
30  + 4
31  + mode.size()
32  + tag.size()
33  + trailer.size()
34  + 9;
35 
36  if (total_size > max_size) {
37  throw std::runtime_error("buffer too small");
38  }
39 
40  std::memset(buffer, '\0', total_size);
41 
42  std::memcpy(buffer, header.c_str(), header.size());
43  buffer += header.size();
44  std::memcpy(buffer, &tagsize, sizeof(tagsize));
45  buffer += sizeof(tagsize);
46  buffer += 4;
47  std::memcpy(buffer, mode.c_str(), mode.size());
48  buffer += mode.size();
49  std::memcpy(buffer, tag.c_str(), tag.size());
50  buffer += tag.size();
51  std::memcpy(buffer, trailer.c_str(), trailer.size());
52 
53  return total_size;
54 }
55 
56 std::size_t read_data_size(char const* const buffer)
57 {
58  uint32_t const& message_size = *static_cast<uint32_t const* const>(
59  static_cast<void const* const>(buffer + 8)
60  );
61 
62  return ntohl(message_size);
63 }
64 
65 } // ns ligier
std::size_t prepare_tag(char *buffer, std::size_t max_size, std::string const &tag)
std::size_t read_data_size(char const *const buffer)