ParticipantGenericMessage.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 ParticipantGenericMessage.h
  16. */
  17. #ifndef _FASTDDS_RTPS_SECURITY_COMMON_PARTICIPANTGENERICMESSAGE_H_
  18. #define _FASTDDS_RTPS_SECURITY_COMMON_PARTICIPANTGENERICMESSAGE_H_
  19. #include <fastdds/rtps/common/Guid.h>
  20. #include <fastdds/rtps/common/Token.h>
  21. namespace eprosima {
  22. namespace fastrtps {
  23. namespace rtps {
  24. namespace security {
  25. class MessageIdentity
  26. {
  27. public:
  28. MessageIdentity() : sequence_number_(0) {}
  29. MessageIdentity(const MessageIdentity& mi) :
  30. source_guid_(mi.source_guid_),
  31. sequence_number_(mi.sequence_number_)
  32. {
  33. }
  34. MessageIdentity(MessageIdentity&& mi) :
  35. source_guid_(std::move(mi.source_guid_)),
  36. sequence_number_(mi.sequence_number_)
  37. {
  38. }
  39. MessageIdentity& operator=(const MessageIdentity& mi)
  40. {
  41. source_guid_ = mi.source_guid_;
  42. sequence_number_ = mi.sequence_number_;
  43. return *this;
  44. }
  45. MessageIdentity& operator=(MessageIdentity&& mi)
  46. {
  47. source_guid_ = std::move(mi.source_guid_);
  48. sequence_number_ = mi.sequence_number_;
  49. return *this;
  50. }
  51. void source_guid(const GUID_t& source_guid)
  52. {
  53. source_guid_ = source_guid;
  54. }
  55. void source_guid(GUID_t&& source_guid)
  56. {
  57. source_guid_ = std::move(source_guid);
  58. }
  59. const GUID_t& source_guid() const
  60. {
  61. return source_guid_;
  62. }
  63. GUID_t& source_guid()
  64. {
  65. return source_guid_;
  66. }
  67. void sequence_number(int64_t sequence_number)
  68. {
  69. sequence_number_ = sequence_number;
  70. }
  71. int64_t sequence_number() const
  72. {
  73. return sequence_number_;
  74. }
  75. int64_t& sequence_number()
  76. {
  77. return sequence_number_;
  78. }
  79. private:
  80. GUID_t source_guid_;
  81. int64_t sequence_number_;
  82. };
  83. class MessageIdentityHelper
  84. {
  85. public:
  86. static size_t serialized_size(const MessageIdentity& /*message*/, size_t current_alignment = 0)
  87. {
  88. size_t initial_alignment = current_alignment;
  89. current_alignment += 16;
  90. current_alignment += alignment(current_alignment, 8) + 8;
  91. return current_alignment - initial_alignment;
  92. }
  93. private:
  94. inline static size_t alignment(size_t current_alignment, size_t dataSize) { return (dataSize - (current_alignment % dataSize)) & (dataSize-1);}
  95. };
  96. class ParticipantGenericMessage
  97. {
  98. public:
  99. ParticipantGenericMessage() {}
  100. ParticipantGenericMessage(const ParticipantGenericMessage& message) :
  101. message_identity_(message.message_identity_),
  102. related_message_identity_(message.related_message_identity_),
  103. destination_participant_key_(message.destination_participant_key_),
  104. destination_endpoint_key_(message.destination_endpoint_key_),
  105. source_endpoint_key_(message.source_endpoint_key_),
  106. message_class_id_(message.message_class_id_),
  107. message_data_(message.message_data_)
  108. {}
  109. ParticipantGenericMessage(ParticipantGenericMessage&& message) :
  110. message_identity_(std::move(message.message_identity_)),
  111. related_message_identity_(std::move(message.related_message_identity_)),
  112. destination_participant_key_(std::move(message.destination_participant_key_)),
  113. destination_endpoint_key_(std::move(message.destination_endpoint_key_)),
  114. source_endpoint_key_(std::move(message.source_endpoint_key_)),
  115. message_class_id_(std::move(message.message_class_id_)),
  116. message_data_(std::move(message.message_data_))
  117. {}
  118. ParticipantGenericMessage& operator=(const ParticipantGenericMessage& message)
  119. {
  120. message_identity_ = message.message_identity_;
  121. related_message_identity_ = message.related_message_identity_;
  122. destination_participant_key_ = message.destination_participant_key_;
  123. destination_endpoint_key_ = message.destination_endpoint_key_;
  124. source_endpoint_key_ = message.source_endpoint_key_;
  125. message_class_id_ = message.message_class_id_;
  126. message_data_ = message.message_data_;
  127. return *this;
  128. }
  129. ParticipantGenericMessage& operator=(ParticipantGenericMessage&& message)
  130. {
  131. message_identity_ = std::move(message.message_identity_);
  132. related_message_identity_ = std::move(message.related_message_identity_);
  133. destination_participant_key_ = std::move(message.destination_participant_key_);
  134. destination_endpoint_key_ = std::move(message.destination_endpoint_key_);
  135. source_endpoint_key_ = std::move(message.source_endpoint_key_);
  136. message_class_id_ = std::move(message.message_class_id_);
  137. message_data_ = std::move(message.message_data_);
  138. return *this;
  139. }
  140. void message_identity(const MessageIdentity& message_identity)
  141. {
  142. message_identity_ = message_identity;
  143. }
  144. void message_identity(MessageIdentity&& message_identity)
  145. {
  146. message_identity_ = std::move(message_identity);
  147. }
  148. const MessageIdentity& message_identity() const
  149. {
  150. return message_identity_;
  151. }
  152. MessageIdentity& message_identity()
  153. {
  154. return message_identity_;
  155. }
  156. void related_message_identity(const MessageIdentity& related_message_identity)
  157. {
  158. related_message_identity_ = related_message_identity;
  159. }
  160. void related_message_identity(MessageIdentity&& related_message_identity)
  161. {
  162. related_message_identity_ = std::move(related_message_identity);
  163. }
  164. const MessageIdentity& related_message_identity() const
  165. {
  166. return related_message_identity_;
  167. }
  168. MessageIdentity& related_message_identity()
  169. {
  170. return related_message_identity_;
  171. }
  172. void destination_participant_key(const GUID_t& destination_participant_key)
  173. {
  174. destination_participant_key_ = destination_participant_key;
  175. }
  176. void destination_participant_key(GUID_t&& destination_participant_key)
  177. {
  178. destination_participant_key_ = std::move(destination_participant_key);
  179. }
  180. const GUID_t& destination_participant_key() const
  181. {
  182. return destination_participant_key_;
  183. }
  184. GUID_t& destination_participant_key()
  185. {
  186. return destination_participant_key_;
  187. }
  188. void destination_endpoint_key(const GUID_t& destination_endpoint_key)
  189. {
  190. destination_endpoint_key_ = destination_endpoint_key;
  191. }
  192. void destination_endpoint_key(GUID_t&& destination_endpoint_key)
  193. {
  194. destination_endpoint_key_ = std::move(destination_endpoint_key);
  195. }
  196. const GUID_t& destination_endpoint_key() const
  197. {
  198. return destination_endpoint_key_;
  199. }
  200. GUID_t& destination_endpoint_key()
  201. {
  202. return destination_endpoint_key_;
  203. }
  204. void source_endpoint_key(const GUID_t& source_endpoint_key)
  205. {
  206. source_endpoint_key_ = source_endpoint_key;
  207. }
  208. void source_endpoint_key(GUID_t&& source_endpoint_key)
  209. {
  210. source_endpoint_key_ = std::move(source_endpoint_key);
  211. }
  212. const GUID_t& source_endpoint_key() const
  213. {
  214. return source_endpoint_key_;
  215. }
  216. GUID_t& source_endpoint_key()
  217. {
  218. return source_endpoint_key_;
  219. }
  220. void message_class_id(const std::string& message_class_id)
  221. {
  222. message_class_id_ = message_class_id;
  223. }
  224. void message_class_id(std::string&& message_class_id)
  225. {
  226. message_class_id_ = std::move(message_class_id);
  227. }
  228. const std::string& message_class_id() const
  229. {
  230. return message_class_id_;
  231. }
  232. std::string& message_class_id()
  233. {
  234. return message_class_id_;
  235. }
  236. void message_data(const DataHolderSeq& message_data)
  237. {
  238. message_data_ = message_data;
  239. }
  240. void message_data(DataHolderSeq&& message_data)
  241. {
  242. message_data_ = std::move(message_data);
  243. }
  244. const DataHolderSeq& message_data() const
  245. {
  246. return message_data_;
  247. }
  248. DataHolderSeq& message_data()
  249. {
  250. return message_data_;
  251. }
  252. private:
  253. MessageIdentity message_identity_;
  254. MessageIdentity related_message_identity_;
  255. GUID_t destination_participant_key_;
  256. GUID_t destination_endpoint_key_;
  257. GUID_t source_endpoint_key_;
  258. std::string message_class_id_;
  259. DataHolderSeq message_data_;
  260. };
  261. class ParticipantGenericMessageHelper
  262. {
  263. public:
  264. static size_t serialized_size(const ParticipantGenericMessage& message, size_t current_alignment = 0)
  265. {
  266. size_t initial_alignment = current_alignment;
  267. current_alignment += MessageIdentityHelper::serialized_size(message.message_identity(), current_alignment);
  268. current_alignment += MessageIdentityHelper::serialized_size(message.related_message_identity(), current_alignment);
  269. current_alignment += 16 * 3;
  270. current_alignment += 4 + alignment(current_alignment, 4) + message.message_class_id().size() + 1;
  271. current_alignment += DataHolderHelper::serialized_size(message.message_data(), current_alignment);
  272. return current_alignment - initial_alignment;
  273. }
  274. private:
  275. inline static size_t alignment(size_t current_alignment, size_t dataSize) { return (dataSize - (current_alignment % dataSize)) & (dataSize-1);}
  276. };
  277. } //namespace security
  278. } //namespace rtps
  279. } //namespace fastrtps
  280. } //namespace eprosima
  281. #endif // _FASTDDS_RTPS_SECURITY_COMMON_PARTICIPANTGENERICMESSAGE_H_