eclipse-cyclonedds / cyclonedds

Eclipse Cyclone DDS project
https://projects.eclipse.org/projects/iot.cyclonedds
Other
799 stars 349 forks source link

how to support string using zero-copy transport #2001

Closed Autostone-c closed 1 month ago

Autostone-c commented 1 month ago

When I use cyclonedds, I send a string using the publisher_member_function of ROS2 and subscribe to rt/topic using pure dds. If SharedMemory is not enabled, I can communicate and receive string data normally. If SharedMemory is enabled, communication is normal, but I receive an empty string

my String.idl `module stdmsgs { module msg { module dds { struct String_ { string data; };

}; }; }; // module msg::std_msgs`

and my dds sub code: `#include "dds/dds.h"

include "String.h"

include "dds/ddsrt/time.h"

include "stdio.h"

include "stdlib.h"

define MAX_SAMPLES 1

int main() { dds_entity_t participant; dds_entity_t topic; dds_entity_t reader; dds_return_t rc; dds_qos_t *qos;

std_msgs_msg_dds_String msg; void samples[MAX_SAMPLES]; dds_sample_info_t infos[MAX_SAMPLES]; uint32_t status = 0;

/ Create a Participant. / participant = dds_create_participant (DDS_DOMAIN_DEFAULT, NULL, NULL); if (participant < 0) DDS_FATAL("dds_create_participant: %s\n", dds_strretcode(-participant)); / Create a Topic. / topic = dds_create_topic ( participant, &std_msgs_msg_ddsStringdesc, "rt/topic", NULL, NULL); if (topic < 0) DDS_FATAL("dds_create_topic: %s\n", dds_strretcode(-topic));

/ Create a Reader. / qos = dds_create_qos (); dds_qset_reliability (qos, DDS_RELIABILITY_RELIABLE, DDS_SECS (10)); reader = dds_create_reader (participant, topic, NULL, NULL); if (reader < 0) DDS_FATAL("dds_create_reader: %s\n", dds_strretcode(-reader));

dds_delete_qos(qos);

printf ("\n=== [Subscriber] Waiting for a sample ...\n"); fflush (stdout);

samples[0] = std_msgs_msg_dds__String___alloc(); / Create a message to write. /

while(1) { /* Do the actual read.

/ Check if we read some data and it is valid. / if ((rc > 0) && (infos[0].valid_data)) { / Print Message. / msg = (std_msgs_msg_dds_String) samples[0]; printf ("=== [Subscriber] Received : "); printf ("Message (%s)\n", msg->data); fflush (stdout); } else { / Polling sleep. */ dds_sleepfor (DDS_MSECS (20)); } }

/ Deleting the participant will delete all its children recursively as well. / rc = dds_delete (participant); if (rc != DDS_RETCODE_OK) DDS_FATAL("dds_delete: %s\n", dds_strretcode(-rc));

return EXIT_SUCCESS;`