Something went wrong. Try again.
the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
Something went wrong. Try again.
57 kB · 1769 lines
C++
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770#include "stdafx.h"#include "..\..\..\Minecraft.World\Socket.h"#include "..\..\..\Minecraft.World\StringHelpers.h"#include "PlatformNetworkManagerXbox.h"#include "NetworkPlayerXbox.h"#include "..\..\Common\Network\GameNetworkManager.h"
CPlatformNetworkManagerXbox *g_pPlatformNetworkManager;
VOID CPlatformNetworkManagerXbox::NotifyStateChanged( __in QNET_STATE OldState, __in QNET_STATE NewState, __in HRESULT hrInfo ){ static const char * c_apszStateNames[] = { "QNET_STATE_IDLE", "QNET_STATE_SESSION_HOSTING", "QNET_STATE_SESSION_JOINING", "QNET_STATE_GAME_LOBBY", "QNET_STATE_SESSION_REGISTERING", "QNET_STATE_SESSION_STARTING", "QNET_STATE_GAME_PLAY", "QNET_STATE_SESSION_ENDING", "QNET_STATE_SESSION_LEAVING", "QNET_STATE_SESSION_DELETING", };
app.DebugPrintf( "State: %s ==> %s, result 0x%08x.\n", c_apszStateNames[ OldState ], c_apszStateNames[ NewState ], hrInfo ); if( NewState == QNET_STATE_SESSION_HOSTING ) { m_bLeavingGame = false; m_bLeaveGameOnTick = false; m_bHostChanged = false; g_NetworkManager.StateChange_AnyToHosting(); } else if( NewState == QNET_STATE_SESSION_JOINING ) { m_bLeavingGame = false; m_bLeaveGameOnTick = false; m_bHostChanged = false; g_NetworkManager.StateChange_AnyToJoining(); } else if( NewState == QNET_STATE_IDLE && OldState == QNET_STATE_SESSION_JOINING ) { // 4J-PB - now and then we get ERROR_DEVICE_REMOVED when qnet says // " Couldn't join, removed from session!" or //[qnet]: Received data change notification from partially connected player! //[qnet]: Couldn't join, removed from session! // instead of a QNET_E_SESSION_FULL as should be reported
eJoinFailedReason reason; switch( hrInfo ) { case QNET_E_INSUFFICIENT_PRIVILEGES: reason = JOIN_FAILED_INSUFFICIENT_PRIVILEGES; break; case QNET_E_SESSION_FULL: reason = JOIN_FAILED_SERVER_FULL; break; default: reason = JOIN_FAILED_NONSPECIFIC; break; } g_NetworkManager.StateChange_JoiningToIdle(reason); } else if( NewState == QNET_STATE_IDLE && OldState == QNET_STATE_SESSION_HOSTING ) { m_bLeavingGame = true; } else if( NewState == QNET_STATE_SESSION_STARTING ) { m_lastPlayerEventTimeStart = app.getAppTime();
g_NetworkManager.StateChange_AnyToStarting(); } // Fix for #93148 - TCR 001: BAS Game Stability: Title will crash for the multiplayer client if host of the game will exit during the clients loading to created world. // 4J Stu - If the client joins just as the host is exiting, then they can skip to leaving without passing through ending else if( NewState == QNET_STATE_SESSION_ENDING || (NewState == QNET_STATE_SESSION_LEAVING && OldState == QNET_STATE_GAME_PLAY) ) { g_NetworkManager.StateChange_AnyToEnding( OldState == QNET_STATE_GAME_PLAY );
if( m_pIQNet->IsHost() ) { m_bLeavingGame = true; } }
if( NewState == QNET_STATE_IDLE ) { g_NetworkManager.StateChange_AnyToIdle(); }}
VOID CPlatformNetworkManagerXbox::NotifyPlayerJoined( __in IQNetPlayer * pQNetPlayer ){ const char * pszDescription;
// 4J Stu - We create a fake socket for every where that we need an INBOUND queue of game data. Outbound // is all handled by QNet so we don't need that. Therefore each client player has one, and the host has one // for each client player. bool createFakeSocket = false; bool localPlayer = false;
NetworkPlayerXbox *networkPlayer = (NetworkPlayerXbox *)addNetworkPlayer(pQNetPlayer);
if( pQNetPlayer->IsLocal() ) { localPlayer = true; if( pQNetPlayer->IsHost() ) { pszDescription = "local host"; // 4J Stu - No socket for the localhost as it uses a special loopback queue
m_machineQNetPrimaryPlayers.push_back( pQNetPlayer ); } else { pszDescription = "local";
// We need an inbound queue on all local players to receive data from the host createFakeSocket = true; } } else { if( pQNetPlayer->IsHost() ) { pszDescription = "remote host"; } else { pszDescription = "remote";
// If we are the host, then create a fake socket for every remote player if( m_pIQNet->IsHost() ) { createFakeSocket = true; } }
if( m_pIQNet->IsHost() && !m_bHostChanged ) { // Do we already have a primary player for this system? bool systemHasPrimaryPlayer = false; for(AUTO_VAR(it, m_machineQNetPrimaryPlayers.begin()); it < m_machineQNetPrimaryPlayers.end(); ++it) { IQNetPlayer *pQNetPrimaryPlayer = *it; if( pQNetPlayer->IsSameSystem(pQNetPrimaryPlayer) ) { systemHasPrimaryPlayer = true; break; } } if( !systemHasPrimaryPlayer ) m_machineQNetPrimaryPlayers.push_back( pQNetPlayer ); } } g_NetworkManager.PlayerJoining( networkPlayer ); if( createFakeSocket == true && !m_bHostChanged ) { g_NetworkManager.CreateSocket( networkPlayer, localPlayer ); }
app.DebugPrintf( "Player 0x%p \"%ls\" joined; %s; voice %i; camera %i.\n", pQNetPlayer, pQNetPlayer->GetGamertag(), pszDescription, (int) pQNetPlayer->HasVoice(), (int) pQNetPlayer->HasCamera() );
if( m_pIQNet->IsHost() ) { // 4J-PB - only the host should do this g_NetworkManager.UpdateAndSetGameSessionData(); SystemFlagAddPlayer( networkPlayer ); } for( int idx = 0; idx < XUSER_MAX_COUNT; ++idx) { if(playerChangedCallback[idx] != NULL) playerChangedCallback[idx]( playerChangedCallbackParam[idx], networkPlayer, false ); }
if(m_pIQNet->GetState() == QNET_STATE_GAME_PLAY) { int localPlayerCount = 0; for(unsigned int idx = 0; idx < XUSER_MAX_COUNT; ++idx) { if( m_pIQNet->GetLocalPlayerByUserIndex(idx) != NULL ) ++localPlayerCount; }
float appTime = app.getAppTime();
// Only record stats for the primary player here m_lastPlayerEventTimeStart = appTime; }}
VOID CPlatformNetworkManagerXbox::NotifyPlayerLeaving( __in IQNetPlayer * pQNetPlayer ){ //__debugbreak();
app.DebugPrintf( "Player 0x%p \"%ls\" leaving.\n", pQNetPlayer, pQNetPlayer->GetGamertag() );
INetworkPlayer *networkPlayer = getNetworkPlayer(pQNetPlayer);
// Get our wrapper object associated with this player. Socket *socket = networkPlayer->GetSocket(); if( socket != NULL ) { // If we are in game then remove this player from the game as well. // We may get here either from the player requesting to exit the game, // in which case we they will already have left the game server, or from a disconnection // where we then have to remove them from the game server if( m_pIQNet->IsHost() && !m_bHostChanged ) { g_NetworkManager.CloseConnection(networkPlayer); }
// Free the wrapper object memory. // TODO 4J Stu - We may still be using this at the point that the player leaves the session. // We need this as long as the game server still needs to communicate with the player //delete socket;
networkPlayer->SetSocket( NULL ); }
if( m_pIQNet->IsHost() && !m_bHostChanged ) { if( isSystemPrimaryPlayer(pQNetPlayer) ) { IQNetPlayer *pNewQNetPrimaryPlayer = NULL; for(unsigned int i = 0; i < m_pIQNet->GetPlayerCount(); ++i ) { IQNetPlayer *pQNetPlayer2 = m_pIQNet->GetPlayerByIndex( i );
if( pQNetPlayer2 != pQNetPlayer && pQNetPlayer2->IsSameSystem( pQNetPlayer ) ) { pNewQNetPrimaryPlayer = pQNetPlayer2; break; } } AUTO_VAR(it, find( m_machineQNetPrimaryPlayers.begin(), m_machineQNetPrimaryPlayers.end(), pQNetPlayer)); if( it != m_machineQNetPrimaryPlayers.end() ) { m_machineQNetPrimaryPlayers.erase( it ); }
if( pNewQNetPrimaryPlayer != NULL ) m_machineQNetPrimaryPlayers.push_back( pNewQNetPrimaryPlayer ); }
g_NetworkManager.UpdateAndSetGameSessionData( networkPlayer ); SystemFlagRemovePlayer( networkPlayer );
}
g_NetworkManager.PlayerLeaving( networkPlayer ); for( int idx = 0; idx < XUSER_MAX_COUNT; ++idx) { if(playerChangedCallback[idx] != NULL) playerChangedCallback[idx]( playerChangedCallbackParam[idx], networkPlayer, true ); }
if(m_pIQNet->GetState() == QNET_STATE_GAME_PLAY) { int localPlayerCount = 0; for(unsigned int idx = 0; idx < XUSER_MAX_COUNT; ++idx) { if( m_pIQNet->GetLocalPlayerByUserIndex(idx) != NULL ) ++localPlayerCount; }
float appTime = app.getAppTime(); m_lastPlayerEventTimeStart = appTime; }
removeNetworkPlayer(pQNetPlayer);}
VOID CPlatformNetworkManagerXbox::NotifyNewHost( __in IQNetPlayer * pQNetPlayer ){ app.DebugPrintf( "Player 0x%p \"%ls\" (local %i) is new host.\n", pQNetPlayer, pQNetPlayer->GetGamertag(), (int) pQNetPlayer->IsLocal() );
m_bHostChanged = true;
if( m_pIQNet->IsHost() && !IsLeavingGame() ) { m_pGameNetworkManager->HostChanged(); }}
VOID CPlatformNetworkManagerXbox::NotifyDataReceived( __in IQNetPlayer * pQNetPlayerFrom, __in DWORD dwNumPlayersTo, __in_ecount(dwNumPlayersTo) IQNetPlayer ** apQNetPlayersTo, __in_bcount(dwDataSize) const BYTE * pbData, __in DWORD dwDataSize ){ if(m_pIQNet->GetState() == QNET_STATE_SESSION_ENDING) return;
DWORD dwPlayer;
// Loop through all the local players that were targeted and print info // regarding this message. /* for( dwPlayer = 0; dwPlayer < dwNumPlayersTo; dwPlayer++ ) { app.DebugPrintf( "Received %u bytes of data from \"%ls\" to \"%ls\"\n", dwDataSize, pPlayerFrom->GetGamertag(), apPlayersTo[ dwPlayer ]->GetGamertag()); } */
// Loop through all the players and push this data into their read queue for( dwPlayer = 0; dwPlayer < dwNumPlayersTo; dwPlayer++ ) { if( apQNetPlayersTo[dwPlayer]->IsHost() ) { // If we are the host we care who this came from //app.DebugPrintf( "Pushing data into host read queue for user \"%ls\"\n", pPlayerFrom->GetGamertag()); // Push this data into the read queue for the player that sent it INetworkPlayer *pPlayerFrom = getNetworkPlayer(pQNetPlayerFrom); Socket *socket = pPlayerFrom->GetSocket();
if(socket != NULL) socket->pushDataToQueue(pbData, dwDataSize, false); } else { // If we are not the host the message must have come from the host, so we care more about who it is addressed to INetworkPlayer *pPlayerTo = getNetworkPlayer(apQNetPlayersTo[dwPlayer]); Socket *socket = pPlayerTo->GetSocket(); //app.DebugPrintf( "Pushing data into read queue for user \"%ls\"\n", apPlayersTo[dwPlayer]->GetGamertag()); if(socket != NULL) socket->pushDataToQueue(pbData, dwDataSize); } }}
VOID CPlatformNetworkManagerXbox::NotifyWriteStats( __in IQNetPlayer * pQNetPlayer ){ app.DebugPrintf( "QNet: NotifyWriteStats\n" );
g_NetworkManager.WriteStats( getNetworkPlayer( pQNetPlayer ) );}
VOID CPlatformNetworkManagerXbox::NotifyReadinessChanged( __in IQNetPlayer * pQNetPlayer, __in BOOL bReady ){ app.DebugPrintf( "Player 0x%p readiness is now %i.\n", pQNetPlayer, (int) bReady );}
VOID CPlatformNetworkManagerXbox::NotifyCommSettingsChanged( __in IQNetPlayer * pQNetPlayer ){}
VOID CPlatformNetworkManagerXbox::NotifyGameSearchComplete( __in IQNetGameSearch * pGameSearch, __in HRESULT hrComplete, __in DWORD dwNumResults ){ // Not currently used}
VOID CPlatformNetworkManagerXbox::NotifyGameInvite( __in DWORD dwUserIndex, __in const INVITE_INFO * pInviteInfo ){ g_NetworkManager.GameInviteReceived( dwUserIndex, pInviteInfo );}
VOID CPlatformNetworkManagerXbox::NotifyContextChanged( __in const XUSER_CONTEXT * pContext ){ app.DebugPrintf( "Context 0x%p changed.\n", pContext );}
VOID CPlatformNetworkManagerXbox::NotifyPropertyChanged( __in const XUSER_PROPERTY * pProperty ){ app.DebugPrintf( "Property 0x%p changed.\n", pProperty );}
bool CPlatformNetworkManagerXbox::Initialise(CGameNetworkManager *pGameNetworkManager, int flagIndexSize){ m_pGameNetworkManager = pGameNetworkManager; m_flagIndexSize = flagIndexSize; g_pPlatformNetworkManager = this; for( int i = 0; i < XUSER_MAX_COUNT; i++ ) { playerChangedCallback[ i ] = NULL; } HRESULT hr; int iResult; DWORD dwResult;
// Start up XNet with default settings. iResult = XNetStartup( NULL ); if( iResult != 0 ) { app.DebugPrintf( "Starting up XNet failed (err = %i)!\n", iResult ); return false; }
// Start up XOnline. dwResult = XOnlineStartup(); if( dwResult != ERROR_SUCCESS ) { app.DebugPrintf( "Starting up XOnline failed (err = %u)!\n", dwResult ); XNetCleanup(); return false; }
// Create the QNet object. hr = QNetCreateUsingXAudio2( QNET_SESSIONTYPE_LIVE_STANDARD, this, NULL, g_pXAudio2, &m_pIQNet ); if( FAILED( hr ) ) { app.DebugPrintf( "Creating QNet object failed (err = 0x%08x)!\n", hr ); XOnlineCleanup(); XNetCleanup(); return false; }
BOOL enableNotify = FALSE; m_pIQNet->SetOpt( QNET_OPTION_NOTIFY_LISTENER, &enableNotify, sizeof BOOL );
BOOL enableJip = FALSE; m_pIQNet->SetOpt( QNET_OPTION_JOIN_IN_PROGRESS_ALLOWED, &enableJip, sizeof BOOL ); BOOL enableInv = FALSE; m_pIQNet->SetOpt( QNET_OPTION_INVITES_ALLOWED, &enableInv, sizeof BOOL ); BOOL enablePres = FALSE; m_pIQNet->SetOpt( QNET_OPTION_PRESENCE_JOIN_MODE, &enablePres, sizeof BOOL ); // We DO NOT want QNet to handle XN_SYS_SIGNINCHANGED but so far everything else should be fine // We DO WANT QNet to handle XN_LIVE_INVITE_ACCEPTED at a minimum // Receive all types that QNet needs, and filter out the specific ones we don't want later m_notificationListener = XNotifyCreateListener(XNOTIFY_SYSTEM | XNOTIFY_FRIENDS | XNOTIFY_LIVE);
m_bLeavingGame = false; m_bLeaveGameOnTick = false; m_bHostChanged = false;
m_bSearchResultsReady = false; m_bSearchPending = false;
m_bIsOfflineGame = false; m_pSearchParam = NULL; m_SessionsUpdatedCallback = NULL;
for(unsigned int i = 0; i < XUSER_MAX_COUNT; ++i) { m_searchResultsCount[i] = 0; m_lastSearchStartTime[i] = 0;
// The results that will be filled in with the current search m_pSearchResults[i] = NULL; m_pQoSResult[i] = NULL; m_pCurrentSearchResults[i] = NULL; m_pCurrentQoSResult[i] = NULL; m_currentSearchResultsCount[i] = 0; }
// Success! return true;}
void CPlatformNetworkManagerXbox::Terminate(){}
int CPlatformNetworkManagerXbox::GetJoiningReadyPercentage(){ return 100;}
int CPlatformNetworkManagerXbox::CorrectErrorIDS(int IDS){ return IDS;}
bool CPlatformNetworkManagerXbox::isSystemPrimaryPlayer(IQNetPlayer *pQNetPlayer){ bool playerIsSystemPrimary = false; for(AUTO_VAR(it, m_machineQNetPrimaryPlayers.begin()); it < m_machineQNetPrimaryPlayers.end(); ++it) { IQNetPlayer *pQNetPrimaryPlayer = *it; if( pQNetPrimaryPlayer == pQNetPlayer ) { playerIsSystemPrimary = true; break; } } return playerIsSystemPrimary;}
// We call this twice a frame, either side of the render call so is a good place to "tick" thingsvoid CPlatformNetworkManagerXbox::DoWork(){ DWORD dwNotifyId; ULONG_PTR ulpNotifyParam;
while( XNotifyGetNext( m_notificationListener, 0, // Any notification &dwNotifyId, &ulpNotifyParam) ) {
switch(dwNotifyId) {
case XN_SYS_SIGNINCHANGED: app.DebugPrintf("Signinchanged - %d\n", ulpNotifyParam); break; case XN_LIVE_INVITE_ACCEPTED: // ignore these - we're catching them from the game listener, so we can get the one from the dashboard break; default: m_pIQNet->Notify(dwNotifyId,ulpNotifyParam); break; }
}
TickSearch();
if( m_bLeaveGameOnTick ) { m_pIQNet->LeaveGame(m_migrateHostOnLeave); m_bLeaveGameOnTick = false; }
m_pIQNet->DoWork();}
int CPlatformNetworkManagerXbox::GetPlayerCount(){ return m_pIQNet->GetPlayerCount();}
bool CPlatformNetworkManagerXbox::ShouldMessageForFullSession(){ return false;}
int CPlatformNetworkManagerXbox::GetOnlinePlayerCount(){ DWORD playerCount = GetPlayerCount(); DWORD onlinePlayerCount = 0;
for(DWORD i = 0; i < playerCount; ++i) { IQNetPlayer *pQNetPlayer = m_pIQNet->GetPlayerByIndex(i); if(!pQNetPlayer->IsLocal())++onlinePlayerCount; } return onlinePlayerCount;}
int CPlatformNetworkManagerXbox::GetLocalPlayerMask(int playerIndex){ switch(playerIndex) { case 0: return QNET_USER_MASK_USER0; case 1: return QNET_USER_MASK_USER1; case 2: return QNET_USER_MASK_USER2; case 3: return QNET_USER_MASK_USER3; default: return 0; }}
bool CPlatformNetworkManagerXbox::AddLocalPlayerByUserIndex( int userIndex ){ return ( m_pIQNet->AddLocalPlayerByUserIndex(userIndex) == S_OK );}
bool CPlatformNetworkManagerXbox::RemoveLocalPlayerByUserIndex( int userIndex ){ IQNetPlayer *pQNetPlayer = m_pIQNet->GetLocalPlayerByUserIndex(userIndex); INetworkPlayer *pNetworkPlayer = getNetworkPlayer(pQNetPlayer);
if(pNetworkPlayer != NULL) { Socket *socket = pNetworkPlayer->GetSocket();
if( socket != NULL ) { // We can't remove the player from qnet until we have stopped using it to communicate C4JThread* thread = new C4JThread(&CPlatformNetworkManagerXbox::RemovePlayerOnSocketClosedThreadProc, pNetworkPlayer, "RemovePlayerOnSocketClosed"); thread->SetProcessor( CPU_CORE_REMOVE_PLAYER ); thread->Run(); } else { // Safe to remove the player straight away return ( m_pIQNet->RemoveLocalPlayerByUserIndex(userIndex) == S_OK ); } } return true;}
bool CPlatformNetworkManagerXbox::IsInStatsEnabledSession(){
DWORD dataSize = sizeof(QNET_LIVE_STATS_MODE); QNET_LIVE_STATS_MODE statsMode; m_pIQNet->GetOpt(QNET_OPTION_LIVE_STATS_MODE, &statsMode , &dataSize ); // Use QNET_LIVE_STATS_MODE_AUTO if there is another way to check if stats are enabled or not bool statsEnabled = statsMode == QNET_LIVE_STATS_MODE_ENABLED; return m_pIQNet->GetState() != QNET_STATE_IDLE && statsEnabled;}
bool CPlatformNetworkManagerXbox::SessionHasSpace(unsigned int spaceRequired /*= 1*/){ // This function is used while a session is running, so all players trying to join // should use public slots, DWORD publicSlots = 0; DWORD filledPublicSlots = 0; DWORD privateSlots = 0; DWORD filledPrivateSlots = 0;
DWORD dataSize = sizeof(DWORD); m_pIQNet->GetOpt(QNET_OPTION_TOTAL_PUBLIC_SLOTS, &publicSlots, &dataSize ); m_pIQNet->GetOpt(QNET_OPTION_FILLED_PUBLIC_SLOTS, &filledPublicSlots, &dataSize ); m_pIQNet->GetOpt(QNET_OPTION_TOTAL_PRIVATE_SLOTS, &privateSlots, &dataSize ); m_pIQNet->GetOpt(QNET_OPTION_FILLED_PRIVATE_SLOTS, &filledPrivateSlots, &dataSize );
DWORD spaceLeft = (publicSlots - filledPublicSlots) + (privateSlots - filledPrivateSlots);
return spaceLeft >= spaceRequired;}
void CPlatformNetworkManagerXbox::SendInviteGUI(int quadrant){}
bool CPlatformNetworkManagerXbox::IsAddingPlayer(){ return false;}
bool CPlatformNetworkManagerXbox::LeaveGame(bool bMigrateHost){ if( m_bLeavingGame ) return true;
m_bLeavingGame = true;
// If we are a client, wait for all client connections to close // TODO Possibly need to do multiple objects depending on how split screen online works IQNetPlayer *pQNetPlayer = m_pIQNet->GetLocalPlayerByUserIndex(g_NetworkManager.GetPrimaryPad()); INetworkPlayer *pNetworkPlayer = getNetworkPlayer(pQNetPlayer);
if(pNetworkPlayer != NULL) { Socket *socket = pNetworkPlayer->GetSocket();
if( socket != NULL ) { //printf("Waiting for socket closed event\n"); DWORD result = socket->m_socketClosedEvent->WaitForSignal(INFINITE);
// The session might be gone once the socket releases if( IsInSession() ) { //printf("Socket closed event has fired\n"); // 4J Stu - Clear our reference to this socket pQNetPlayer = m_pIQNet->GetLocalPlayerByUserIndex(g_NetworkManager.GetPrimaryPad()); pNetworkPlayer = getNetworkPlayer(pQNetPlayer); if(pNetworkPlayer) pNetworkPlayer->SetSocket( NULL ); } delete socket; } else { //printf("Socket is already NULL\n"); } }
// If we are the host wait for the game server to end if(m_pIQNet->IsHost() && g_NetworkManager.ServerStoppedValid()) { m_pIQNet->EndGame(); g_NetworkManager.ServerStoppedWait(); g_NetworkManager.ServerStoppedDestroy(); }
return _LeaveGame(bMigrateHost, true);}
bool CPlatformNetworkManagerXbox::_LeaveGame(bool bMigrateHost, bool bLeaveRoom){ // 4J Stu - Fix for #10490 - TCR 001 BAS Game Stability: When a party of four players leave a world to join another world without saving the title will crash. // Changed this to make it threadsafe m_bLeaveGameOnTick = true; m_migrateHostOnLeave = bMigrateHost;
return true;}
void CPlatformNetworkManagerXbox::HostGame(int localUsersMask, bool bOnlineGame, bool bIsPrivate, unsigned char publicSlots /*= MINECRAFT_NET_MAX_PLAYERS*/, unsigned char privateSlots /*= 0*/){// #ifdef _XBOX // 4J Stu - We probably did this earlier as well, but just to be sure! SetLocalGame( !bOnlineGame ); SetPrivateGame( bIsPrivate ); SystemFlagReset();
// Make sure that the Primary Pad is in by default localUsersMask |= GetLocalPlayerMask( g_NetworkManager.GetPrimaryPad() );
_HostGame( localUsersMask, publicSlots, privateSlots );//#endif}
void CPlatformNetworkManagerXbox::_HostGame(int usersMask, unsigned char publicSlots /*= MINECRAFT_NET_MAX_PLAYERS*/, unsigned char privateSlots /*= 0*/){ HRESULT hr; // Create a session using the standard game type, in multiplayer game mode, // The constants used to specify game mode, context ID and context value are // defined in the title's .spa.h file, generated using the XLAST tool. // TODO 4J Stu - Game mode should be CONTEXT_GAME_MODE_MULTIPLAYER? XUSER_CONTEXT aXUserContexts[] = { { X_CONTEXT_GAME_TYPE, X_CONTEXT_GAME_TYPE_STANDARD }, { X_CONTEXT_GAME_MODE, CONTEXT_GAME_MODE_GAMEMODE } };
// We need at least one other slot otherwise it's not multiplayer, is it! if(publicSlots==1 && privateSlots==0) privateSlots = 1;
//printf("Hosting game with %d public slots and %d private slots\n", publicSlots, privateSlots);
BOOL enableJip = FALSE; m_pIQNet->SetOpt( QNET_OPTION_JOIN_IN_PROGRESS_ALLOWED, &enableJip, sizeof BOOL ); BOOL enableInv = FALSE; m_pIQNet->SetOpt( QNET_OPTION_INVITES_ALLOWED, &enableInv, sizeof BOOL ); BOOL enablePres = FALSE; m_pIQNet->SetOpt( QNET_OPTION_PRESENCE_JOIN_MODE, &enablePres, sizeof BOOL );
// Start hosting a new game // Use only the contexts defined above, and no properties. hr = m_pIQNet->HostGame( g_NetworkManager.GetLockedProfile(), // dwUserIndex usersMask, // dwUserMask publicSlots, // dwPublicSlots privateSlots, // dwPrivateSlots 0, // cProperties NULL, // pProperties ARRAYSIZE( aXUserContexts ), // cContexts aXUserContexts ); // pContexts
m_hostGameSessionData.netVersion = MINECRAFT_NET_VERSION; m_hostGameSessionData.isJoinable = !IsPrivateGame();
char* hostName = new char[XUSER_NAME_SIZE]; hostName = g_NetworkManager.GetOnlineName( g_NetworkManager.GetPrimaryPad() ); memcpy(m_hostGameSessionData.hostName,hostName,XUSER_NAME_SIZE);
hr = m_pIQNet->SetOpt( QNET_OPTION_QOS_DATA_BUFFER, &m_hostGameSessionData, sizeof(GameSessionData) );}
bool CPlatformNetworkManagerXbox::_StartGame(){ // Set the options that now allow players to join this game BOOL enableJip = TRUE; // Must always be true othewise nobody can join the game while in the PLAY state m_pIQNet->SetOpt( QNET_OPTION_JOIN_IN_PROGRESS_ALLOWED, &enableJip, sizeof BOOL ); BOOL enableInv = !IsLocalGame(); m_pIQNet->SetOpt( QNET_OPTION_INVITES_ALLOWED, &enableInv, sizeof BOOL ); BOOL enablePres = !IsPrivateGame() && !IsLocalGame(); m_pIQNet->SetOpt( QNET_OPTION_PRESENCE_JOIN_MODE, &enablePres, sizeof BOOL );
return ( m_pIQNet->StartGame() == S_OK );}
int CPlatformNetworkManagerXbox::JoinGame(FriendSessionInfo *searchResult, int localUsersMask, int primaryUserIndex){ // Being a bit over-cautious here, but the xbox code pre-refactoring took a copy of XSESSION_SEARCHRESULT (although not in static memory) // so seems safest to replicate this kind of thing here rather than risk data being pointed to by the searchResult being altered whilst // JoinGameFromSearchResult is running. static XSESSION_SEARCHRESULT searchResultCopy; searchResultCopy = searchResult->searchResult; HRESULT hr = m_pIQNet->JoinGameFromSearchResult( primaryUserIndex, // dwUserIndex localUsersMask, // dwUserMask &searchResultCopy ); // pSearchResult
if( FAILED( hr ) ) { app.DebugPrintf( "Failed joining game (err = 0x%08x)!\n", hr ); }
switch( hr ) { case S_OK: return CGameNetworkManager::JOINGAME_SUCCESS; case QNET_E_SESSION_FULL: return CGameNetworkManager::JOINGAME_FAIL_SERVER_FULL; default: return CGameNetworkManager::JOINGAME_FAIL_GENERAL; }}
bool CPlatformNetworkManagerXbox::SetLocalGame(bool isLocal){ if( m_pIQNet->GetState() == QNET_STATE_IDLE ) { QNET_SESSIONTYPE sessionType = isLocal ? QNET_SESSIONTYPE_LOCAL : QNET_SESSIONTYPE_LIVE_STANDARD; m_pIQNet->SetOpt(QNET_OPTION_TYPE_SESSIONTYPE, &sessionType , sizeof QNET_SESSIONTYPE);
// The default value for this is QNET_LIVE_STATS_MODE_AUTO, but that decides based on the players // in when the game starts. As we may want a non-live player to join the game we cannot have stats enabled // when we create the sessions. As a result of this, the NotifyWriteStats callback will not be called for // LIVE players that are connected to LIVE so we write their stats data on a state change. QNET_LIVE_STATS_MODE statsMode = isLocal ? QNET_LIVE_STATS_MODE_DISABLED : QNET_LIVE_STATS_MODE_ENABLED; m_pIQNet->SetOpt(QNET_OPTION_LIVE_STATS_MODE, &statsMode , sizeof QNET_LIVE_STATS_MODE);
// Also has a default of QNET_LIVE_PRESENCE_MODE_AUTO as above, although the effects are less of an issue QNET_LIVE_PRESENCE_MODE presenceMode = isLocal ? QNET_LIVE_PRESENCE_MODE_NOT_ADVERTISED : QNET_LIVE_PRESENCE_MODE_ADVERTISED; m_pIQNet->SetOpt(QNET_OPTION_LIVE_PRESENCE_MODE, &presenceMode , sizeof QNET_LIVE_PRESENCE_MODE);
m_bIsOfflineGame = isLocal; app.DebugPrintf("Setting as local game: %s\n", isLocal ? "yes" : "no" ); } else { app.DebugPrintf("Tried to change QNet Session type while not in idle state\n"); } return true;}
void CPlatformNetworkManagerXbox::SetPrivateGame(bool isPrivate){ app.DebugPrintf("Setting as private game: %s\n", isPrivate ? "yes" : "no" ); m_bIsPrivateGame = isPrivate;}
void CPlatformNetworkManagerXbox::RegisterPlayerChangedCallback(int iPad, void (*callback)(void *callbackParam, INetworkPlayer *pPlayer, bool leaving), void *callbackParam){ playerChangedCallback[iPad] = callback; playerChangedCallbackParam[iPad] = callbackParam;}
void CPlatformNetworkManagerXbox::UnRegisterPlayerChangedCallback(int iPad, void (*callback)(void *callbackParam, INetworkPlayer *pPlayer, bool leaving), void *callbackParam){ if(playerChangedCallbackParam[iPad] == callbackParam) { playerChangedCallback[iPad] = NULL; playerChangedCallbackParam[iPad] = NULL; }}
void CPlatformNetworkManagerXbox::HandleSignInChange(){ return; }
bool CPlatformNetworkManagerXbox::_RunNetworkGame(){ // We delay actually starting the session so that we know the game server is running by the time the clients try to join // This does result in a host advantage HRESULT hr = m_pIQNet->StartGame(); if(FAILED(hr)) return false;
// Set the options that now allow players to join this game BOOL enableJip = TRUE; // Must always be true othewise nobody can join the game while in the PLAY state m_pIQNet->SetOpt( QNET_OPTION_JOIN_IN_PROGRESS_ALLOWED, &enableJip, sizeof BOOL ); BOOL enableInv = !IsLocalGame(); m_pIQNet->SetOpt( QNET_OPTION_INVITES_ALLOWED, &enableInv, sizeof BOOL ); BOOL enablePres = !IsPrivateGame() && !IsLocalGame(); m_pIQNet->SetOpt( QNET_OPTION_PRESENCE_JOIN_MODE, &enablePres, sizeof BOOL );
return true;}
void CPlatformNetworkManagerXbox::UpdateAndSetGameSessionData(INetworkPlayer *pNetworkPlayerLeaving /*= NULL*/){ DWORD playerCount = m_pIQNet->GetPlayerCount();
if( this->m_bLeavingGame ) return;
if( GetHostPlayer() == NULL ) return;
for(unsigned int i = 0; i < MINECRAFT_NET_MAX_PLAYERS; ++i) { if( i < playerCount ) { INetworkPlayer *pNetworkPlayer = GetPlayerByIndex(i);
// We can call this from NotifyPlayerLeaving but at that point the player is still considered in the session if( pNetworkPlayer != pNetworkPlayerLeaving ) { m_hostGameSessionData.players[i] = ((NetworkPlayerXbox *)pNetworkPlayer)->GetUID();
char *temp; temp = (char *)wstringtofilename( pNetworkPlayer->GetOnlineName() ); memcpy(m_hostGameSessionData.szPlayers[i],temp,XUSER_NAME_SIZE); } else { m_hostGameSessionData.players[i] = NULL; memset(m_hostGameSessionData.szPlayers[i],0,XUSER_NAME_SIZE); } } else { m_hostGameSessionData.players[i] = NULL; memset(m_hostGameSessionData.szPlayers[i],0,XUSER_NAME_SIZE); } }
m_hostGameSessionData.hostPlayerUID = ((NetworkPlayerXbox *)GetHostPlayer())->GetQNetPlayer()->GetXuid(); m_hostGameSessionData.m_uiGameHostSettings = app.GetGameHostOption(eGameHostOption_All);
HRESULT hr = S_OK; hr = m_pIQNet->SetOpt( QNET_OPTION_QOS_DATA_BUFFER, &m_hostGameSessionData, sizeof(GameSessionData) );}
int CPlatformNetworkManagerXbox::RemovePlayerOnSocketClosedThreadProc( void* lpParam ){ INetworkPlayer *pNetworkPlayer = (INetworkPlayer *)lpParam;
Socket *socket = pNetworkPlayer->GetSocket();
if( socket != NULL ) { //printf("Waiting for socket closed event\n"); socket->m_socketClosedEvent->WaitForSignal(INFINITE);
//printf("Socket closed event has fired\n"); // 4J Stu - Clear our reference to this socket pNetworkPlayer->SetSocket( NULL ); delete socket; }
return g_pPlatformNetworkManager->RemoveLocalPlayer( pNetworkPlayer );}
bool CPlatformNetworkManagerXbox::RemoveLocalPlayer( INetworkPlayer *pNetworkPlayer ){ if( pNetworkPlayer->IsLocal() ) { return ( m_pIQNet->RemoveLocalPlayerByUserIndex( pNetworkPlayer->GetUserIndex() ) == S_OK ); }
return true;}
CPlatformNetworkManagerXbox::PlayerFlags::PlayerFlags(INetworkPlayer *pNetworkPlayer, unsigned int count){ // 4J Stu - Don't assert, just make it a multiple of 8! This count is calculated from a load of separate values, // and makes tweaking world/render sizes a pain if we hit an assert here count = (count + 8 - 1) & ~(8 - 1); //assert( ( count % 8 ) == 0 ); this->m_pNetworkPlayer = pNetworkPlayer; this->flags = new unsigned char [ count / 8 ]; memset( this->flags, 0, count / 8 ); this->count = count;}CPlatformNetworkManagerXbox::PlayerFlags::~PlayerFlags(){ delete [] flags;}
// Add a player to the per system flag storage - if we've already got a player from that system, copy its flags overvoid CPlatformNetworkManagerXbox::SystemFlagAddPlayer(INetworkPlayer *pNetworkPlayer){ PlayerFlags *newPlayerFlags = new PlayerFlags( pNetworkPlayer, m_flagIndexSize); // If any of our existing players are on the same system, then copy over flags from that one for( unsigned int i = 0; i < m_playerFlags.size(); i++ ) { if( pNetworkPlayer->IsSameSystem(m_playerFlags[i]->m_pNetworkPlayer) ) { memcpy( newPlayerFlags->flags, m_playerFlags[i]->flags, m_playerFlags[i]->count / 8 ); break; } } m_playerFlags.push_back(newPlayerFlags);}
// Remove a player from the per system flag storage - just maintains the m_playerFlags vector without any gaps in itvoid CPlatformNetworkManagerXbox::SystemFlagRemovePlayer(INetworkPlayer *pNetworkPlayer){ for( unsigned int i = 0; i < m_playerFlags.size(); i++ ) { if( m_playerFlags[i]->m_pNetworkPlayer == pNetworkPlayer ) { delete m_playerFlags[i]; m_playerFlags[i] = m_playerFlags.back(); m_playerFlags.pop_back(); return; } }}
void CPlatformNetworkManagerXbox::SystemFlagReset(){ for( unsigned int i = 0; i < m_playerFlags.size(); i++ ) { delete m_playerFlags[i]; } m_playerFlags.clear();}
// Set a per system flag - this is done by setting the flag on every player that shares that systemvoid CPlatformNetworkManagerXbox::SystemFlagSet(INetworkPlayer *pNetworkPlayer, int index){ if( ( index < 0 ) || ( index >= m_flagIndexSize ) ) return; if( pNetworkPlayer == NULL ) return;
for( unsigned int i = 0; i < m_playerFlags.size(); i++ ) { if( pNetworkPlayer->IsSameSystem(m_playerFlags[i]->m_pNetworkPlayer) ) { m_playerFlags[i]->flags[ index / 8 ] |= ( 128 >> ( index % 8 ) ); } }}
// Get value of a per system flag - can be read from the flags of the passed in player as anything else sent to that// system should also have been duplicated herebool CPlatformNetworkManagerXbox::SystemFlagGet(INetworkPlayer *pNetworkPlayer, int index){ if( ( index < 0 ) || ( index >= m_flagIndexSize ) ) return false; if( pNetworkPlayer == NULL ) { return false; }
for( unsigned int i = 0; i < m_playerFlags.size(); i++ ) { if( m_playerFlags[i]->m_pNetworkPlayer == pNetworkPlayer ) { return ( ( m_playerFlags[i]->flags[ index / 8 ] & ( 128 >> ( index % 8 ) ) ) != 0 ); } } return false;}
wstring CPlatformNetworkManagerXbox::GatherStats(){ return L"Queue messages: " + _toString(((NetworkPlayerXbox *)GetHostPlayer())->GetQNetPlayer()->GetSendQueueSize( NULL, QNET_GETSENDQUEUESIZE_MESSAGES ) ) + L" Queue bytes: " + _toString( ((NetworkPlayerXbox *)GetHostPlayer())->GetQNetPlayer()->GetSendQueueSize( NULL, QNET_GETSENDQUEUESIZE_BYTES ) );}
wstring CPlatformNetworkManagerXbox::GatherRTTStats(){ wstring stats(L"Rtt: ");
wchar_t stat[32];
for(unsigned int i = 0; i < GetPlayerCount(); ++i) { IQNetPlayer *pQNetPlayer = ((NetworkPlayerXbox *)GetPlayerByIndex( i ))->GetQNetPlayer();
if(!pQNetPlayer->IsLocal()) { ZeroMemory(stat,sizeof(WCHAR)*32); swprintf(stat, 32, L"%d: %d/", i, pQNetPlayer->GetCurrentRtt() ); stats.append(stat); } } return stats;}
void CPlatformNetworkManagerXbox::TickSearch(){ if( m_bSearchPending ) { if( m_bSearchResultsReady ) { m_currentSearchResultsCount[m_lastSearchPad] = m_searchResultsCount[m_lastSearchPad];
// Store the current search results so that we don't delete them too early if( m_pCurrentSearchResults[m_lastSearchPad] != NULL ) { delete m_pCurrentSearchResults[m_lastSearchPad]; m_pCurrentSearchResults[m_lastSearchPad] = NULL; } m_pCurrentSearchResults[m_lastSearchPad] = m_pSearchResults[m_lastSearchPad]; m_pSearchResults[m_lastSearchPad] = NULL;
if( m_pCurrentQoSResult[m_lastSearchPad] != NULL ) { XNetQosRelease(m_pCurrentQoSResult[m_lastSearchPad]); m_pCurrentQoSResult[m_lastSearchPad] = NULL; } m_pCurrentQoSResult[m_lastSearchPad] = m_pQoSResult[m_lastSearchPad]; m_pQoSResult[m_lastSearchPad] = NULL;
if( m_SessionsUpdatedCallback != NULL ) m_SessionsUpdatedCallback(m_pSearchParam); m_bSearchResultsReady = false; m_bSearchPending = false; } } else { // Don't start searches unless we have registered a callback if( m_SessionsUpdatedCallback != NULL && (m_lastSearchStartTime[g_NetworkManager.GetPrimaryPad()] + MINECRAFT_XSESSION_SEARCH_DELAY_MILLISECONDS) < GetTickCount() ) { SearchForGames(); } }}
void CPlatformNetworkManagerXbox::SearchForGames(){ // Don't start a new search until we have finished the last one if(m_bSearchPending) return;
m_lastSearchPad = g_NetworkManager.GetPrimaryPad();
m_lastSearchStartTime[m_lastSearchPad] = GetTickCount(); m_bSearchPending = true; m_bSearchResultsReady = false;
for(AUTO_VAR(it, friendsSessions[m_lastSearchPad].begin()); it < friendsSessions[m_lastSearchPad].end(); ++it) { delete (*it); } friendsSessions[m_lastSearchPad].clear(); if( m_pSearchResults[m_lastSearchPad] != NULL ) { delete m_pSearchResults[m_lastSearchPad]; m_pSearchResults[m_lastSearchPad] = NULL; } if( m_pQoSResult[m_lastSearchPad] != NULL ) { XNetQosRelease(m_pQoSResult[m_lastSearchPad]); m_pQoSResult[m_lastSearchPad] = NULL; }
bool bMultiplayerAllowed = g_NetworkManager.IsSignedInLive( g_NetworkManager.GetPrimaryPad() ) && g_NetworkManager.AllowedToPlayMultiplayer( g_NetworkManager.GetPrimaryPad() );
if( bMultiplayerAllowed ) { // PARTY XPARTY_USER_LIST partyUserList; HRESULT partyResult = XPartyGetUserList( &partyUserList ); if((partyResult != XPARTY_E_NOT_IN_PARTY) && (partyUserList.dwUserCount>1)) { for(unsigned int i = 0; i<partyUserList.dwUserCount; i++) { if( ( (partyUserList.Users[i].dwFlags & XPARTY_USER_ISLOCAL ) != XPARTY_USER_ISLOCAL ) && ( (partyUserList.Users[i].dwFlags & XPARTY_USER_ISINGAMESESSION) == XPARTY_USER_ISINGAMESESSION ) && partyUserList.Users[i].dwTitleId == TITLEID_MINECRAFT ) { bool sessionAlreadyAdded = false; for(AUTO_VAR(it, friendsSessions[m_lastSearchPad].begin()); it < friendsSessions[m_lastSearchPad].end(); ++it) { FriendSessionInfo *current = *it; if( memcmp( &partyUserList.Users[i].SessionInfo.sessionID, ¤t->sessionId, sizeof(SessionID) ) == 0 ) { //printf("We already have this session from another player.\n"); sessionAlreadyAdded = true; break; } }
if(!sessionAlreadyAdded) { FriendSessionInfo *sessionInfo = new FriendSessionInfo(); sessionInfo->sessionId = partyUserList.Users[i].SessionInfo.sessionID; sessionInfo->hasPartyMember = true; friendsSessions[m_lastSearchPad].push_back(sessionInfo); } } } }
// FRIENDS
DWORD bufferSize = 0; HANDLE hFriendsEnumerator; DWORD hr = XFriendsCreateEnumerator( g_NetworkManager.GetPrimaryPad(), 0, MAX_FRIENDS, &bufferSize, &hFriendsEnumerator );
char *buffer = new char[bufferSize]; DWORD itemsReturned; DWORD result = XEnumerate( hFriendsEnumerator, buffer, bufferSize, &itemsReturned, NULL );
DWORD flagPlayingOnline = XONLINE_FRIENDSTATE_FLAG_ONLINE; // | XONLINE_FRIENDSTATE_FLAG_PLAYING;
XONLINE_FRIEND *friends = (XONLINE_FRIEND *)buffer; for(unsigned int i = 0; i<itemsReturned; i++) { //printf("%s\n",friends[i].szGamertag); if( (friends[i].dwFriendState & flagPlayingOnline) == flagPlayingOnline && ( (friends[i].dwFriendState & XONLINE_FRIENDSTATE_FLAG_JOINABLE) == XONLINE_FRIENDSTATE_FLAG_JOINABLE || (friends[i].dwFriendState & XONLINE_FRIENDSTATE_FLAG_JOINABLE_FRIENDS_ONLY) == XONLINE_FRIENDSTATE_FLAG_JOINABLE_FRIENDS_ONLY) && ( friends[i].dwFriendState & ( XONLINE_FRIENDSTATE_FLAG_SENTREQUEST | XONLINE_FRIENDSTATE_FLAG_RECEIVEDREQUEST ) ) == 0 && friends[i].dwTitleID == TITLEID_MINECRAFT) { //printf("Valid game to join\n");
bool sessionAlreadyAdded = false; for(AUTO_VAR(it, friendsSessions[m_lastSearchPad].begin()); it < friendsSessions[m_lastSearchPad].end(); ++it) { FriendSessionInfo *current = *it; if( memcmp( &friends[i].sessionID, ¤t->sessionId, sizeof(SessionID) ) == 0 ) { //printf("We already have this session from another player.\n"); sessionAlreadyAdded = true; break; } }
if(!sessionAlreadyAdded) { FriendSessionInfo *sessionInfo = new FriendSessionInfo(); sessionInfo->sessionId = friends[i].sessionID; friendsSessions[m_lastSearchPad].push_back(sessionInfo); //g_NetworkManager.SearchForGameById(friends[i].sessionID,&SearchForGameCallback, m_hObj); //++m_searches; } } } delete [] buffer; }
if( friendsSessions[m_lastSearchPad].empty() ) { SetSearchResultsReady(); return; }
DWORD sessionIDCount = min( XSESSION_SEARCH_MAX_IDS, friendsSessions[m_lastSearchPad].size() ); SessionID *sessionIDList = new SessionID[sessionIDCount];
for(DWORD i = 0; i < sessionIDCount; ++i) { sessionIDList[i] = friendsSessions[m_lastSearchPad].at(i)->sessionId; }
DWORD dwStatus = ERROR_SUCCESS; DWORD cbResults = 0; // In this first call, explicitly pass in a null pointer and a buffer size // of 0. This forces the function to set cbResults to the correct buffer // size.
dwStatus = XSessionSearchByIds( sessionIDCount, sessionIDList, g_NetworkManager.GetPrimaryPad(), &cbResults, // Pass in the address of the size variable NULL, NULL // This example uses the synchronous model );
XOVERLAPPED *pOverlapped = new XOVERLAPPED(); ZeroMemory(pOverlapped, sizeof(XOVERLAPPED));
// If the function returns ERROR_INSUFFICIENT_BUFFER cbResults has been // changed to reflect the size buffer that will be necessary for this call. Use // the new size to allocate a buffer of the appropriate size. if (ERROR_INSUFFICIENT_BUFFER == dwStatus && cbResults > 0) { // Allocate this on the main thread rather in the search thread which might run out of memory m_pSearchResults[m_lastSearchPad] = (XSESSION_SEARCHRESULT_HEADER *) new BYTE[cbResults];
if (!m_pSearchResults[m_lastSearchPad]) { dwStatus = ERROR_OUTOFMEMORY; // Handle this "out of title memory" case and abort the read. }
ZeroMemory(m_pSearchResults[m_lastSearchPad], cbResults);
// Next, call the function again with the exact same parameters, except // this time use the modified buffer size and a pointer to a buffer that // matches it. dwStatus = XSessionSearchByIds( sessionIDCount, sessionIDList, g_NetworkManager.GetPrimaryPad(), &cbResults, // Pass in the address of the size variable m_pSearchResults[m_lastSearchPad], pOverlapped ); }
// Test the result of either the first call (if it failed with // something other than ERROR_INSUFFICIENT_BUFFER) or the subsequent call. // If the function does not succeed after allocating a buffer of the appropriate size // succeed, something else is wrong. if (ERROR_IO_PENDING != dwStatus)//ERROR_SUCCESS != dwStatus) { // Handle other errors. app.DebugPrintf("An error occured while enumerating sessions\n"); SetSearchResultsReady();
delete [] sessionIDList; } else if ( cbResults > 0 ) { SearchForGamesData *threadData = new SearchForGamesData(); threadData->sessionIDCount = sessionIDCount; threadData->searchBuffer = m_pSearchResults[m_lastSearchPad]; threadData->ppQos = &m_pQoSResult[m_lastSearchPad]; threadData->pOverlapped = pOverlapped; threadData->sessionIDList = sessionIDList;
m_SearchingThread = new C4JThread(&CPlatformNetworkManagerXbox::SearchForGamesThreadProc, threadData, "SearchForGames"); m_SearchingThread->SetProcessor( 2 ); m_SearchingThread->Run(); } else { SetSearchResultsReady(); }}
int CPlatformNetworkManagerXbox::SearchForGamesThreadProc( void* lpParameter ){ SearchForGamesData *threadData = (SearchForGamesData *)lpParameter;
DWORD sessionIDCount = threadData->sessionIDCount;
XOVERLAPPED *pOverlapped = threadData->pOverlapped;
DWORD dwStatus = ERROR_SUCCESS; DWORD cbResults = sessionIDCount; XSESSION_SEARCHRESULT_HEADER *pSearchResults = (XSESSION_SEARCHRESULT_HEADER *)threadData->searchBuffer;
while( !XHasOverlappedIoCompleted(pOverlapped) ) { Sleep(100); } delete pOverlapped; delete [] threadData->sessionIDList;
if( pSearchResults->dwSearchResults == 0 ) { g_pPlatformNetworkManager->SetSearchResultsReady(); return 0; }
// TODO 4J Stu - Is there a nicer way to allocate less here? const XNADDR *QoSxnaddr[XSESSION_SEARCH_MAX_IDS];// = new XNADDR*[sessionIDCount]; const SessionID *QoSxnkid[XSESSION_SEARCH_MAX_IDS];// = new XNKID*[sessionIDCount]; // Note SessionID is just typedef'd to be a XNKID on xbox const XNKEY *QoSxnkey[XSESSION_SEARCH_MAX_IDS];// = new XNKEY*[sessionIDCount];
for(DWORD i = 0; i < pSearchResults->dwSearchResults; ++i) { QoSxnaddr[i] = &pSearchResults->pResults[i].info.hostAddress; QoSxnkid[i] = &pSearchResults->pResults[i].info.sessionID; QoSxnkey[i] = &pSearchResults->pResults[i].info.keyExchangeKey; } // Create an event object that is autoreset with an initial state of "not signaled". // Pass this event handle to the QoSLookup to receive notification of each QoS lookup. HANDLE QoSLookupHandle = CreateEvent(NULL, false, false, NULL);
*threadData->ppQos = new XNQOS(); INT iRet = XNetQosLookup( pSearchResults->dwSearchResults, // Number of remote Xbox 360 consoles to probe QoSxnaddr, // Array of pointers to XNADDR structures QoSxnkid, // Array of pointers to XNKID structures that contain session IDs for the remote Xbox 360 consoles QoSxnkey, // Array of pointers to XNKEY structures that contain key-exchange keys for the remote Xbox 360 consoles 0, // Number of security gateways to probe NULL, // Pointer to an array of IN_ADDR structures that contain the IP addresses of the security gateways NULL, // Pointer to an array of service IDs for the security gateway 8, // Number of desired probe replies to receive 0, // Maximum upstream bandwidth that the outgoing QoS probe packets can consume 0, // Flags QoSLookupHandle, // Event handle threadData->ppQos ); // Pointer to a pointer to an XNQOS structure that receives the results from the QoS probes if( 0 != iRet ) { app.DebugPrintf( "XNetQosLookup failed with error 0x%08x", iRet); g_pPlatformNetworkManager->SetSearchResultsReady(); } else { //m_bQoSTesting = TRUE; // Wait for results to all complete. cxnqosPending will eventually hit zero. // Pause thread waiting for QosLookup events to be triggered. while ( (*threadData->ppQos)->cxnqosPending != 0 ) { // 4J Stu - We could wait for INFINITE if we weren't watching for the kill flag WaitForSingleObject(QoSLookupHandle, 100); } // Close handle CloseHandle( QoSLookupHandle );
g_pPlatformNetworkManager->SetSearchResultsReady(pSearchResults->dwSearchResults); }
return 0;}
void CPlatformNetworkManagerXbox::SetSearchResultsReady(int resultCount ){ m_bSearchResultsReady = true; m_searchResultsCount[m_lastSearchPad] = resultCount;}
vector<FriendSessionInfo *> *CPlatformNetworkManagerXbox::GetSessionList(int iPad, int localPlayers, bool partyOnly){ vector<FriendSessionInfo *> *filteredList = new vector<FriendSessionInfo *>();;
const XSESSION_SEARCHRESULT *pSearchResult; const XNQOSINFO * pxnqi;
if( m_currentSearchResultsCount[iPad] > 0 ) { // Loop through all the results. for( DWORD dwResult = 0; dwResult < m_currentSearchResultsCount[iPad]; dwResult++ ) { pSearchResult = &m_pCurrentSearchResults[iPad]->pResults[dwResult]; // No room for us, so ignore it // 4J Stu - pSearchResult should never be NULL, but just in case... if(pSearchResult == NULL || pSearchResult->dwOpenPublicSlots < localPlayers) continue;
bool foundSession = false; FriendSessionInfo *sessionInfo = NULL; AUTO_VAR(itFriendSession, friendsSessions[iPad].begin()); for(itFriendSession = friendsSessions[iPad].begin(); itFriendSession < friendsSessions[iPad].end(); ++itFriendSession) { sessionInfo = *itFriendSession; if(memcmp( &pSearchResult->info.sessionID, &sessionInfo->sessionId, sizeof(SessionID) ) == 0 && (!partyOnly || (partyOnly && sessionInfo->hasPartyMember) ) ) { sessionInfo->searchResult = *pSearchResult; sessionInfo->displayLabel = new wchar_t[100]; ZeroMemory( sessionInfo->displayLabel, 100 * sizeof(wchar_t) ); foundSession = true; break; } }
// We received a search result for a session no longer in our list of friends sessions if(!foundSession) continue;
// Print some info about this result. app.DebugPrintf( "Search result %u:\n", dwResult ); //app.DebugPrintf( " public slots open = %u, filled = %u\n", pSearchResult->dwOpenPublicSlots, pSearchResult->dwFilledPublicSlots ); //app.DebugPrintf( " private slots open = %u, filled = %u\n", pSearchResult->dwOpenPrivateSlots, pSearchResult->dwFilledPrivateSlots );
// See if this result was contacted successfully via QoS probes. pxnqi = &m_pCurrentQoSResult[iPad]->axnqosinfo[dwResult]; if( pxnqi->bFlags & XNET_XNQOSINFO_TARGET_CONTACTED ) { // Print the round trip time and the rough estimation of // bandwidth. //app.DebugPrintf( " RTT min = %u, med = %u\n", pxnqi->wRttMinInMsecs, pxnqi->wRttMedInMsecs ); //app.DebugPrintf( " bps up = %u, down = %u\n", pxnqi->dwUpBitsPerSec, pxnqi->dwDnBitsPerSec );
if(pxnqi->cbData > 0) { sessionInfo->data = *(GameSessionData *)pxnqi->pbData;
wstring gamerName = convStringToWstring(sessionInfo->data.hostName);#ifndef _CONTENT_PACKAGE if(app.DebugSettingsOn() && (app.GetGameSettingsDebugMask()&(1L<<eDebugSetting_DebugLeaderboards))) { swprintf(sessionInfo->displayLabel,app.GetString(IDS_GAME_HOST_NAME),L"WWWWWWWWWWWWWWWW"); } else#endif { swprintf(sessionInfo->displayLabel,app.GetString(IDS_GAME_HOST_NAME),gamerName.c_str() ); } } else { swprintf(sessionInfo->displayLabel,app.GetString(IDS_GAME_HOST_NAME_UNKNOWN)); } sessionInfo->displayLabelLength = wcslen( sessionInfo->displayLabel );
// If this host wasn't disabled use this one. if( !( pxnqi->bFlags & XNET_XNQOSINFO_TARGET_DISABLED ) && sessionInfo->data.netVersion == MINECRAFT_NET_VERSION && sessionInfo->data.isJoinable) { //printf("This game is valid\n"); //if( foundSession ) friendsSessions.erase(itFriendSession); FriendSessionInfo *newInfo = new FriendSessionInfo(); newInfo->data = sessionInfo->data; newInfo->displayLabel = new wchar_t[100]; memcpy(newInfo->displayLabel, sessionInfo->displayLabel, 100 * sizeof(wchar_t) ); newInfo->displayLabelLength = sessionInfo->displayLabelLength; newInfo->hasPartyMember = sessionInfo->hasPartyMember; newInfo->searchResult = sessionInfo->searchResult; newInfo->sessionId = sessionInfo->sessionId; filteredList->push_back(newInfo); } #ifndef _CONTENT_PACKAGE if( sessionInfo->data.netVersion != MINECRAFT_NET_VERSION ) { wprintf(L"%ls version of %d does not match our version of %d\n", sessionInfo->displayLabel, sessionInfo->data.netVersion, MINECRAFT_NET_VERSION); } #endif } } }
return filteredList;}
// This runs through the search results for a session matching sessionId, then returns the full details in foundSessionInfobool CPlatformNetworkManagerXbox::GetGameSessionInfo(int iPad, SessionID sessionId, FriendSessionInfo *foundSessionInfo){ HRESULT hr = E_FAIL;
const XSESSION_SEARCHRESULT *pSearchResult; const XNQOSINFO * pxnqi;
if( m_currentSearchResultsCount[iPad] > 0 ) { // Loop through all the results. for( DWORD dwResult = 0; dwResult < m_currentSearchResultsCount[iPad]; dwResult++ ) { pSearchResult = &m_pCurrentSearchResults[iPad]->pResults[dwResult];
if(memcmp( &pSearchResult->info.sessionID, &sessionId, sizeof(SessionID) ) != 0) continue;
bool foundSession = false; FriendSessionInfo *sessionInfo = NULL; AUTO_VAR(itFriendSession, friendsSessions[iPad].begin()); for(itFriendSession = friendsSessions[iPad].begin(); itFriendSession < friendsSessions[iPad].end(); ++itFriendSession) { sessionInfo = *itFriendSession; if(memcmp( &pSearchResult->info.sessionID, &sessionInfo->sessionId, sizeof(SessionID) ) == 0) { sessionInfo->searchResult = *pSearchResult; sessionInfo->displayLabel = new wchar_t[100]; ZeroMemory( sessionInfo->displayLabel, 100 * sizeof(wchar_t) ); foundSession = true; break; } }
// We received a search result for a session no longer in our list of friends sessions if(!foundSession) break;
// See if this result was contacted successfully via QoS probes. pxnqi = &m_pCurrentQoSResult[iPad]->axnqosinfo[dwResult]; if( pxnqi->bFlags & XNET_XNQOSINFO_TARGET_CONTACTED ) {
if(pxnqi->cbData > 0) { sessionInfo->data = *(GameSessionData *)pxnqi->pbData;
wstring gamerName = convStringToWstring(sessionInfo->data.hostName); swprintf(sessionInfo->displayLabel,app.GetString(IDS_GAME_HOST_NAME),L"MWWWWWWWWWWWWWWM");// gamerName.c_str() ); } else { swprintf(sessionInfo->displayLabel,app.GetString(IDS_GAME_HOST_NAME_UNKNOWN)); } sessionInfo->displayLabelLength = wcslen( sessionInfo->displayLabel );
// If this host wasn't disabled use this one. if( !( pxnqi->bFlags & XNET_XNQOSINFO_TARGET_DISABLED ) && sessionInfo->data.netVersion == MINECRAFT_NET_VERSION && sessionInfo->data.isJoinable) { foundSessionInfo->data = sessionInfo->data; if(foundSessionInfo->displayLabel != NULL) delete [] foundSessionInfo->displayLabel; foundSessionInfo->displayLabel = new wchar_t[100]; memcpy(foundSessionInfo->displayLabel, sessionInfo->displayLabel, 100 * sizeof(wchar_t) ); foundSessionInfo->displayLabelLength = sessionInfo->displayLabelLength; foundSessionInfo->hasPartyMember = sessionInfo->hasPartyMember; foundSessionInfo->searchResult = sessionInfo->searchResult; foundSessionInfo->sessionId = sessionInfo->sessionId;
hr = S_OK; } } } }
return ( hr == S_OK );}
void CPlatformNetworkManagerXbox::SetSessionsUpdatedCallback( void (*SessionsUpdatedCallback)(LPVOID pParam), LPVOID pSearchParam ){ m_SessionsUpdatedCallback = SessionsUpdatedCallback; m_pSearchParam = pSearchParam;}
void CPlatformNetworkManagerXbox::GetFullFriendSessionInfo( FriendSessionInfo *foundSession, void (* FriendSessionUpdatedFn)(bool success, void *pParam), void *pParam ){ FriendSessionUpdatedFn(true, pParam);}
void CPlatformNetworkManagerXbox::ForceFriendsSessionRefresh(){ app.DebugPrintf("Resetting friends session search data\n"); for(unsigned int i = 0; i < XUSER_MAX_COUNT; ++i) { m_searchResultsCount[i] = 0; m_lastSearchStartTime[i] = 0; delete m_pSearchResults[i]; m_pSearchResults[i] = NULL; }}
INetworkPlayer *CPlatformNetworkManagerXbox::addNetworkPlayer(IQNetPlayer *pQNetPlayer){ NetworkPlayerXbox *pNetworkPlayer = new NetworkPlayerXbox(pQNetPlayer); pQNetPlayer->SetCustomDataValue((ULONG_PTR)pNetworkPlayer); currentNetworkPlayers.push_back( pNetworkPlayer ); return pNetworkPlayer;}
void CPlatformNetworkManagerXbox::removeNetworkPlayer(IQNetPlayer *pQNetPlayer){ INetworkPlayer *pNetworkPlayer = getNetworkPlayer(pQNetPlayer); for( AUTO_VAR(it, currentNetworkPlayers.begin()); it != currentNetworkPlayers.end(); it++ ) { if( *it == pNetworkPlayer ) { currentNetworkPlayers.erase(it); return; } }}
INetworkPlayer *CPlatformNetworkManagerXbox::getNetworkPlayer(IQNetPlayer *pQNetPlayer){ return pQNetPlayer ? (INetworkPlayer *)(pQNetPlayer->GetCustomDataValue()) : NULL;}
INetworkPlayer *CPlatformNetworkManagerXbox::GetLocalPlayerByUserIndex(int userIndex ){ return getNetworkPlayer(m_pIQNet->GetLocalPlayerByUserIndex(userIndex)); }
INetworkPlayer *CPlatformNetworkManagerXbox::GetPlayerByIndex(int playerIndex){ return getNetworkPlayer(m_pIQNet->GetPlayerByIndex(playerIndex));}
INetworkPlayer * CPlatformNetworkManagerXbox::GetPlayerByXuid(PlayerUID xuid){ return getNetworkPlayer( m_pIQNet->GetPlayerByXuid(xuid)) ;}
INetworkPlayer * CPlatformNetworkManagerXbox::GetPlayerBySmallId(unsigned char smallId){ return getNetworkPlayer(m_pIQNet->GetPlayerBySmallId(smallId));}
INetworkPlayer *CPlatformNetworkManagerXbox::GetHostPlayer(){ return getNetworkPlayer(m_pIQNet->GetHostPlayer());}
bool CPlatformNetworkManagerXbox::IsHost(){ return m_pIQNet->IsHost() && !m_bHostChanged;}
bool CPlatformNetworkManagerXbox::JoinGameFromInviteInfo( int userIndex, int userMask, const INVITE_INFO *pInviteInfo){ return ( m_pIQNet->JoinGameFromInviteInfo( userIndex, userMask, pInviteInfo ) == S_OK);}
void CPlatformNetworkManagerXbox::SetSessionTexturePackParentId( int id ){ m_hostGameSessionData.texturePackParentId = id;}
void CPlatformNetworkManagerXbox::SetSessionSubTexturePackId( int id ){ m_hostGameSessionData.subTexturePackId = id;}
void CPlatformNetworkManagerXbox::Notify(int ID, ULONG_PTR Param){ m_pIQNet->Notify( ID, Param );}
bool CPlatformNetworkManagerXbox::IsInSession(){ return m_pIQNet->GetState() != QNET_STATE_IDLE;}
bool CPlatformNetworkManagerXbox::IsInGameplay(){ return m_pIQNet->GetState() == QNET_STATE_GAME_PLAY;}
bool CPlatformNetworkManagerXbox::IsReadyToPlayOrIdle(){ return true;}