mirror of
https://github.com/LHY0125/Gobang-Game.git
synced 2026-05-09 18:09:46 +08:00
feat(network): 集成ENet库并实现局域网联机对战功能
- 添加ENet库作为网络通信基础,替换原有的原生Socket实现 - 扩展游戏模式支持局域网联机对战(PvP网络模式) - 重构网络状态结构以适配ENet的Host/Peer模型 - 在图形界面中添加网络对战菜单,支持创建房间和加入房间 - 实现网络消息的发送与接收,包括落子、断开连接等消息类型 - 为网络对战添加定时器轮询机制,实时处理网络事件 - 更新构建系统以编译和链接ENet库
This commit is contained in:
@@ -23,6 +23,9 @@ int screen_to_board(int screen_x, int screen_y, int *board_x, int *board_y);
|
||||
|
||||
// 游戏窗口 (在 gui_game.c 中定义)
|
||||
void create_game_window();
|
||||
void start_pvp_game_gui();
|
||||
void start_pve_game_gui();
|
||||
void start_network_game_gui(); // 新增:启动网络对战游戏窗口
|
||||
int action_cb(Ihandle *ih);
|
||||
int button_cb(Ihandle *ih, int button, int pressed, int x, int y, char *status);
|
||||
int k_any_cb(Ihandle *ih, int c);
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
#ifndef GUI_MENU_H
|
||||
#define GUI_MENU_H
|
||||
|
||||
#include <iup.h>
|
||||
|
||||
// 主菜单对话框句柄,需要暴露给其他模块(如返回菜单时)
|
||||
extern Ihandle *menu_dlg;
|
||||
|
||||
/**
|
||||
* @brief 创建并显示主菜单
|
||||
*/
|
||||
|
||||
+7
-18
@@ -10,27 +10,16 @@
|
||||
#ifndef NETWORK_H
|
||||
#define NETWORK_H
|
||||
|
||||
#include "gobang.h"
|
||||
#include "type.h"
|
||||
#include "config.h"
|
||||
#include <stdbool.h>
|
||||
#include <enet/enet.h> // 引入 ENet 头文件
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#ifdef _MSC_VER
|
||||
#pragma comment(lib, "ws2_32.lib")
|
||||
#endif
|
||||
#else
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#define SOCKET int
|
||||
#define INVALID_SOCKET -1
|
||||
#define SOCKET_ERROR -1
|
||||
#define closesocket close
|
||||
#endif
|
||||
// 网络状态结构体在 type.h 中定义,我们需要修改 type.h 来包含 ENet 类型
|
||||
// 或者在这里重新定义(如果 type.h 中可以移除旧的定义)
|
||||
|
||||
// 全局网络状态
|
||||
extern NetworkGameState network_state;
|
||||
|
||||
// 函数声明
|
||||
|
||||
@@ -74,7 +63,7 @@ bool send_network_message(const NetworkMessage *msg);
|
||||
/**
|
||||
* @brief 接收网络消息
|
||||
* @param msg 接收消息的缓冲区
|
||||
* @param timeout_ms 超时时间(毫秒),0表示阻塞等待
|
||||
* @param timeout_ms 超时时间(毫秒),0表示不阻塞等待
|
||||
* @return true 接收成功
|
||||
* @return false 接收失败或超时
|
||||
*/
|
||||
|
||||
+9
-6
@@ -80,15 +80,18 @@ typedef struct
|
||||
* @brief 网络游戏状态结构
|
||||
* @details 用于管理网络游戏状态
|
||||
*/
|
||||
// ENet 头文件需要前置声明或直接包含,但在 type.h 中包含可能引起循环依赖
|
||||
// 我们可以使用 void* 来避免在 type.h 中包含 enet.h
|
||||
typedef struct
|
||||
{
|
||||
uintptr_t socket; // 套接字 (使用uintptr_t代替SOCKET以避免引入winsock2.h)
|
||||
bool is_server; // 是否为服务器
|
||||
bool is_connected; // 是否已连接
|
||||
int local_player_id; // 本地玩家ID
|
||||
void *host; // ENetHost * (Server 或 Client)
|
||||
void *peer; // ENetPeer * (连接的对象)
|
||||
bool is_server; // 是否为服务器(主机)
|
||||
bool is_connected; // 是否已连接
|
||||
int local_player_id; // 本地玩家ID
|
||||
int remote_player_id; // 远程玩家ID
|
||||
char remote_ip[16]; // 远程IP地址(MAX_IP_LENGTH = 16)
|
||||
int port; // 端口号
|
||||
char remote_ip[64]; // 远程IP
|
||||
int port; // 端口
|
||||
} NetworkGameState;
|
||||
|
||||
#endif // TYPE_H
|
||||
Reference in New Issue
Block a user