DataWriter.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. /**
  15. * @file DataWriter.hpp
  16. */
  17. #ifndef _FASTRTPS_DATAWRITER_HPP_
  18. #define _FASTRTPS_DATAWRITER_HPP_
  19. #include <fastdds/rtps/common/Time_t.h>
  20. #include <fastrtps/qos/DeadlineMissedStatus.h>
  21. #include <fastdds/dds/core/status/BaseStatus.hpp>
  22. #include <fastrtps/types/TypesBase.h>
  23. #include <fastdds/dds/core/Entity.hpp>
  24. #include <fastdds/dds/core/status/StatusMask.hpp>
  25. #include <fastdds/dds/publisher/qos/DataWriterQos.hpp>
  26. using eprosima::fastrtps::types::ReturnCode_t;
  27. namespace eprosima {
  28. namespace fastrtps {
  29. class TopicAttributes;
  30. namespace rtps {
  31. class WriteParams;
  32. class WriterAttributes;
  33. struct InstanceHandle_t;
  34. struct GUID_t;
  35. } // namespace rtps
  36. } // namespace fastrtps
  37. namespace fastdds {
  38. namespace dds {
  39. class PublisherListener;
  40. class PublisherImpl;
  41. class Publisher;
  42. class TypeSupport;
  43. class DataWriterImpl;
  44. class DataWriterListener;
  45. class DataWriterQos;
  46. class Topic;
  47. /**
  48. * Class DataWriter, contains the actual implementation of the behaviour of the DataWriter.
  49. * @ingroup FASTDDS_MODULE
  50. */
  51. class DataWriter : public DomainEntity
  52. {
  53. friend class PublisherImpl;
  54. friend class DataWriterImpl;
  55. /**
  56. * Create a data writer, assigning its pointer to the associated writer.
  57. * Don't use directly, create Publisher using DomainRTPSParticipant static function.
  58. */
  59. RTPS_DllAPI DataWriter(
  60. DataWriterImpl* impl,
  61. const StatusMask& mask = StatusMask::all());
  62. RTPS_DllAPI DataWriter(
  63. Publisher* pub,
  64. Topic* topic,
  65. const DataWriterQos& qos = DATAWRITER_QOS_DEFAULT,
  66. DataWriterListener* listener = nullptr,
  67. const StatusMask& mask = StatusMask::all());
  68. public:
  69. RTPS_DllAPI virtual ~DataWriter();
  70. /**
  71. * @brief This operation enables the DataWriter
  72. * @return RETCODE_OK is successfully enabled. RETCODE_PRECONDITION_NOT_MET if the Publisher creating this
  73. * DataWriter is not enabled.
  74. */
  75. RTPS_DllAPI ReturnCode_t enable() override;
  76. /**
  77. * Write data to the topic.
  78. * @param data Pointer to the data
  79. * @return True if correct, false otherwise
  80. */
  81. RTPS_DllAPI bool write(
  82. void* data);
  83. /**
  84. * Write data with params to the topic.
  85. * @param data Pointer to the data
  86. * @param params Extra write parameters.
  87. * @return True if correct, false otherwise
  88. */
  89. RTPS_DllAPI bool write(
  90. void* data,
  91. fastrtps::rtps::WriteParams& params);
  92. /**
  93. * Write data with handle.
  94. *
  95. * The special value HANDLE_NIL can be used for the parameter handle.This indicates that the identity of the
  96. * instance should be automatically deduced from the instance_data (by means of the key).
  97. *
  98. * @param data Pointer to the data
  99. * @param handle InstanceHandle_t.
  100. * @return RETCODE_PRECONDITION_NOT_MET if the handle introduced does not match with the one associated to the data,
  101. * RETCODE_OK if the data is correctly sent and RETCODE_ERROR otherwise.
  102. */
  103. RTPS_DllAPI ReturnCode_t write(
  104. void* data,
  105. const fastrtps::rtps::InstanceHandle_t& handle);
  106. /*!
  107. * @brief Informs that the application will be modifying a particular instance.
  108. * It gives an opportunity to the middleware to pre-configure itself to improve performance.
  109. * @param[in] instance Sample used to get the instance's key.
  110. * @return Handle containing the instance's key.
  111. * This handle could be used in successive `write` or `dispose` operations.
  112. * In case of error, HANDLE_NIL will be returned.
  113. */
  114. RTPS_DllAPI fastrtps::rtps::InstanceHandle_t register_instance(
  115. void* instance);
  116. /*!
  117. * @brief This operation reverses the action of `register_instance`.
  118. * It should only be called on an instance that is currently registered.
  119. * Informs the middleware that the DataWriter is not intending to modify any more of that data instance.
  120. * Also indicates that the middleware can locally remove all information regarding that instance.
  121. * @param[in] instance Sample used to deduce instance's key in case of `handle` parameter is HANDLE_NIL.
  122. * @param[in] handle Instance's key to be unregistered.
  123. * @return Returns the operation's result.
  124. * If the operation finishes successfully, ReturnCode_t::RETCODE_OK is returned.
  125. */
  126. RTPS_DllAPI ReturnCode_t unregister_instance(
  127. void* instance,
  128. const fastrtps::rtps::InstanceHandle_t& handle);
  129. /**
  130. * Returns the DataWriter's GUID
  131. * @return Reference to the DataWriter GUID
  132. */
  133. RTPS_DllAPI const fastrtps::rtps::GUID_t& guid();
  134. /**
  135. * Returns the DataWriter's InstanceHandle
  136. * @return Copy of the DataWriter InstanceHandle
  137. */
  138. RTPS_DllAPI fastrtps::rtps::InstanceHandle_t get_instance_handle() const;
  139. /**
  140. * Get data type associated to the DataWriter
  141. * @return Copy of the TypeSupport
  142. */
  143. RTPS_DllAPI TypeSupport get_type() const;
  144. /**
  145. * Waits the current thread until all writers have received their acknowledgments.
  146. * @param max_wait Maximum blocking time for this operation
  147. * @return RETCODE_OK if the DataWriter receive the acknowledgments before the time expires and RETCODE_ERROR otherwise
  148. */
  149. RTPS_DllAPI ReturnCode_t wait_for_acknowledgments(
  150. const fastrtps::Duration_t& max_wait);
  151. /**
  152. * @brief Returns the offered deadline missed status
  153. * @param status Deadline missed status struct
  154. * @return RETCODE_OK
  155. */
  156. RTPS_DllAPI ReturnCode_t get_offered_deadline_missed_status(
  157. fastrtps::OfferedDeadlineMissedStatus& status);
  158. /**
  159. * Establishes the DataWriterQos for this DataWriter.
  160. * @param qos DataWriterQos to be set
  161. * @return RETCODE_IMMUTABLE_POLICY if any of the Qos cannot be changed, RETCODE_INCONSISTENT_POLICY if the Qos is not
  162. * self consistent and RETCODE_OK if the qos is changed correctly.
  163. */
  164. RTPS_DllAPI ReturnCode_t set_qos(
  165. const DataWriterQos& qos);
  166. /**
  167. * Retrieves the DataWriterQos for this DataWriter.
  168. * @return Reference to the current DataWriterQos
  169. */
  170. RTPS_DllAPI const DataWriterQos& get_qos() const;
  171. /**
  172. * Fills the DataWriterQos with the values of this DataWriter.
  173. * @param qos DataWriterQos object where the qos is returned.
  174. * @return RETCODE_OK
  175. */
  176. RTPS_DllAPI ReturnCode_t get_qos(
  177. DataWriterQos& qos) const;
  178. /**
  179. * Retrieves the topic for this DataWriter.
  180. * @return Pointer to the associated Topic
  181. */
  182. RTPS_DllAPI Topic* get_topic() const;
  183. /**
  184. * Retrieves the listener for this DataWriter.
  185. * @return Pointer to the DataWriterListener
  186. */
  187. RTPS_DllAPI const DataWriterListener* get_listener() const;
  188. /**
  189. * Establishes the listener for this DataWriter.
  190. * @param listener Pointer to DataWriterListener to be set
  191. * @return RETCODE_OK
  192. */
  193. RTPS_DllAPI ReturnCode_t set_listener(
  194. DataWriterListener* listener);
  195. /* TODO
  196. bool get_key_value(
  197. void* key_holder,
  198. const fastrtps::rtps::InstanceHandle_t& handle);
  199. */
  200. /**
  201. * @brief This operation requests the middleware to delete the data (the actual deletion is postponed until there is no
  202. * more use for that data in the whole system). In general, applications are made aware of the deletion by means of
  203. * operations on the DataReader objects that already knew that instance. This operation does not modify the value of
  204. * the instance. The instance parameter is passed just for the purposes of identifying the instance.
  205. * When this operation is used, the Service will automatically supply the value of the source_timestamp that is made
  206. * available to DataReader objects by means of the source_timestamp attribute inside the SampleInfo. The constraints
  207. * on the values of the handle parameter and the corresponding error behavior are the same specified for the
  208. * unregister_instance operation.
  209. * @param[in] data Sample used to deduce instance's key in case of `handle` parameter is HANDLE_NIL.
  210. * @param[in] handle InstanceHandle of the data
  211. * @return RETCODE_PRECONDITION_NOT_MET if the handle introduced does not match with the one associated to the data,
  212. * RETCODE_OK if the data is correctly sent and RETCODE_ERROR otherwise.
  213. */
  214. RTPS_DllAPI ReturnCode_t dispose(
  215. void* data,
  216. const fastrtps::rtps::InstanceHandle_t& handle);
  217. /**
  218. * @brief Returns the liveliness lost status
  219. * @param status Liveliness lost status struct
  220. * @return RETCODE_OK
  221. */
  222. RTPS_DllAPI ReturnCode_t get_liveliness_lost_status(
  223. LivelinessLostStatus& status);
  224. /* TODO
  225. bool get_offered_incompatible_qos_status(
  226. OfferedIncompatibleQosStatus& status)
  227. {
  228. // Not implemented
  229. (void)status;
  230. return false;
  231. }
  232. */
  233. /**
  234. * @brief Getter for the Publisher that creates this DataWriter
  235. * @return Pointer to the Publisher
  236. */
  237. RTPS_DllAPI const Publisher* get_publisher() const;
  238. /**
  239. * @brief This operation manually asserts the liveliness of the DataWriter. This is used in combination with the
  240. * LivelinessQosPolicy to indicate to the Service that the entity remains active.
  241. * This operation need only be used if the LIVELINESS setting is either MANUAL_BY_PARTICIPANT or MANUAL_BY_TOPIC.
  242. * Otherwise, it has no effect.
  243. * @note Writing data via the write operation on a DataWriter asserts liveliness on the DataWriter itself and its
  244. * DomainParticipant. Consequently the use of assert_liveliness is only needed if the application is not writing data
  245. * regularly.
  246. * @return RETCODE_OK if asserted, RETCODE_ERROR otherwise
  247. */
  248. RTPS_DllAPI ReturnCode_t assert_liveliness();
  249. /**
  250. * @brief Clears the DataWriter history
  251. * @param removed size_t pointer to return the size of the data removed
  252. * @return RETCODE_OK if the samples are removed and RETCODE_ERROR otherwise
  253. */
  254. RTPS_DllAPI ReturnCode_t clear_history(
  255. size_t* removed);
  256. private:
  257. DataWriterImpl* impl_;
  258. };
  259. } /* namespace dds */
  260. } /* namespace fastdds */
  261. } /* namespace eprosima */
  262. #endif //_FASTRTPS_DATAWRITER_HPP_