-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetsession.cpp
462 lines (361 loc) · 10.6 KB
/
netsession.cpp
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
//-----------------------------------------------------------------------------
// File: netSession
// Desc: net session
// Auth: Lyp
// Date: 2003/11/13
//
// Copyright (c) 1985-2003 CTCRST Entertainment All rights reserved.
//-----------------------------------------------------------------------------
#include "stdafx.h"
#include "gDefine.h"
#include "gApp.h"
#include "gGame.h"
#include "netSession.h"
//-----------------------------------------------------------------------------
// construction/destruction
//-----------------------------------------------------------------------------
CSession::CSession(CApp* pApp)
{
ZeroMemory(this, sizeof(*this));
m_pApp = pApp;
// 客户端是否已经初始化
m_bClientInitialized = false;
m_pWinTcp = new CWinTcp;
m_pSync = NULL;
// m_pSync = new CWinTcp; // TEST
// 未组织的游戏信息
m_nUncomposeGameMsg = 0;
m_nMaxUncomposeGameMsg = BC_MAX_GAME_MSG * 2;
}
CSession::~CSession()
{
SAFE_DEL(m_pWinTcp);
SAFE_DEL(m_pSync);
}
//-----------------------------------------------------------------------------
// init/destroy
//-----------------------------------------------------------------------------
BOOL CSession::Init(bool bServer)
{
m_dwLatency = 3;
m_bServer = bServer;
m_bRequireWorldInfoSended = false;
m_bClientInitialized = false;
m_bClientPlayerActived = false;
m_dwWorldInfoSendTick = 0;
m_dwLastGameTick = ~0;
m_dwGameTick = 1;
m_nUncomposeGameMsg = 0;
ZeroMemory(m_UncomposeArray, sizeof(m_UncomposeArray));
// 已经组织好的游戏信息
m_pGameMsg = new CMsgList;
if( m_pSync )
m_pSync->Init(BC_SYNC_PORT, 3, FALSE);
return m_pWinTcp->Init(BC_PORT, BC_MAX_PLAYER, m_bServer);
}
void CSession::Destroy()
{
SAFE_DEL(m_pGameMsg);
m_pWinTcp->Destroy();
if( m_pSync )
m_pSync->Destroy();
}
//-----------------------------------------------------------------------------
// update
//-----------------------------------------------------------------------------
bool CSession::Update()
{
if( ( !m_bServer && !m_pWinTcp->Connected() )
|| ( m_bServer && m_pWinTcp->GetClientNum()<1 ) )
return true;
if( m_bServer && m_dwGameTick == 1 )
{
SendLatencyCmd();
}
if( !m_bServer && !m_bRequireWorldInfoSended )
{
DWORD dwCmd = GT_NET_CMD_REQ_WORLD_INFO;
m_pWinTcp->Send((BYTE*)&dwCmd, 4);
m_bRequireWorldInfoSended = true;
}
GameMsg TempMsg;
ZeroMemory(&TempMsg, sizeof(TempMsg));
BYTE byLocalInput = 0;
m_pApp->GetDInput()->GetKey(byLocalInput);
if( m_bServer )
{
if( byLocalInput != 0 )
{
TempMsg.dwCmd = GT_NET_CMD_PLAYER;
TempMsg.dwTick = m_dwGameTick;
TempMsg.byComputer = 1;
TempMsg.byPlayerInput[0] = byLocalInput;
TempMsg.byPlayerInput[1] = 0;
AddUncomposeMsg( (BYTE*)&TempMsg );
}
if( m_dwLastGameTick != m_dwGameTick )
{
ComposeAndSendGameMsg(m_dwGameTick+m_dwLatency);
m_dwLastGameTick = m_dwGameTick;
}
}
else
{
// 客户端初始化好后才能将操作传送出去
if( m_bClientInitialized )
{
// if( byLocalInput != 0 )
{
TempMsg.dwCmd = GT_NET_CMD_PLAYER;
TempMsg.dwTick = m_dwGameTick;
TempMsg.byComputer = 2;
TempMsg.byPlayerInput[0] = 0;
TempMsg.byPlayerInput[1] = byLocalInput;
m_pWinTcp->Send( (BYTE*)&TempMsg, sizeof(TempMsg) );
}
}
}
BYTE* pNetMsg = NULL;
DWORD dwNetMsgSize = 0;
DWORD dwSenderID;
for( int n=0; n<3; n++ )
{
pNetMsg = NULL;
if( FALSE == m_pWinTcp->Recv(&pNetMsg, dwNetMsgSize, dwSenderID) )
continue;
switch( *(DWORD*)pNetMsg )
{
case GT_NET_CMD_CUT_LINE: // 断线信息
SendCutLineMsg();
TSMemMgr::Free(pNetMsg);
Sleep(200);
return false;
break;
case GT_NET_CMD_TICK: // 加工好的游戏信息
m_pGameMsg->AddMsg(pNetMsg, dwNetMsgSize, ((GameMsg*)pNetMsg)->dwTick );
ASSERT( dwNetMsgSize == sizeof(TempMsg) );
break;
case GT_NET_CMD_PLAYER:
memcpy(&TempMsg, pNetMsg, dwNetMsgSize);
if( TempMsg.byPlayerInput[0] != 0
|| TempMsg.byPlayerInput[1] != 0 )
AddUncomposeMsg( (BYTE*)&TempMsg );
break;
case GT_NET_CMD_REQ_WORLD_INFO: // 需要游戏世界信息
ASSERT(m_bServer);
SendWorldInfo(dwSenderID);
break;
case GT_NET_CMD_WORLD_INFO: // 游戏世界信息
if( !m_bServer )
SetWorldInfo(pNetMsg);
break;
}
TSMemMgr::Free(pNetMsg);
}
// 运转游戏世界
GameMsg* pTempGameMsg = NULL;
if( m_pGameMsg->PeekMsg( (BYTE**)&pTempGameMsg, m_dwGameTick ) )
{
if( m_pSync ) //m_pSync->Connected() )
{
DWORD dwZoneSize = m_pApp->GetSnapshot()->GetAllSize();
void* pMem = MemMgr::Alloc(dwZoneSize+4+4);
DWORD dwCmd = GT_NET_CMD_SYNC;
DWORD dwGameTick = m_dwGameTick;
memcpy(pMem, &dwCmd, 4);
memcpy((BYTE*)pMem+4, &dwGameTick, 4);
m_pApp->GetSnapshot()->Snapshot((BYTE*)pMem+4+4);
m_pSync->Send( (BYTE*)pMem, dwZoneSize+4+4 );
MemMgr::Free(pMem);
}
m_pApp->GetGame()->Update(m_dwGameTick++, (BYTE*)&(pTempGameMsg->byPlayerInput[0]));
if( pTempGameMsg->bActivePlayer )
m_pApp->GetGame()->ActivatePlayer(1);
MemMgr::Free(pTempGameMsg);
}
// 网络同步弹簧机制
if( !m_bServer )
{
if( GetReadyMsgNum() <= 1 )
m_pApp->SetSlowdown();
if( GetReadyMsgNum() > (int)m_dwLatency )
m_pApp->SetSpeedup();
}
return true;
}
//-----------------------------------------------------------------------------
// 发送延迟命令
//-----------------------------------------------------------------------------
void CSession::SendLatencyCmd()
{
GameMsg TempMsg;
ZeroMemory( &TempMsg, sizeof(TempMsg) );
TempMsg.dwCmd = GT_NET_CMD_TICK;
TempMsg.dwTick = m_dwGameTick;
TempMsg.byComputer = 0;
for( int n=0; n<(int)m_dwLatency; n++ )
{
TempMsg.dwTick = m_dwGameTick+n;
m_pWinTcp->Send( (BYTE*)&TempMsg, sizeof(TempMsg) );
}
}
//-----------------------------------------------------------------------------
// 发送游戏世界信息
//-----------------------------------------------------------------------------
void CSession::SendWorldInfo(DWORD dwReceiverID)
{
DWORD dwZoneSize = m_pApp->GetSnapshot()->GetAllSize();
void* pMem = MemMgr::Alloc(dwZoneSize+4+4);
DWORD dwCmd = GT_NET_CMD_WORLD_INFO;
DWORD dwGameTick = m_dwGameTick;
m_dwWorldInfoSendTick = dwGameTick;
memcpy(pMem, &dwCmd, 4);
memcpy((BYTE*)pMem+4, &dwGameTick, 4);
m_pApp->GetSnapshot()->Snapshot((BYTE*)pMem+4+4);
m_pWinTcp->Send( (BYTE*)pMem, dwZoneSize+4+4, dwReceiverID );
MemMgr::Free(pMem);
// 需要将缓存里的游戏命令发送出去
BYTE* pMsg = NULL;
DWORD dwSize = 0;
DWORD dwFirstGameTick = dwGameTick;
CMsgList list;
do
{
pMsg = NULL;
dwSize = m_pGameMsg->PeekMsg(&pMsg, dwGameTick);
if( dwSize )
{
list.AddMsg(pMsg, dwSize, dwGameTick);
m_pWinTcp->Send( pMsg, dwSize, dwReceiverID );
MemMgr::Free(pMsg);
}
dwGameTick++;
}while( dwSize != 0 );
do
{
pMsg = NULL;
dwSize = list.PeekMsg(&pMsg, dwFirstGameTick);
if( dwSize )
{
m_pGameMsg->AddMsg(pMsg, dwSize, dwFirstGameTick);
MemMgr::Free(pMsg);
}
dwFirstGameTick++;
}while( dwSize != 0 );
m_bClientInitialized = true;
}
//-----------------------------------------------------------------------------
// 根据得到的信息,设置游戏世界
//-----------------------------------------------------------------------------
void CSession::SetWorldInfo(BYTE* pNetMsg)
{
GameMsg TempMsg;
ZeroMemory(&TempMsg, sizeof(TempMsg));
DWORD dwRandHolder = 0;
memcpy(&m_dwGameTick, pNetMsg+4, 4);
m_pApp->GetSnapshot()->LoadFrom((BYTE*)pNetMsg+4+4);
m_bClientInitialized = true;
}
//-----------------------------------------------------------------------------
// 发送断线信息
//-----------------------------------------------------------------------------
void CSession::SendCutLineMsg()
{
GameMsg TempMsg;
ZeroMemory(&TempMsg, sizeof(TempMsg));
TempMsg.dwCmd = GT_NET_CMD_CUT_LINE;
m_pWinTcp->Send( (BYTE*)&TempMsg, sizeof(TempMsg) );
}
//-----------------------------------------------------------------------------
// 组织消息放入组织队列
//-----------------------------------------------------------------------------
void CSession::ComposeAndSendGameMsg(DWORD dwGameTick)
{
GameMsg TempMsg;
ZeroMemory( &TempMsg, sizeof(TempMsg) );
TempMsg.dwCmd = GT_NET_CMD_TICK;
TempMsg.dwTick = dwGameTick;
TempMsg.byComputer = 0;
DWORD dwMinTick1 = ~0;
DWORD dwMinTick2 = ~0;
int nMinIndex1 = ~0;
int nMinIndex2 = ~0;
for( int n=0; n<m_nMaxUncomposeGameMsg; n++ )
{
if( !m_UncomposeArray[n].bValid )
continue;
if( m_UncomposeArray[n].byComputer == 1 )
{
if( m_UncomposeArray[n].dwTick < dwMinTick1 )
{
TempMsg.byPlayerInput[0] = m_UncomposeArray[n].byPlayerInput[0];
dwMinTick1 = m_UncomposeArray[n].dwTick;
nMinIndex1 = n;
}
}
else
{
if( m_UncomposeArray[n].dwTick < dwMinTick2 )
{
TempMsg.byPlayerInput[1] = m_UncomposeArray[n].byPlayerInput[1];
dwMinTick2 = m_UncomposeArray[n].dwTick;
nMinIndex2 = n;
}
}
}
if( nMinIndex1 != ~0 )
DelUncomposeMsg(nMinIndex1);
if( nMinIndex2 != ~0 )
DelUncomposeMsg(nMinIndex2);
if( !m_bClientPlayerActived && m_pWinTcp->GetClientNum() > 1 )
{
if( TempMsg.dwTick == m_dwWorldInfoSendTick + 10 )
{
TempMsg.bActivePlayer = true;
m_bClientPlayerActived = true;
}
}
m_pWinTcp->Send( (BYTE*)&TempMsg, sizeof(TempMsg) );
}
//-----------------------------------------------------------------------------
// 将消息放入未组织队列
//-----------------------------------------------------------------------------
bool CSession::AddUncomposeMsg(BYTE* pMsg)
{
if( m_nUncomposeGameMsg >= m_nMaxUncomposeGameMsg )
{
// too much msg
FATAL_ERR("too much msg in uncompose array");
return false;
}
for( int n=0; n<m_nMaxUncomposeGameMsg; n++ )
{
if( false == m_UncomposeArray[n].bValid )
{
memcpy(&m_UncomposeArray[n], pMsg, sizeof(GameMsg));
m_UncomposeArray[n].bValid = true;
m_nUncomposeGameMsg++;
return true;
}
}
FATAL_ERR("too much msg in uncompose array");
return false;
}
//-----------------------------------------------------------------------------
// 删除未组织队列的消息
//-----------------------------------------------------------------------------
void CSession::DelUncomposeMsg(int nIndex)
{
if( nIndex >= m_nMaxUncomposeGameMsg
|| m_nUncomposeGameMsg <= 0 )
{
return; // no this msg
}
m_UncomposeArray[nIndex].bValid = false;
m_nUncomposeGameMsg--;
return;
}
int CSession::GetReadyMsgNum()
{
return m_pGameMsg->GetMsgNumInList();
}