42
42
43
43
namespace
44
44
{
45
+ // /=============================================================================
46
+ struct ClientDataWrapper
47
+ {
48
+ explicit ClientDataWrapper (std::shared_ptr<rmw_zenoh_cpp::ClientData> data)
49
+ : client_data(std::move(data))
50
+ {
51
+ }
52
+
53
+ std::shared_ptr<rmw_zenoh_cpp::ClientData> client_data;
54
+ };
45
55
46
56
// /=============================================================================
47
57
void client_data_handler (z_loaned_reply_t * reply, void * data)
48
58
{
49
- auto client_data = static_cast <rmw_zenoh_cpp::ClientData *>(data);
50
- if (client_data == nullptr ) {
59
+ auto wrapper = static_cast <ClientDataWrapper *>(data);
60
+ if (wrapper == nullptr ) {
51
61
RMW_ZENOH_LOG_ERROR_NAMED (
52
62
" rmw_zenoh_cpp" ,
53
63
" Unable to obtain client_data_t from data in client_data_handler."
54
64
);
55
65
return ;
56
66
}
57
67
58
- if (client_data->is_shutdown ()) {
68
+ if (wrapper-> client_data ->is_shutdown ()) {
59
69
return ;
60
70
}
61
71
@@ -69,7 +79,7 @@ void client_data_handler(z_loaned_reply_t * reply, void * data)
69
79
RMW_ZENOH_LOG_ERROR_NAMED (
70
80
" rmw_zenoh_cpp" ,
71
81
" z_reply_is_ok returned False for keyexpr %s. Reason: %.*s" ,
72
- client_data->topic_info ().topic_keyexpr_ .c_str (),
82
+ wrapper-> client_data ->topic_info ().topic_keyexpr_ .c_str (),
73
83
static_cast <int >(z_string_len (z_loan (err_str))),
74
84
z_string_data (z_loan (err_str)));
75
85
z_drop (z_move (err_str));
@@ -80,23 +90,23 @@ void client_data_handler(z_loaned_reply_t * reply, void * data)
80
90
std::chrono::nanoseconds::rep received_timestamp =
81
91
std::chrono::system_clock::now ().time_since_epoch ().count ();
82
92
83
- client_data->add_new_reply (
93
+ wrapper-> client_data ->add_new_reply (
84
94
std::make_unique<rmw_zenoh_cpp::ZenohReply>(reply, received_timestamp));
85
95
}
86
96
87
97
// /=============================================================================
88
98
void client_data_drop (void * data)
89
99
{
90
- auto client_data = static_cast <rmw_zenoh_cpp::ClientData *>(data);
91
- if (client_data == nullptr ) {
100
+ auto wrapper = static_cast <ClientDataWrapper *>(data);
101
+ if (wrapper == nullptr ) {
92
102
RMW_ZENOH_LOG_ERROR_NAMED (
93
103
" rmw_zenoh_cpp" ,
94
104
" Unable to obtain client_data_t from data in client_data_drop."
95
105
);
96
106
return ;
97
107
}
98
108
99
- client_data-> decrement_in_flight_and_conditionally_remove () ;
109
+ delete wrapper ;
100
110
}
101
111
102
112
} // namespace
@@ -228,8 +238,7 @@ ClientData::ClientData(
228
238
wait_set_data_(nullptr ),
229
239
sequence_number_(1 ),
230
240
is_shutdown_(false ),
231
- initialized_(false ),
232
- num_in_flight_(0 )
241
+ initialized_(false )
233
242
{
234
243
// Do nothing.
235
244
}
@@ -470,9 +479,9 @@ rmw_ret_t ClientData::send_request(
470
479
471
480
// TODO(Yadunund): Once we switch to zenoh-cpp with lambda closures,
472
481
// capture shared_from_this() instead of this.
473
- num_in_flight_++ ;
482
+ ClientDataWrapper * wrapper = new ClientDataWrapper ( shared_from_this ()) ;
474
483
z_owned_closure_reply_t zn_closure_reply;
475
- z_closure (&zn_closure_reply, client_data_handler, client_data_drop, this );
484
+ z_closure (&zn_closure_reply, client_data_handler, client_data_drop, wrapper );
476
485
z_get (
477
486
sess_->loan (),
478
487
z_loan (keyexpr_), " " ,
@@ -527,10 +536,11 @@ bool ClientData::detach_condition_and_queue_is_empty()
527
536
}
528
537
529
538
// /=============================================================================
530
- void ClientData::_shutdown ()
539
+ rmw_ret_t ClientData::shutdown ()
531
540
{
541
+ std::lock_guard<std::recursive_mutex> lock (mutex_);
532
542
if (is_shutdown_) {
533
- return ;
543
+ return RMW_RET_OK ;
534
544
}
535
545
536
546
// Unregister this node from the ROS graph.
@@ -541,45 +551,10 @@ void ClientData::_shutdown()
541
551
542
552
sess_.reset ();
543
553
is_shutdown_ = true ;
544
- }
545
554
546
- // /=============================================================================
547
- rmw_ret_t ClientData::shutdown ()
548
- {
549
- std::lock_guard<std::recursive_mutex> lock (mutex_);
550
- _shutdown ();
551
555
return RMW_RET_OK;
552
556
}
553
557
554
- // /=============================================================================
555
- bool ClientData::shutdown_and_query_in_flight ()
556
- {
557
- std::lock_guard<std::recursive_mutex> lock (mutex_);
558
- _shutdown ();
559
- return num_in_flight_ > 0 ;
560
- }
561
-
562
- // /=============================================================================
563
- void ClientData::decrement_in_flight_and_conditionally_remove ()
564
- {
565
- std::unique_lock<std::recursive_mutex> lock (mutex_);
566
- --num_in_flight_;
567
-
568
- if (is_shutdown_ && num_in_flight_ == 0 ) {
569
- rmw_context_impl_s * context_impl = static_cast <rmw_context_impl_s *>(rmw_node_->data );
570
- if (context_impl == nullptr ) {
571
- return ;
572
- }
573
- std::shared_ptr<rmw_zenoh_cpp::NodeData> node_data = context_impl->get_node_data (rmw_node_);
574
- if (node_data == nullptr ) {
575
- return ;
576
- }
577
- // We have to unlock here since we are about to delete ourself, and thus the unlock would be UB.
578
- lock.unlock ();
579
- node_data->delete_client_data (rmw_client_);
580
- }
581
- }
582
-
583
558
// /=============================================================================
584
559
bool ClientData::is_shutdown () const
585
560
{
0 commit comments