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