Class GenericClient
Defined in File generic_client.hpp
Nested Relationships
Nested Types
Inheritance Relationships
Base Type
public rclcpp::ClientBase
(Class ClientBase)
Class Documentation
-
class GenericClient : public rclcpp::ClientBase
Public Types
-
using Request = void*
-
using Response = void*
-
using Promise = std::promise<SharedResponse>
-
using Future = std::future<SharedResponse>
-
using CallbackType = std::function<void(SharedFuture)>
Public Functions
-
virtual SharedResponse create_response() override
-
virtual std::shared_ptr<rmw_request_id_t> create_request_header() override
-
FutureAndRequestId async_send_request(const Request request)
Send a request to the service server.
This method returns a
FutureAndRequestId
instance that can be passed to Executor::spin_until_future_complete() to wait until it has been completed.If the future never completes, e.g. the call to Executor::spin_until_future_complete() times out, GenericClient::remove_pending_request() must be called to clean the client internal state. Not doing so will make the
GenericClient
instance to use more memory each time a response is not received from the service server.auto future = generic_client->async_send_request(my_request); if ( rclcpp::FutureReturnCode::TIMEOUT == executor->spin_until_future_complete(future, timeout)) { generic_client->remove_pending_request(future); // handle timeout } else { handle_response(future.get()); }
- Parameters:
request – [in] request to be send.
- Returns:
a FutureAndRequestId instance.
Send a request to the service server and schedule a callback in the executor.
Similar to the previous overload, but a callback will automatically be called when a response is received.
If the callback is never called, because we never got a reply for the service server, remove_pending_request() has to be called with the returned request id or prune_pending_requests(). Not doing so will make the
GenericClient
instance use more memory each time a response is not received from the service server. In this case, it’s convenient to setup a timer to cleanup the pending requests.- Parameters:
request – [in] request to be send.
cb – [in] callback that will be called when we get a response for this request.
- Returns:
the request id representing the request just sent.
-
template<typename AllocatorT = std::allocator<int64_t>>
inline size_t prune_requests_older_than(std::chrono::time_point<std::chrono::system_clock> time_point, std::vector<int64_t, AllocatorT> *pruned_requests = nullptr) Clean all pending requests older than a time_point.
- Parameters:
time_point – [in] Requests that were sent before this point are going to be removed.
pruned_requests – [inout] Removed requests id will be pushed to the vector if a pointer is provided.
- Returns:
number of pending requests that were removed.
-
size_t prune_pending_requests()
Clean all pending requests.
- Returns:
number of pending requests that were removed.
-
bool remove_pending_request(int64_t request_id)
Cleanup a pending request.
This notifies the client that we have waited long enough for a response from the server to come, we have given up and we are not waiting for a response anymore.
Not calling this will make the client start using more memory for each request that never got a reply from the server.
- Parameters:
request_id – [in] request id returned by async_send_request().
- Returns:
true when a pending request was removed, false if not (e.g. a response was received).
-
bool remove_pending_request(const FutureAndRequestId &future)
Cleanup a pending request.
Convenient overload, same as:
GenericClient::remove_pending_request(this, future.request_id)
.
Cleanup a pending request.
Convenient overload, same as:
GenericClient::remove_pending_request(this, future.request_id)
.
-
inline bool take_response(Response response_out, rmw_request_id_t &request_header_out)
Take the next response for this client.
See also
- Parameters:
response_out – [out] The reference to a Service Response into which the middleware will copy the response being taken.
request_header_out – [out] The request header to be filled by the middleware when taking, and which can be used to associate the response to a specific request.
- Throws:
rclcpp::exceptions::RCLError – based exceptions if the underlying rcl function fail.
- Returns:
true if the response was taken, otherwise false.
Protected Types
-
using CallbackTypeValueVariant = std::tuple<CallbackType, SharedFuture, Promise>
-
using CallbackInfoVariant = std::variant<std::promise<SharedResponse>, CallbackTypeValueVariant>
Protected Functions
-
int64_t async_send_request_impl(const Request request, CallbackInfoVariant value)
-
std::optional<CallbackInfoVariant> get_and_erase_pending_request(int64_t request_number)
Protected Attributes
-
std::map<int64_t, std::pair<std::chrono::time_point<std::chrono::system_clock>, CallbackInfoVariant>> pending_requests_
-
std::mutex pending_requests_mutex_
-
struct FutureAndRequestId : public rclcpp::detail::FutureAndRequestId<Future>
A convenient GenericClient::Future and request id pair.
Public members:
future: a std::future<void *>.
request_id: the request id associated with the future.
All the other methods are equivalent to the ones std::future provides.
Public Functions
See std::future::share().
-
FutureAndRequestId(FutureAndRequestId &&other) noexcept = default
Move constructor.
-
FutureAndRequestId(const FutureAndRequestId &other) = delete
Deleted copy constructor, each instance is a unique owner of the future.
-
FutureAndRequestId &operator=(FutureAndRequestId &&other) noexcept = default
Move assignment.
-
FutureAndRequestId &operator=(const FutureAndRequestId &other) = delete
Deleted copy assignment, each instance is a unique owner of the future.
-
~FutureAndRequestId() = default
Destructor.
A convenient GenericClient::SharedFuture and request id pair.
Public members:
future: a std::shared_future<SharedResponse>.
request_id: the request id associated with the future.
All the other methods are equivalent to the ones std::shared_future provides.
-
using Request = void*