|
|
|
@ -9,6 +9,8 @@ |
|
|
|
|
|
|
|
#ifdef _WIN32 |
|
|
|
#include <windows.h> |
|
|
|
#else |
|
|
|
#include <netdb.h> |
|
|
|
#endif |
|
|
|
|
|
|
|
/*
|
|
|
|
@ -141,52 +143,76 @@ int rpc_call(rpc_client_t* client, const char* func_name, |
|
|
|
} |
|
|
|
|
|
|
|
/*
|
|
|
|
* 演示调用远程加法函数 |
|
|
|
* 调用远程文件信息发送函数 UpFileService.SendFileInfo |
|
|
|
* 假设参数格式:文件名(string)、文件大小(int)、文件类型(string) |
|
|
|
*/ |
|
|
|
int demo_add(rpc_client_t* client, int a, int b, int* result) { |
|
|
|
rpc_param_t params[2]; |
|
|
|
int send_file_info(rpc_client_t* client, const char* file_name, int file_size, const char* file_type, bool* result) { |
|
|
|
rpc_param_t params[3]; |
|
|
|
rpc_param_t return_value; |
|
|
|
|
|
|
|
// 设置参数
|
|
|
|
rpc_init_param(¶ms[0], RPC_TYPE_INT); |
|
|
|
params[0].value.int_val = a; |
|
|
|
rpc_init_param(¶ms[0], RPC_TYPE_STRING); |
|
|
|
params[0].value.string_val = file_name; |
|
|
|
|
|
|
|
rpc_init_param(¶ms[1], RPC_TYPE_INT); |
|
|
|
params[1].value.int_val = b; |
|
|
|
params[1].value.int_val = file_size; |
|
|
|
|
|
|
|
rpc_init_param(¶ms[2], RPC_TYPE_STRING); |
|
|
|
params[2].value.string_val = file_type; |
|
|
|
|
|
|
|
// 设置返回值
|
|
|
|
rpc_init_param(&return_value, RPC_TYPE_INT); |
|
|
|
rpc_init_param(&return_value, RPC_TYPE_BOOL); |
|
|
|
|
|
|
|
// 调用远程函数
|
|
|
|
int ret = rpc_call(client, "add", params, 2, &return_value); |
|
|
|
int ret = rpc_call(client, "UpFileService.SendFileInfo", params, 3, &return_value); |
|
|
|
if (ret == RPC_SUCCESS && result) { |
|
|
|
*result = return_value.value.int_val; |
|
|
|
*result = return_value.value.bool_val; |
|
|
|
} |
|
|
|
|
|
|
|
return ret; |
|
|
|
} |
|
|
|
|
|
|
|
/*
|
|
|
|
* 演示获取服务器信息 |
|
|
|
* 解析命令行参数 |
|
|
|
*/ |
|
|
|
int demo_get_server_info(rpc_client_t* client, char** info) { |
|
|
|
rpc_param_t return_value; |
|
|
|
void parse_args(int argc, char* argv[], const char** server_host, uint16_t* server_port, |
|
|
|
const char** file_name, int* file_size, const char** file_type) { |
|
|
|
// 默认值
|
|
|
|
*server_host = "127.0.0.1"; |
|
|
|
*server_port = 8080; |
|
|
|
*file_name = "example.txt"; |
|
|
|
*file_size = 1024; |
|
|
|
*file_type = "text/plain"; |
|
|
|
|
|
|
|
// 设置返回值
|
|
|
|
rpc_init_param(&return_value, RPC_TYPE_STRING); |
|
|
|
// 解析命令行参数
|
|
|
|
if (argc >= 3) { |
|
|
|
*server_host = argv[1]; |
|
|
|
*server_port = (uint16_t)atoi(argv[2]); |
|
|
|
} |
|
|
|
|
|
|
|
// 调用远程函数
|
|
|
|
int ret = rpc_call(client, "get_server_info", NULL, 0, &return_value); |
|
|
|
if (ret == RPC_SUCCESS && info) { |
|
|
|
*info = strdup(return_value.value.string_val); |
|
|
|
if (argc >= 4) { |
|
|
|
*file_name = argv[3]; |
|
|
|
} |
|
|
|
|
|
|
|
// 释放返回值中的资源
|
|
|
|
if (return_value.type == RPC_TYPE_STRING && return_value.value.string_val) { |
|
|
|
free((void*)return_value.value.string_val); |
|
|
|
if (argc >= 5) { |
|
|
|
*file_size = atoi(argv[4]); |
|
|
|
} |
|
|
|
|
|
|
|
return ret; |
|
|
|
if (argc >= 6) { |
|
|
|
*file_type = argv[5]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/*
|
|
|
|
* 打印帮助信息 |
|
|
|
*/ |
|
|
|
void print_help() { |
|
|
|
printf("Usage: rpc_client <server_host> <server_port> [file_name] [file_size] [file_type]\n"); |
|
|
|
printf(" server_host: RPC服务器地址 (默认: 127.0.0.1)\n"); |
|
|
|
printf(" server_port: RPC服务器端口 (默认: 8080)\n"); |
|
|
|
printf(" file_name: 文件名 (默认: example.txt)\n"); |
|
|
|
printf(" file_size: 文件大小 (默认: 1024)\n"); |
|
|
|
printf(" file_type: 文件类型 (默认: text/plain)\n"); |
|
|
|
} |
|
|
|
|
|
|
|
/*
|
|
|
|
@ -194,8 +220,11 @@ int demo_get_server_info(rpc_client_t* client, char** info) { |
|
|
|
*/ |
|
|
|
int main(int argc, char* argv[]) { |
|
|
|
rpc_client_t client; |
|
|
|
const char* server_host = "127.0.0.1"; |
|
|
|
uint16_t server_port = 8080; |
|
|
|
const char* server_host; |
|
|
|
uint16_t server_port; |
|
|
|
const char* file_name; |
|
|
|
int file_size; |
|
|
|
const char* file_type; |
|
|
|
|
|
|
|
#ifdef _WIN32 |
|
|
|
// 初始化Windows套接字库
|
|
|
|
@ -205,6 +234,22 @@ int main(int argc, char* argv[]) { |
|
|
|
} |
|
|
|
#endif |
|
|
|
|
|
|
|
// 检查是否需要打印帮助信息
|
|
|
|
if (argc >= 2 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)) { |
|
|
|
print_help(); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
// 解析命令行参数
|
|
|
|
parse_args(argc, argv, &server_host, &server_port, &file_name, &file_size, &file_type); |
|
|
|
|
|
|
|
// 打印参数信息
|
|
|
|
printf("RPC Client configuration:\n"); |
|
|
|
printf(" Server: %s:%d\n", server_host, server_port); |
|
|
|
printf(" File name: %s\n", file_name); |
|
|
|
printf(" File size: %d\n", file_size); |
|
|
|
printf(" File type: %s\n", file_type); |
|
|
|
|
|
|
|
// 初始化客户端
|
|
|
|
printf("Connecting to RPC server at %s:%d...\n", server_host, server_port); |
|
|
|
int ret = rpc_client_create(&client, server_host, server_port); |
|
|
|
@ -215,23 +260,17 @@ int main(int argc, char* argv[]) { |
|
|
|
|
|
|
|
printf("Connected to server successfully\n"); |
|
|
|
|
|
|
|
// 测试远程加法函数
|
|
|
|
int a = 10, b = 20, sum; |
|
|
|
ret = demo_add(&client, a, b, &sum); |
|
|
|
// 调用远程文件信息发送函数
|
|
|
|
bool send_result = false; |
|
|
|
ret = send_file_info(&client, file_name, file_size, file_type, &send_result); |
|
|
|
if (ret == RPC_SUCCESS) { |
|
|
|
printf("Remote call: %d + %d = %d\n", a, b, sum); |
|
|
|
} else { |
|
|
|
printf("Failed to call 'add': %s\n", rpc_error_to_string(ret)); |
|
|
|
} |
|
|
|
|
|
|
|
// 测试获取服务器信息
|
|
|
|
char* server_info = NULL; |
|
|
|
ret = demo_get_server_info(&client, &server_info); |
|
|
|
if (ret == RPC_SUCCESS && server_info) { |
|
|
|
printf("Server info: %s\n", server_info); |
|
|
|
free(server_info); |
|
|
|
printf("UpFileService.SendFileInfo call %s\n", send_result ? "succeeded" : "failed"); |
|
|
|
if (send_result) { |
|
|
|
printf("File info sent successfully: %s (size: %d, type: %s)\n", |
|
|
|
file_name, file_size, file_type); |
|
|
|
} |
|
|
|
} else { |
|
|
|
printf("Failed to call 'get_server_info': %s\n", rpc_error_to_string(ret)); |
|
|
|
printf("Failed to call 'UpFileService.SendFileInfo': %s\n", rpc_error_to_string(ret)); |
|
|
|
} |
|
|
|
|
|
|
|
// 清理资源
|
|
|
|
|