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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//
#include "Session.h"
#include "strtools.h"
#include "matchmaking.h"
#include "utllinkedlist.h"
#include "tslist.h"
#include "hl2orange.spa.h"
#include "dbg.h"
#ifdef IS_WINDOWS_PC
#include "winlite.h" // for CloseHandle()
#endif
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
extern IXboxSystem *g_pXboxSystem;
#define ASYNC_OK 0
#define ASYNC_FAIL 1
//-----------------------------------------------------------------------------
// Purpose: CSession class
//-----------------------------------------------------------------------------
CSession::CSession()
{
m_pParent = NULL;
m_hSession = INVALID_HANDLE_VALUE;
m_SessionState = SESSION_STATE_NONE;
m_pRegistrationResults = NULL;
ResetSession();
}
CSession::~CSession()
{
ResetSession();
}
//-----------------------------------------------------------------------------
// Purpose: Reset a session to it's initial state
//-----------------------------------------------------------------------------
void CSession::ResetSession()
{
// Cleanup first
switch( m_SessionState )
{
case SESSION_STATE_CREATING:
CancelCreateSession();
break;
case SESSION_STATE_MIGRATING:
// X360TBD:
break;
}
if ( m_hSession != INVALID_HANDLE_VALUE )
{
Msg( "ResetSession: Destroying current session.\n" );
DestroySession();
m_hSession = INVALID_HANDLE_VALUE;
}
SwitchToState( SESSION_STATE_NONE );
m_bIsHost = false;
m_bIsArbitrated = false;
m_bUsingQoS = false;
m_bIsSystemLink = false;
Q_memset( &m_nPlayerSlots, 0, sizeof( m_nPlayerSlots ) );
Q_memset( &m_SessionInfo, 0, sizeof( m_SessionInfo ) );
if ( m_pRegistrationResults )
{
delete m_pRegistrationResults;
}
m_nSessionFlags = 0;
}
//-----------------------------------------------------------------------------
// Purpose: Set session contexts and properties
//-----------------------------------------------------------------------------
void CSession::SetContext( const uint nContextId, const uint nContextValue, const bool bAsync )
{
g_pXboxSystem->UserSetContext( XBX_GetPrimaryUserId(), nContextId, nContextValue, bAsync );
}
void CSession::SetProperty( const uint nPropertyId, const uint cbValue, const void *pvValue, const bool bAsync )
{
g_pXboxSystem->UserSetProperty( XBX_GetPrimaryUserId(), nPropertyId, cbValue, pvValue, bAsync );
}
//-----------------------------------------------------------------------------
// Purpose: Send a notification to GameUI
//-----------------------------------------------------------------------------
void CSession::SendNotification( SESSION_NOTIFY notification )
{
Assert( m_pParent );
m_pParent->SessionNotification( notification );
}
//-----------------------------------------------------------------------------
// Purpose: Update the number of player slots filled
//-----------------------------------------------------------------------------
void CSession::UpdateSlots( const CClientInfo *pClient, bool bAddPlayers )
{
int cPlayers = pClient->m_cPlayers;
if ( bAddPlayers )
{
if ( pClient->m_bInvited )
{
// Fill private slots first, then overflow into public slots
m_nPlayerSlots[SLOTS_FILLEDPRIVATE] += cPlayers;
pClient->m_numPrivateSlotsUsed = cPlayers;
cPlayers = 0;
if ( m_nPlayerSlots[SLOTS_FILLEDPRIVATE] > m_nPlayerSlots[SLOTS_TOTALPRIVATE] )
{
cPlayers = m_nPlayerSlots[SLOTS_FILLEDPRIVATE] - m_nPlayerSlots[SLOTS_TOTALPRIVATE];
pClient->m_numPrivateSlotsUsed -= cPlayers;
m_nPlayerSlots[SLOTS_FILLEDPRIVATE] = m_nPlayerSlots[SLOTS_TOTALPRIVATE];
}
}
m_nPlayerSlots[SLOTS_FILLEDPUBLIC] += cPlayers;
if ( m_nPlayerSlots[SLOTS_FILLEDPUBLIC] > m_nPlayerSlots[SLOTS_TOTALPUBLIC] )
{
//Handle error
Warning( "Too many players!\n" );
}
}
else
{
// The cast to 'int' is needed since otherwise underflow will wrap around to very large
// numbers and the 'max' macro will do nothing.
m_nPlayerSlots[SLOTS_FILLEDPRIVATE] = max( 0, (int)( m_nPlayerSlots[SLOTS_FILLEDPRIVATE] - pClient->m_numPrivateSlotsUsed ) );
m_nPlayerSlots[SLOTS_FILLEDPUBLIC] = max( 0, (int)( m_nPlayerSlots[SLOTS_FILLEDPUBLIC] - ( pClient->m_cPlayers - pClient->m_numPrivateSlotsUsed ) ) );
}
}
//-----------------------------------------------------------------------------
// Purpose: Join players on the local client
//-----------------------------------------------------------------------------
void CSession::JoinLocal( const CClientInfo *pClient )
{
uint nUserIndex[MAX_PLAYERS_PER_CLIENT] = {0};
bool bPrivate[MAX_PLAYERS_PER_CLIENT] = {0};
for( int i = 0; i < pClient->m_cPlayers; ++i )
{
nUserIndex[i] = pClient->m_iControllers[i];
bPrivate[i] = pClient->m_bInvited;
}
// X360TBD: Make async?
uint ret = g_pXboxSystem->SessionJoinLocal( m_hSession, pClient->m_cPlayers, nUserIndex, bPrivate, false );
if ( ret != ERROR_SUCCESS )
{
// Handle error
Warning( "Failed to add local players\n" );
}
else
{
UpdateSlots( pClient, true );
}
}
//-----------------------------------------------------------------------------
// Purpose: Join players on a remote client
//-----------------------------------------------------------------------------
void CSession::JoinRemote( const CClientInfo *pClient )
{
XUID xuids[MAX_PLAYERS_PER_CLIENT] = {0};
bool bPrivate[MAX_PLAYERS_PER_CLIENT] = {0};
for( int i = 0; i < pClient->m_cPlayers; ++i )
{
xuids[i] = pClient->m_xuids[i];
bPrivate[i] = pClient->m_bInvited;
}
// X360TBD: Make async?
uint ret = g_pXboxSystem->SessionJoinRemote( m_hSession, pClient->m_cPlayers, xuids, bPrivate, false );
if ( ret != ERROR_SUCCESS )
{
// Handle error
Warning( "Join Remote Error\n" );
}
else
{
UpdateSlots( pClient, true );
}
}
//-----------------------------------------------------------------------------
// Purpose: Remove players on the local client
//-----------------------------------------------------------------------------
void CSession::RemoveLocal( const CClientInfo *pClient )
{
uint nUserIndex[MAX_PLAYERS_PER_CLIENT] = {0};
for( int i = 0; i < pClient->m_cPlayers; ++i )
{
nUserIndex[i] = pClient->m_iControllers[i];
}
// X360TBD: Make async?
uint ret = g_pXboxSystem->SessionLeaveLocal( m_hSession, pClient->m_cPlayers, nUserIndex, false );
if ( ret != ERROR_SUCCESS )
{
// Handle error
Warning( "Failed to remove local players\n" );
}
else
{
UpdateSlots( pClient, false );
}
}
//-----------------------------------------------------------------------------
// Purpose: Remove players on a remote client
//-----------------------------------------------------------------------------
void CSession::RemoveRemote( const CClientInfo *pClient )
{
XUID xuids[MAX_PLAYERS_PER_CLIENT] = {0};
for( int i = 0; i < pClient->m_cPlayers; ++i )
{
xuids[i] = pClient->m_xuids[i];
}
// X360TBD: Make async?
uint ret = g_pXboxSystem->SessionLeaveRemote( m_hSession, pClient->m_cPlayers, xuids, false );
if ( ret != ERROR_SUCCESS )
{
// Handle error
Warning( "Failed to remove remote players\n" );
}
else
{
UpdateSlots( pClient, false );
}
}
//-----------------------------------------------------------------------------
// Purpose: Create a new session
//-----------------------------------------------------------------------------
bool CSession::CreateSession()
{
if( INVALID_HANDLE_VALUE != m_hSession )
{
Warning( "CreateSession called on existing session!" );
DestroySession();
m_hSession = INVALID_HANDLE_VALUE;
}
uint flags = m_nSessionFlags;
if( m_bIsHost )
{
flags |= XSESSION_CREATE_HOST;
}
if ( flags & XSESSION_CREATE_USES_ARBITRATION )
{
m_bIsArbitrated = true;
}
m_hCreateHandle = g_pXboxSystem->CreateAsyncHandle();
// Create the session
uint ret = g_pXboxSystem->CreateSession( flags,
XBX_GetPrimaryUserId(),
m_nPlayerSlots[SLOTS_TOTALPUBLIC],
m_nPlayerSlots[SLOTS_TOTALPRIVATE],
&m_SessionNonce,
&m_SessionInfo,
&m_hSession,
true,
&m_hCreateHandle );
if( ret != ERROR_SUCCESS && ret != ERROR_IO_PENDING )
{
Warning( "XSessionCreate failed with error %d\n", ret );
return false;
}
SwitchToState( SESSION_STATE_CREATING );
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Check for completion while creating a new session
//-----------------------------------------------------------------------------
void CSession::UpdateCreating()
{
DWORD ret = g_pXboxSystem->GetOverlappedResult( m_hCreateHandle, NULL, false );
if ( ret == ERROR_IO_INCOMPLETE )
{
// Still waiting
return;
}
else
{
SESSION_STATE nextState = SESSION_STATE_IDLE;
// Operation completed
SESSION_NOTIFY notification = IsHost() ? SESSION_NOTIFY_CREATED_HOST : SESSION_NOTIFY_CREATED_CLIENT;
if ( ret != ERROR_SUCCESS )
{
Warning( "CSession: CreateSession failed. Error %d\n", ret );
nextState = SESSION_STATE_NONE;
notification = SESSION_NOTIFY_FAIL_CREATE;
}
g_pXboxSystem->ReleaseAsyncHandle( m_hCreateHandle );
SendNotification( notification );
SwitchToState( nextState );
}
}
//-----------------------------------------------------------------------------
// Purpose: Cancel async session creation
//-----------------------------------------------------------------------------
void CSession::CancelCreateSession()
{
if ( m_SessionState != SESSION_STATE_CREATING )
return;
g_pXboxSystem->CancelOverlappedOperation( &m_hCreateHandle );
g_pXboxSystem->ReleaseAsyncHandle( m_hCreateHandle );
#ifndef POSIX
if( INVALID_HANDLE_VALUE != m_hSession )
{
CloseHandle( m_hSession );
m_hSession = INVALID_HANDLE_VALUE;
}
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Close an existing session
//-----------------------------------------------------------------------------
void CSession::DestroySession()
{
// TODO: Make this async
uint ret = g_pXboxSystem->DeleteSession( m_hSession, false );
if ( ret != ERROR_SUCCESS )
{
Warning( "Failed to delete session with error %d.\n", ret );
}
}
//-----------------------------------------------------------------------------
// Purpose: Register for arbritation in a ranked match
//-----------------------------------------------------------------------------
void CSession::RegisterForArbitration()
{
uint bytes = 0;
m_pRegistrationResults = NULL;
// Call once to determine size of results buffer
int ret = g_pXboxSystem->SessionArbitrationRegister( m_hSession, 0, m_SessionNonce, &bytes, NULL, false );
if ( ret != ERROR_INSUFFICIENT_BUFFER )
{
Warning( "Failed registering for arbitration\n" );
return;
}
m_pRegistrationResults = (XSESSION_REGISTRATION_RESULTS*)new byte[ bytes ];
m_hRegisterHandle = g_pXboxSystem->CreateAsyncHandle();
ret = g_pXboxSystem->SessionArbitrationRegister( m_hSession, 0, m_SessionNonce, &bytes, m_pRegistrationResults, true, &m_hRegisterHandle );
if( ret != ERROR_IO_PENDING )
{
Warning( "Failed registering for arbitration\n" );
}
m_SessionState = SESSION_STATE_REGISTERING;
}
//-----------------------------------------------------------------------------
// Purpose: Check for completion of arbitration registration
//-----------------------------------------------------------------------------
void CSession::UpdateRegistering()
{
DWORD ret = g_pXboxSystem->GetOverlappedResult( m_hRegisterHandle, NULL, false );
if ( ret == ERROR_IO_INCOMPLETE )
{
// Still waiting
return;
}
else
{
SESSION_STATE nextState = SESSION_STATE_IDLE;
// Operation completed
SESSION_NOTIFY notification = SESSION_NOTIFY_REGISTER_COMPLETED;
if ( ret != ERROR_SUCCESS )
{
Warning( "CSession: Registration failed. Error %d\n", ret );
nextState = SESSION_STATE_NONE;
notification = SESSION_NOTIFY_FAIL_REGISTER;
}
g_pXboxSystem->ReleaseAsyncHandle( m_hRegisterHandle );
SendNotification( notification );
SwitchToState( nextState );
}
}
//-----------------------------------------------------------------------------
// Purpose: Migrate the session to a new host
//-----------------------------------------------------------------------------
bool CSession::MigrateHost()
{
if ( IsHost() )
{
// Migrate call will fill this in for us
Q_memcpy( &m_NewSessionInfo, &m_SessionInfo, sizeof( m_NewSessionInfo ) );
}
m_hMigrateHandle = g_pXboxSystem->CreateAsyncHandle();
int ret = g_pXboxSystem->SessionMigrate( m_hSession, m_nOwnerId, &m_NewSessionInfo, true, &m_hMigrateHandle );
if ( ret != ERROR_IO_PENDING )
{
return false;
}
SwitchToState( SESSION_STATE_MIGRATING );
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Check for completion while migrating a session
//-----------------------------------------------------------------------------
void CSession::UpdateMigrating()
{
DWORD ret = g_pXboxSystem->GetOverlappedResult( m_hMigrateHandle, NULL, false );
if ( ret == ERROR_IO_INCOMPLETE )
{
// Still waiting
return;
}
else
{
SESSION_STATE nextState = SESSION_STATE_IDLE;
// Operation completed
SESSION_NOTIFY notification = SESSION_NOTIFY_MIGRATION_COMPLETED;
if ( ret != ERROR_SUCCESS )
{
Warning( "CSession: MigrateSession failed. Error %d\n", ret );
nextState = SESSION_STATE_NONE;
notification = SESSION_NOTIFY_FAIL_MIGRATE;
}
g_pXboxSystem->ReleaseAsyncHandle( m_hMigrateHandle );
SendNotification( notification );
SwitchToState( nextState );
}
}
//-----------------------------------------------------------------------------
// Purpose: Change state
//-----------------------------------------------------------------------------
void CSession::SwitchToState( SESSION_STATE newState )
{
m_SessionState = newState;
}
//-----------------------------------------------------------------------------
// Purpose: Per-frame update
//-----------------------------------------------------------------------------
void CSession::RunFrame()
{
switch( m_SessionState )
{
case SESSION_STATE_CREATING:
UpdateCreating();
break;
case SESSION_STATE_REGISTERING:
UpdateRegistering();
break;
case SESSION_STATE_MIGRATING:
UpdateMigrating();
break;
default:
break;
}
}
//-----------------------------------------------------------------------------
// Accessors
//-----------------------------------------------------------------------------
HANDLE CSession::GetSessionHandle()
{
return m_hSession;
}
void CSession::SetSessionInfo( XSESSION_INFO *pInfo )
{
memcpy( &m_SessionInfo, pInfo, sizeof( XSESSION_INFO ) );
}
void CSession::SetNewSessionInfo( XSESSION_INFO *pInfo )
{
memcpy( &m_NewSessionInfo, pInfo, sizeof( XSESSION_INFO ) );
}
void CSession::GetSessionInfo( XSESSION_INFO *pInfo )
{
Assert( pInfo );
memcpy( pInfo, &m_SessionInfo, sizeof( XSESSION_INFO ) );
}
void CSession::GetNewSessionInfo( XSESSION_INFO *pInfo )
{
Assert( pInfo );
memcpy( pInfo, &m_NewSessionInfo, sizeof( XSESSION_INFO ) );
}
void CSession::SetSessionNonce( int64 nonce )
{
m_SessionNonce = nonce;
}
uint64 CSession::GetSessionNonce()
{
return m_SessionNonce;
}
XNKID CSession::GetSessionId()
{
return m_SessionInfo.sessionID;
}
void CSession::SetSessionSlots(unsigned int nSlot, unsigned int nPlayers)
{
Assert( nSlot < SLOTS_LAST );
m_nPlayerSlots[nSlot] = nPlayers;
}
unsigned int CSession::GetSessionSlots( unsigned int nSlot )
{
Assert( nSlot < SLOTS_LAST );
return m_nPlayerSlots[nSlot];
}
void CSession::SetSessionFlags( uint flags )
{
m_nSessionFlags = flags;
}
uint CSession::GetSessionFlags()
{
return m_nSessionFlags;
}
int CSession::GetPlayerCount()
{
return m_nPlayerSlots[SLOTS_FILLEDPRIVATE] + m_nPlayerSlots[SLOTS_FILLEDPUBLIC];
}
void CSession::SetFlag( uint nFlag )
{
m_nSessionFlags |= nFlag;
}
void CSession::SetIsHost( bool bHost )
{
m_bIsHost = bHost;
}
void CSession::SetIsSystemLink( bool bSystemLink )
{
m_bIsSystemLink = bSystemLink;
}
void CSession::SetOwnerId( uint id )
{
m_nOwnerId = id;
}
bool CSession::IsHost()
{
return m_bIsHost;
}
bool CSession::IsFull()
{
return ( GetSessionSlots( SLOTS_TOTALPRIVATE ) == GetSessionSlots( SLOTS_FILLEDPRIVATE ) &&
GetSessionSlots( SLOTS_TOTALPUBLIC ) == GetSessionSlots( SLOTS_FILLEDPUBLIC ) );
}
bool CSession::IsArbitrated()
{
return m_bIsArbitrated;
}
bool CSession::IsSystemLink()
{
return m_bIsSystemLink;
}
void CSession::SetParent( CMatchmaking *pParent )
{
m_pParent = pParent;
}
double CSession::GetTime()
{
return Plat_FloatTime();
}
|