You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.3 KiB
81 lines
2.3 KiB
/*
|
|
* rpc_common.h
|
|
* 定义RPC框架的公共数据结构、常量和工具函数
|
|
*/
|
|
|
|
#ifndef RPC_COMMON_H
|
|
#define RPC_COMMON_H
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
|
|
/* 错误码定义 */
|
|
#define RPC_SUCCESS 0 // 成功
|
|
#define RPC_ERROR -1 // 一般错误
|
|
#define RPC_NET_ERROR -2 // 网络错误
|
|
#define RPC_TIMEOUT -3 // 超时错误
|
|
#define RPC_INVALID_ARGS -4 // 参数无效
|
|
#define RPC_FUNC_NOT_FOUND -5 // 函数未找到
|
|
|
|
/* 最大消息大小 */
|
|
#define MAX_MESSAGE_SIZE 4096
|
|
|
|
/* 最大函数名长度 */
|
|
#define MAX_FUNC_NAME_LEN 64
|
|
|
|
/* 最大参数数量 */
|
|
#define MAX_ARGS_COUNT 16
|
|
|
|
/* 参数类型枚举 */
|
|
typedef enum {
|
|
RPC_TYPE_INT,
|
|
RPC_TYPE_FLOAT,
|
|
RPC_TYPE_DOUBLE,
|
|
RPC_TYPE_STRING,
|
|
RPC_TYPE_BOOL,
|
|
RPC_TYPE_VOID
|
|
} rpc_param_type_t;
|
|
|
|
/* 参数数据结构 */
|
|
typedef struct {
|
|
rpc_param_type_t type; // 参数类型
|
|
union {
|
|
int32_t int_val; // 整数值
|
|
float float_val; // 浮点值
|
|
double double_val; // 双精度值
|
|
const char* string_val; // 字符串值
|
|
bool bool_val; // 布尔值
|
|
} value; // 参数值
|
|
} rpc_param_t;
|
|
|
|
/* RPC请求数据结构 */
|
|
typedef struct {
|
|
char func_name[MAX_FUNC_NAME_LEN]; // 函数名
|
|
int args_count; // 参数数量
|
|
rpc_param_t args[MAX_ARGS_COUNT]; // 参数数组
|
|
} rpc_request_t;
|
|
|
|
/* RPC响应数据结构 */
|
|
typedef struct {
|
|
int result_code; // 结果代码
|
|
rpc_param_type_t return_type; // 返回值类型
|
|
union {
|
|
int32_t int_val; // 整数值
|
|
float float_val; // 浮点值
|
|
double double_val; // 双精度值
|
|
const char* string_val; // 字符串值
|
|
bool bool_val; // 布尔值
|
|
} return_value; // 返回值
|
|
} rpc_response_t;
|
|
|
|
/* 工具函数声明 */
|
|
void rpc_init_param(rpc_param_t* param, rpc_param_type_t type);
|
|
void rpc_free_params(rpc_param_t* params, int count);
|
|
void rpc_free_request(rpc_request_t* request);
|
|
void rpc_free_response(rpc_response_t* response);
|
|
const char* rpc_error_to_string(int error_code);
|
|
|
|
#endif /* RPC_COMMON_H */
|