diff --git a/scalib/CMakeLists.txt b/scalib/CMakeLists.txt index 869ac73..ac1b8b5 100644 --- a/scalib/CMakeLists.txt +++ b/scalib/CMakeLists.txt @@ -29,10 +29,8 @@ add_subdirectory(src) list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake) # 安装目标 -install(TARGETS rpc_server rpc_client +install(TARGETS minimal_client rpc_file_client RUNTIME DESTINATION bin - LIBRARY DESTINATION lib - ARCHIVE DESTINATION lib ) # 安装头文件 diff --git a/scalib/src/CMakeLists.txt b/scalib/src/CMakeLists.txt index ffd3002..87d5f78 100644 --- a/scalib/src/CMakeLists.txt +++ b/scalib/src/CMakeLists.txt @@ -1,126 +1,21 @@ -# 输出目录设置由顶层CMakeLists.txt控制 - -# 以下设置已被禁用,使用顶层设置 - -# set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) - -# set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) - -# set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) - - - -# RPC共享库 - -add_library(rpc_common STATIC - - rpc_common.c - - rpc_message.c - - rpc_transport.c - -) - - - -# RPC服务器 - -add_executable(rpc_server - - rpc_server.c - -) - - - -# 根据不同平台链接不同的库 - -if(WIN32) - - target_link_libraries(rpc_server - - rpc_common - - ws2_32 # Windows套接字库 - - ) - -else() - - target_link_libraries(rpc_server - - rpc_common - - pthread - - ) - -endif() - - - -# RPC客户端 -add_executable(rpc_client - rpc_client.c +# 最小化客户端(仅标准C库) +add_executable(minimal_client + minimal_client.c ) -# 根据不同平台链接不同的库 -if(WIN32) - target_link_libraries(rpc_client - rpc_common - ws2_32 # Windows套接字库 - ) -else() - target_link_libraries(rpc_client - rpc_common - pthread - ) -endif() - -# 简化版RPC客户端 -add_executable(rpc_client_simple - rpc_client_simple.c +# RPC文件客户端(支持UpFileService.SendFileInfo调用) +add_executable(rpc_file_client + rpc_file_client.c ) -# 简化版客户端链接(不依赖rpc_common,直接使用系统API) +# RPC文件客户端链接 if(WIN32) - target_link_libraries(rpc_client_simple + target_link_libraries(rpc_file_client ws2_32 # Windows套接字库 ) -else() - target_link_libraries(rpc_client_simple - pthread - ) endif() -# 基础版RPC客户端(不包含网络功能) -add_executable(rpc_client_basic - rpc_client_basic.c -) - -# 基础版客户端链接 -if(WIN32) - target_link_libraries(rpc_client_basic - ws2_32 # Windows套接字库 - ) -endif() - -# 最小化客户端(仅标准C库) -add_executable(minimal_client - minimal_client.c -) - - - # 安装目标 - -install(TARGETS rpc_common rpc_server rpc_client - +install(TARGETS minimal_client rpc_file_client RUNTIME DESTINATION bin - - LIBRARY DESTINATION lib - - ARCHIVE DESTINATION lib - ) \ No newline at end of file diff --git a/scalib/src/minimal_client.c b/scalib/src/minimal_client.c index 93abf64..1dfda7b 100644 --- a/scalib/src/minimal_client.c +++ b/scalib/src/minimal_client.c @@ -2,6 +2,129 @@ #include #include +#ifdef _WIN32 + #include + #include + #pragma comment(lib, "ws2_32.lib") + #define close closesocket +#else + #include + #include + #include + #include +#endif + +// 文件信息结构体 +typedef struct { + char filepath[256]; + long long file_size; + time_t modify_time; +} FileInfo; + +// 初始化Winsock库(Windows平台) +#ifdef _WIN32 +int init_winsock() { + WSADATA wsaData; + if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { + printf("Winsock initialization failed\n"); + return -1; + } + return 0; +} + +// 清理Winsock库(Windows平台) +void cleanup_winsock() { + WSACleanup(); +} +#else +#define init_winsock() 0 +#define cleanup_winsock() +#endif + +// UpFileService.SendFileInfo RPC调用函数 +int send_file_info(const char* server, int port, const char* filepath) { + // 创建套接字 + int sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd < 0) { + printf("Failed to create socket\n"); + return -1; + } + + // 设置服务器地址 + struct sockaddr_in server_addr; + memset(&server_addr, 0, sizeof(server_addr)); + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(port); + + // 将服务器地址字符串转换为网络地址 + #ifdef _WIN32 + if (inet_pton(AF_INET, server, &(server_addr.sin_addr)) <= 0) { + #else + if (inet_aton(server, &(server_addr.sin_addr)) == 0) { + #endif + printf("Invalid server address\n"); + close(sockfd); + return -1; + } + + // 连接到服务器 + if (connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) { + printf("Failed to connect to server\n"); + close(sockfd); + return -1; + } + + printf("Connected to UpFileService at %s:%d\n", server, port); + + // 准备文件信息 + FileInfo file_info; + strncpy(file_info.filepath, filepath, sizeof(file_info.filepath) - 1); + file_info.filepath[sizeof(file_info.filepath) - 1] = '\0'; + + // 这里简化处理,实际应用中应该从文件系统获取真实的文件大小和修改时间 + file_info.file_size = 0; + file_info.modify_time = time(NULL); + + // 构造RPC请求:调用UpFileService.SendFileInfo方法 + char rpc_request[1024]; + sprintf(rpc_request, "RPC CALL: UpFileService.SendFileInfo\n" \ + "Filepath: %s\n" \ + "FileSize: %lld\n" \ + "ModifyTime: %lld\n", + file_info.filepath, file_info.file_size, file_info.modify_time); + + // 发送RPC请求 + if (send(sockfd, rpc_request, strlen(rpc_request), 0) < 0) { + printf("Failed to send RPC request\n"); + close(sockfd); + return -1; + } + + printf("Sent file info to server: %s\n", filepath); + + // 接收服务器响应 + char buffer[1024] = {0}; + int bytes_read = recv(sockfd, buffer, sizeof(buffer) - 1, 0); + if (bytes_read < 0) { + printf("Failed to receive response\n"); + close(sockfd); + return -1; + } else if (bytes_read == 0) { + printf("Server closed connection\n"); + close(sockfd); + return -1; + } + + // 输出服务器响应 + printf("Server response: %s\n", buffer); + + // 关闭套接字 + close(sockfd); + + return 0; +} + +// 最小化的RPC客户端示例 int main(int argc, char* argv[]) { const char* server = "127.0.0.1"; int port = 8080; @@ -12,12 +135,25 @@ int main(int argc, char* argv[]) { port = atoi(argv[2]); } + // 初始化网络库 + if (init_winsock() != 0) { + return 1; + } + printf("Client connecting to %s:%d\n", server, port); printf("Enter file path: "); fgets(filepath, sizeof(filepath), stdin); filepath[strcspn(filepath, "\n")] = '\0'; - printf("File: %s\n", filepath); + // 调用UpFileService.SendFileInfo方法 + if (send_file_info(server, port, filepath) != 0) { + printf("Failed to call UpFileService.SendFileInfo\n"); + cleanup_winsock(); + return 1; + } + + // 清理网络库 + cleanup_winsock(); return 0; } \ No newline at end of file diff --git a/scalib/src/rpc_file_client.c b/scalib/src/rpc_file_client.c new file mode 100644 index 0000000..29f8783 --- /dev/null +++ b/scalib/src/rpc_file_client.c @@ -0,0 +1,135 @@ +#include +#include +#include +#include + +#ifdef _WIN32 + #include + #include + #pragma comment(lib, "ws2_32.lib") + #define CLOSE_SOCKET(s) closesocket(s) +#else + #include + #include + #include + #include + #define CLOSE_SOCKET(s) close(s) + #define SOCKET int + #define INVALID_SOCKET -1 + #define SOCKET_ERROR -1 +#endif + +// RPC client for UpFileService +int send_file_info_rpc(const char* server_ip, int server_port, const char* file_path) { + #ifdef _WIN32 + WSADATA wsaData; + if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { + printf("WSAStartup failed\n"); + return -1; + } + #endif + + // Create socket + SOCKET sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock == INVALID_SOCKET) { + printf("Socket creation failed\n"); + #ifdef _WIN32 + WSACleanup(); + #endif + return -1; + } + + // Server address structure + struct sockaddr_in server_addr; + memset(&server_addr, 0, sizeof(server_addr)); + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(server_port); + + // Convert IP address + #ifdef _WIN32 + if (inet_pton(AF_INET, server_ip, &(server_addr.sin_addr)) <= 0) { + #else + if (inet_aton(server_ip, &(server_addr.sin_addr)) == 0) { + #endif + printf("Invalid server IP address\n"); + CLOSE_SOCKET(sock); + #ifdef _WIN32 + WSACleanup(); + #endif + return -1; + } + + // Connect to server + if (connect(sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) == SOCKET_ERROR) { + printf("Connection to server failed\n"); + CLOSE_SOCKET(sock); + #ifdef _WIN32 + WSACleanup(); + #endif + return -1; + } + + printf("Connected to UpFileService at %s:%d\n", server_ip, server_port); + + // Prepare RPC request for UpFileService.SendFileInfo + char request[1024]; + time_t now = time(NULL); + + sprintf(request, "{\"service\":\"UpFileService\",\"method\":\"SendFileInfo\",\"params\":{\"filepath\":\"%s\",\"timestamp\":%lld}}", + file_path, (long long)now); + + // Send RPC request + if (send(sock, request, strlen(request), 0) == SOCKET_ERROR) { + printf("Failed to send RPC request\n"); + CLOSE_SOCKET(sock); + #ifdef _WIN32 + WSACleanup(); + #endif + return -1; + } + + printf("Sent file info: %s\n", file_path); + + // Receive response (simple implementation) + char buffer[1024] = {0}; + int bytes_received = recv(sock, buffer, sizeof(buffer) - 1, 0); + if (bytes_received > 0) { + printf("Server response: %s\n", buffer); + } else { + printf("No response from server\n"); + } + + // Cleanup + CLOSE_SOCKET(sock); + #ifdef _WIN32 + WSACleanup(); + #endif + + return 0; +} + +// 函数主要入口 +int main(int argc, char* argv[]) { + const char* server_ip = "127.0.0.1"; + int server_port = 8080; + char file_path[256] = {0}; + + // Parse command line arguments + if (argc >= 3) { + server_ip = argv[1]; + server_port = atoi(argv[2]); + } + + // Get file path from user + printf("Enter file path: "); + fgets(file_path, sizeof(file_path), stdin); + file_path[strcspn(file_path, "\n")] = '\0'; + + // Call UpFileService.SendFileInfo + if (send_file_info_rpc(server_ip, server_port, file_path) != 0) { + printf("RPC call failed\n"); + return 1; + } + + return 0; +} \ No newline at end of file