Skip to content

Commit 378262a

Browse files
committed
Reuse create_map_and_set_sequence_number
Signed-off-by: Yadunund <[email protected]>
1 parent 0712429 commit 378262a

File tree

3 files changed

+7
-92
lines changed

3 files changed

+7
-92
lines changed

rmw_zenoh_cpp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ add_library(rmw_zenoh_cpp SHARED
4747
src/detail/type_support_common.cpp
4848
src/detail/zenoh_config.cpp
4949
src/detail/zenoh_router_check.cpp
50+
src/detail/zenoh_utils.cpp
5051
src/rmw_event.cpp
5152
src/rmw_get_network_flow_endpoints.cpp
5253
src/rmw_get_node_info_and_types.cpp

rmw_zenoh_cpp/src/detail/rmw_publisher_data.cpp

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "message_type_support.hpp"
2828
#include "logging_macros.hpp"
2929
#include "qos.hpp"
30+
#include "zenoh_utils.hpp"
3031

3132
#include "rcpputils/scope_exit.hpp"
3233

@@ -36,50 +37,6 @@
3637

3738
namespace rmw_zenoh_cpp
3839
{
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
8340
///=============================================================================
8441
std::shared_ptr<PublisherData> PublisherData::make(
8542
z_session_t session,

rmw_zenoh_cpp/src/rmw_zenoh.cpp

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "detail/rmw_data_types.hpp"
4040
#include "detail/serialization_format.hpp"
4141
#include "detail/type_support_common.hpp"
42+
#include "detail/zenoh_utils.hpp"
4243

4344
#include "rcpputils/scope_exit.hpp"
4445

@@ -568,51 +569,6 @@ rmw_return_loaned_message_from_publisher(
568569
return RMW_RET_UNSUPPORTED;
569570
}
570571

571-
namespace
572-
{
573-
z_owned_bytes_map_t
574-
create_map_and_set_sequence_num(int64_t sequence_number, const uint8_t gid[RMW_GID_STORAGE_SIZE])
575-
{
576-
z_owned_bytes_map_t map = z_bytes_map_new();
577-
if (!z_check(map)) {
578-
RMW_SET_ERROR_MSG("failed to allocate map for sequence number");
579-
return z_bytes_map_null();
580-
}
581-
auto free_attachment_map = rcpputils::make_scope_exit(
582-
[&map]() {
583-
z_bytes_map_drop(z_move(map));
584-
});
585-
586-
// The largest possible int64_t number is INT64_MAX, i.e. 9223372036854775807.
587-
// That is 19 characters long, plus one for the trailing \0, means we need 20 bytes.
588-
char seq_id_str[20];
589-
if (rcutils_snprintf(seq_id_str, sizeof(seq_id_str), "%" PRId64, sequence_number) < 0) {
590-
RMW_SET_ERROR_MSG("failed to print sequence_number into buffer");
591-
return z_bytes_map_null();
592-
}
593-
z_bytes_map_insert_by_copy(&map, z_bytes_new("sequence_number"), z_bytes_new(seq_id_str));
594-
595-
auto now = std::chrono::system_clock::now().time_since_epoch();
596-
auto now_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(now);
597-
char source_ts_str[20];
598-
if (rcutils_snprintf(source_ts_str, sizeof(source_ts_str), "%" PRId64, now_ns.count()) < 0) {
599-
RMW_SET_ERROR_MSG("failed to print sequence_number into buffer");
600-
return z_bytes_map_null();
601-
}
602-
z_bytes_map_insert_by_copy(&map, z_bytes_new("source_timestamp"), z_bytes_new(source_ts_str));
603-
604-
z_bytes_t gid_bytes;
605-
gid_bytes.len = RMW_GID_STORAGE_SIZE;
606-
gid_bytes.start = gid;
607-
608-
z_bytes_map_insert_by_copy(&map, z_bytes_new("source_gid"), gid_bytes);
609-
610-
free_attachment_map.cancel();
611-
612-
return map;
613-
}
614-
} // namespace
615-
616572
//==============================================================================
617573
/// Publish a ROS message.
618574
rmw_ret_t
@@ -766,7 +722,7 @@ rmw_publish_serialized_message(
766722
uint64_t sequence_number = publisher_data->get_next_sequence_number();
767723

768724
z_owned_bytes_map_t map =
769-
create_map_and_set_sequence_num(sequence_number, publisher_data->gid());
725+
rmw_zenoh_cpp::create_map_and_set_sequence_num(sequence_number, publisher_data->gid());
770726

771727
if (!z_check(map)) {
772728
// create_map_and_set_sequence_num already set the error
@@ -2170,7 +2126,8 @@ rmw_send_request(
21702126
// Send request
21712127
z_get_options_t opts = z_get_options_default();
21722128

2173-
z_owned_bytes_map_t map = create_map_and_set_sequence_num(*sequence_id, client_data->client_gid);
2129+
z_owned_bytes_map_t map = rmw_zenoh_cpp::create_map_and_set_sequence_num(*sequence_id,
2130+
client_data->client_gid);
21742131
if (!z_check(map)) {
21752132
// create_map_and_set_sequence_num already set the error
21762133
return RMW_RET_ERROR;
@@ -2861,7 +2818,7 @@ rmw_send_response(
28612818
const z_query_t loaned_query = query->get_query();
28622819
z_query_reply_options_t options = z_query_reply_options_default();
28632820

2864-
z_owned_bytes_map_t map = create_map_and_set_sequence_num(
2821+
z_owned_bytes_map_t map = rmw_zenoh_cpp::create_map_and_set_sequence_num(
28652822
request_header->sequence_number, request_header->writer_guid);
28662823
if (!z_check(map)) {
28672824
// create_map_and_set_sequence_num already set the error

0 commit comments

Comments
 (0)