CDRMessage_t.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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_t.h
  16. */
  17. #ifndef _FASTDDS_RTPS_CDRMESSAGE_T_H_
  18. #define _FASTDDS_RTPS_CDRMESSAGE_T_H_
  19. #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
  20. #include <fastdds/rtps/common/SerializedPayload.h>
  21. #include <fastdds/rtps/common/Types.h>
  22. #include <cassert>
  23. #include <cstdlib>
  24. #include <cstring>
  25. namespace eprosima {
  26. namespace fastrtps {
  27. namespace rtps {
  28. //!Max size of RTPS message in bytes.
  29. #define RTPSMESSAGE_DEFAULT_SIZE 10500 //max size of rtps message in bytes
  30. #define RTPSMESSAGE_COMMON_RTPS_PAYLOAD_SIZE 536 //common payload a rtps message has TODO(Ricardo) It is necessary?
  31. #define RTPSMESSAGE_COMMON_DATA_PAYLOAD_SIZE 10000 //common data size
  32. #define RTPSMESSAGE_HEADER_SIZE 20 //header size in bytes
  33. #define RTPSMESSAGE_SUBMESSAGEHEADER_SIZE 4
  34. #define RTPSMESSAGE_DATA_EXTRA_INLINEQOS_SIZE 4
  35. #define RTPSMESSAGE_INFOTS_SIZE 12
  36. #define RTPSMESSAGE_OCTETSTOINLINEQOS_DATASUBMSG 16 //may change in future versions
  37. #define RTPSMESSAGE_OCTETSTOINLINEQOS_DATAFRAGSUBMSG 28 //may change in future versions
  38. #define RTPSMESSAGE_DATA_MIN_LENGTH 24
  39. /**
  40. * @brief Structure CDRMessage_t, contains a serialized message.
  41. * @ingroup COMMON_MODULE
  42. */
  43. struct RTPS_DllAPI CDRMessage_t final
  44. {
  45. // TODO(Miguel C): Deprecate when not used in mocks
  46. CDRMessage_t()
  47. : CDRMessage_t(RTPSMESSAGE_DEFAULT_SIZE)
  48. {
  49. }
  50. ~CDRMessage_t()
  51. {
  52. if (buffer != nullptr && !wraps)
  53. {
  54. free(buffer);
  55. }
  56. }
  57. /**
  58. * Constructor with maximum size
  59. * @param size Maximum size
  60. */
  61. explicit CDRMessage_t(
  62. uint32_t size)
  63. {
  64. wraps = false;
  65. pos = 0;
  66. length = 0;
  67. if (size != 0)
  68. {
  69. buffer = (octet*)malloc(size);
  70. }
  71. else
  72. {
  73. buffer = nullptr;
  74. }
  75. max_size = size;
  76. reserved_size = size;
  77. #if __BIG_ENDIAN__
  78. msg_endian = BIGEND;
  79. #else
  80. msg_endian = LITTLEEND;
  81. #endif
  82. }
  83. /**
  84. * Constructor to wrap a serialized payload
  85. * @param payload Payload to wrap
  86. */
  87. explicit CDRMessage_t(
  88. const SerializedPayload_t& payload)
  89. : wraps(true)
  90. {
  91. msg_endian = payload.encapsulation == PL_CDR_BE ? BIGEND : LITTLEEND;
  92. pos = payload.pos;
  93. length = payload.length;
  94. buffer = payload.data;
  95. max_size = payload.max_size;
  96. reserved_size = payload.max_size;
  97. }
  98. CDRMessage_t(
  99. const CDRMessage_t& message)
  100. {
  101. wraps = false;
  102. pos = 0;
  103. length = message.length;
  104. max_size = message.max_size;
  105. msg_endian = message.msg_endian;
  106. reserved_size = max_size;
  107. if (max_size != 0)
  108. {
  109. buffer = (octet*)malloc(max_size);
  110. memcpy(buffer, message.buffer, length);
  111. }
  112. else
  113. {
  114. buffer = nullptr;
  115. }
  116. }
  117. CDRMessage_t(
  118. CDRMessage_t&& message)
  119. {
  120. wraps = message.wraps;
  121. message.wraps = false;
  122. pos = message.pos;
  123. message.pos = 0;
  124. length = message.length;
  125. message.length = 0;
  126. max_size = message.max_size;
  127. message.max_size = 0;
  128. reserved_size = message.reserved_size;
  129. message.reserved_size = 0;
  130. msg_endian = message.msg_endian;
  131. #if __BIG_ENDIAN__
  132. message.msg_endian = BIGEND;
  133. #else
  134. message.msg_endian = LITTLEEND;
  135. #endif
  136. buffer = message.buffer;
  137. message.buffer = nullptr;
  138. }
  139. CDRMessage_t& operator =(
  140. CDRMessage_t&& message)
  141. {
  142. wraps = message.wraps;
  143. message.wraps = false;
  144. pos = message.pos;
  145. message.pos = 0;
  146. length = message.length;
  147. message.length = 0;
  148. max_size = message.max_size;
  149. message.max_size = 0;
  150. reserved_size = message.reserved_size;
  151. message.reserved_size = 0;
  152. msg_endian = message.msg_endian;
  153. #if __BIG_ENDIAN__
  154. message.msg_endian = BIGEND;
  155. #else
  156. message.msg_endian = LITTLEEND;
  157. #endif
  158. buffer = message.buffer;
  159. message.buffer = nullptr;
  160. return *(this);
  161. }
  162. void init(
  163. octet* buffer_ptr,
  164. uint32_t size)
  165. {
  166. assert(buffer == nullptr);
  167. wraps = true;
  168. pos = 0;
  169. length = 0;
  170. buffer = buffer_ptr;
  171. max_size = size;
  172. reserved_size = size;
  173. #if __BIG_ENDIAN__
  174. msg_endian = BIGEND;
  175. #else
  176. msg_endian = LITTLEEND;
  177. #endif
  178. }
  179. void reserve(
  180. uint32_t size)
  181. {
  182. assert(wraps == false);
  183. if (size > reserved_size)
  184. {
  185. octet* new_buffer = (octet*) realloc(buffer, size);
  186. if (new_buffer == nullptr)
  187. {
  188. // TODO: Exception? Assertion?
  189. }
  190. else
  191. {
  192. buffer = new_buffer;
  193. reserved_size = size;
  194. }
  195. }
  196. max_size = size;
  197. }
  198. //!Pointer to the buffer where the data is stored.
  199. octet* buffer;
  200. //!Read or write position.
  201. uint32_t pos;
  202. //!Max size of the message.
  203. uint32_t max_size;
  204. //!Size allocated on buffer. May be higher than max_size.
  205. uint32_t reserved_size;
  206. //!Current length of the message.
  207. uint32_t length;
  208. //!Endianness of the message.
  209. Endianness_t msg_endian;
  210. //Whether this message is wrapping a buffer managed elsewhere.
  211. bool wraps;
  212. };
  213. } // namespace rtps
  214. } // namespace fastrtps
  215. } // namespace eprosima
  216. #endif /* DOXYGEN_SHOULD_SKIP_THIS_PUBLIC */
  217. #endif /* _FASTDDS_RTPS_CDRMESSAGE_T_H_ */