RTCPHeader.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // Copyright 2019 Proyectos y Sistemas de Mantenimiento SL (eProsima).
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef _FASTDDS_RTCP_HEADER_H_
  15. #define _FASTDDS_RTCP_HEADER_H_
  16. #include <fastdds/rtps/common/Types.h>
  17. #include <cstring>
  18. #include <fastcdr/FastCdr.h>
  19. #include <fastcdr/Cdr.h>
  20. #include <fastcdr/exceptions/BadParamException.h>
  21. namespace eprosima{
  22. namespace fastdds{
  23. namespace rtps{
  24. #define TCPHEADER_SIZE 14
  25. // TCP Header structs and enums.
  26. struct TCPHeader
  27. {
  28. char rtcp[4];
  29. uint32_t length;
  30. uint32_t crc;
  31. uint16_t logical_port;
  32. TCPHeader()
  33. : length(sizeof(TCPHeader))
  34. , crc(0)
  35. , logical_port(0)
  36. {
  37. // There isn't a explicit constructor because VS2013 doesn't support it.
  38. rtcp[0] = 'R';
  39. rtcp[1] = 'T';
  40. rtcp[2] = 'C';
  41. rtcp[3] = 'P';
  42. }
  43. const fastrtps::rtps::octet* address() const
  44. {
  45. return reinterpret_cast<const fastrtps::rtps::octet*>(this);
  46. }
  47. fastrtps::rtps::octet* address()
  48. {
  49. return (fastrtps::rtps::octet*)this;
  50. }
  51. /*!
  52. * @brief This function returns the maximum serialized size of an object
  53. * depending on the buffer alignment.
  54. * @return Maximum serialized size.
  55. */
  56. static inline size_t size()
  57. {
  58. return TCPHEADER_SIZE;
  59. }
  60. };
  61. union TCPTransactionId
  62. {
  63. uint32_t ints[3];
  64. fastrtps::rtps::octet octets[12];
  65. TCPTransactionId()
  66. {
  67. memset(ints, 0, 3 * sizeof(uint32_t));
  68. }
  69. TCPTransactionId(const TCPTransactionId& t)
  70. {
  71. memcpy(ints, t.ints, 3 * sizeof(uint32_t));
  72. }
  73. TCPTransactionId& operator++()
  74. {
  75. if (ints[0] == 0xffffffff)
  76. {
  77. if (ints[1] == 0xffffffff)
  78. {
  79. if (ints[2] == 0xffffffff)
  80. {
  81. memset(ints, 0, 3 * sizeof(uint32_t));
  82. }
  83. else
  84. {
  85. ints[2] += 1;
  86. }
  87. }
  88. else
  89. {
  90. ints[1] += 1;
  91. }
  92. }
  93. else
  94. {
  95. ints[0] += 1;
  96. }
  97. return *this;
  98. }
  99. TCPTransactionId operator++(int)
  100. {
  101. TCPTransactionId prev = *this;
  102. ++(*this);
  103. return prev;
  104. }
  105. TCPTransactionId& operator=(const TCPTransactionId& t)
  106. {
  107. memcpy(ints, t.ints, 3 * sizeof(uint32_t));
  108. return *this;
  109. }
  110. TCPTransactionId& operator=(const fastrtps::rtps::octet* id)
  111. {
  112. memcpy(octets, id, 12 * sizeof(fastrtps::rtps::octet));
  113. return *this;
  114. }
  115. TCPTransactionId& operator=(const char* id)
  116. {
  117. memcpy(octets, id, 12 * sizeof(fastrtps::rtps::octet));
  118. return *this;
  119. }
  120. TCPTransactionId& operator=(const uint32_t* id)
  121. {
  122. memcpy(ints, id, 3 * sizeof(uint32_t));
  123. return *this;
  124. }
  125. TCPTransactionId& operator=(uint32_t id)
  126. {
  127. ints[0] = id;
  128. ints[1] = 0;
  129. ints[2] = 0;
  130. return *this;
  131. }
  132. TCPTransactionId& operator=(uint64_t id)
  133. {
  134. memset(ints, 0, sizeof(uint32_t) * 3);
  135. memcpy(ints, &id, sizeof(uint64_t));
  136. return *this;
  137. }
  138. bool operator==(const TCPTransactionId& t) const
  139. {
  140. return memcmp(ints, t.ints, 3 * sizeof(uint32_t)) == 0;
  141. }
  142. bool operator<(const TCPTransactionId& t) const
  143. {
  144. return memcmp(ints, t.ints, 3 * sizeof(uint32_t)) < 0;
  145. }
  146. };
  147. inline std::ostream& operator<<(std::ostream& output,const TCPTransactionId& t)
  148. {
  149. bool printed = false; // Don't skip cases like 99 0 34
  150. for (int i = 2; i >= 0; --i)
  151. {
  152. if (printed || i == 0 || t.ints[i] > 0)
  153. {
  154. output << t.ints[i];
  155. printed = true;
  156. }
  157. }
  158. return output;
  159. }
  160. enum TCPCPMKind : fastrtps::rtps::octet
  161. {
  162. BIND_CONNECTION_REQUEST = 0xD1,
  163. BIND_CONNECTION_RESPONSE = 0xE1,
  164. OPEN_LOGICAL_PORT_REQUEST = 0xD2,
  165. OPEN_LOGICAL_PORT_RESPONSE = 0xE2,
  166. CHECK_LOGICAL_PORT_REQUEST = 0xD3,
  167. CHECK_LOGICAL_PORT_RESPONSE = 0xE3,
  168. KEEP_ALIVE_REQUEST = 0xD4,
  169. KEEP_ALIVE_RESPONSE = 0xE4,
  170. LOGICAL_PORT_IS_CLOSED_REQUEST = 0xD5,
  171. UNBIND_CONNECTION_REQUEST = 0xD6
  172. };
  173. class TCPControlMsgHeader
  174. {
  175. TCPCPMKind kind_; // 1 byte
  176. fastrtps::rtps::octet flags_; // 1 byte
  177. uint16_t length_; // 2 bytes
  178. TCPTransactionId transaction_id_; // 12 bytes
  179. public:
  180. TCPControlMsgHeader()
  181. {
  182. kind_ = static_cast<TCPCPMKind>(0x00);
  183. flags_ = static_cast<fastrtps::rtps::octet>(0x00);
  184. length_ = 0;
  185. }
  186. void kind(TCPCPMKind kind)
  187. {
  188. kind_ = kind;
  189. }
  190. TCPCPMKind kind() const
  191. {
  192. return kind_;
  193. }
  194. TCPCPMKind& kind()
  195. {
  196. return kind_;
  197. }
  198. void length(uint16_t length)
  199. {
  200. length_ = length;
  201. }
  202. uint16_t length() const
  203. {
  204. return length_;
  205. }
  206. uint16_t& length()
  207. {
  208. return length_;
  209. }
  210. void transaction_id(TCPTransactionId transaction_id)
  211. {
  212. transaction_id_ = transaction_id;
  213. }
  214. TCPTransactionId transaction_id() const
  215. {
  216. return transaction_id_;
  217. }
  218. TCPTransactionId& transaction_id()
  219. {
  220. return transaction_id_;
  221. }
  222. void flags(
  223. bool endianess,
  224. bool payload,
  225. bool requires_response)
  226. {
  227. //TODO: Optimize receiving a Endianness_t
  228. fastrtps::rtps::octet e = (endianess) ? BIT(1) : 0x00;
  229. fastrtps::rtps::octet p = (payload) ? BIT(2) : 0x00;
  230. fastrtps::rtps::octet r = (requires_response) ? BIT(3) : 0x00;
  231. flags_ = e | p | r;
  232. }
  233. void endianess(fastrtps::rtps::Endianness_t endianess)
  234. {
  235. // Endianess flag has inverse logic than Endianness_t :-/
  236. if (endianess == fastrtps::rtps::Endianness_t::BIGEND)
  237. {
  238. flags_ &= 0xFE;
  239. }
  240. else
  241. {
  242. flags_ |= BIT(1);
  243. }
  244. }
  245. void payload(bool payload)
  246. {
  247. if (payload)
  248. {
  249. flags_ |= BIT(2);
  250. }
  251. else
  252. {
  253. flags_ &= 0xFD;
  254. }
  255. }
  256. void requires_response(bool requires_response)
  257. {
  258. if (requires_response)
  259. {
  260. flags_ |= BIT(3);
  261. }
  262. else
  263. {
  264. flags_ &= 0xFB;
  265. }
  266. }
  267. bool endianess()
  268. {
  269. return (flags_ & BIT(1)) != 0;
  270. }
  271. bool payload()
  272. {
  273. return (flags_ & BIT(2)) != 0;
  274. }
  275. bool requires_response()
  276. {
  277. return (flags_ & BIT(3)) != 0;
  278. }
  279. static inline size_t size()
  280. {
  281. return 16;
  282. }
  283. };
  284. } // namespace rtps
  285. } // namespace fastdds
  286. } // namespace eprosima
  287. #endif // _FASTDDS_RTCP_HEADER_H_