10#ifndef NROS_CPP_SERVICE_HPP
11#define NROS_CPP_SERVICE_HPP
19#include "nros_cpp_ffi.h"
28 uint8_t* resp,
size_t resp_cap,
29 size_t* resp_len,
void* ctx);
32 const char* service_name,
const char* type_name,
35 void* context, uint8_t sched_context,
36 size_t* out_handle_id);
100 uint8_t buf[ResponseType::SERIALIZED_SIZE_MAX];
102 if (ResponseType::ffi_serialize(&
resp,
buf,
sizeof(
buf), &len) != 0) {
118 if (initialized_ && !callback_mode_) {
121 initialized_ =
false;
130 : initialized_(
other.initialized_), user_fn_(
other.user_fn_),
131 user_fn_ctx_(
other.user_fn_ctx_), user_ctx_(
other.user_ctx_),
132 handle_id_(
other.handle_id_), callback_mode_(
other.callback_mode_) {
133 if (
other.initialized_ && !
other.callback_mode_) {
136 other.initialized_ =
false;
140 if (
this != &
other) {
141 if (initialized_ && !callback_mode_) {
144 initialized_ =
other.initialized_;
145 user_fn_ =
other.user_fn_;
146 user_fn_ctx_ =
other.user_fn_ctx_;
147 user_ctx_ =
other.user_ctx_;
148 handle_id_ =
other.handle_id_;
149 callback_mode_ =
other.callback_mode_;
150 if (
other.initialized_ && !
other.callback_mode_) {
153 other.initialized_ =
false;
179 if (
self ==
nullptr)
return false;
183 if (
self->user_fn_ !=
nullptr) {
185 }
else if (
self->user_fn_ctx_ !=
nullptr) {
191 if (ResponseType::ffi_serialize(&response, resp, resp_cap, &len) != 0)
return false;
196 alignas(8) uint8_t storage_[NROS_SERVICE_SERVER_SIZE];
201 void* user_ctx_ =
nullptr;
202 size_t handle_id_ =
static_cast<size_t>(-1);
203 bool callback_mode_ =
false;
224 ffi_qos.liveliness_lease_ms =
qos.liveliness_lease_ms();
225 ffi_qos.avoid_ros_namespace_conventions =
qos.avoid_ros_namespace_conventions() ? 1 : 0;
229 out.initialized_ =
true;
237template <
typename S,
typename F,
typename>
249 ffi_qos.liveliness_lease_ms =
qos.liveliness_lease_ms();
250 ffi_qos.avoid_ros_namespace_conventions =
qos.avoid_ros_namespace_conventions() ? 1 : 0;
254 out.user_fn_ctx_ =
nullptr;
255 out.user_ctx_ =
nullptr;
260 size_t handle =
static_cast<size_t>(-1);
264 &
out, sched, &handle);
266 out.handle_id_ = handle;
267 out.callback_mode_ =
true;
268 out.initialized_ =
true;
Result create_service(Service< S > &out, const char *service_name, const QoS &qos=QoS::services())
Definition service.hpp:214
static constexpr Result success()
Named constructors.
Definition result.hpp:74
Definition service.hpp:59
void(*)(const RequestType &request, ResponseType &response, void *ctx) TypedServiceFnWithCtx
Definition service.hpp:69
~Service()
Definition service.hpp:117
Result try_recv_request(RequestType &req, int64_t &seq_id)
Definition service.hpp:79
typename S::Request RequestType
Definition service.hpp:61
Service(Service &&other)
Definition service.hpp:129
Service & operator=(Service &&other)
Definition service.hpp:139
Result send_reply(int64_t seq_id, const ResponseType &resp)
Definition service.hpp:98
typename S::Response ResponseType
Definition service.hpp:62
void(*)(const RequestType &request, ResponseType &response) TypedServiceFn
Definition service.hpp:67
size_t handle_id() const
Definition service.hpp:164
Service()
Definition service.hpp:160
bool is_valid() const
Check if the service is initialized and valid.
Definition service.hpp:109
Inline storage-size macros for opaque entity buffers.
int nros_cpp_ret_t
Definition future.hpp:20
@ Error
Generic failure not covered by a more specific code.
@ TryAgain
Transient — no data ready yet (non-blocking take). Retry later.
nros::Node and global session helpers.
nros_cpp_qos_history_t
Definition qos.hpp:31
nros_cpp_qos_liveliness_t
Definition qos.hpp:35
nros_cpp_qos_durability_t
Definition qos.hpp:27
nros_cpp_qos_reliability_t
Definition qos.hpp:23
nros::Result, nros::ErrorCode, and the NROS_TRY macro.
bool(* nros_cpp_service_request_callback_t)(const uint8_t *req, size_t req_len, uint8_t *resp, size_t resp_cap, size_t *resp_len, void *ctx)
Definition service.hpp:27
nros_cpp_ret_t nros_cpp_service_server_register(const nros_cpp_node_t *node, const char *service_name, const char *type_name, const char *type_hash, nros_cpp_qos_t qos, nros_cpp_service_request_callback_t callback, void *context, uint8_t sched_context, size_t *out_handle_id)