/
Session.cpp
325 lines (276 loc) · 8.71 KB
/
Session.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
// ======================================================================================
// File : Session.cpp
// Author : Li Chen
// Last Change : 07/29/2010 | 15:48:02 PM | Thursday,July
// Description :
// ======================================================================================
#include "stdafx.h"
#include "InterfaceMgr.h"
#include "SignIn.h"
#include "Task.h"
#include "Session.h"
#include "Master.h"
#include "Stats.h"
namespace GameService
{
//--------------------------------------------------------------------------------------
// Name: Init()
// Desc: Initialize all variables.
//--------------------------------------------------------------------------------------
SessionSrv::SessionSrv()
{
m_IsCreated = m_IsStarted = FALSE;
// Initialize variables
#if defined(_XBOX) || defined(_XENON)
m_hSession = INVALID_HANDLE_VALUE;
#endif
}
SessionSrv::SessionSrv(MessageMgr* msgMgr)
{
m_IsCreated = m_IsStarted = FALSE;
// Initialize variables
#if defined(_XBOX) || defined(_XENON)
m_hSession = INVALID_HANDLE_VALUE;
#endif
if (msgMgr)
{
msgMgr->Register(EMessage_OnlineTaskDone,this);
}
}
//--------------------------------------------------------------------------------------
// Name: BeginSession()
// Desc: Create a GS_NEW session for writing stats
//--------------------------------------------------------------------------------------
GS_BOOL SessionSrv::BeginSession()
{
#if defined(_XBOX) || defined(_XENON)
if (m_hSession != INVALID_HANDLE_VALUE)
return FALSE;
GS_DWORD dwUser = SignIn::GetSignedInUser();
if (-1 == dwUser)
return FALSE;
// Set the context to standard (not ranked)
XUserSetContext( dwUser, X_CONTEXT_GAME_TYPE, X_CONTEXT_GAME_TYPE_STANDARD );
// Create a (non-matchmaking) session
CTaskID id = 0;
GS_DWORD ret = XSessionCreate(
XSESSION_CREATE_USES_STATS,
dwUser,
m_dwPublicSlots,
m_dwPrivateSlots,
&m_SessionNonce,
&m_SessionInfo,
Master::G()->GetTaskMgr()->AddTask(EGSTaskType_SessionCreate,this,&id),
&m_hSession );
Master::G()->GetTaskMgr()->StartTask(id,ret);
if( ret != ERROR_IO_PENDING )
{
Master::G()->Log("XSessionCreate failed with error %d", ret);
return FALSE;
}
#endif
return TRUE;
}
//--------------------------------------------------------------------------------------
// Name: JoinSession()
// Desc: Join the session
//--------------------------------------------------------------------------------------
GS_BOOL SessionSrv::JoinSession()
{
#if defined(_XBOX) || defined(_XENON)
if (m_hSession == INVALID_HANDLE_VALUE)
return FALSE;
// Join the session
CTaskID id = 0;
GS_DWORD ret = XSessionJoinRemote(
m_hSession,
SignIn::GetUserNum(),
SignIn::GetXUIDArray(),
SignIn::GetIsPrivateArray(),
Master::G()->GetTaskMgr()->AddTask(EGSTaskType_SessionJoin,this,&id) );
Master::G()->GetTaskMgr()->StartTask(id,ret);
if( ret != ERROR_IO_PENDING )
{
Master::G()->Log( "XSessionJoinRemote failed with error %d", ret );
return FALSE;
}
#endif
return TRUE;
}
//--------------------------------------------------------------------------------------
// Name: StartSession()
// Desc: Start the session
//--------------------------------------------------------------------------------------
GS_BOOL SessionSrv::StartSession()
{
#if defined(_XBOX) || defined(_XENON)
if (m_hSession == INVALID_HANDLE_VALUE)
return FALSE;
// Start the session
CTaskID id = 0;
GS_DWORD ret = XSessionStart(
m_hSession,
0,
Master::G()->GetTaskMgr()->AddTask(EGSTaskType_SessionStart,this,&id) );
Master::G()->GetTaskMgr()->StartTask(id,ret);
if( ret != ERROR_IO_PENDING )
{
Master::G()->Log( "XSessionStart failed with error %d", ret );
return FALSE;
}
m_iLastWriteStatsInSession = 0;
#endif
return TRUE;
}
// ========================================================
// Record last write stats time
// ========================================================
#define WRITE_STATS_INTERVAL 300000 // 5m
GS_BOOL SessionSrv::CanWriteStats()
{
#if defined(_XBOX) || defined(_XENON)
if (0 == m_iLastWriteStatsInSession || GetTickCount() - m_iLastWriteStatsInSession > WRITE_STATS_INTERVAL)
{
m_iLastWriteStatsInSession = GetTickCount();
return TRUE;
}
#endif
return FALSE;
}
//--------------------------------------------------------------------------------------
// Name: EndSession()
// Desc: End the current session (and implicitly write stats)
//--------------------------------------------------------------------------------------
GS_BOOL SessionSrv::EndSession()
{
// Master::G()->GetStatsSrv()->Finalize();
#if defined(_XBOX) || defined(_XENON)
// Don't end the session if it was never created
if( m_hSession == INVALID_HANDLE_VALUE )
{
return FALSE;
}
// End the session
CTaskID id = 0;
GS_DWORD ret = XSessionEnd(
m_hSession,
Master::G()->GetTaskMgr()->AddTask(EGSTaskType_SessionEnd,this,&id) );
Master::G()->GetTaskMgr()->StartTask(id,ret);
if( ret != ERROR_IO_PENDING )
{
Master::G()->Log( "XSessionEnd failed with error %d", ret );
return FALSE;
}
#endif
return TRUE;
}
//--------------------------------------------------------------------------------------
// Name: LeaveSession()
// Desc: Remove local users from the session preparatory to deleting it
//--------------------------------------------------------------------------------------
GS_BOOL SessionSrv::LeaveSession()
{
#if defined(_XBOX) || defined(_XENON)
// Don't leave the session if it was never created
if( m_hSession == INVALID_HANDLE_VALUE )
{
return FALSE;
}
// End the session
CTaskID id = 0;
GS_DWORD ret = XSessionLeaveRemote(
m_hSession,
SignIn::GetUserNum(),
SignIn::GetXUIDArray(),
Master::G()->GetTaskMgr()->AddTask(EGSTaskType_SessionLeave,this,&id) );
Master::G()->GetTaskMgr()->StartTask(id,ret);
if( ret != ERROR_IO_PENDING )
{
Master::G()->Log( "XSessionLeaveRemote failed with error %d", ret );
return FALSE;
}
#endif
return TRUE;
}
//--------------------------------------------------------------------------------------
// Name: DeleteSession()
// Desc: Remove the session
//--------------------------------------------------------------------------------------
GS_BOOL SessionSrv::DeleteSession()
{
#if defined(_XBOX) || defined(_XENON)
// Don't delete the session if it was never created
if( m_hSession == INVALID_HANDLE_VALUE )
{
return FALSE;
}
// End the session
CTaskID id = 0;
GS_DWORD ret = XSessionDelete(
m_hSession,
Master::G()->GetTaskMgr()->AddTask(EGSTaskType_SessionDelete,this,&id) );
Master::G()->GetTaskMgr()->StartTask(id,ret);
if( ret != ERROR_IO_PENDING )
{
Master::G()->Log( "XSessionDelete failed with error %d", ret );
return FALSE;
}
#endif
return TRUE;
}
void SessionSrv::MessageResponse(Message* message)
{
CTaskID task_id = *(CTaskID*)message->ReadPayload(0);
GS_INT taskType = (*(GS_INT*)message->ReadPayload(1));
GS_DWORD taskResult = *(GS_DWORD*)message->ReadPayload(2);
GS_DWORD taskDetailedResult = *(GS_DWORD*)message->ReadPayload(3);
#if defined(_XBOX) || defined(_XENON)
GS_DWORD taskExtendedResult = *(GS_DWORD*)message->ReadPayload(4);
#endif
#if defined(_XBOX) || defined(_XENON)
if (ERROR_SUCCESS == taskResult)
#elif defined(_PS3)
if (0 == taskResult)
#endif
{
switch (taskType)
{
case EGSTaskType_SessionCreate:
case EGSTaskType_SessionJoin:
m_IsCreated = TRUE;
break;
case EGSTaskType_SessionStart:
m_IsStarted = TRUE;
break;
case EGSTaskType_SessionEnd:
m_IsStarted = FALSE;
break;
case EGSTaskType_SessionDelete:
case EGSTaskType_SessionLeave:
m_IsCreated = FALSE;
#if defined(_XBOX) || defined(_XENON)
m_hSession = INVALID_HANDLE_VALUE;
#endif
break;
default:
break;
}
}
// Message to Interface
Message* msg = Message::Create(EMessage_CallBackInterface);
if (msg)
{
msg->AddPayload(taskType);
#if defined(_XBOX) || defined(_XENON)
GS_BOOL result = (ERROR_SUCCESS == taskResult) ? TRUE : FALSE;
#elif defined(_PS3)
GS_BOOL result = (0 == taskResult) ? TRUE : FALSE;
#else
GS_BOOL result = TRUE;
#endif
msg->AddPayload(result);
msg->AddTarget(Master::G()->GetInterfaceMgr());
Master::G()->GetMessageMgr()->Send(msg);
}
}
} // namespace GameService