CDRMessage.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // Copyright 2016 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. /**
  15. * @file CDRMessage.h
  16. */
  17. #ifndef _FASTDDS_RTPS_CDRMESSAGE_H_
  18. #define _FASTDDS_RTPS_CDRMESSAGE_H_
  19. #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
  20. #include <fastdds/rtps/common/CDRMessage_t.h>
  21. #include <fastdds/rtps/common/Property.h>
  22. #include <fastdds/rtps/common/BinaryProperty.h>
  23. #include <fastdds/rtps/common/SequenceNumber.h>
  24. #include <fastdds/rtps/common/FragmentNumber.h>
  25. #include <fastdds/rtps/common/SampleIdentity.h>
  26. #include <fastdds/rtps/common/Time_t.h>
  27. #include <fastdds/rtps/common/Locator.h>
  28. #include <fastrtps/utils/fixed_size_string.hpp>
  29. #include <fastdds/rtps/security/common/ParticipantGenericMessage.h>
  30. namespace eprosima {
  31. namespace fastrtps {
  32. namespace rtps {
  33. /**
  34. * Namespace CDRMessage, contains inline methods to initialize CDRMessage_t and add or read different data types.
  35. @ingroup COMMON_MODULE
  36. */
  37. namespace CDRMessage {
  38. /** @name Read from a CDRMessage_t.
  39. * Methods to read different data types from a CDR message. Pointers to the message and to the data types are provided.
  40. * The read position is updated in the message. It fails if you attempt to read outside the
  41. * boundaries of the message.
  42. * @param[in] msg Pointer to message.
  43. * @param[out] data_ptr Pointer to data.
  44. * @param[in] size Number of bytes (if necessary).
  45. * @return True if correct.
  46. */
  47. /// @{
  48. inline bool readEntityId(
  49. CDRMessage_t* msg,
  50. EntityId_t* id);
  51. inline bool readData(
  52. CDRMessage_t* msg,
  53. octet* o,
  54. uint32_t length);
  55. inline bool read_array_with_max_size(
  56. CDRMessage_t* msg,
  57. octet* arr,
  58. size_t max_size);
  59. inline bool readDataReversed(
  60. CDRMessage_t* msg,
  61. octet* o,
  62. uint32_t length);
  63. inline bool readInt32(
  64. CDRMessage_t* msg,
  65. int32_t* lo);
  66. inline bool readUInt32(
  67. CDRMessage_t* msg,
  68. uint32_t* ulo);
  69. inline bool readInt64(
  70. CDRMessage_t* msg,
  71. int64_t* lolo);
  72. inline bool readSequenceNumber(
  73. CDRMessage_t* msg,
  74. SequenceNumber_t* sn);
  75. inline bool readInt16(
  76. CDRMessage_t* msg,
  77. int16_t* i16);
  78. inline bool readUInt16(
  79. CDRMessage_t* msg,
  80. uint16_t* i16);
  81. inline bool readLocator(
  82. CDRMessage_t* msg,
  83. Locator_t* loc);
  84. inline bool readOctet(
  85. CDRMessage_t* msg,
  86. octet* o);
  87. inline SequenceNumberSet_t readSequenceNumberSet(
  88. CDRMessage_t* msg);
  89. inline bool readFragmentNumberSet(
  90. CDRMessage_t* msg,
  91. FragmentNumberSet_t* snset);
  92. inline bool readTimestamp(
  93. CDRMessage_t* msg,
  94. Time_t* ts);
  95. inline bool readString(
  96. CDRMessage_t* msg,
  97. std::string* p_str);
  98. inline bool readString(
  99. CDRMessage_t* msg,
  100. string_255* stri);
  101. inline bool readOctetVector(
  102. CDRMessage_t* msg,
  103. std::vector<octet>* ocvec);
  104. inline bool readProperty(
  105. CDRMessage_t* msg,
  106. Property& property);
  107. inline bool readBinaryProperty(
  108. CDRMessage_t* msg,
  109. BinaryProperty& binary_property);
  110. inline bool readPropertySeq(
  111. CDRMessage_t* msg,
  112. PropertySeq& properties);
  113. inline bool readBinaryPropertySeq(
  114. CDRMessage_t* msg,
  115. BinaryPropertySeq& binary_properties);
  116. inline bool readDataHolder(
  117. CDRMessage_t* msg,
  118. DataHolder& data_holder);
  119. inline bool readDataHolderSeq(
  120. CDRMessage_t* msg,
  121. DataHolderSeq& data_holders);
  122. inline bool readMessageIdentity(
  123. CDRMessage_t* msg,
  124. security::MessageIdentity& message_identity);
  125. inline bool readParticipantGenericMessage(
  126. CDRMessage_t* msg,
  127. security::ParticipantGenericMessage& message);
  128. ///@}
  129. /**
  130. * Initialize given CDR message with default size. It frees the memory already allocated and reserves new one.
  131. * @param[in,out] msg Pointer to the message to initialize.
  132. * @param data_size Size of the data the message is suppose to carry
  133. * @return True if correct.
  134. */
  135. inline bool initCDRMsg(
  136. CDRMessage_t* msg,
  137. uint32_t data_size = RTPSMESSAGE_COMMON_DATA_PAYLOAD_SIZE);
  138. inline bool wrapVector(
  139. CDRMessage_t* msg,
  140. std::vector<octet>& vectorToWrap);
  141. /**
  142. * Append given CDRMessage to existing CDR Message. Joins two messages into the first one if it has space.
  143. * @param[out] first Pointer to first message.
  144. * @param[in] second Pointer to second message.
  145. ** @return True if correct.
  146. */
  147. inline bool appendMsg(
  148. CDRMessage_t* first,
  149. CDRMessage_t* second);
  150. /** @name Add to a CDRMessage_t.
  151. * Methods to add different data types to a CDR message. Pointers to the message and to the data types are provided.
  152. * The write position is updated in the message. It fails if you attempt to write outside the
  153. * boundaries of the message.
  154. * @param[in,out] Pointer to message.
  155. * @param[in] data Data to add (might be a pointer).
  156. * @param[in] byteSize Number of bytes (if necessary).
  157. * @return True if correct.
  158. */
  159. /// @{
  160. inline bool addData(
  161. CDRMessage_t*,
  162. const octet*,
  163. const uint32_t number_bytes);
  164. inline bool addDataReversed(
  165. CDRMessage_t*,
  166. const octet*,
  167. const uint32_t byte_number);
  168. inline bool addOctet(
  169. CDRMessage_t* msg,
  170. octet o);
  171. inline bool addUInt16(
  172. CDRMessage_t* msg,
  173. uint16_t us);
  174. inline bool addInt32(
  175. CDRMessage_t* msg,
  176. int32_t lo);
  177. inline bool addUInt32(
  178. CDRMessage_t* msg,
  179. uint32_t lo);
  180. inline bool addInt64(
  181. CDRMessage_t* msg,
  182. int64_t lo);
  183. inline bool addEntityId(
  184. CDRMessage_t* msg,
  185. const EntityId_t* id);
  186. inline bool addSequenceNumber(
  187. CDRMessage_t* msg,
  188. const SequenceNumber_t* sn);
  189. inline bool addSequenceNumberSet(
  190. CDRMessage_t* msg,
  191. const SequenceNumberSet_t* sns);
  192. inline bool addFragmentNumberSet(
  193. CDRMessage_t* msg,
  194. FragmentNumberSet_t* fns);
  195. inline bool addLocator(
  196. CDRMessage_t* msg,
  197. const Locator_t& loc);
  198. inline bool add_string(
  199. CDRMessage_t* msg,
  200. const char* in_str);
  201. inline bool add_string(
  202. CDRMessage_t* msg,
  203. const std::string& in_str);
  204. inline bool add_string(
  205. CDRMessage_t* msg,
  206. const string_255& in_str);
  207. inline bool addOctetVector(
  208. CDRMessage_t* msg,
  209. const std::vector<octet>* ocvec,
  210. bool add_final_padding = true);
  211. inline bool addProperty(
  212. CDRMessage_t* msg,
  213. const Property& property);
  214. inline bool addBinaryProperty(
  215. CDRMessage_t* msg,
  216. const BinaryProperty& binary_property,
  217. bool add_final_padding = true);
  218. inline bool addPropertySeq(
  219. CDRMessage_t* msg,
  220. const PropertySeq& properties);
  221. inline bool addBinaryPropertySeq(
  222. CDRMessage_t* msg,
  223. const BinaryPropertySeq& binary_properties,
  224. bool add_final_padding);
  225. inline bool addBinaryPropertySeq(
  226. CDRMessage_t* msg,
  227. const BinaryPropertySeq& binary_properties,
  228. const std::string& name_start,
  229. bool add_final_padding);
  230. inline bool addDataHolder(
  231. CDRMessage_t* msg,
  232. const DataHolder& data_holder);
  233. inline bool addDataHolderSeq(
  234. CDRMessage_t* msg,
  235. const DataHolderSeq& data_holders);
  236. inline bool addMessageIdentity(
  237. CDRMessage_t* msg,
  238. const security::MessageIdentity& message_identity);
  239. inline bool addParticipantGenericMessage(
  240. CDRMessage_t* msg,
  241. const security::ParticipantGenericMessage& message);
  242. ///@}
  243. } /* namespace CDRMessage */
  244. } /* namespace rtps */
  245. } /* namespace fastrtps */
  246. } /* namespace eprosima */
  247. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  248. #include <fastdds/rtps/messages/CDRMessage.hpp>
  249. #endif /* DOXYGEN_SHOULD_SKIP_THIS */
  250. #endif
  251. #endif /* _FASTDDS_RTPS_CDRMESSAGE_H_ */