2023-09-24 01:26:07 +08:00
|
|
|
#include "u_rpc.h"
|
|
|
|
|
#include "u_rpc_svr.h"
|
|
|
|
|
#include "ns_types.h"
|
|
|
|
|
#include "rpc_prot.h"
|
|
|
|
|
#include "u_env.h"
|
|
|
|
|
#include "u_prot.h"
|
|
|
|
|
#include "u_hd_man.h"
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
|
|
#define NS_CLI_CACHE_NR 8 //!< 一个客户端最多可以请求8个服务
|
|
|
|
|
typedef struct ns_cli_entry
|
|
|
|
|
{
|
|
|
|
|
char path[NAMESPACE_PATH_LEN];
|
|
|
|
|
obj_handler_t hd;
|
|
|
|
|
} ns_cli_entry_t;
|
|
|
|
|
|
|
|
|
|
typedef struct ns_cli_cache
|
|
|
|
|
{
|
|
|
|
|
ns_cli_entry_t cache[NS_CLI_CACHE_NR];
|
|
|
|
|
} ns_cli_cache_t;
|
|
|
|
|
|
|
|
|
|
static ns_cli_cache_t ns_cli_cache;
|
|
|
|
|
|
|
|
|
|
static obj_handler_t find_hd(const char *path)
|
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
|
|
|
|
int empty = -1;
|
|
|
|
|
for (i = 0; i < NS_CLI_CACHE_NR; i++)
|
|
|
|
|
{
|
|
|
|
|
if (ns_cli_cache.cache[i].path[0] != 0)
|
|
|
|
|
{
|
|
|
|
|
if (strcmp(ns_cli_cache.cache[i].path, path) == 0)
|
|
|
|
|
{
|
|
|
|
|
return ns_cli_cache.cache[i].hd;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return HANDLER_INVALID;
|
|
|
|
|
}
|
|
|
|
|
static bool_t reg_hd(const char *path, obj_handler_t hd)
|
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
|
|
|
|
int empty = -1;
|
|
|
|
|
for (i = 0; i < NS_CLI_CACHE_NR; i++)
|
|
|
|
|
{
|
|
|
|
|
if (ns_cli_cache.cache[i].path[0] == 0)
|
|
|
|
|
{
|
|
|
|
|
strcpy(ns_cli_cache.cache[i].path, path);
|
|
|
|
|
ns_cli_cache.cache[i].hd = hd;
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RPC_GENERATION_CALL2(ns_t, NS_REGISTER_OP, register,
|
|
|
|
|
rpc_ref_array_uint32_t_uint8_t_32_t, rpc_array_uint32_t_uint8_t_32_t, RPC_DIR_IN, RPC_TYPE_DATA, path,
|
|
|
|
|
rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_IN, RPC_TYPE_BUF, svr_hd)
|
|
|
|
|
|
2023-09-29 01:03:19 +08:00
|
|
|
RPC_GENERATION_CALL2(ns_t, NS_QUERY_OP, query,
|
|
|
|
|
rpc_ref_array_uint32_t_uint8_t_32_t, rpc_array_uint32_t_uint8_t_32_t, RPC_DIR_IN, RPC_TYPE_DATA, path,
|
|
|
|
|
rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_INOUT, RPC_TYPE_BUF, cli_hd)
|
|
|
|
|
|
2023-09-24 01:26:07 +08:00
|
|
|
int ns_register(const char *path, obj_handler_t svr_hd)
|
|
|
|
|
{
|
|
|
|
|
assert(path);
|
|
|
|
|
|
|
|
|
|
rpc_ref_array_uint32_t_uint8_t_32_t rpc_path = {
|
|
|
|
|
.data = path,
|
|
|
|
|
.len = strlen(path) + 1,
|
|
|
|
|
};
|
|
|
|
|
rpc_obj_handler_t_t rpc_svr_hd = {
|
|
|
|
|
.data = svr_hd,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
msg_tag_t tag = ns_t_register_call(u_get_global_env()->ns_hd, &rpc_path, &rpc_svr_hd);
|
|
|
|
|
|
|
|
|
|
return msg_tag_get_val(tag);
|
|
|
|
|
}
|
|
|
|
|
int ns_query(const char *path, obj_handler_t *svr_hd)
|
|
|
|
|
{
|
|
|
|
|
assert(path);
|
|
|
|
|
assert(svr_hd);
|
|
|
|
|
|
|
|
|
|
obj_handler_t newfd;
|
|
|
|
|
|
|
|
|
|
newfd = find_hd(path);
|
|
|
|
|
if (newfd != HANDLER_INVALID)
|
|
|
|
|
{
|
2023-09-24 19:26:05 +08:00
|
|
|
*svr_hd = newfd;
|
|
|
|
|
return 0;
|
2023-09-24 01:26:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newfd = handler_alloc();
|
|
|
|
|
|
|
|
|
|
if (newfd == HANDLER_INVALID)
|
|
|
|
|
{
|
|
|
|
|
return -ENOENT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rpc_ref_array_uint32_t_uint8_t_32_t rpc_path = {
|
|
|
|
|
.data = path,
|
|
|
|
|
.len = strlen(path) + 1,
|
|
|
|
|
};
|
|
|
|
|
rpc_obj_handler_t_t rpc_svr_hd = {
|
|
|
|
|
.data = newfd,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
msg_tag_t tag = ns_t_query_call(u_get_global_env()->ns_hd, &rpc_path, &rpc_svr_hd);
|
|
|
|
|
|
|
|
|
|
if (msg_tag_get_val(tag) < 0)
|
|
|
|
|
{
|
|
|
|
|
handler_free(newfd);
|
|
|
|
|
return msg_tag_get_val(tag);
|
|
|
|
|
}
|
|
|
|
|
if (reg_hd(path, newfd) == FALSE)
|
|
|
|
|
{
|
|
|
|
|
printf("客户端服务缓存区已满.\n");
|
|
|
|
|
handler_free_umap(newfd);
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
}
|
|
|
|
|
*svr_hd = newfd;
|
|
|
|
|
return msg_tag_get_val(tag);
|
|
|
|
|
}
|