@@ -147,16 +147,29 @@ struct DataMsgUtils
147147 msg, change->write_params .original_writer_info ());
148148 }
149149
150- if (WITH_KEY == topicKind && (!change->writerGUID .is_builtin () || expectsInlineQos || ALIVE != change->kind ))
150+ if (WITH_KEY == topicKind && change->instanceHandle .isDefined () &&
151+ (!change->writerGUID .is_builtin () || expectsInlineQos || ALIVE != change->kind ))
151152 {
153+ /* *
154+ * If instanceHandle is not defined, this means the key hash is not populated. It makes no sense
155+ * to serialize a parameter with an undefined or empty value because it could be interpreted as
156+ * a 'valid' key hash on the reader side
157+ **/
152158 fastdds::dds::ParameterSerializer<fastdds::dds::Parameter_t>::add_parameter_key (msg,
153159 change->instanceHandle );
154160
161+ /* * Changes like UNREGISTER or DISPOSE must include the key, they can't be sent without key */
155162 if (ALIVE != change->kind )
156163 {
157164 fastdds::dds::ParameterSerializer<fastdds::dds::Parameter_t>::add_parameter_status (msg, status);
158165 }
159166 }
167+ else if (ALIVE != change->kind && change->serializedPayload .length > 0 )
168+ {
169+ // This case is added in order to support DISPOSE or UNREGISTER changes when the data
170+ // is passed in the payload. Thus the KEY_HASH inline QoS is not compulsory for us
171+ fastdds::dds::ParameterSerializer<fastdds::dds::Parameter_t>::add_parameter_status (msg, status);
172+ }
160173
161174 if (inlineQos != nullptr )
162175 {
@@ -257,6 +270,22 @@ bool RTPSMessageCreator::addSubmessageData(
257270 // Add INLINE QOS AND SERIALIZED PAYLOAD DEPENDING ON FLAGS:
258271 if (inlineQosFlag) // inlineQoS
259272 {
273+ if (WITH_KEY == topicKind &&
274+ change->instanceHandle .isDefined () == false &&
275+ change->kind != ALIVE && change->serializedPayload .length == 0 )
276+ {
277+ // Instance handle is required but not defined
278+ EPROSIMA_LOG_ERROR (RTPS_WRITER,
279+ " DISPOSE or UNREGISTER Changes in KEYED Writers need a valid instanceHandle or the payload to be transmitted. Message won't be serialized" );
280+ return false ;
281+ }
282+ if (WITH_KEY == topicKind &&
283+ change->instanceHandle .isDefined () == false )
284+ {
285+ // Instance handle should be defined but is not compulsory, this is just a warning
286+ EPROSIMA_LOG_WARNING (RTPS_WRITER,
287+ " Change does not have a valid instanceHandle. KEY_HASH will not be serialized." );
288+ }
260289 DataMsgUtils::serialize_inline_qos (msg, change, topicKind, expectsInlineQos, inlineQos, status);
261290 }
262291
@@ -292,11 +321,16 @@ bool RTPSMessageCreator::addSubmessageData(
292321 }
293322
294323 added_no_error &= CDRMessage::addUInt16 (msg, 0 ); // ENCAPSULATION OPTIONS
295- added_no_error &=
296- fastdds::dds::ParameterSerializer<fastdds::dds::Parameter_t>::add_parameter_key (msg,
297- change->instanceHandle );
298- added_no_error &=
299- fastdds::dds::ParameterSerializer<fastdds::dds::Parameter_t>::add_parameter_status (msg,
324+
325+ if (change->instanceHandle .isDefined ())
326+ {
327+ // Even if requested in the serializedPayload, it makes no sense an undefined key hash
328+ added_no_error &=
329+ fastdds::dds::ParameterSerializer<fastdds::dds::Parameter_t>::add_parameter_key (msg,
330+ change->instanceHandle );
331+ }
332+
333+ added_no_error &= fastdds::dds::ParameterSerializer<fastdds::dds::Parameter_t>::add_parameter_status (msg,
300334 status);
301335 added_no_error &= fastdds::dds::ParameterSerializer<fastdds::dds::Parameter_t>::add_parameter_sentinel (msg);
302336 }
@@ -442,6 +476,22 @@ bool RTPSMessageCreator::addSubmessageDataFrag(
442476 // Add INLINE QOS AND SERIALIZED PAYLOAD DEPENDING ON FLAGS:
443477 if (inlineQosFlag) // inlineQoS
444478 {
479+ if (WITH_KEY == topicKind &&
480+ change->instanceHandle .isDefined () == false &&
481+ change->kind != ALIVE && change->serializedPayload .length == 0 )
482+ {
483+ // Instance handle is required but not defined
484+ EPROSIMA_LOG_ERROR (RTPS_WRITER,
485+ " DISPOSE or UNREGISTER Changes in KEYED Writers need a valid instanceHandle or the payload to be transmitted. Message won't be serialized" );
486+ return false ;
487+ }
488+ if (WITH_KEY == topicKind &&
489+ change->instanceHandle .isDefined () == false )
490+ {
491+ // Instance handle should be defined but is not compulsory, this is just a warning
492+ EPROSIMA_LOG_WARNING (RTPS_WRITER,
493+ " Change does not have a valid instanceHandle. KEY_HASH will not be serialized." );
494+ }
445495 DataMsgUtils::serialize_inline_qos (msg, change, topicKind, expectsInlineQos, inlineQos, status);
446496 }
447497
0 commit comments