36
36
37
37
#include " rcpputils/scope_exit.hpp"
38
38
#include " rmw/error_handling.h"
39
+ #include " zenoh_utils.hpp"
39
40
40
41
// Megabytes of SHM to reserve.
41
42
// TODO(clalancette): Make this configurable, or get it from the configuration
@@ -86,13 +87,18 @@ class rmw_context_impl_s::Data final
86
87
});
87
88
88
89
// Initialize the zenoh session.
89
- if (z_open (&session_, z_move (config), NULL ) != Z_OK) {
90
+ z_owned_session_t raw_session;
91
+ if (z_open (&raw_session, z_move (config), NULL ) != Z_OK) {
90
92
RMW_SET_ERROR_MSG (" Error setting up zenoh session." );
91
93
throw std::runtime_error (" Error setting up zenoh session." );
92
94
}
95
+ if (session_ != nullptr ) {
96
+ session_.reset ();
97
+ }
98
+ session_ = std::make_shared<rmw_zenoh_cpp::ZenohSession>(raw_session);
93
99
auto close_session = rcpputils::make_scope_exit (
94
- [this ]() {
95
- z_close (z_loan_mut (session_ ), NULL );
100
+ [&raw_session ]() {
101
+ z_close (z_loan_mut (raw_session ), NULL );
96
102
});
97
103
98
104
// Verify if the zenoh router is running if configured.
@@ -102,7 +108,7 @@ class rmw_context_impl_s::Data final
102
108
uint64_t connection_attempts = 0 ;
103
109
constexpr std::chrono::milliseconds sleep_time (1000 );
104
110
constexpr int64_t ticks_between_print (std::chrono::milliseconds (1000 ) / sleep_time);
105
- while ((ret = rmw_zenoh_cpp::zenoh_router_check (z_loan ( session_))) != RMW_RET_OK) {
111
+ while ((ret = rmw_zenoh_cpp::zenoh_router_check (session_-> loan ( ))) != RMW_RET_OK) {
106
112
if ((connection_attempts % ticks_between_print) == 0 ) {
107
113
RMW_ZENOH_LOG_WARN_NAMED (
108
114
" rmw_zenoh_cpp" ,
@@ -117,7 +123,7 @@ class rmw_context_impl_s::Data final
117
123
}
118
124
119
125
// Initialize the graph cache.
120
- const z_id_t zid = z_info_zid (z_loan ( session_));
126
+ const z_id_t zid = z_info_zid (session_-> loan ( ));
121
127
graph_cache_ = std::make_shared<rmw_zenoh_cpp::GraphCache>(zid);
122
128
// Setup liveliness subscriptions for discovery.
123
129
std::string liveliness_str = rmw_zenoh_cpp::liveliness::subscription_token (domain_id);
@@ -144,7 +150,7 @@ class rmw_context_impl_s::Data final
144
150
z_view_keyexpr_t keyexpr;
145
151
z_view_keyexpr_from_str (&keyexpr, liveliness_str.c_str ());
146
152
z_liveliness_get (
147
- z_loan ( session_), z_loan (keyexpr),
153
+ session_-> loan ( ), z_loan (keyexpr),
148
154
z_move (closure), NULL );
149
155
z_owned_reply_t reply;
150
156
while (z_recv (z_loan (handler), &reply) == Z_OK) {
@@ -203,7 +209,7 @@ class rmw_context_impl_s::Data final
203
209
z_view_keyexpr_t liveliness_ke;
204
210
z_view_keyexpr_from_str (&liveliness_ke, liveliness_str.c_str ());
205
211
if (z_liveliness_declare_subscriber (
206
- z_loan ( session_),
212
+ session_-> loan ( ),
207
213
&graph_subscriber_, z_loan (liveliness_ke),
208
214
z_move (callback), &sub_options) != Z_OK)
209
215
{
@@ -240,11 +246,8 @@ class rmw_context_impl_s::Data final
240
246
// to avoid an AB/BA deadlock if shutdown is racing with graph_sub_data_handler().
241
247
}
242
248
243
- // Close the zenoh session
244
- if (z_close (z_loan_mut (session_), NULL ) != Z_OK) {
245
- RMW_SET_ERROR_MSG (" Error while closing zenoh session" );
246
- return RMW_RET_ERROR;
247
- }
249
+ // Drop the shared session.
250
+ session_.reset ();
248
251
249
252
return RMW_RET_OK;
250
253
}
@@ -255,10 +258,10 @@ class rmw_context_impl_s::Data final
255
258
return enclave_;
256
259
}
257
260
258
- const z_loaned_session_t * session () const
261
+ std::shared_ptr<rmw_zenoh_cpp::ZenohSession> session () const
259
262
{
260
263
std::lock_guard<std::recursive_mutex> lock (mutex_);
261
- return z_loan ( session_) ;
264
+ return session_;
262
265
}
263
266
264
267
std::optional<z_owned_shm_provider_t > & shm_provider ()
@@ -288,7 +291,7 @@ class rmw_context_impl_s::Data final
288
291
bool session_is_valid () const
289
292
{
290
293
std::lock_guard<std::recursive_mutex> lock (mutex_);
291
- return !z_session_is_closed (z_loan ( session_));
294
+ return !z_session_is_closed (session_-> loan ( ));
292
295
}
293
296
294
297
std::shared_ptr<rmw_zenoh_cpp::GraphCache> graph_cache ()
@@ -309,7 +312,7 @@ class rmw_context_impl_s::Data final
309
312
}
310
313
311
314
// Check that the Zenoh session is still valid.
312
- if (z_session_is_closed (z_loan ( session_))) {
315
+ if (z_session_is_closed (session_-> loan ( ))) {
313
316
RMW_ZENOH_LOG_ERROR_NAMED (
314
317
" rmw_zenoh_cpp" ,
315
318
" Unable to create NodeData as Zenoh session is invalid." );
@@ -319,7 +322,7 @@ class rmw_context_impl_s::Data final
319
322
auto node_data = rmw_zenoh_cpp::NodeData::make (
320
323
node,
321
324
this ->get_next_entity_id (),
322
- z_loan ( session_),
325
+ session_-> loan ( ),
323
326
domain_id_,
324
327
ns,
325
328
node_name,
@@ -395,8 +398,8 @@ class rmw_context_impl_s::Data final
395
398
std::size_t domain_id_;
396
399
// Enclave, name used to find security artifacts in a sros2 keystore.
397
400
std::string enclave_;
398
- // An owned session.
399
- z_owned_session_t session_;
401
+ // A shared session.
402
+ std::shared_ptr<rmw_zenoh_cpp::ZenohSession> session_{ nullptr } ;
400
403
// An optional SHM manager that is initialized of SHM is enabled in the
401
404
// zenoh session config.
402
405
std::optional<z_owned_shm_provider_t > shm_provider_;
@@ -472,7 +475,7 @@ std::string rmw_context_impl_s::enclave() const
472
475
}
473
476
474
477
// /=============================================================================
475
- const z_loaned_session_t * rmw_context_impl_s::session () const
478
+ std::shared_ptr<rmw_zenoh_cpp::ZenohSession> rmw_context_impl_s::session () const
476
479
{
477
480
return data_->session ();
478
481
}
0 commit comments