hmbdc
simplify-high-performance-messaging-programming
 All Classes Namespaces Functions Variables Friends Pages
Messages.hpp
1 #include "hmbdc/Copyright.hpp"
2 #pragma once
3 
4 #include "hmbdc/time/Time.hpp"
5 #include "hmbdc/app/Base.hpp"
6 
7 #include <ostream>
8 
9 namespace hmbdc { namespace app { namespace udpcast {
10 
12  uint8_t topicLen;
13 
14  uint16_t& messagePayloadLen() {
15  return *reinterpret_cast<uint16_t*>(payload());
16  }
17 
18  uint16_t const& messagePayloadLen() const {
19  return *reinterpret_cast<uint16_t const*>(payload());
20  }
21 
22  std::pair<char const*, char const*> topic() const {
23  char const* b = reinterpret_cast<const char*>(this)
24  + sizeof(TransportMessageHeader);
25  return std::make_pair(b, b + topicLen);
26  }
27 
28  void const* payload() const {
29  return reinterpret_cast<const char*>(this)
30  + sizeof(TransportMessageHeader) + topicLen;
31  }
32 
33  void * payload() {
34  return reinterpret_cast<char*>(this)
35  + sizeof(TransportMessageHeader) + topicLen;
36  }
37 
38  uint16_t typeTag() const {
39  auto h = static_cast<app::MessageHead const*>(payload());
40  return h->typeTag;
41  }
42 
43  template <typename Message>
44  Message& wrapped() {
45  auto wrap = static_cast<app::MessageWrap<Message>*>(payload());
46  return wrap->payload;
47  }
48 
49  template <typename Message>
50  Message const& wrapped() const {
51  auto wrap = static_cast<app::MessageWrap<Message> const *>(payload());
52  return wrap->payload;
53  }
54 
55  size_t wireSize() const {
56  return sizeof(TransportMessageHeader) + topicLen
57  + messagePayloadLen();
58  }
59 
60  size_t wireSizeContainsTopic() const {
61  return sizeof(TransportMessageHeader) + topicLen;
62  }
63 } __attribute__((packed));
64 
65 struct Subscribe
66 : hasTag<101> {
67  Subscribe(Topic const&topic)
68  : topic(topic){}
69  Topic topic;
70 };
71 static_assert(sizeof(Subscribe) == 64
72  , "do you have a pack pragma unclosed that influencs the above struct packing unexpectedly?");
73 struct Unsubscribe
74 : hasTag<102> {
75  Unsubscribe(Topic const&topic)
76  : topic(topic){}
77  Topic topic;
78 };
79 
80 }}}
topic as in the publish / subscribe communication paradigm
Definition: Topic.hpp:14
each message type has 16 bit tag
Definition: Message.hpp:60
Definition: Messages.hpp:65
Definition: Message.hpp:78
Definition: Message.hpp:112
Definition: Messages.hpp:73