4 changed files with 282 additions and 118 deletions
@ -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 |
|||
) |
|||
|
|||
# 根据不同平台链接不同的库 |
|||
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_common,直接使用系统API) |
|||
if(WIN32) |
|||
target_link_libraries(rpc_client_simple |
|||
ws2_32 # Windows套接字库 |
|||
) |
|||
else() |
|||
target_link_libraries(rpc_client_simple |
|||
pthread |
|||
# 最小化客户端(仅标准C库) |
|||
add_executable(minimal_client |
|||
minimal_client.c |
|||
) |
|||
endif() |
|||
|
|||
# 基础版RPC客户端(不包含网络功能) |
|||
add_executable(rpc_client_basic |
|||
rpc_client_basic.c |
|||
# RPC文件客户端(支持UpFileService.SendFileInfo调用) |
|||
add_executable(rpc_file_client |
|||
rpc_file_client.c |
|||
) |
|||
|
|||
# 基础版客户端链接 |
|||
# RPC文件客户端链接 |
|||
if(WIN32) |
|||
target_link_libraries(rpc_client_basic |
|||
target_link_libraries(rpc_file_client |
|||
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 |
|||
|
|||
) |
|||
@ -0,0 +1,135 @@ |
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
#include <time.h> |
|||
|
|||
#ifdef _WIN32 |
|||
#include <winsock2.h> |
|||
#include <ws2tcpip.h> |
|||
#pragma comment(lib, "ws2_32.lib") |
|||
#define CLOSE_SOCKET(s) closesocket(s) |
|||
#else |
|||
#include <unistd.h> |
|||
#include <sys/socket.h> |
|||
#include <netinet/in.h> |
|||
#include <arpa/inet.h> |
|||
#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; |
|||
} |
|||
Loading…
Reference in new issue