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/app/Message.hpp"
6 
7 #include <type_traits>
8 #include <ostream>
9 
10 namespace hmbdc { namespace app { namespace netmap {
11 
12 using namespace hmbdc;
13 using namespace hmbdc::comm;
14 
16  uint8_t topicLen;
17  uint16_t& messagePayloadLen() {
18  return *reinterpret_cast<uint16_t*>(payload());
19  }
20  uint16_t const& messagePayloadLen() const {
21  return *reinterpret_cast<uint16_t const*>(payload());
22  }
23 
24 //copy message into the right place in memory
25  template <typename M>
27  copyTo(void* addrIn, Topic const& t, M&& m) {
28  using Message = typename std::remove_reference<M>::type;
29  auto addr = (char*)addrIn;
30  auto h = reinterpret_cast<TransportMessageHeader*>(addr);
31  auto tl = t.copyTo(addr + sizeof(TransportMessageHeader));
32  h->topicLen = tl;
33  new (addr + sizeof(TransportMessageHeader) + tl)
34  MessageWrap<Message>(std::forward<M>(m));
35  h->messagePayloadLen() = sizeof(MessageWrap<Message>);
36  return h;
37  }
38 
40  copyTo(void* addrIn, Topic const& t, uint16_t tag, void const* bytes, size_t len) {
41  auto addr = (char*)addrIn;
42  auto h = reinterpret_cast<TransportMessageHeader*>(addr);
43  auto tl = t.copyTo(addr + sizeof(TransportMessageHeader));
44  h->topicLen = tl;
45  new (addr + sizeof(TransportMessageHeader) + tl) MessageWrap<JustBytes>(tag, bytes, len);
46  h->messagePayloadLen() = sizeof(MessageWrap<JustBytes>) - sizeof(MessageWrap<JustBytes>::payload) + len;
47  return h;
48  }
49 
50  template <typename Message, typename ... Args>
52  copyToInPlace(void* addrIn, Topic const& t, Args&&... args) {
53  char* addr = (char*)addrIn;
54  auto h = reinterpret_cast<TransportMessageHeader*>(addr);
55  auto tl = t.copyTo(addr + sizeof(TransportMessageHeader));
56  h->topicLen = tl;
57  new (addr + sizeof(TransportMessageHeader) + tl) MessageWrap<Message>(std::forward<Args>(args)...);
58  h->messagePayloadLen() = sizeof(MessageWrap<Message>);
59  return h;
60  }
61 
62  std::pair<char const*, char const*> topic() const {
63  char const* b = reinterpret_cast<const char*>(this)
64  + sizeof(TransportMessageHeader);
65  return std::make_pair(b, b + topicLen);
66  }
67 
68  void const* payload() const {
69  return reinterpret_cast<const char*>(this)
70  + sizeof(TransportMessageHeader) + topicLen;
71  }
72 
73  void * payload() {
74  return reinterpret_cast<char*>(this)
75  + sizeof(TransportMessageHeader) + topicLen;
76  }
77 
78  uint16_t typeTag() const {
79  auto h = static_cast<app::MessageHead const*>(payload());
80  return h->typeTag;
81  }
82 
83  template <typename Message>
84  Message& wrapped() {
85  auto wrap = static_cast<app::MessageWrap<Message>*>(payload());
86  return wrap->payload;
87  }
88 
89  template <typename Message>
90  Message const& wrapped() const {
91  auto wrap = static_cast<app::MessageWrap<Message> const *>(payload());
92  return wrap->payload;
93  }
94 
95  size_t wireSize() const {
96  return sizeof(TransportMessageHeader) + topicLen
97  + messagePayloadLen();
98  }
99 
100  size_t wireSizeContainsTopic() const {
101  return sizeof(TransportMessageHeader) + topicLen;
102  }
103 
104  template <typename Message>
105  static
106  size_t wireSize(Topic const& t) {
107 
108  return sizeof(TransportMessageHeader) + strnlen(t.c_str(), sizeof(t))
109  + sizeof(MessageWrap<Message>);
110  }
111 
112 } __attribute__((packed));
113 
114 
115 // struct PlaceHolder
116 // : hasTag<200> {
117 // };
118 
119 struct Subscribe
120 : hasTag<301> {
121  Subscribe(Topic const&topic)
122  : topic(topic){}
123  Topic topic;
124 };
125 
126 struct Unsubscribe
127 : hasTag<302> {
128  Unsubscribe(Topic const&topic)
129  : topic(topic){}
130  Topic topic;
131 };
132 
133 // struct NetmapIoError
134 // : hasTag<308> {
135 // char netmapPort[8];
136 // bool isPolling;
137 // int errno;
138 // };
139 
140 
141 }}}
Definition: Message.hpp:125
Definition: Messages.hpp:119
topic as in the publish / subscribe communication paradigm
Definition: Topic.hpp:14
Definition: Message.hpp:21
Definition: Message.hpp:25
Definition: Messages.hpp:126
Definition: Misc.h:9
Definition: Message.hpp:46
Definition: Client.hpp:11