Files
Gobang-Game/include/network.h
T
Serendipity 96a94aaddf feat: 集成大模型AI、重构构建系统并修复多项代码质量问题
- 构建系统:Makefile 迁移至 CMakeLists.txt,支持 cJSON 和 WinHTTP
- 项目结构:src/ 按功能拆分为 core/、gui/、network/、record/、llm/ 子目录
- 新功能:集成大模型 AI(WinHTTP + cJSON,兼容 OpenAI 协议),支持异步请求
- 渲染修复:IupDraw* 替换为 Windows GDI,修复画布黑屏问题
- 网络修复:ENet 初始化幂等化,实现真实 get_local_ip() (Winsock)
- 代码质量:删除死代码 (dfs/count_threats_in_direction),修复头文件守卫,
  sprintf→snprintf 防溢出,strncpy 安全终止,GDI 资源泄漏修复

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-02 15:32:54 +08:00

131 lines
2.8 KiB
C

/**
* @file network.h
* @brief 五子棋网络对战模块头文件
* @note 本文件定义了五子棋游戏的网络对战功能:
* 1. 服务器模式(主机)
* 2. 客户端模式(加入游戏)
* 3. 网络消息传输
*/
#ifndef NETWORK_H
#define NETWORK_H
#include "type.h"
#include "config.h"
#include <stdbool.h>
#include <enet/enet.h>
// network_state 的 extern 声明在 globals.h 中
// 函数声明
/**
* @brief 初始化网络模块
* @return true 初始化成功
* @return false 初始化失败
*/
bool init_network();
/**
* @brief 清理网络模块
*/
void cleanup_network();
/**
* @brief 创建服务器(主机模式)
* @param port 监听端口
* @return true 创建成功
* @return false 创建失败
*/
bool create_server(int port);
/**
* @brief 连接到服务器(客户端模式)
* @param ip 服务器IP地址
* @param port 服务器端口
* @return true 连接成功
* @return false 连接失败
*/
bool connect_to_server(const char *ip, int port);
/**
* @brief 发送网络消息
* @param msg 要发送的消息
* @return true 发送成功
* @return false 发送失败
*/
bool send_network_message(const NetworkMessage *msg);
/**
* @brief 接收网络消息
* @param msg 接收消息的缓冲区
* @param timeout_ms 超时时间(毫秒),0表示不阻塞等待
* @return true 接收成功
* @return false 接收失败或超时
*/
bool receive_network_message(NetworkMessage *msg, int timeout_ms);
/**
* @brief 断开网络连接
*/
void disconnect_network();
/**
* @brief 检查网络连接状态
* @return true 连接正常
* @return false 连接断开
*/
bool is_network_connected();
/**
* @brief 获取本机IP地址
* @param ip_buffer 存储IP地址的缓冲区
* @param buffer_size 缓冲区大小
* @return true 获取成功
* @return false 获取失败
*/
bool get_local_ip(char *ip_buffer, int buffer_size);
/**
* @brief 发送落子消息
* @param x 行坐标
* @param y 列坐标
* @param player_id 玩家ID
* @return true 发送成功
* @return false 发送失败
*/
bool send_move(int x, int y, int player_id);
/**
* @brief 发送聊天消息
* @param message 聊天内容
* @return true 发送成功
* @return false 发送失败
*/
bool send_chat_message(const char *message);
/**
* @brief 发送认输消息
* @return true 发送成功
* @return false 发送失败
*/
bool send_surrender();
/**
* @brief 发送悔棋请求
* @param steps 悔棋步数
* @return true 发送成功
* @return false 发送失败
*/
bool send_undo_request(int steps);
/**
* @brief 发送悔棋回应
* @param accepted 是否同意悔棋
* @param steps 悔棋步数
* @return true 发送成功
* @return false 发送失败
*/
bool send_undo_response(bool accepted, int steps);
#endif // NETWORK_H