-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameClient.cpp
More file actions
716 lines (625 loc) · 14 KB
/
GameClient.cpp
File metadata and controls
716 lines (625 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
#include "GameClient.h"
#include "chess-game.h"
/**
* @brief 客户端构造函数
*
* 执行以下初始化操作:
* 1. 注册所有网络事件的回调函数
* 2. 启动请求超时处理线程
* 3. 初始化同步时间戳
*
* 注意:同步线程在 initGameState() 中启动,而不是在构造函数中
*/
Client::Client(lanp2p::LanP2PNode &node)
: _node(node)
{
// 注册匹配请求回调
_node.setOnMatchRequest([this](const lanp2p::PeerInfo &p, const std::string &mid)
{
this->onMatchRequest(p, mid);
});
// 注册匹配响应回调
_node.setOnMatchResponse([this](const lanp2p::PeerInfo &p, bool a, const std::string &mid)
{
this->onMatchResponse(p, a, mid);
});
// 注册匹配中断回调
_node.setOnMatchInterrupted([this](const lanp2p::PeerInfo &p, const std::string &mid)
{
this->onMatchInterrupted(p, mid);
});
// 注册游戏落子回调
_node.setOnGameMove([this](const lanp2p::PeerInfo &p, int x, int y, int z)
{
this->onGameMove(p, x, y, z);
});
// 注册棋盘同步回调
_node.setOnBoardSync([this](const lanp2p::PeerInfo &p, const std::string &bs)
{
this->onBoardSync(p, bs);
});
// 启动请求超时处理线程
startTimeoutThread();
// 初始化同步时间戳为当前时间
_lastSyncTime = std::chrono::steady_clock::now();
}
/**
* @brief 客户端析构函数
*
* 清理所有资源,执行顺序:
* 1. 如果在匹配中,发送中断消息
* 2. 停止游戏运行标志
* 3. 停止网络节点
* 4. 移除所有网络回调
* 5. 停止所有线程
* 6. 清理游戏状态(释放棋盘内存)
*/
Client::~Client()
{
// 如果正在匹配中,发送中断消息
{
std::lock_guard<std::mutex> lk(_matchMutex);
if (_match.inMatch)
{
_node.interruptMatch(_match.peer.ip, _match.peer.tcpPort, _match.matchId);
_match = MatchState{};
}
}
// 停止游戏运行
_gameRunning = false;
// 停止网络节点
_node.stop();
// 移除所有回调函数
_node.setOnMatchRequest(nullptr);
_node.setOnMatchResponse(nullptr);
_node.setOnMatchInterrupted(nullptr);
_node.setOnGameMove(nullptr);
_node.setOnBoardSync(nullptr);
// 停止线程
stopTimeoutThread();
stopSyncThread();
// 清理游戏资源
cleanupGameState();
}
/**
* @brief 获取当前可用的对等节点列表
* @return 节点信息列表
*/
std::vector<lanp2p::PeerInfo> Client::getAvailablePeers()
{
return _node.getPeersSnapshot();
}
/**
* @brief 请求与指定节点匹配
*
* @param peer 目标节点信息
* @return true 请求已发送,false 当前已在匹配中
*/
bool Client::requestMatch(const lanp2p::PeerInfo &peer)
{
// 防止在已处于对局时再次发起匹配
{
std::lock_guard<std::mutex> lk(_matchMutex);
if (_match.inMatch)
{
return false;
}
}
// 生成随机匹配ID并发送请求
std::string mid = lanp2p::LanP2PNode::generateMatchId();
return _node.sendMatchRequest(peer.ip, peer.tcpPort, mid);
}
/**
* @brief 检查是否正在匹配中
*/
bool Client::isInMatch() const
{
std::lock_guard<std::mutex> lk(_matchMutex);
return _match.inMatch;
}
/**
* @brief 主动结束当前匹配
*
* 执行步骤:
* 1. 读取并清空匹配状态
* 2. 向对手发送中断消息
* 3. 停止游戏运行
* 4. 停止同步线程
*/
void Client::endMatch()
{
// 读取并清空匹配状态
std::string matchId;
lanp2p::PeerInfo peer;
bool wasInMatch = false;
{
std::lock_guard<std::mutex> lk(_matchMutex);
if (!_match.inMatch)
return;
matchId = _match.matchId;
peer = _match.peer;
wasInMatch = true;
_match = MatchState{};
}
// 向对手发送中断消息并清理游戏状态
if (wasInMatch)
{
_node.interruptMatch(peer.ip, peer.tcpPort, matchId);
_gameRunning = false;
stopSyncThread();
}
}
/**
* @brief 获取当前匹配ID
*/
std::string Client::getMatchId() const
{
std::lock_guard<std::mutex> lk(_matchMutex);
return _match.matchId;
}
/**
* @brief 获取对手节点信息
*/
lanp2p::PeerInfo Client::getMatchPeer() const
{
std::lock_guard<std::mutex> lk(_matchMutex);
return _match.peer;
}
/**
* @brief 获取待处理请求列表的快照
*
* @return 待处理请求信息列表
*/
std::vector<Client::PendingRequestInfo> Client::getPendingRequestsSnapshot()
{
std::vector<PendingRequestInfo> out;
std::lock_guard<std::mutex> lk(_pendingMutex);
for (const auto &pr : _pendingQueue)
{
PendingRequestInfo info;
info.matchId = pr.matchId;
info.peer = pr.peer;
info.ip = pr.ip;
info.port = pr.port;
out.push_back(info);
}
return out;
}
/**
* @brief 响应待处理的匹配请求
*
* @param req 请求信息
* @param accept true接受,false拒绝
* @return true 响应成功,false 请求不存在或已过期
*/
bool Client::respondToPendingRequest(const PendingRequestInfo &req, bool accept)
{
PendingRequest target;
bool found = false;
// 从队列中查找并移除指定请求
{
std::lock_guard<std::mutex> lk(_pendingMutex);
for (auto it = _pendingQueue.begin(); it != _pendingQueue.end(); ++it)
{
if (it->matchId == req.matchId && it->peer.id == req.peer.id)
{
target = *it;
_pendingQueue.erase(it);
found = true;
break;
}
}
}
if (!found)
return false;
// 发送响应
_node.respondToMatch(target.ip, target.port, target.matchId, accept);
// 如果接受请求,建立匹配状态
if (accept)
{
std::lock_guard<std::mutex> lk(_matchMutex);
_match.inMatch = true;
_match.peer = target.peer;
_match.matchId = target.matchId;
_iAmMatchInitiator = false; // 响应者不是发起者
_node.markMatchActive(target.ip, target.port, target.peer.id, target.matchId);
}
return true;
}
/**
* @brief 检查是否轮到自己下棋
*/
bool Client::isMyTurn() const
{
return _myTurn;
}
/**
* @brief 获取自己的玩家标识
*/
char Client::getMyPlayer() const
{
return _myPlayer;
}
/**
* @brief 检查游戏是否正在运行
*/
bool Client::isGameRunning() const
{
return _gameRunning.load();
}
/**
* @brief 获取游戏结果
*/
int Client::getGameResult() const
{
return _gameResult;
}
// ========== 网络回调函数 ==========
/**
* @brief 收到匹配请求的回调
*
* 将请求加入待处理队列,等待用户响应
*/
void Client::onMatchRequest(const lanp2p::PeerInfo &p, const std::string &matchId)
{
PendingRequest pr;
pr.has = true;
pr.ip = p.ip;
pr.port = p.tcpPort;
pr.matchId = matchId;
pr.peer = p;
pr.ts = std::chrono::steady_clock::now(); // 记录接收时间
{
std::lock_guard<std::mutex> lk(_pendingMutex);
_pendingQueue.push_back(std::move(pr));
}
}
/**
* @brief 收到匹配响应的回调
*
* 如果对方接受,作为发起者建立匹配状态
*/
void Client::onMatchResponse(const lanp2p::PeerInfo &p, bool accepted, const std::string &matchId)
{
if (accepted)
{
// 我方作为请求发起者时,对方接受后建立本地对局状态
std::lock_guard<std::mutex> lk(_matchMutex);
_match.inMatch = true;
_match.peer = p;
_match.matchId = matchId;
_iAmMatchInitiator = true; // 标记为发起者
}
}
/**
* @brief 收到匹配中断的回调
*
* 清空匹配状态,停止游戏
*/
void Client::onMatchInterrupted(const lanp2p::PeerInfo &p, const std::string &matchId)
{
std::lock_guard<std::mutex> lk(_matchMutex);
if (_match.inMatch && _match.matchId == matchId && _match.peer.id == p.id)
{
_match = MatchState{};
_gameRunning = false;
}
}
/**
* @brief 收到对手落子的回调
*
* 验证并保存对手的落子信息
*/
void Client::onGameMove(const lanp2p::PeerInfo &p, int x, int y, int z)
{
bool shouldProcess = false;
// 验证是否来自当前对手
{
std::lock_guard<std::mutex> lk(_matchMutex);
shouldProcess = (_match.inMatch && p.id == _match.peer.id);
}
if (shouldProcess)
{
// 验证坐标合法性
if (x < 1 || x > _boardSize || y < 1 || y > _boardSize || z < 1 || z > _boardSize)
return;
// 保存对手落子信息
std::lock_guard<std::mutex> lk(_moveMutex);
_opponentMove[0] = x;
_opponentMove[1] = y;
_opponentMove[2] = z;
_opponentMoved = true;
}
}
/**
* @brief 收到棋盘同步数据的回调
*
* 用对方的棋盘状态填补己方棋盘的空位,防止网络丢包导致不同步
*/
void Client::onBoardSync(const lanp2p::PeerInfo &p, const std::string &boardState)
{
std::lock_guard<std::mutex> lk(_matchMutex);
// 验证来源
if (!_match.inMatch || p.id != _match.peer.id)
return;
_isSyncing = true;
if (_chessBoard)
{
// 创建临时棋盘用于解析
char *tempBoard = (char*)calloc(_boardSize * _boardSize * _boardSize, sizeof(char));
if (tempBoard)
{
// 解析对方的棋盘状态
DeserializeBoardState(_boardSize, tempBoard, boardState);
// 只同步空位:如果己方某位置为空但对方有棋子,则补上
for (int i = 0; i < _boardSize * _boardSize * _boardSize; ++i)
{
if (_chessBoard[i] == 0 && tempBoard[i] != 0)
{
_chessBoard[i] = tempBoard[i];
}
}
free(tempBoard);
}
}
_isSyncing = false;
}
// ========== 线程管理 ==========
/**
* @brief 超时处理线程循环
*
* 每200毫秒检查一次队列,移除超时的请求(30秒超时)
*/
void Client::timeoutThreadLoop()
{
while (_timeoutThreadRunning.load())
{
PendingRequest pr;
bool expired = false;
// 检查队列首个请求是否超时
{
std::lock_guard<std::mutex> lk(_pendingMutex);
if (!_pendingQueue.empty())
{
auto &front = _pendingQueue.front();
if (std::chrono::steady_clock::now() - front.ts > _requestTimeout)
{
pr = front;
_pendingQueue.pop_front();
expired = true;
}
}
}
// 如果请求超时,自动拒绝
if (expired && pr.has)
{
_node.respondToMatch(pr.ip, pr.port, pr.matchId, false);
}
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
}
/**
* @brief 启动超时处理线程
*/
void Client::startTimeoutThread()
{
_timeoutThreadRunning = true;
_timeoutThread = std::thread(&Client::timeoutThreadLoop, this);
}
/**
* @brief 停止超时处理线程
*/
void Client::stopTimeoutThread()
{
_timeoutThreadRunning = false;
if (_timeoutThread.joinable())
{
_timeoutThread.join();
}
}
/**
* @brief 同步线程循环
*
* 每5秒向对手发送一次完整的棋盘状态,防止因网络丢包导致不同步
*/
void Client::syncThreadLoop()
{
while (_syncThreadRunning.load())
{
auto now = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(now - _lastSyncTime);
// 检查是否到达同步时间(5秒)
if (elapsed.count() >= 5)
{
bool shouldSync = false;
{
std::lock_guard<std::mutex> lk(_matchMutex);
shouldSync = _match.inMatch && _gameRunning.load();
}
if (shouldSync)
{
_isSyncing = true;
// 序列化当前棋盘状态
std::string boardState = getBoardStateString();
lanp2p::PeerInfo opponent;
{
std::lock_guard<std::mutex> lk(_matchMutex);
opponent = _match.peer;
}
// 发送棋盘状态给对手
_node.sendBoardState(opponent.ip, opponent.tcpPort, boardState);
_lastSyncTime = now;
_isSyncing = false;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}
/**
* @brief 启动同步线程
*/
void Client::startSyncThread()
{
_syncThreadRunning = true;
_syncThread = std::thread(&Client::syncThreadLoop, this);
}
/**
* @brief 停止同步线程
*/
void Client::stopSyncThread()
{
_syncThreadRunning = false;
if (_syncThread.joinable())
{
_syncThread.join();
}
}
/**
* @brief 获取当前棋盘状态的序列化字符串
*/
std::string Client::getBoardStateString() const
{
if (!_chessBoard)
return "";
return SerializeBoardState(_boardSize, _chessBoard);
}
/**
* @brief 初始化游戏状态
*
* 执行步骤:
* 1. 清理旧的游戏状态
* 2. 创建新的棋盘
* 3. 根据匹配ID和角色确定先后手
* 4. 初始化游戏变量
* 5. 启动同步线程
*/
void Client::initGameState()
{
// 清理旧状态(包括停止同步线程)
cleanupGameState();
// 初始化新棋盘
if (!OnlineInitChessBoard(&_chessBoard, _boardSize))
{
endMatch();
return;
}
// 获取匹配ID
std::string matchId;
{
std::lock_guard<std::mutex> lk(_matchMutex);
matchId = _match.matchId;
}
// 根据匹配ID的第一个字符判断奇偶性
bool matchIdIsOdd = false;
if (!matchId.empty())
{
char firstChar = matchId[0];
if (firstChar >= '0' && firstChar <= '9')
matchIdIsOdd = ((firstChar - '0') % 2 == 1);
else if (firstChar >= 'a' && firstChar <= 'f')
matchIdIsOdd = ((firstChar - 'a') % 2 == 1);
else if (firstChar >= 'A' && firstChar <= 'F')
matchIdIsOdd = ((firstChar - 'A') % 2 == 1);
}
// 先后手规则:发起者+奇数ID=先手;响应者+偶数ID=先手
bool iAmFirstPlayer = (_iAmMatchInitiator && matchIdIsOdd) || (!_iAmMatchInitiator && !matchIdIsOdd);
// 初始化游戏变量
_myPlayer = iAmFirstPlayer ? '1' : '2';
_myTurn = (_myPlayer == '1');
_gameRunning = true;
_opponentMoved = false;
_gameResult = 0;
// 确保旧的同步线程已停止,再启动新的
stopSyncThread();
startSyncThread();
}
/**
* @brief 清理游戏状态
*
* 先停止同步线程(防止访问已释放的内存),再释放棋盘内存
*/
void Client::cleanupGameState()
{
// 先停止同步线程,防止访问即将释放的棋盘内存
stopSyncThread();
if (_chessBoard)
{
free(_chessBoard);
_chessBoard = nullptr;
}
}
/**
* @brief 尝试放置己方棋子
*
* @return true 落子成功,false 落子失败
*/
bool Client::tryPlaceMyPiece(int x, int y, int z)
{
// 检查前置条件:必须轮到自己、游戏运行中、未在同步
if (!_myTurn || !_gameRunning.load() || _isSyncing.load())
return false;
int coords[3] = { x, y, z };
// 更新棋盘状态
if (UpdateBoardState(_boardSize, _chessBoard, coords, _myPlayer))
{
// 获取对手信息
lanp2p::PeerInfo opponent;
{
std::lock_guard<std::mutex> lk(_matchMutex);
opponent = _match.peer;
}
// 向对手发送落子信息
_node.sendGameMove(opponent.ip, opponent.tcpPort, coords[0], coords[1], coords[2]);
// 检查是否获胜
if (CheckWin(_boardSize, _chessBoard, coords, _myPlayer))
{
_gameRunning = false;
_gameResult = 1; // 己方获胜
return true;
}
// 交换回合
_myTurn = false;
return true;
}
return false;
}
/**
* @brief 尝试获取对手的落子
*
* @return true 获取成功,false 对手未落子
*/
bool Client::tryGetOpponentMove(int& outX, int& outY, int& outZ)
{
std::lock_guard<std::mutex> lk(_moveMutex);
if (_opponentMoved)
{
// 输出对手落子坐标
outX = _opponentMove[0];
outY = _opponentMove[1];
outZ = _opponentMove[2];
// 确定对手玩家标识
char opponentPlayer = (_myPlayer == '1') ? '2' : '1';
// 更新棋盘
UpdateBoardState(_boardSize, _chessBoard, _opponentMove, opponentPlayer);
// 检查对手是否获胜
if (CheckWin(_boardSize, _chessBoard, _opponentMove, opponentPlayer))
{
_gameRunning = false;
_gameResult = 2; // 对方获胜
}
// 重置标志并交换回合
_opponentMoved = false;
_myTurn = true;
return true;
}
return false;
}
/**
* @brief 获取距离上次同步的秒数
*/
int Client::getTimeSinceLastSync() const
{
auto now = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(now - _lastSyncTime);
return static_cast<int>(elapsed.count());
}