-
Notifications
You must be signed in to change notification settings - Fork 0
/
Synchronizer.cpp
85 lines (80 loc) · 2.33 KB
/
Synchronizer.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
#include "main.h"
#include "ext.h"
// ユーザーキャラクターを同期化するスレッド
DWORD __stdcall Thread_Synchronizer(LPVOID param)
{
// WCHAR logmsg[MAX_LOG_BUFFER];
// CSyncProc* pSyncProc = g_pSyncRoom;
CCriticalSection* pCriticalSection = ((CCriticalSection*) param);
CNetworkSession* pNWSess = g_pNetSess;
timeBeginPeriod(1);
for(;;)
{
//>> Frame move
// 指定時間以上経過ならゲーム処理開始
static DWORD dwNextTick = FRAME_RATE;
DWORD dwNowTick = 0;
while(dwNextTick-3 > (dwNowTick=timeGetTime()))
Sleep(1);
while(dwNextTick > (dwNowTick=timeGetTime()))
Sleep(0);
dwNextTick = dwNowTick+FRAME_RATE;
// 現在の時間を最後に更新した時間にする
//> 同期処理
BOOL bPacket = FALSE;
switch (g_eGamePhase)
{
case GAME_PHASE_ROOM:
pCriticalSection->EnterCriticalSection_Session(L'F');
bPacket = g_pSyncRoom->Frame();
pCriticalSection->LeaveCriticalSection_Session();
if (bPacket)
{
g_pCriticalSection->EnterCriticalSection_Packet(L'Y');
g_pPacketQueue->Enqueue(g_pSyncRoom->DequeuePacket());
g_pCriticalSection->LeaveCriticalSection_Packet();
}
break;
case GAME_PHASE_LOAD:
bPacket = g_pSyncLoad->Frame();
if (bPacket)
{
g_pCriticalSection->EnterCriticalSection_Packet(L'U');
g_pPacketQueue->Enqueue(g_pSyncLoad->DequeuePacket());
g_pCriticalSection->LeaveCriticalSection_Packet();
}
if (g_pSyncLoad->IsNextPhase())
{
g_pPPPhase->SetGamePhase(GAME_PHASE_MAIN);
if (!g_pPPMain->Init(pNWSess, g_pPPLoad))
{
AddMessageLog(L"Main Packet Init error\n");
return 0;
}
g_pPPPhase = g_pPPMain;
g_eGamePhase = GAME_PHASE_MAIN;
if (! g_pSyncMain->Init(pNWSess, g_pFiler, g_pPPMain))
g_pSyncMain->GameEnd();
g_pCriticalSection->EnterCriticalSection_Packet(L'I');
g_pPacketQueue->Enqueue(g_pSyncMain->DequeuePacket());
g_pCriticalSection->LeaveCriticalSection_Packet();
}
break;
case GAME_PHASE_MAIN:
bPacket = g_pSyncMain->Frame();
if (bPacket)
{
g_pCriticalSection->EnterCriticalSection_Packet(L'O');
g_pPacketQueue->Enqueue(g_pSyncMain->DequeuePacket());
g_pCriticalSection->LeaveCriticalSection_Packet();
}
break;
case GAME_PHASE_RESULT:
break;
}
//< 同期処理
Sleep(1); // 必ず CPU に休みを入れる
} // for ループ終了
timeEndPeriod(1); // タイマーの精度を戻す
return 0;
}