Skip to content

应答器 (Responder)

应答器是请求-响应式交互的服务端一侧:声明一个 responder 后,凡是匹配其 key expression 的查询都会触发回调,你在回调内(同步)或之后(异步)通过 onepath_request_reply 发回一条或多条回复。客户端一侧见 查询 Get

onepath_declare_responder

c
int onepath_declare_responder(onepath_session_t s, onepath_responder_t *out,
                            const char *key, onepath_request_cb cb,
                            void *userdata, int complete);

声明一个应答器,响应其他节点的查询请求。

  • 参数
    • s — 会话句柄
    • out — 输出 responder 句柄
    • key — key expression
    • cb — 查询到达时的回调函数(见 回调函数
    • userdata — 传递给回调的用户数据指针
    • complete1 表示此 responder 能提供完整回复,0 表示部分回复
  • 返回值ONEPATH_OK 成功
  • 注意:可在回调内同步调用 onepath_request_reply() 发送回复,也可保存 query 后异步回复。回复后须调用 onepath_request_release() 释放

onepath_request_reply

c
int onepath_request_reply(const onepath_request_t *query,
                      const char *key, const void *data, size_t len);

发送二进制数据回复。可在回调内调用,也可保存 query 后异步调用。

  • 参数
    • query — 查询信息指针
    • key — 回复的 key expression
    • data — 回复数据指针;len — 数据长度
  • 返回值ONEPATH_OK 成功
  • 注意:回复后须调用 onepath_request_release() 释放 query。同一查询可多次调用以发送多条回复

onepath_request_reply_str

c
int onepath_request_reply_str(const onepath_request_t *query,
                          const char *key, const char *value);

发送字符串回复。

  • 参数query — 查询信息指针;key — 回复的 key expression;value — 以 null 结尾的回复字符串
  • 返回值ONEPATH_OK 成功

onepath_request_release

c
void onepath_request_release(onepath_request_t *query);

释放查询信息及其持有的内部资源。回复完成后或不再需要回复时调用。

  • 参数query — 查询信息指针,释放后清零
  • 注意:回调模式和异步模式均须调用

onepath_responder_destroy

c
void onepath_responder_destroy(onepath_responder_t q);

销毁 responder。

  • 参数q — responder 句柄

示例

同步回复

c
static void on_query(onepath_request_t *query, void *userdata) {
    (void)userdata;
    printf("收到查询 [%s] 参数: %s\n", query->key, query->parameters);
    onepath_request_reply_str(query, query->key, "pong");
    onepath_request_release(query);   /* 必须释放 */
}

onepath_responder_t resp;
onepath_declare_responder(s, &resp, "demo/service/ping", on_query, NULL, 1);
/* ... 运行 ... */
onepath_responder_destroy(resp);

查询请求结构见 onepath_request_tquery->_opaque 为内部保留字段,必须原样保持,不可读取或修改。

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