27
27
#include " message_type_support.hpp"
28
28
#include " logging_macros.hpp"
29
29
#include " qos.hpp"
30
+ #include " zenoh_utils.hpp"
30
31
31
32
#include " rcpputils/scope_exit.hpp"
32
33
36
37
37
38
namespace rmw_zenoh_cpp
38
39
{
39
- namespace
40
- {
41
- z_owned_bytes_map_t
42
- create_map_and_set_sequence_num (int64_t sequence_number, uint8_t gid[RMW_GID_STORAGE_SIZE])
43
- {
44
- z_owned_bytes_map_t map = z_bytes_map_new ();
45
- if (!z_check (map)) {
46
- RMW_SET_ERROR_MSG (" failed to allocate map for sequence number" );
47
- return z_bytes_map_null ();
48
- }
49
- auto free_attachment_map = rcpputils::make_scope_exit (
50
- [&map]() {
51
- z_bytes_map_drop (z_move (map));
52
- });
53
-
54
- // The largest possible int64_t number is INT64_MAX, i.e. 9223372036854775807.
55
- // That is 19 characters long, plus one for the trailing \0, means we need 20 bytes.
56
- char seq_id_str[20 ];
57
- if (rcutils_snprintf (seq_id_str, sizeof (seq_id_str), " %" PRId64, sequence_number) < 0 ) {
58
- RMW_SET_ERROR_MSG (" failed to print sequence_number into buffer" );
59
- return z_bytes_map_null ();
60
- }
61
- z_bytes_map_insert_by_copy (&map, z_bytes_new (" sequence_number" ), z_bytes_new (seq_id_str));
62
-
63
- auto now = std::chrono::system_clock::now ().time_since_epoch ();
64
- auto now_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(now);
65
- char source_ts_str[20 ];
66
- if (rcutils_snprintf (source_ts_str, sizeof (source_ts_str), " %" PRId64, now_ns.count ()) < 0 ) {
67
- RMW_SET_ERROR_MSG (" failed to print sequence_number into buffer" );
68
- return z_bytes_map_null ();
69
- }
70
- z_bytes_map_insert_by_copy (&map, z_bytes_new (" source_timestamp" ), z_bytes_new (source_ts_str));
71
-
72
- z_bytes_t gid_bytes;
73
- gid_bytes.len = RMW_GID_STORAGE_SIZE;
74
- gid_bytes.start = gid;
75
-
76
- z_bytes_map_insert_by_copy (&map, z_bytes_new (" source_gid" ), gid_bytes);
77
-
78
- free_attachment_map.cancel ();
79
-
80
- return map;
81
- }
82
- } // namespace
83
40
// /=============================================================================
84
41
std::shared_ptr<PublisherData> PublisherData::make (
85
42
z_session_t session,
@@ -332,10 +289,8 @@ rmw_ret_t PublisherData::publish(
332
289
333
290
const size_t data_length = ser.get_serialized_data_length ();
334
291
335
- const int64_t sequence_number = sequence_number_++;
336
-
337
292
z_owned_bytes_map_t map =
338
- create_map_and_set_sequence_num (sequence_number , gid_);
293
+ create_map_and_set_sequence_num (sequence_number_++ , gid_);
339
294
if (!z_check (map)) {
340
295
// create_map_and_set_sequence_num already set the error
341
296
return RMW_RET_ERROR;
@@ -373,24 +328,67 @@ rmw_ret_t PublisherData::publish(
373
328
}
374
329
375
330
// /=============================================================================
376
- std::size_t PublisherData::guid () const
331
+ rmw_ret_t PublisherData::publish_serialized_message (
332
+ const rmw_serialized_message_t * serialized_message,
333
+ std::optional<zc_owned_shm_manager_t > & /* shm_manager*/ )
377
334
{
335
+ eprosima::fastcdr::FastBuffer buffer (
336
+ reinterpret_cast <char *>(serialized_message->buffer ), serialized_message->buffer_length );
337
+ rmw_zenoh_cpp::Cdr ser (buffer);
338
+ if (!ser.get_cdr ().jump (serialized_message->buffer_length )) {
339
+ RMW_SET_ERROR_MSG (" cannot correctly set serialized buffer" );
340
+ return RMW_RET_ERROR;
341
+ }
342
+
378
343
std::lock_guard<std::mutex> lock (mutex_);
379
- return entity_->guid ();
344
+
345
+ z_owned_bytes_map_t map =
346
+ rmw_zenoh_cpp::create_map_and_set_sequence_num (sequence_number_++, gid_);
347
+
348
+ if (!z_check (map)) {
349
+ // create_map_and_set_sequence_num already set the error
350
+ return RMW_RET_ERROR;
351
+ }
352
+ auto free_attachment_map = rcpputils::make_scope_exit (
353
+ [&map]() {
354
+ z_bytes_map_drop (z_move (map));
355
+ });
356
+
357
+ const size_t data_length = ser.get_serialized_data_length ();
358
+
359
+ // The encoding is simply forwarded and is useful when key expressions in the
360
+ // session use different encoding formats. In our case, all key expressions
361
+ // will be encoded with CDR so it does not really matter.
362
+ z_publisher_put_options_t options = z_publisher_put_options_default ();
363
+ options.attachment = z_bytes_map_as_attachment (&map);
364
+
365
+ // Returns 0 if success.
366
+ int8_t ret = z_publisher_put (
367
+ z_loan (pub_),
368
+ serialized_message->buffer ,
369
+ data_length,
370
+ &options);
371
+
372
+ if (ret) {
373
+ RMW_SET_ERROR_MSG (" unable to publish message" );
374
+ return RMW_RET_ERROR;
375
+ }
376
+
377
+ return RMW_RET_OK;
380
378
}
381
379
382
380
// /=============================================================================
383
- liveliness::TopicInfo PublisherData::topic_info () const
381
+ std:: size_t PublisherData::guid () const
384
382
{
385
383
std::lock_guard<std::mutex> lock (mutex_);
386
- return entity_->topic_info (). value ();
384
+ return entity_->guid ();
387
385
}
388
386
389
387
// /=============================================================================
390
- size_t PublisherData::get_next_sequence_number ()
388
+ liveliness::TopicInfo PublisherData::topic_info () const
391
389
{
392
390
std::lock_guard<std::mutex> lock (mutex_);
393
- return sequence_number_++ ;
391
+ return entity_-> topic_info (). value () ;
394
392
}
395
393
396
394
// /=============================================================================
@@ -400,13 +398,6 @@ const uint8_t * PublisherData::gid() const
400
398
return gid_;
401
399
}
402
400
403
- // /=============================================================================
404
- z_publisher_t PublisherData::publisher () const
405
- {
406
- std::lock_guard<std::mutex> lock (mutex_);
407
- return z_loan (pub_);
408
- }
409
-
410
401
// /=============================================================================
411
402
bool PublisherData::liveliness_is_valid () const
412
403
{
0 commit comments