SerializedPayload.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 SerializedPayload.h
  16. */
  17. #ifndef _FASTDDS_RTPS_SERIALIZEDPAYLOAD_H_
  18. #define _FASTDDS_RTPS_SERIALIZEDPAYLOAD_H_
  19. #include <fastrtps/fastrtps_dll.h>
  20. #include <fastdds/rtps/common/Types.h>
  21. #include <cstring>
  22. #include <new>
  23. #include <stdexcept>
  24. #include <stdint.h>
  25. #include <stdlib.h>
  26. /*!
  27. * @brief Maximum payload is maximum of UDP packet size minus 536bytes (RTPSMESSAGE_COMMON_RTPS_PAYLOAD_SIZE)
  28. * With those 536 bytes (RTPSMESSAGE_COMMON_RTPS_PAYLOAD_SIZE) bytes is posible to send RTPS Header plus RTPS Data submessage plus RTPS Heartbeat submessage.
  29. */
  30. namespace eprosima {
  31. namespace fastrtps {
  32. namespace rtps {
  33. //Pre define data encapsulation schemes
  34. #define CDR_BE 0x0000
  35. #define CDR_LE 0x0001
  36. #define PL_CDR_BE 0x0002
  37. #define PL_CDR_LE 0x0003
  38. //!@brief Structure SerializedPayload_t.
  39. //!@ingroup COMMON_MODULE
  40. struct RTPS_DllAPI SerializedPayload_t
  41. {
  42. //!Encapsulation of the data as suggested in the RTPS 2.1 specification chapter 10.
  43. uint16_t encapsulation;
  44. //!Actual length of the data
  45. uint32_t length;
  46. //!Pointer to the data.
  47. octet* data;
  48. //!Maximum size of the payload
  49. uint32_t max_size;
  50. //!Position when reading
  51. uint32_t pos;
  52. //!Default constructor
  53. SerializedPayload_t()
  54. : encapsulation(CDR_BE)
  55. , length(0)
  56. , data(nullptr)
  57. , max_size(0)
  58. , pos(0)
  59. {
  60. }
  61. /**
  62. * @param len Maximum size of the payload
  63. */
  64. SerializedPayload_t(
  65. uint32_t len)
  66. : SerializedPayload_t()
  67. {
  68. this->reserve(len);
  69. }
  70. ~SerializedPayload_t()
  71. {
  72. this->empty();
  73. }
  74. bool operator == (
  75. const SerializedPayload_t& other) const
  76. {
  77. return ((encapsulation == other.encapsulation) &&
  78. (length == other.length) &&
  79. (0 == memcmp(data, other.data, length)));
  80. }
  81. /*!
  82. * Copy another structure (including allocating new space for the data.)
  83. * @param[in] serData Pointer to the structure to copy
  84. * @param with_limit if true, the function will fail when providing a payload too big
  85. * @return True if correct
  86. */
  87. bool copy(
  88. const SerializedPayload_t* serData,
  89. bool with_limit = true)
  90. {
  91. length = serData->length;
  92. if (serData->length > max_size)
  93. {
  94. if (with_limit)
  95. {
  96. return false;
  97. }
  98. else
  99. {
  100. this->reserve(serData->length);
  101. }
  102. }
  103. encapsulation = serData->encapsulation;
  104. memcpy(data, serData->data, length);
  105. return true;
  106. }
  107. /*!
  108. * Allocate new space for fragmented data
  109. * @param[in] serData Pointer to the structure to copy
  110. * @return True if correct
  111. */
  112. bool reserve_fragmented(
  113. SerializedPayload_t* serData)
  114. {
  115. length = serData->length;
  116. max_size = serData->length;
  117. encapsulation = serData->encapsulation;
  118. data = (octet*)calloc(length, sizeof(octet));
  119. return true;
  120. }
  121. //! Empty the payload
  122. void empty()
  123. {
  124. length = 0;
  125. encapsulation = CDR_BE;
  126. max_size = 0;
  127. if (data != nullptr)
  128. {
  129. free(data);
  130. }
  131. data = nullptr;
  132. }
  133. void reserve(
  134. uint32_t new_size)
  135. {
  136. if (new_size <= this->max_size)
  137. {
  138. return;
  139. }
  140. if (data == nullptr)
  141. {
  142. data = (octet*)calloc(new_size, sizeof(octet));
  143. if (!data)
  144. {
  145. throw std::bad_alloc();
  146. }
  147. }
  148. else
  149. {
  150. void* old_data = data;
  151. data = (octet*)realloc(data, new_size);
  152. if (!data)
  153. {
  154. free(old_data);
  155. throw std::bad_alloc();
  156. }
  157. }
  158. max_size = new_size;
  159. }
  160. };
  161. } /* namespace rtps */
  162. } /* namespace fastrtps */
  163. } /* namespace eprosima */
  164. #endif /* _FASTDDS_RTPS_SERIALIZEDPAYLOAD_H_ */