From 76da33043b27f4b8b432d4adaba102676d207cd2 Mon Sep 17 00:00:00 2001 From: xc Date: Sun, 28 Sep 2025 17:37:06 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E6=B5=8B=E8=AF=95=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=92=8C=E7=A9=BA=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scalib/include/rpc_message.h | 115 +++++++++++++++++------------------ scalib/src/rpc_transport.c | 23 +++---- 2 files changed, 64 insertions(+), 74 deletions(-) diff --git a/scalib/include/rpc_message.h b/scalib/include/rpc_message.h index a37abf3..d3d19d5 100644 --- a/scalib/include/rpc_message.h +++ b/scalib/include/rpc_message.h @@ -1,59 +1,58 @@ -/* - * rpc_message.h - * 定义RPC消息的序列化和反序列化接口 - */ - -#ifndef RPC_MESSAGE_H -#define RPC_MESSAGE_H - -#include "rpc_common.h" -#include "rpc_transport.h" - -/* 消息类型枚举 */ -typedef enum { - RPC_MESSAGE_REQUEST, // 请求消息 - RPC_MESSAGE_RESPONSE // 响应消息 -} rpc_message_type_t; - -/* RPC消息头部 */ -typedef struct { - rpc_message_type_t type; // 消息类型 - uint32_t payload_size; // 负载大小 -} rpc_message_header_t; - -/* RPC消息结构 */ -typedef struct { - rpc_message_header_t header; // 消息头部 - union { - rpc_request_t request; // 请求消息 - rpc_response_t response; // 响应消息 - } payload; // 消息负载 -} rpc_message_t; - -/* 消息处理函数声明 */ - -// 序列化请求消息到缓冲区 -int rpc_serialize_request(const rpc_request_t* request, void* buffer, size_t buffer_size); - -// 反序列化缓冲区到请求消息 -int rpc_deserialize_request(const void* buffer, size_t buffer_size, rpc_request_t* request); - -// 序列化响应消息到缓冲区 -int rpc_serialize_response(const rpc_response_t* response, void* buffer, size_t buffer_size); - -// 反序列化缓冲区到响应消息 -int rpc_deserialize_response(const void* buffer, size_t buffer_size, rpc_response_t* response); - -// 发送请求消息 -int rpc_send_request(rpc_transport_t* transport, const rpc_request_t* request); - -// 接收请求消息 -int rpc_recv_request(rpc_transport_t* transport, rpc_request_t* request); - -// 发送响应消息 -int rpc_send_response(rpc_transport_t* transport, const rpc_response_t* response); - -// 接收响应消息 -int rpc_recv_response(rpc_transport_t* transport, rpc_response_t* response); - +/* + * rpc_message.h + * 定义RPC消息的序列化和反序列化接口 + */ +#ifndef RPC_MESSAGE_H +#define RPC_MESSAGE_H + +#include "rpc_common.h" +#include "rpc_transport.h" + +/* 消息类型枚举 */ +typedef enum { + RPC_MESSAGE_REQUEST, // 请求消息 + RPC_MESSAGE_RESPONSE // 响应消息 +} rpc_message_type_t; + +/* RPC消息头部 */ +typedef struct { + rpc_message_type_t type; // 消息类型 + uint32_t payload_size; // 负载大小 +} rpc_message_header_t; + +/* RPC消息结构 */ +typedef struct { + rpc_message_header_t header; // 消息头部 + union { + rpc_request_t request; // 请求消息 + rpc_response_t response; // 响应消息 + } payload; // 消息负载 +} rpc_message_t; + +/* 消息处理函数声明 */ + +// 序列化请求消息到缓冲区 +int rpc_serialize_request(const rpc_request_t* request, void* buffer, size_t buffer_size); + +// 反序列化缓冲区到请求消息 +int rpc_deserialize_request(const void* buffer, size_t buffer_size, rpc_request_t* request); + +// 序列化响应消息到缓冲区 +int rpc_serialize_response(const rpc_response_t* response, void* buffer, size_t buffer_size); + +// 反序列化缓冲区到响应消息 +int rpc_deserialize_response(const void* buffer, size_t buffer_size, rpc_response_t* response); + +// 发送请求消息 +int rpc_send_request(rpc_transport_t* transport, const rpc_request_t* request); + +// 接收请求消息 +int rpc_recv_request(rpc_transport_t* transport, rpc_request_t* request); + +// 发送响应消息 +int rpc_send_response(rpc_transport_t* transport, const rpc_response_t* response); + +// 接收响应消息 +int rpc_recv_response(rpc_transport_t* transport, rpc_response_t* response); + #endif /* RPC_MESSAGE_H */ \ No newline at end of file diff --git a/scalib/src/rpc_transport.c b/scalib/src/rpc_transport.c index 65f820a..793e7bf 100644 --- a/scalib/src/rpc_transport.c +++ b/scalib/src/rpc_transport.c @@ -5,12 +5,6 @@ #include "rpc_transport.h" -// 调试测试代码 -void debug_test_struct_definition() { - rpc_server_t server; - server.address.sin_family = AF_INET; // 测试访问address成员 -} - #ifdef _WIN32 /* @@ -35,6 +29,7 @@ void rpc_winsock_cleanup() { #endif +#ifdef _WIN32 /* * Windows平台的错误打印函数 */ @@ -101,7 +96,9 @@ int rpc_server_init(rpc_server_t* server, const char* host, uint16_t port, int b server->address.sin_port = htons(port); // 绑定地址到套接字 - if (bind(server->server_fd, (struct sockaddr*)&server->address, sizeof(server->address)) != 0) { + struct sockaddr_in* addr_ptr = &server->address; + int addr_len = sizeof(server->address); + if (bind(server->server_fd, (struct sockaddr*)addr_ptr, addr_len) != 0) { PRINT_ERROR("bind failed"); CLOSE_SOCKET(server->server_fd); return RPC_NET_ERROR; @@ -244,22 +241,16 @@ int rpc_client_init(rpc_transport_t* transport, const char* server_host, uint16_ // 连接到服务器 - if (connect(transport->socket_fd, (struct sockaddr*)&transport->address, sizeof(transport->address)) != 0) { - + struct sockaddr_in* conn_addr_ptr = &transport->address; + int conn_addr_len = sizeof(transport->address); + if (connect(transport->socket_fd, (struct sockaddr*)conn_addr_ptr, conn_addr_len) != 0) { PRINT_ERROR("connection failed"); - CLOSE_SOCKET(transport->socket_fd); - return RPC_NET_ERROR; - } - - printf("Connected to server %s:%d\n", server_host, server_port); - return RPC_SUCCESS; - }