the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 865 lines 24 kB view raw
1#include "stdafx.h" 2#include "..\..\..\Minecraft.World\Socket.h" 3#include "..\..\..\Minecraft.World\StringHelpers.h" 4#include "PlatformNetworkManagerStub.h" 5#include "..\..\Xbox\Network\NetworkPlayerXbox.h" 6#ifdef _WINDOWS64 7#include "..\..\Windows64\Network\WinsockNetLayer.h" 8#include "..\..\Minecraft.h" 9#include "..\..\User.h" 10#endif 11 12CPlatformNetworkManagerStub *g_pPlatformNetworkManager; 13 14 15void CPlatformNetworkManagerStub::NotifyPlayerJoined(IQNetPlayer *pQNetPlayer ) 16{ 17 const char * pszDescription; 18 19 // 4J Stu - We create a fake socket for every where that we need an INBOUND queue of game data. Outbound 20 // is all handled by QNet so we don't need that. Therefore each client player has one, and the host has one 21 // for each client player. 22 bool createFakeSocket = false; 23 bool localPlayer = false; 24 25 NetworkPlayerXbox *networkPlayer = (NetworkPlayerXbox *)addNetworkPlayer(pQNetPlayer); 26 27 if( pQNetPlayer->IsLocal() ) 28 { 29 localPlayer = true; 30 if( pQNetPlayer->IsHost() ) 31 { 32 pszDescription = "local host"; 33 // 4J Stu - No socket for the localhost as it uses a special loopback queue 34 35 m_machineQNetPrimaryPlayers.push_back( pQNetPlayer ); 36 } 37 else 38 { 39 pszDescription = "local"; 40 41 // We need an inbound queue on all local players to receive data from the host 42 createFakeSocket = true; 43 } 44 } 45 else 46 { 47 if( pQNetPlayer->IsHost() ) 48 { 49 pszDescription = "remote host"; 50 } 51 else 52 { 53 pszDescription = "remote"; 54 55 // If we are the host, then create a fake socket for every remote player 56 if( m_pIQNet->IsHost() ) 57 { 58 createFakeSocket = true; 59 } 60 } 61 62 if( m_pIQNet->IsHost() && !m_bHostChanged ) 63 { 64 // Do we already have a primary player for this system? 65 bool systemHasPrimaryPlayer = false; 66 for(AUTO_VAR(it, m_machineQNetPrimaryPlayers.begin()); it < m_machineQNetPrimaryPlayers.end(); ++it) 67 { 68 IQNetPlayer *pQNetPrimaryPlayer = *it; 69 if( pQNetPlayer->IsSameSystem(pQNetPrimaryPlayer) ) 70 { 71 systemHasPrimaryPlayer = true; 72 break; 73 } 74 } 75 if( !systemHasPrimaryPlayer ) 76 m_machineQNetPrimaryPlayers.push_back( pQNetPlayer ); 77 } 78 } 79 g_NetworkManager.PlayerJoining( networkPlayer ); 80 81 if( createFakeSocket == true && !m_bHostChanged ) 82 { 83 g_NetworkManager.CreateSocket( networkPlayer, localPlayer ); 84 } 85 86 app.DebugPrintf( "Player 0x%p \"%ls\" joined; %s; voice %i; camera %i.\n", 87 pQNetPlayer, 88 pQNetPlayer->GetGamertag(), 89 pszDescription, 90 (int) pQNetPlayer->HasVoice(), 91 (int) pQNetPlayer->HasCamera() ); 92 93 94 if( m_pIQNet->IsHost() ) 95 { 96 // 4J-PB - only the host should do this 97// g_NetworkManager.UpdateAndSetGameSessionData(); 98 SystemFlagAddPlayer( networkPlayer ); 99 } 100 101 for( int idx = 0; idx < XUSER_MAX_COUNT; ++idx) 102 { 103 if(playerChangedCallback[idx] != NULL) 104 playerChangedCallback[idx]( playerChangedCallbackParam[idx], networkPlayer, false ); 105 } 106 107 if(m_pIQNet->GetState() == QNET_STATE_GAME_PLAY) 108 { 109 int localPlayerCount = 0; 110 for(unsigned int idx = 0; idx < XUSER_MAX_COUNT; ++idx) 111 { 112 if( m_pIQNet->GetLocalPlayerByUserIndex(idx) != NULL ) ++localPlayerCount; 113 } 114 115 float appTime = app.getAppTime(); 116 117 // Only record stats for the primary player here 118 m_lastPlayerEventTimeStart = appTime; 119 } 120} 121 122void CPlatformNetworkManagerStub::NotifyPlayerLeaving(IQNetPlayer* pQNetPlayer) 123{ 124 app.DebugPrintf("Player 0x%p \"%ls\" leaving.\n", pQNetPlayer, pQNetPlayer->GetGamertag()); 125 126 INetworkPlayer* networkPlayer = getNetworkPlayer(pQNetPlayer); 127 if (networkPlayer == NULL) 128 return; 129 130 Socket* socket = networkPlayer->GetSocket(); 131 if (socket != NULL) 132 { 133 if (m_pIQNet->IsHost()) 134 g_NetworkManager.CloseConnection(networkPlayer); 135 } 136 137 if (m_pIQNet->IsHost()) 138 { 139 SystemFlagRemovePlayer(networkPlayer); 140 } 141 142 g_NetworkManager.PlayerLeaving(networkPlayer); 143 144 for (int idx = 0; idx < XUSER_MAX_COUNT; ++idx) 145 { 146 if (playerChangedCallback[idx] != NULL) 147 playerChangedCallback[idx](playerChangedCallbackParam[idx], networkPlayer, true); 148 } 149 150 removeNetworkPlayer(pQNetPlayer); 151} 152 153 154bool CPlatformNetworkManagerStub::Initialise(CGameNetworkManager *pGameNetworkManager, int flagIndexSize) 155{ 156 m_pGameNetworkManager = pGameNetworkManager; 157 m_flagIndexSize = flagIndexSize; 158 m_pIQNet = new IQNet(); 159 g_pPlatformNetworkManager = this; 160 for( int i = 0; i < XUSER_MAX_COUNT; i++ ) 161 { 162 playerChangedCallback[ i ] = NULL; 163 } 164 165 m_bLeavingGame = false; 166 m_bLeaveGameOnTick = false; 167 m_bHostChanged = false; 168 169 m_bSearchResultsReady = false; 170 m_bSearchPending = false; 171 172 m_bIsOfflineGame = false; 173 m_pSearchParam = NULL; 174 m_SessionsUpdatedCallback = NULL; 175 176 for(unsigned int i = 0; i < XUSER_MAX_COUNT; ++i) 177 { 178 m_searchResultsCount[i] = 0; 179 m_lastSearchStartTime[i] = 0; 180 181 // The results that will be filled in with the current search 182 m_pSearchResults[i] = NULL; 183 m_pQoSResult[i] = NULL; 184 m_pCurrentSearchResults[i] = NULL; 185 m_pCurrentQoSResult[i] = NULL; 186 m_currentSearchResultsCount[i] = 0; 187 } 188 189 // Success! 190 return true; 191} 192 193void CPlatformNetworkManagerStub::Terminate() 194{ 195} 196 197int CPlatformNetworkManagerStub::GetJoiningReadyPercentage() 198{ 199 return 100; 200} 201 202int CPlatformNetworkManagerStub::CorrectErrorIDS(int IDS) 203{ 204 return IDS; 205} 206 207bool CPlatformNetworkManagerStub::isSystemPrimaryPlayer(IQNetPlayer *pQNetPlayer) 208{ 209 return true; 210} 211 212// We call this twice a frame, either side of the render call so is a good place to "tick" things 213void CPlatformNetworkManagerStub::DoWork() 214{ 215#ifdef _WINDOWS64 216 extern QNET_STATE _iQNetStubState; 217 if (_iQNetStubState == QNET_STATE_SESSION_STARTING && app.GetGameStarted()) 218 { 219 _iQNetStubState = QNET_STATE_GAME_PLAY; 220 if (m_pIQNet->IsHost()) 221 WinsockNetLayer::UpdateAdvertiseJoinable(true); 222 } 223 if (_iQNetStubState == QNET_STATE_IDLE) 224 TickSearch(); 225 if (_iQNetStubState == QNET_STATE_GAME_PLAY && m_pIQNet->IsHost()) 226 { 227 BYTE disconnectedSmallId; 228 while (WinsockNetLayer::PopDisconnectedSmallId(&disconnectedSmallId)) 229 { 230 IQNetPlayer* qnetPlayer = m_pIQNet->GetPlayerBySmallId(disconnectedSmallId); 231 if (qnetPlayer != NULL && qnetPlayer->m_smallId == disconnectedSmallId) 232 { 233 NotifyPlayerLeaving(qnetPlayer); 234 qnetPlayer->m_smallId = 0; 235 qnetPlayer->m_isRemote = false; 236 qnetPlayer->m_isHostPlayer = false; 237 qnetPlayer->m_gamertag[0] = 0; 238 qnetPlayer->SetCustomDataValue(0); 239 WinsockNetLayer::PushFreeSmallId(disconnectedSmallId); 240 if (IQNet::s_playerCount > 1) 241 IQNet::s_playerCount--; 242 } 243 } 244 } 245#endif 246} 247 248int CPlatformNetworkManagerStub::GetPlayerCount() 249{ 250 return m_pIQNet->GetPlayerCount(); 251} 252 253bool CPlatformNetworkManagerStub::ShouldMessageForFullSession() 254{ 255 return false; 256} 257 258int CPlatformNetworkManagerStub::GetOnlinePlayerCount() 259{ 260 return 1; 261} 262 263int CPlatformNetworkManagerStub::GetLocalPlayerMask(int playerIndex) 264{ 265 return 1 << playerIndex; 266} 267 268bool CPlatformNetworkManagerStub::AddLocalPlayerByUserIndex( int userIndex ) 269{ 270 NotifyPlayerJoined(m_pIQNet->GetLocalPlayerByUserIndex(userIndex)); 271 return ( m_pIQNet->AddLocalPlayerByUserIndex(userIndex) == S_OK ); 272} 273 274bool CPlatformNetworkManagerStub::RemoveLocalPlayerByUserIndex( int userIndex ) 275{ 276 return true; 277} 278 279bool CPlatformNetworkManagerStub::IsInStatsEnabledSession() 280{ 281 return true; 282} 283 284bool CPlatformNetworkManagerStub::SessionHasSpace(unsigned int spaceRequired /*= 1*/) 285{ 286 return true; 287} 288 289void CPlatformNetworkManagerStub::SendInviteGUI(int quadrant) 290{ 291} 292 293bool CPlatformNetworkManagerStub::IsAddingPlayer() 294{ 295 return false; 296} 297 298bool CPlatformNetworkManagerStub::LeaveGame(bool bMigrateHost) 299{ 300 if( m_bLeavingGame ) return true; 301 302 m_bLeavingGame = true; 303 304#ifdef _WINDOWS64 305 WinsockNetLayer::StopAdvertising(); 306#endif 307 308 // If we are the host wait for the game server to end 309 if(m_pIQNet->IsHost() && g_NetworkManager.ServerStoppedValid()) 310 { 311 m_pIQNet->EndGame(); 312 g_NetworkManager.ServerStoppedWait(); 313 g_NetworkManager.ServerStoppedDestroy(); 314 } 315 else 316 { 317 m_pIQNet->EndGame(); 318 } 319 320 for (AUTO_VAR(it, currentNetworkPlayers.begin()); it != currentNetworkPlayers.end(); it++) 321 delete* it; 322 currentNetworkPlayers.clear(); 323 m_machineQNetPrimaryPlayers.clear(); 324 SystemFlagReset(); 325 326#ifdef _WINDOWS64 327 WinsockNetLayer::Shutdown(); 328 WinsockNetLayer::Initialize(); 329#endif 330 331 return true; 332} 333 334bool CPlatformNetworkManagerStub::_LeaveGame(bool bMigrateHost, bool bLeaveRoom) 335{ 336 return true; 337} 338 339void CPlatformNetworkManagerStub::HostGame(int localUsersMask, bool bOnlineGame, bool bIsPrivate, unsigned char publicSlots /*= MINECRAFT_NET_MAX_PLAYERS*/, unsigned char privateSlots /*= 0*/) 340{ 341// #ifdef _XBOX 342 // 4J Stu - We probably did this earlier as well, but just to be sure! 343 SetLocalGame( !bOnlineGame ); 344 SetPrivateGame( bIsPrivate ); 345 SystemFlagReset(); 346 347 // Make sure that the Primary Pad is in by default 348 localUsersMask |= GetLocalPlayerMask( g_NetworkManager.GetPrimaryPad() ); 349 350 m_bLeavingGame = false; 351 352 m_pIQNet->HostGame(); 353 354#ifdef _WINDOWS64 355 IQNet::m_player[0].m_smallId = 0; 356 IQNet::m_player[0].m_isRemote = false; 357 IQNet::m_player[0].m_isHostPlayer = true; 358 IQNet::s_playerCount = 1; 359#endif 360 361 _HostGame( localUsersMask, publicSlots, privateSlots ); 362 363#ifdef _WINDOWS64 364 int port = WIN64_NET_DEFAULT_PORT; 365 if (!WinsockNetLayer::IsActive()) 366 WinsockNetLayer::HostGame(port); 367 368 const wchar_t* hostName = IQNet::m_player[0].m_gamertag; 369 unsigned int settings = app.GetGameHostOption(eGameHostOption_All); 370 WinsockNetLayer::StartAdvertising(port, hostName, settings, 0, 0, MINECRAFT_NET_VERSION); 371#endif 372//#endif 373} 374 375void CPlatformNetworkManagerStub::_HostGame(int usersMask, unsigned char publicSlots /*= MINECRAFT_NET_MAX_PLAYERS*/, unsigned char privateSlots /*= 0*/) 376{ 377} 378 379bool CPlatformNetworkManagerStub::_StartGame() 380{ 381 return true; 382} 383 384int CPlatformNetworkManagerStub::JoinGame(FriendSessionInfo* searchResult, int localUsersMask, int primaryUserIndex) 385{ 386#ifdef _WINDOWS64 387 if (searchResult == NULL) 388 return CGameNetworkManager::JOINGAME_FAIL_GENERAL; 389 390 const char* hostIP = searchResult->data.hostIP; 391 int hostPort = searchResult->data.hostPort; 392 393 if (hostPort <= 0 || hostIP[0] == 0) 394 return CGameNetworkManager::JOINGAME_FAIL_GENERAL; 395 396 m_bLeavingGame = false; 397 IQNet::s_isHosting = false; 398 m_pIQNet->ClientJoinGame(); 399 400 IQNet::m_player[0].m_smallId = 0; 401 IQNet::m_player[0].m_isRemote = true; 402 IQNet::m_player[0].m_isHostPlayer = true; 403 wcsncpy_s(IQNet::m_player[0].m_gamertag, 32, searchResult->data.hostName, _TRUNCATE); 404 405 WinsockNetLayer::StopDiscovery(); 406 407 if (!WinsockNetLayer::JoinGame(hostIP, hostPort)) 408 { 409 app.DebugPrintf("Win64 LAN: Failed to connect to %s:%d\n", hostIP, hostPort); 410 return CGameNetworkManager::JOINGAME_FAIL_GENERAL; 411 } 412 413 BYTE localSmallId = WinsockNetLayer::GetLocalSmallId(); 414 415 IQNet::m_player[localSmallId].m_smallId = localSmallId; 416 IQNet::m_player[localSmallId].m_isRemote = false; 417 IQNet::m_player[localSmallId].m_isHostPlayer = false; 418 419 Minecraft* pMinecraft = Minecraft::GetInstance(); 420 wcscpy_s(IQNet::m_player[localSmallId].m_gamertag, 32, pMinecraft->user->name.c_str()); 421 IQNet::s_playerCount = localSmallId + 1; 422 423 NotifyPlayerJoined(&IQNet::m_player[0]); 424 NotifyPlayerJoined(&IQNet::m_player[localSmallId]); 425 426 m_pGameNetworkManager->StateChange_AnyToStarting(); 427 428 return CGameNetworkManager::JOINGAME_SUCCESS; 429#else 430 return CGameNetworkManager::JOINGAME_SUCCESS; 431#endif 432} 433 434bool CPlatformNetworkManagerStub::SetLocalGame(bool isLocal) 435{ 436 m_bIsOfflineGame = isLocal; 437 438 return true; 439} 440 441void CPlatformNetworkManagerStub::SetPrivateGame(bool isPrivate) 442{ 443 app.DebugPrintf("Setting as private game: %s\n", isPrivate ? "yes" : "no" ); 444 m_bIsPrivateGame = isPrivate; 445} 446 447void CPlatformNetworkManagerStub::RegisterPlayerChangedCallback(int iPad, void (*callback)(void *callbackParam, INetworkPlayer *pPlayer, bool leaving), void *callbackParam) 448{ 449 playerChangedCallback[iPad] = callback; 450 playerChangedCallbackParam[iPad] = callbackParam; 451} 452 453void CPlatformNetworkManagerStub::UnRegisterPlayerChangedCallback(int iPad, void (*callback)(void *callbackParam, INetworkPlayer *pPlayer, bool leaving), void *callbackParam) 454{ 455 if(playerChangedCallbackParam[iPad] == callbackParam) 456 { 457 playerChangedCallback[iPad] = NULL; 458 playerChangedCallbackParam[iPad] = NULL; 459 } 460} 461 462void CPlatformNetworkManagerStub::HandleSignInChange() 463{ 464 return; 465} 466 467bool CPlatformNetworkManagerStub::_RunNetworkGame() 468{ 469#ifdef _WINDOWS64 470 extern QNET_STATE _iQNetStubState; 471 _iQNetStubState = QNET_STATE_GAME_PLAY; 472 473 for (DWORD i = 0; i < IQNet::s_playerCount; i++) 474 { 475 if (IQNet::m_player[i].m_isRemote) 476 { 477 INetworkPlayer* pNetworkPlayer = getNetworkPlayer(&IQNet::m_player[i]); 478 if (pNetworkPlayer != NULL && pNetworkPlayer->GetSocket() != NULL) 479 { 480 Socket::addIncomingSocket(pNetworkPlayer->GetSocket()); 481 } 482 } 483 } 484#endif 485 return true; 486} 487 488void CPlatformNetworkManagerStub::UpdateAndSetGameSessionData(INetworkPlayer *pNetworkPlayerLeaving /*= NULL*/) 489{ 490// DWORD playerCount = m_pIQNet->GetPlayerCount(); 491// 492// if( this->m_bLeavingGame ) 493// return; 494// 495// if( GetHostPlayer() == NULL ) 496// return; 497// 498// for(unsigned int i = 0; i < MINECRAFT_NET_MAX_PLAYERS; ++i) 499// { 500// if( i < playerCount ) 501// { 502// INetworkPlayer *pNetworkPlayer = GetPlayerByIndex(i); 503// 504// // We can call this from NotifyPlayerLeaving but at that point the player is still considered in the session 505// if( pNetworkPlayer != pNetworkPlayerLeaving ) 506// { 507// m_hostGameSessionData.players[i] = ((NetworkPlayerXbox *)pNetworkPlayer)->GetUID(); 508// 509// char *temp; 510// temp = (char *)wstringtofilename( pNetworkPlayer->GetOnlineName() ); 511// memcpy(m_hostGameSessionData.szPlayers[i],temp,XUSER_NAME_SIZE); 512// } 513// else 514// { 515// m_hostGameSessionData.players[i] = NULL; 516// memset(m_hostGameSessionData.szPlayers[i],0,XUSER_NAME_SIZE); 517// } 518// } 519// else 520// { 521// m_hostGameSessionData.players[i] = NULL; 522// memset(m_hostGameSessionData.szPlayers[i],0,XUSER_NAME_SIZE); 523// } 524// } 525// 526// m_hostGameSessionData.hostPlayerUID = ((NetworkPlayerXbox *)GetHostPlayer())->GetQNetPlayer()->GetXuid(); 527// m_hostGameSessionData.m_uiGameHostSettings = app.GetGameHostOption(eGameHostOption_All); 528} 529 530int CPlatformNetworkManagerStub::RemovePlayerOnSocketClosedThreadProc( void* lpParam ) 531{ 532 INetworkPlayer *pNetworkPlayer = (INetworkPlayer *)lpParam; 533 534 Socket *socket = pNetworkPlayer->GetSocket(); 535 536 if( socket != NULL ) 537 { 538 //printf("Waiting for socket closed event\n"); 539 socket->m_socketClosedEvent->WaitForSignal(INFINITE); 540 541 //printf("Socket closed event has fired\n"); 542 // 4J Stu - Clear our reference to this socket 543 pNetworkPlayer->SetSocket( NULL ); 544 delete socket; 545 } 546 547 return g_pPlatformNetworkManager->RemoveLocalPlayer( pNetworkPlayer ); 548} 549 550bool CPlatformNetworkManagerStub::RemoveLocalPlayer( INetworkPlayer *pNetworkPlayer ) 551{ 552 return true; 553} 554 555CPlatformNetworkManagerStub::PlayerFlags::PlayerFlags(INetworkPlayer *pNetworkPlayer, unsigned int count) 556{ 557 // 4J Stu - Don't assert, just make it a multiple of 8! This count is calculated from a load of separate values, 558 // and makes tweaking world/render sizes a pain if we hit an assert here 559 count = (count + 8 - 1) & ~(8 - 1); 560 //assert( ( count % 8 ) == 0 ); 561 this->m_pNetworkPlayer = pNetworkPlayer; 562 this->flags = new unsigned char [ count / 8 ]; 563 memset( this->flags, 0, count / 8 ); 564 this->count = count; 565} 566CPlatformNetworkManagerStub::PlayerFlags::~PlayerFlags() 567{ 568 delete [] flags; 569} 570 571// Add a player to the per system flag storage - if we've already got a player from that system, copy its flags over 572void CPlatformNetworkManagerStub::SystemFlagAddPlayer(INetworkPlayer *pNetworkPlayer) 573{ 574 PlayerFlags *newPlayerFlags = new PlayerFlags( pNetworkPlayer, m_flagIndexSize); 575 // If any of our existing players are on the same system, then copy over flags from that one 576 for( unsigned int i = 0; i < m_playerFlags.size(); i++ ) 577 { 578 if( pNetworkPlayer->IsSameSystem(m_playerFlags[i]->m_pNetworkPlayer) ) 579 { 580 memcpy( newPlayerFlags->flags, m_playerFlags[i]->flags, m_playerFlags[i]->count / 8 ); 581 break; 582 } 583 } 584 m_playerFlags.push_back(newPlayerFlags); 585} 586 587// Remove a player from the per system flag storage - just maintains the m_playerFlags vector without any gaps in it 588void CPlatformNetworkManagerStub::SystemFlagRemovePlayer(INetworkPlayer *pNetworkPlayer) 589{ 590 for( unsigned int i = 0; i < m_playerFlags.size(); i++ ) 591 { 592 if( m_playerFlags[i]->m_pNetworkPlayer == pNetworkPlayer ) 593 { 594 delete m_playerFlags[i]; 595 m_playerFlags[i] = m_playerFlags.back(); 596 m_playerFlags.pop_back(); 597 return; 598 } 599 } 600} 601 602void CPlatformNetworkManagerStub::SystemFlagReset() 603{ 604 for( unsigned int i = 0; i < m_playerFlags.size(); i++ ) 605 { 606 delete m_playerFlags[i]; 607 } 608 m_playerFlags.clear(); 609} 610 611// Set a per system flag - this is done by setting the flag on every player that shares that system 612void CPlatformNetworkManagerStub::SystemFlagSet(INetworkPlayer *pNetworkPlayer, int index) 613{ 614 if( ( index < 0 ) || ( index >= m_flagIndexSize ) ) return; 615 if( pNetworkPlayer == NULL ) return; 616 617 for( unsigned int i = 0; i < m_playerFlags.size(); i++ ) 618 { 619 if( pNetworkPlayer->IsSameSystem(m_playerFlags[i]->m_pNetworkPlayer) ) 620 { 621 m_playerFlags[i]->flags[ index / 8 ] |= ( 128 >> ( index % 8 ) ); 622 } 623 } 624} 625 626// Get value of a per system flag - can be read from the flags of the passed in player as anything else sent to that 627// system should also have been duplicated here 628bool CPlatformNetworkManagerStub::SystemFlagGet(INetworkPlayer *pNetworkPlayer, int index) 629{ 630 if( ( index < 0 ) || ( index >= m_flagIndexSize ) ) return false; 631 if( pNetworkPlayer == NULL ) 632 { 633 return false; 634 } 635 636 for( unsigned int i = 0; i < m_playerFlags.size(); i++ ) 637 { 638 if( m_playerFlags[i]->m_pNetworkPlayer == pNetworkPlayer ) 639 { 640 return ( ( m_playerFlags[i]->flags[ index / 8 ] & ( 128 >> ( index % 8 ) ) ) != 0 ); 641 } 642 } 643 return false; 644} 645 646wstring CPlatformNetworkManagerStub::GatherStats() 647{ 648 return L""; 649} 650 651wstring CPlatformNetworkManagerStub::GatherRTTStats() 652{ 653 wstring stats(L"Rtt: "); 654 655 wchar_t stat[32]; 656 657 for(unsigned int i = 0; i < GetPlayerCount(); ++i) 658 { 659 IQNetPlayer *pQNetPlayer = ((NetworkPlayerXbox *)GetPlayerByIndex( i ))->GetQNetPlayer(); 660 661 if(!pQNetPlayer->IsLocal()) 662 { 663 ZeroMemory(stat,32*sizeof(WCHAR)); 664 swprintf(stat, 32, L"%d: %d/", i, pQNetPlayer->GetCurrentRtt() ); 665 stats.append(stat); 666 } 667 } 668 return stats; 669} 670 671void CPlatformNetworkManagerStub::TickSearch() 672{ 673#ifdef _WINDOWS64 674 if (m_SessionsUpdatedCallback == NULL) 675 return; 676 677 static DWORD lastSearchTime = 0; 678 DWORD now = GetTickCount(); 679 if (now - lastSearchTime < 2000) 680 return; 681 lastSearchTime = now; 682 683 SearchForGames(); 684#endif 685} 686 687void CPlatformNetworkManagerStub::SearchForGames() 688{ 689#ifdef _WINDOWS64 690 std::vector<Win64LANSession> lanSessions = WinsockNetLayer::GetDiscoveredSessions(); 691 692 for (size_t i = 0; i < friendsSessions[0].size(); i++) 693 delete friendsSessions[0][i]; 694 friendsSessions[0].clear(); 695 696 for (size_t i = 0; i < lanSessions.size(); i++) 697 { 698 FriendSessionInfo* info = new FriendSessionInfo(); 699 size_t nameLen = wcslen(lanSessions[i].hostName); 700 info->displayLabel = new wchar_t[nameLen + 1]; 701 wcscpy_s(info->displayLabel, nameLen + 1, lanSessions[i].hostName); 702 info->displayLabelLength = (unsigned char)nameLen; 703 info->displayLabelViewableStartIndex = 0; 704 705 info->data.netVersion = lanSessions[i].netVersion; 706 info->data.m_uiGameHostSettings = lanSessions[i].gameHostSettings; 707 info->data.texturePackParentId = lanSessions[i].texturePackParentId; 708 info->data.subTexturePackId = lanSessions[i].subTexturePackId; 709 info->data.isReadyToJoin = lanSessions[i].isJoinable; 710 info->data.isJoinable = lanSessions[i].isJoinable; 711 strncpy_s(info->data.hostIP, sizeof(info->data.hostIP), lanSessions[i].hostIP, _TRUNCATE); 712 info->data.hostPort = lanSessions[i].hostPort; 713 wcsncpy_s(info->data.hostName, XUSER_NAME_SIZE, lanSessions[i].hostName, _TRUNCATE); 714 info->data.playerCount = lanSessions[i].playerCount; 715 info->data.maxPlayers = lanSessions[i].maxPlayers; 716 717 info->sessionId = (SessionID)((unsigned __int64)inet_addr(lanSessions[i].hostIP) | ((unsigned __int64)lanSessions[i].hostPort << 32)); 718 719 friendsSessions[0].push_back(info); 720 } 721 722 m_searchResultsCount[0] = (int)friendsSessions[0].size(); 723 724 if (m_SessionsUpdatedCallback != NULL) 725 m_SessionsUpdatedCallback(m_pSearchParam); 726#endif 727} 728 729int CPlatformNetworkManagerStub::SearchForGamesThreadProc( void* lpParameter ) 730{ 731 return 0; 732} 733 734void CPlatformNetworkManagerStub::SetSearchResultsReady(int resultCount) 735{ 736 m_bSearchResultsReady = true; 737 m_searchResultsCount[m_lastSearchPad] = resultCount; 738} 739 740vector<FriendSessionInfo *> *CPlatformNetworkManagerStub::GetSessionList(int iPad, int localPlayers, bool partyOnly) 741{ 742 vector<FriendSessionInfo*>* filteredList = new vector<FriendSessionInfo*>(); 743 for (size_t i = 0; i < friendsSessions[0].size(); i++) 744 filteredList->push_back(friendsSessions[0][i]); 745 return filteredList; 746} 747 748bool CPlatformNetworkManagerStub::GetGameSessionInfo(int iPad, SessionID sessionId, FriendSessionInfo *foundSessionInfo) 749{ 750 return false; 751} 752 753void CPlatformNetworkManagerStub::SetSessionsUpdatedCallback( void (*SessionsUpdatedCallback)(LPVOID pParam), LPVOID pSearchParam ) 754{ 755 m_SessionsUpdatedCallback = SessionsUpdatedCallback; m_pSearchParam = pSearchParam; 756} 757 758void CPlatformNetworkManagerStub::GetFullFriendSessionInfo( FriendSessionInfo *foundSession, void (* FriendSessionUpdatedFn)(bool success, void *pParam), void *pParam ) 759{ 760 FriendSessionUpdatedFn(true, pParam); 761} 762 763void CPlatformNetworkManagerStub::ForceFriendsSessionRefresh() 764{ 765 app.DebugPrintf("Resetting friends session search data\n"); 766 767 for(unsigned int i = 0; i < XUSER_MAX_COUNT; ++i) 768 { 769 m_searchResultsCount[i] = 0; 770 m_lastSearchStartTime[i] = 0; 771 delete m_pSearchResults[i]; 772 m_pSearchResults[i] = NULL; 773 } 774} 775 776INetworkPlayer *CPlatformNetworkManagerStub::addNetworkPlayer(IQNetPlayer *pQNetPlayer) 777{ 778 NetworkPlayerXbox *pNetworkPlayer = new NetworkPlayerXbox(pQNetPlayer); 779 pQNetPlayer->SetCustomDataValue((ULONG_PTR)pNetworkPlayer); 780 currentNetworkPlayers.push_back( pNetworkPlayer ); 781 return pNetworkPlayer; 782} 783 784void CPlatformNetworkManagerStub::removeNetworkPlayer(IQNetPlayer *pQNetPlayer) 785{ 786 INetworkPlayer *pNetworkPlayer = getNetworkPlayer(pQNetPlayer); 787 for( AUTO_VAR(it, currentNetworkPlayers.begin()); it != currentNetworkPlayers.end(); it++ ) 788 { 789 if( *it == pNetworkPlayer ) 790 { 791 currentNetworkPlayers.erase(it); 792 return; 793 } 794 } 795} 796 797INetworkPlayer *CPlatformNetworkManagerStub::getNetworkPlayer(IQNetPlayer *pQNetPlayer) 798{ 799 return pQNetPlayer ? (INetworkPlayer *)(pQNetPlayer->GetCustomDataValue()) : NULL; 800} 801 802 803INetworkPlayer *CPlatformNetworkManagerStub::GetLocalPlayerByUserIndex(int userIndex ) 804{ 805 return getNetworkPlayer(m_pIQNet->GetLocalPlayerByUserIndex(userIndex)); 806} 807 808INetworkPlayer *CPlatformNetworkManagerStub::GetPlayerByIndex(int playerIndex) 809{ 810 return getNetworkPlayer(m_pIQNet->GetPlayerByIndex(playerIndex)); 811} 812 813INetworkPlayer * CPlatformNetworkManagerStub::GetPlayerByXuid(PlayerUID xuid) 814{ 815 return getNetworkPlayer( m_pIQNet->GetPlayerByXuid(xuid)) ; 816} 817 818INetworkPlayer * CPlatformNetworkManagerStub::GetPlayerBySmallId(unsigned char smallId) 819{ 820 return getNetworkPlayer(m_pIQNet->GetPlayerBySmallId(smallId)); 821} 822 823INetworkPlayer *CPlatformNetworkManagerStub::GetHostPlayer() 824{ 825 return getNetworkPlayer(m_pIQNet->GetHostPlayer()); 826} 827 828bool CPlatformNetworkManagerStub::IsHost() 829{ 830 return m_pIQNet->IsHost() && !m_bHostChanged; 831} 832 833bool CPlatformNetworkManagerStub::JoinGameFromInviteInfo( int userIndex, int userMask, const INVITE_INFO *pInviteInfo) 834{ 835 return ( m_pIQNet->JoinGameFromInviteInfo( userIndex, userMask, pInviteInfo ) == S_OK); 836} 837 838void CPlatformNetworkManagerStub::SetSessionTexturePackParentId( int id ) 839{ 840 m_hostGameSessionData.texturePackParentId = id; 841} 842 843void CPlatformNetworkManagerStub::SetSessionSubTexturePackId( int id ) 844{ 845 m_hostGameSessionData.subTexturePackId = id; 846} 847 848void CPlatformNetworkManagerStub::Notify(int ID, ULONG_PTR Param) 849{ 850} 851 852bool CPlatformNetworkManagerStub::IsInSession() 853{ 854 return m_pIQNet->GetState() != QNET_STATE_IDLE; 855} 856 857bool CPlatformNetworkManagerStub::IsInGameplay() 858{ 859 return m_pIQNet->GetState() == QNET_STATE_GAME_PLAY; 860} 861 862bool CPlatformNetworkManagerStub::IsReadyToPlayOrIdle() 863{ 864 return true; 865}