hmbdc
simplify-high-performance-messaging-programming
Messages.hpp
1 #include "hmbdc/Copyright.hpp"
2 #pragma once
3 
4 #include "hmbdc/comm/inet/Endpoint.hpp"
5 #include "hmbdc/app/Message.hpp"
6 #include "hmbdc/Endian.hpp"
7 
8 #include <iostream>
9 #include <string>
10 
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <arpa/inet.h>
14 
15 namespace hmbdc { namespace tips { namespace tcpcast {
16 
18  uint8_t flag; //1 - hasAttachment
19  XmitEndian<uint16_t> messagePayloadLen;
20 
21  void const* payload() const {
22  return reinterpret_cast<const char*>(this)
23  + sizeof(TransportMessageHeader);
24  }
25 
26  void * payload() {
27  return reinterpret_cast<char*>(this)
28  + sizeof(TransportMessageHeader);
29  }
30 
31  uint16_t typeTag() const {
32  auto h = static_cast<app::MessageHead const*>(payload());
33  return h->typeTag;
34  }
35 
36  template <typename Message>
37  Message& wrapped() {
38  auto wrap = static_cast<app::MessageWrap<Message>*>(payload());
39  return wrap->payload;
40  }
41 
42  template <typename Message>
43  Message const& wrapped() const {
44  auto wrap = static_cast<app::MessageWrap<Message> const *>(payload());
45  return wrap->payload;
46  }
47 
48  size_t wireSize() const {
49  return sizeof(TransportMessageHeader) + messagePayloadLen;
50  }
51 
52  size_t wireSizeContainsTRansportHeader() const {
53  return sizeof(TransportMessageHeader);
54  }
55 } __attribute__((packed));
56 
58 : app::hasTag<250> {
60  : port(0)
61  , srcPid(0)
62  , loopback(false)
63  {}
64 
65  TypeTagSource(std::string const& ipIn
66  , uint16_t portIn
67  , bool loopbackIn)
68  : port(portIn)
69  , srcPid(getpid())
70  , loopback(loopbackIn) {
71  snprintf(ip, sizeof(ip), "%s", ipIn.c_str());
72  }
73  char ip[16];
74  XmitEndian<uint16_t> typeTagCountContained{0};
75  XmitEndian<uint16_t> typeTags[64];
77  XmitEndian<uint32_t> srcPid;
78  bool loopback;
79 
80  bool addTypeTag(uint16_t tag) {
81  auto c = uint16_t{typeTagCountContained};
82  if (c < 64) {
83  typeTags[c++] = tag;
84  typeTagCountContained = c;
85  return true;
86  }
87  return false;
88  }
89 
90  friend
91  std::ostream& operator << (std::ostream& os, TypeTagSource const& m) {
92  os << "TypeTagSource:" << m.ip << ':' << m.port;
93  for (auto i = 0u; i < m.typeTagCountContained; i++) {
94  os << ' ' << m.typeTags[i];
95  }
96 
97  return os;
98  }
99 } __attribute__((packed));
100 
101 /**
102  * @class SessionStarted
103  * @brief this message tipsears in the receiver's buffer indicating a new source is connected
104  * @details only tipsears on the receiving side, and the receiver buffer is big enough to hold this messages
105  */
107 : app::hasTag<254> {
108  char ip[16];
109  friend
110  std::ostream& operator << (std::ostream& os, SessionStarted const & m) {
111  return os << "Session to TypeTag source started " << m.ip;
112  }
113 };
114 
115 /**
116  * @class SessionDropped
117  * @brief this message tipsears in the receiver's buffer indicating a previously connected source is dropped
118  * @details only tipsears on the receiving side, and the receiver buffer is big enough to hold this messages
119  */
121 : app::hasTag<255> {
122  char ip[16];
123  friend
124  std::ostream& operator << (std::ostream& os, SessionDropped const & m) {
125  return os << "Session to source dropped " << m.ip;
126  }
127 };
128 
129 }}}
each message type has 16 bit tag
Definition: Message.hpp:62
this message tipsears in the receiver&#39;s buffer indicating a previously connected source is dropped ...
Definition: Messages.hpp:120
Definition: Message.hpp:212
Definition: Messages.hpp:57
Definition: Message.hpp:263
Definition: Base.hpp:12
this message tipsears in the receiver&#39;s buffer indicating a new source is connected ...
Definition: Messages.hpp:106