Skip to content

请求器 (Requester)

请求器是一个持久的查询客户端:声明一次后可反复发起查询,省去每次 onepath_get 的声明开销,并可注册匹配状态回调感知应答器的上下线。一次性查询见 查询 Get,应答器一侧见 Responder

onepath_declare_requester

c
int onepath_declare_requester(onepath_session_t s, onepath_requester_t *out,
                          const char *key, const onepath_requester_opts_t *opts);

声明一个持久请求者,可多次发起查询。

  • 参数
    • s — 会话句柄
    • out — 输出 requester 句柄
    • key — 查询的 key expression
    • opts — 选项,传 NULL 使用默认值(超时 5000ms,见 onepath_requester_opts_t
  • 返回值ONEPATH_OK 成功

onepath_requester_get

c
int onepath_requester_get(onepath_requester_t q, const char *parameters,
                      onepath_reply_rx_t *out,
                      const void *payload, size_t payload_len);

通过 requester 发起一次查询。

  • 参数
    • q — requester 句柄
    • parameters — 查询参数字符串,可传 NULL
    • out — 输出回复接收通道句柄
    • payload — 可选查询载荷,可传 NULLpayload_len — 载荷长度
  • 返回值ONEPATH_OK 成功
  • 注意:回复的接收与释放方式同 查询 Getonepath_reply_recv / onepath_reply_release / onepath_reply_rx_destroy

onepath_requester_on_matching

c
int onepath_requester_on_matching(onepath_requester_t q,
                              onepath_matching_cb cb, void *userdata);

注册匹配状态变更回调。

  • 参数
    • q — requester 句柄
    • cb — 匹配状态变更回调(见 回调函数
    • userdata — 用户数据指针
  • 返回值ONEPATH_OK 成功
  • 注意:当有新 responder 匹配或所有匹配的 responder 消失时,回调会被触发

onepath_requester_destroy

c
void onepath_requester_destroy(onepath_requester_t q);

销毁 requester。

  • 参数q — requester 句柄

示例

c
static void on_matching(int has_match, void *userdata) {
    (void)userdata;
    printf("应答器%s\n", has_match ? "已就绪" : "已全部下线");
}

onepath_requester_t req;
onepath_declare_requester(s, &req, "demo/service/calc", NULL);
onepath_requester_on_matching(req, on_matching, NULL);

for (int i = 0; i < 5; i++) {
    onepath_reply_rx_t rx;
    if (onepath_requester_get(req, "op=add", &rx, NULL, 0) == ONEPATH_OK) {
        onepath_reply_t reply;
        while (onepath_reply_recv(rx, &reply) == ONEPATH_OK) {
            printf("%.*s\n", (int)reply.data_len, (const char *)reply.data);
            onepath_reply_release(&reply);
        }
        onepath_reply_rx_destroy(rx);
    }
}

onepath_requester_destroy(req);

OnePath™ 以预构建库形式交付,运行时零外部依赖。