-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSyncProc.h
197 lines (181 loc) · 5.25 KB
/
CSyncProc.h
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
#ifndef H_CLASS_SYNC_PROC___
#define H_CLASS_SYNC_PROC___
#include "windows.h"
#include "TCHAR.h"
#include <assert.h>
#include <vector>
#include "util.h"
#include "../include/define.h"
#include "../include/types.h"
#include "PacketMaker.h"
#include "CNetworkSession.h"
class CSyncProc
{
public:
CSyncProc(){
m_bPacket = FALSE;
p_pNWSess = NULL;
ZeroMemory(&m_tQueue, sizeof(type_queue));
InitializeCriticalSection(&m_CriticalSectionPacket);
};
virtual ~CSyncProc()
{ ClearQueue(m_tQueue.next);
DeleteCriticalSection(&m_CriticalSectionPacket);
};
virtual BOOL Init(CNetworkSession* pNetSess)
{ p_pNWSess = pNetSess;
ZeroMemory(&m_tQueue, sizeof(type_queue));
return TRUE;
};
virtual void Clear(){};
// 戻り値:パケットありなし
virtual BOOL Frame(){ return FALSE; };
type_queue* DequeuePacket()
{
EnterCriticalSectionPacket();
type_queue* ret = m_tQueue.next;
m_tQueue.next = NULL;
m_bPacket = FALSE;
LeaveCriticalSectionPacket();
return ret;
};
virtual BOOL HasPacket()
{
EnterCriticalSectionPacket();
BOOL ret = m_bPacket;
LeaveCriticalSectionPacket();
return m_bPacket;
};
protected:
//> セッションへ送るパケット作成
BOOL AddPacket(ptype_session sess, BYTE* data, WORD size)
{
if (!sess || !data || size > MAX_PACKET_SIZE)
return FALSE;
ptype_packet ppkt = NewPacket();
if (!ppkt) return FALSE;
ppkt->cli_sock = sess->sock;
ppkt->session = sess;
ppkt->size = size;
ZeroMemory(ppkt->data,sizeof(char)*MAX_PACKET_SIZE);
CopyMemory(ppkt->data,data,size);
EnterCriticalSectionPacket();
EnqueuePacket(&m_tQueue, ppkt);
m_bPacket = TRUE;
LeaveCriticalSectionPacket();
return TRUE;
};
//< セッションへ送るパケット作成
//> 見えてるユーザに送るパケット作成
// send_sess :BLINDでも送るキャラ
BOOL AddPacketNoBlindUser(ptype_session send_sess, BYTE* data, WORD size)
{
BOOL ret = FALSE;
g_pCriticalSection->EnterCriticalSection_Session(L'=');
EnterCriticalSectionPacket();
//> 接続済みユーザとの処理
int nSearchIndex = 0;
for(ptype_session pSess=p_pNWSess->GetSessionFirst(&nSearchIndex);
pSess;
pSess=p_pNWSess->GetSessionNext(&nSearchIndex))
{
// 認証済みか
if (pSess->connect_state != CONN_STATE_AUTHED) continue;
// 飛ばすセッションか
if (send_sess != pSess
&& pSess->chara_state[CHARA_STATE_BLIND_INDEX]) continue;
ret |=AddPacket(pSess, data, size); // キュー追加
if (!ret) break;
}
LeaveCriticalSectionPacket();
g_pCriticalSection->LeaveCriticalSection_Session();
return ret;
};
//< 見えてるユーザに送るパケット作成
//> チームセッションへ送るパケット作成
// team_no : チーム番号
BOOL AddPacketTeamNoBlindUser(int team_no, BYTE* data, WORD size)
{
BOOL ret=FALSE;
g_pCriticalSection->EnterCriticalSection_Session(L'~');
EnterCriticalSectionPacket();
//> 接続済みユーザとの処理
int nSearchIndex = 0;
for(ptype_session pSess=p_pNWSess->GetSessionFirst(&nSearchIndex);
pSess;
pSess=p_pNWSess->GetSessionNext(&nSearchIndex))
{
if ( pSess->team_no != GALLERY_TEAM_NO
&& ((pSess->team_no != team_no)
|| pSess->chara_state[CHARA_STATE_BLIND_INDEX])) continue; // 飛ばすセッションか
ret |= AddPacket(pSess, data, size); // キュー追加
if (!ret) break;
}
LeaveCriticalSectionPacket();
g_pCriticalSection->LeaveCriticalSection_Session();
return ret;
};
//< チームセッションへ送るパケット作成
//> 全セッションへ送るパケット作成
BOOL AddPacketAllUser(ptype_session ignore_sess, BYTE* data, WORD size)
{
BOOL ret = FALSE;
g_pCriticalSection->EnterCriticalSection_Session(L'|');
EnterCriticalSectionPacket();
//> 接続済みユーザとの処理
int nSearchIndex = 0;
for(ptype_session pSess=p_pNWSess->GetSessionFirst(&nSearchIndex);
pSess;
pSess=p_pNWSess->GetSessionNext(&nSearchIndex))
{
// 認証済みか
if (pSess->connect_state != CONN_STATE_AUTHED) continue;
if (pSess == ignore_sess) continue; // 飛ばすセッションか
ret |=AddPacket(pSess, data, size); // キュー追加
if (!ret) break;
}
LeaveCriticalSectionPacket();
g_pCriticalSection->LeaveCriticalSection_Session();
return ret;
};
//< 全セッションへ送るパケット作成
//> チームセッションへ送るパケット作成
// team_no : チーム番号
BOOL AddPacketTeamUser(int team_no, BYTE* data, WORD size)
{
BOOL ret=FALSE;
g_pCriticalSection->EnterCriticalSection_Session(L'Q');
this->EnterCriticalSectionPacket();
//> 接続済みユーザとの処理
int nSearchIndex = 0;
for(ptype_session pSess=p_pNWSess->GetSessionFirst(&nSearchIndex);
pSess;
pSess=p_pNWSess->GetSessionNext(&nSearchIndex))
{
if (pSess->connect_state != CONN_STATE_AUTHED) continue;
if (pSess->team_no != GALLERY_TEAM_NO // 観戦
&& pSess->team_no != team_no) continue; // 飛ばすセッションか
ret |= AddPacket(pSess, data, size); // キュー追加
if (!ret) break;
}
this->LeaveCriticalSectionPacket();
g_pCriticalSection->LeaveCriticalSection_Session();
return ret;
};
//< チームセッションへ送るパケット作成
CNetworkSession *p_pNWSess;
type_queue m_tQueue; // パケット作る用
BOOL m_bPacket;
//> パケット
inline void EnterCriticalSectionPacket()
{
EnterCriticalSection(&m_CriticalSectionPacket);
};
inline void LeaveCriticalSectionPacket()
{
LeaveCriticalSection(&m_CriticalSectionPacket);
};
CRITICAL_SECTION m_CriticalSectionPacket;
//< パケット
};
#endif