rclcpp::Client wrapper with coroutines support.
More...
#include <service_client.hpp>
|
| | ServiceClient (const ServiceClientOptions &options, std::string_view service_name) |
| | Construct the client with the specified options.
|
| Task< std::expected< std::shared_ptr< Response >, std::string > > | call (const std::shared_ptr< Request > &request) |
| | Coroutine call of the service.
|
| std::expected< std::future< std::shared_ptr< Response > >, std::string > | call_async (const std::shared_ptr< Request > &request) |
| | Async call of the service.
|
| std::string | get_service_name () const |
| | Returns the resolved (full) name of the service handled by this service client.
|
| std::string | get_unresolved_service_name () const |
| | Returns the unresolved name of the service handled by this service client.
|
| template<typename RepT = int64_t, typename RatioT = std::milli> |
| bool | wait_for_service (std::chrono::duration< RepT, RatioT > timeout) |
| | Wait for the service to become available.
|
| bool | is_service_ready () const |
| | Check if the service is available.
|
| size_t | prune_pending_requests () |
| | Remove all pending requests.
|
template<class ServiceType>
class mrs_lib::ServiceClient< ServiceType >
rclcpp::Client wrapper with coroutines support.
- Template Parameters
-
| ServiceType | Type of the service this client will call. |
When using services, it is often useful to wait for the response right after sending it. ServiceClient makes this pattern simpler by allowing the use of coroutines. It provides ServiceClient::call, a coroutine interface for calling the service.
◆ ServiceClient()
template<class ServiceType>
Construct the client with the specified options.
- Parameters
-
| options | Options to configure the service client. |
| service_name | The name of the service to call using this client. |
◆ call()
template<class ServiceType>
Coroutine call of the service.
- Parameters
-
| request | Request to send to the service server. |
- Returns
- Response of the server if the call succeeded, error message otherwise.
◆ call_async()
template<class ServiceType>
Async call of the service.
- Parameters
-
| request | Request to send to the service server. |
- Returns
- Future that will contain the server response if successful, error message otherwise.
When the future is returned, it is up to the user to check, if the service server responded. This can be done for example by checking the readiness of the future in a timer callback.
- Warning
- If the future is not ready, calling get on the future will block the current thread until the response is sent. This may cause deadlocks! (Eg. when service client is on the same mutually exclusive callback group as the currently running function)
◆ get_service_name()
template<class ServiceType>
Returns the resolved (full) name of the service handled by this service client.
- Returns
- The name of the handled service.
◆ get_unresolved_service_name()
template<class ServiceType>
Returns the unresolved name of the service handled by this service client.
- Returns
- The name of the handled service.
◆ is_service_ready()
template<class ServiceType>
Check if the service is available.
- Returns
- true if the service is available, false otherwise.
◆ prune_pending_requests()
template<class ServiceType>
Remove all pending requests.
- Returns
- Number of pending requests that were removed.
- Warning
- Calling this will not wake up threads that are waiting on future::get. These threads will be deadlocked, unless woken by some other means. Coroutines waiting on ServiceClient::call will be cancelled.
◆ wait_for_service()
template<class ServiceType>
template<typename RepT, typename RatioT>
Wait for the service to become available.
- Template Parameters
-
| RepT | Arithmetic type representing the number of ticks. |
| RatioT | std::ratio representing the tick period. |
- Parameters
-
| timeout | Maximum time to wait for the service to be available. |
- Returns
- true if the service became available within the timeout, false otherwise.
The documentation for this class was generated from the following files: