From b691c43c44ff180d10e7d4a9afc83b98551ff586 Mon Sep 17 00:00:00 2001 From: daoge_cmd <3523206925@qq.com> Date: Sun, 1 Mar 2026 12:16:08 +0800 Subject: Initial commit --- .../PSVita/Network/PSVita_NPToolkit.cpp | 491 +++ Minecraft.Client/PSVita/Network/PSVita_NPToolkit.h | 32 + .../Network/SQRNetworkManager_AdHoc_Vita.cpp | 3119 +++++++++++++++ .../PSVita/Network/SQRNetworkManager_AdHoc_Vita.h | 352 ++ .../PSVita/Network/SQRNetworkManager_Vita.cpp | 4080 ++++++++++++++++++++ .../PSVita/Network/SQRNetworkManager_Vita.h | 324 ++ .../PSVita/Network/SonyCommerce_Vita.cpp | 1519 ++++++++ .../PSVita/Network/SonyCommerce_Vita.h | 204 + Minecraft.Client/PSVita/Network/SonyHttp_Vita.cpp | 271 ++ Minecraft.Client/PSVita/Network/SonyHttp_Vita.h | 26 + .../PSVita/Network/SonyRemoteStorage_Vita.cpp | 398 ++ .../PSVita/Network/SonyRemoteStorage_Vita.h | 43 + .../PSVita/Network/SonyVoiceChat_Vita.cpp | 1092 ++++++ .../PSVita/Network/SonyVoiceChat_Vita.h | 222 ++ 14 files changed, 12173 insertions(+) create mode 100644 Minecraft.Client/PSVita/Network/PSVita_NPToolkit.cpp create mode 100644 Minecraft.Client/PSVita/Network/PSVita_NPToolkit.h create mode 100644 Minecraft.Client/PSVita/Network/SQRNetworkManager_AdHoc_Vita.cpp create mode 100644 Minecraft.Client/PSVita/Network/SQRNetworkManager_AdHoc_Vita.h create mode 100644 Minecraft.Client/PSVita/Network/SQRNetworkManager_Vita.cpp create mode 100644 Minecraft.Client/PSVita/Network/SQRNetworkManager_Vita.h create mode 100644 Minecraft.Client/PSVita/Network/SonyCommerce_Vita.cpp create mode 100644 Minecraft.Client/PSVita/Network/SonyCommerce_Vita.h create mode 100644 Minecraft.Client/PSVita/Network/SonyHttp_Vita.cpp create mode 100644 Minecraft.Client/PSVita/Network/SonyHttp_Vita.h create mode 100644 Minecraft.Client/PSVita/Network/SonyRemoteStorage_Vita.cpp create mode 100644 Minecraft.Client/PSVita/Network/SonyRemoteStorage_Vita.h create mode 100644 Minecraft.Client/PSVita/Network/SonyVoiceChat_Vita.cpp create mode 100644 Minecraft.Client/PSVita/Network/SonyVoiceChat_Vita.h (limited to 'Minecraft.Client/PSVita/Network') diff --git a/Minecraft.Client/PSVita/Network/PSVita_NPToolkit.cpp b/Minecraft.Client/PSVita/Network/PSVita_NPToolkit.cpp new file mode 100644 index 00000000..fb859761 --- /dev/null +++ b/Minecraft.Client/PSVita/Network/PSVita_NPToolkit.cpp @@ -0,0 +1,491 @@ +#include "stdafx.h" + +#include "PSVita_NPToolkit.h" +#include "PSVita/PSVitaExtras/Conf.h" +#include "PSVita/Network/SonyCommerce_Vita.h" + +// #define NP_TITLE_ID "CUSA00265_00" +// #define NP_TITLE_SECRET_HEX "c37e30fa1f7fd29e3534834d62781143ae29aa7b51d02320e7aa0b45116ad600e4d309e8431bc37977d98b8db480e721876e7d736e11fd906778c0033bbb6370903477b1dc1e65106afc62007a5feee3158844d721b88c3f4bff2e56417b6910cedfdec78b130d2e0dd35a35a9e2ae31d5889f9398c1d62b52a3630bb03faa5b" +// #define CLIENT_ID_FOR_SAMPLE "c8c483e7-f0b4-420b-877b-307fcb4c3cdc" + +//#define _USE_STANDARD_ALLOC + +// sce::Toolkit::NP::Utilities::Future< sce::Toolkit::NP::NpSessionInformation > PSVitaNPToolkit::sm_createJoinFuture; +// sce::Toolkit::NP::NpSessionInformation PSVitaNPToolkit::m_currentSessionInfo; +sce::Toolkit::NP::Utilities::Future PSVitaNPToolkit::m_messageData; + + +void PSVitaNPToolkit::presenceCallback( const sce::Toolkit::NP::Event& event ) +{ + switch(event.event) + { + case sce::Toolkit::NP::Event::presenceSet: + app.DebugPrintf("presenceSet Successfully\n"); + break; + case sce::Toolkit::NP::Event::presenceSetFailed: + app.DebugPrintf("presenceSetFailed event received = 0x%x\n", event.returnCode); + SQRNetworkManager_Vita::SetPresenceFailedCallback(); +// assert(0); + break; + default: + break; + } +} + +void PSVitaNPToolkit::profileCallback( const sce::Toolkit::NP::Event& event ) +{ + switch(event.event) + { + case sce::Toolkit::NP::Event::profileError: + app.DebugPrintf("User profile error: 0x%x\n", event.returnCode); + break; + default: + app.DebugPrintf("User profile event: %i\n", event.event); + break; + } +} + +void PSVitaNPToolkit::messagingCallback( const sce::Toolkit::NP::Event& event ) +{ + switch(event.event) + { + case sce::Toolkit::NP::Event::serviceError: + app.DebugPrintf("NP messagingCallback - serviceError: 0x%x\n", event.returnCode); + ProfileManager.SetSysUIShowing( false ); + break; + case sce::Toolkit::NP::Event::messageSent: + app.DebugPrintf("NP messagingCallback - messageSent: 0x%x\n", event.returnCode); + ProfileManager.SetSysUIShowing( false ); + break; + case sce::Toolkit::NP::Event::messageError: + app.DebugPrintf("NP messagingCallback - messageError: 0x%x\n", event.returnCode); + if(SQRNetworkManager_Vita::m_bSendingInviteMessage) // MGH - added to fix a sysUI lockup on startup - devtrack #5883 + ProfileManager.SetSysUIShowing( false ); + break; + case sce::Toolkit::NP::Event::messageDialogTerminated: + app.DebugPrintf("NP messagingCallback - messageDialogTerminated: 0x%x\n", event.returnCode); + ProfileManager.SetSysUIShowing( false ); + break; + case sce::Toolkit::NP::Event::messageRetrieved: + app.DebugPrintf("NP messagingCallback - messageRetrieved: 0x%x\n", event.returnCode); + if(m_messageData.hasResult()) + { + SQRNetworkManager_Vita::GetInviteDataAndProcess(m_messageData.get()); + } + else + { + app.DebugPrintf("messageRetrieved error 0x%08x\n", m_messageData.getError()); + } + break; + case sce::Toolkit::NP::Event::messageInGameDataReceived: + app.DebugPrintf("NP messagingCallback - messageInGameDataReceived: 0x%x\n", event.returnCode); + break; + case sce::Toolkit::NP::Event::messageInGameDataRetrievalDone: + app.DebugPrintf("NP messagingCallback - messageInGameDataRetrievalDone: 0x%x\n", event.returnCode); + break; + + default: + assert(0); + break; + } +} + +void PSVitaNPToolkit::coreCallback( const sce::Toolkit::NP::Event& event ) +{ + switch (event.event) + { + case sce::Toolkit::NP::Event::enetUp: ///< An event from the NetCtl service generated when a connection has been established. + app.DebugPrintf("Received core callback: Network Up \n"); + break; + case sce::Toolkit::NP::Event::enetDown: ///< An event from the NetCtl service generated when the connection layer has gone down. + app.DebugPrintf("Received core callback: Network down \n"); + break; + case sce::Toolkit::NP::Event::loggedIn: ///< An event from the NetCtl service generated when a connection to the PSN has been established. + app.DebugPrintf("Received core callback: PSN sign in \n"); + ProfileManager.SetNetworkStatus(true, true); + break; + case sce::Toolkit::NP::Event::loggedOut: ///< An event from the NetCtl service generated when a connection to the PSN has been lost. + app.DebugPrintf("Received core callback: PSN sign out \n"); + ProfileManager.SetNetworkStatus(false, true); + break; + default: + app.DebugPrintf("Received core callback: event Num: %d \n", event.event); + break; + } +} + +void PSVitaNPToolkit::sceNpToolkitCallback( const sce::Toolkit::NP::Event& event) +{ + switch(event.service) + { + case sce::Toolkit::NP::ServiceType::core: + coreCallback(event); + break; +// case sce::Toolkit::NP::ServiceType::netInfo: +// Menu::NetInfo::sceNpToolkitCallback(event); +// break; +// case sce::Toolkit::NP::ServiceType::sessions: +// sessionsCallback(event); +// break; +// case sce::Toolkit::NP::ServiceType::tss: +// Menu::Tss::sceNpToolkitCallback(event); +// break; +// case sce::Toolkit::NP::ServiceType::ranking: +// Menu::Ranking::sceNpToolkitCallback(event); +// break; +// case sce::Toolkit::NP::ServiceType::tus: +// Menu::Tus::sceNpToolkitCallback(event); +// break; + case sce::Toolkit::NP::ServiceType::profile: + profileCallback(event); + break; + case sce::Toolkit::NP::ServiceType::messaging: + messagingCallback(event); +// case sce::Toolkit::NP::ServiceType::friends: +// Menu::Friends::sceNpToolkitCallback(event); +// break; +// case sce::Toolkit::NP::ServiceType::auth: +// Menu::Auth::sceNpToolkitCallback(event); +// break; + case sce::Toolkit::NP::ServiceType::trophy: +// ProfileManager.trophySystemCallback(event); + break; +// case sce::Toolkit::NP::ServiceType::messaging: +// messagingCallback(event); +// case sce::Toolkit::NP::ServiceType::inGameMessage: +// Menu::Messaging::sceNpToolkitCallback(event); +// break; + + case sce::Toolkit::NP::ServiceType::commerce: + SonyCommerce_Vita::commerce2Handler(event); + break; + case sce::Toolkit::NP::ServiceType::presence: + presenceCallback(event); + break; +// case sce::Toolkit::NP::ServiceType::wordFilter: +// Menu::WordFilter::sceNpToolkitCallback(event); +// break; +// case sce::Toolkit::NP::ServiceType::sns: +// Menu::Sns::sceNpToolkitCallback(event); +// break; + +// case sce::Toolkit::NP::ServiceType::gameCustomData: +// gameCustomDataCallback(event); + default: + break; + } +} + + +// +// void PSVitaNPToolkit::sessionsCallback( const sce::Toolkit::NP::Event& event) +// { +// switch(event.event) +// { +// case sce::Toolkit::NP::Event::npSessionCreateResult: ///< An event generated when the %Np session creation process has been completed. +// app.DebugPrintf("npSessionCreateResult"); +// if(sm_createJoinFuture.hasResult()) +// { +// app.DebugPrintf("Session Created Successfully\n"); +// m_currentSessionInfo = *sm_createJoinFuture.get(); +// } +// else +// { +// app.DebugPrintf("Session Creation Failed 0x%x\n",sm_createJoinFuture.getError()); +// } +// sm_createJoinFuture.reset(); +// break; +// case sce::Toolkit::NP::Event::npSessionJoinResult: ///< An event generated when the join %Np session process has been completed. +// app.DebugPrintf("npSessionJoinResult"); +// if(sm_createJoinFuture.hasResult()) +// { +// app.DebugPrintf("Session joined successfully\n"); +// m_currentSessionInfo = *sm_createJoinFuture.get(); +// } +// else +// { +// app.DebugPrintf("Session join Failed 0x%x\n",sm_createJoinFuture.getError()); +// } +// sm_createJoinFuture.reset(); +// break; +// case sce::Toolkit::NP::Event::npSessionError: ///< An event generated when there was error performing the current %Np session process. +// app.DebugPrintf("npSessionError"); +// break; +// case sce::Toolkit::NP::Event::npSessionLeaveResult: ///< An event generated when the user has left the current %Np session. +// app.DebugPrintf("npSessionLeaveResult"); +// break; +// case sce::Toolkit::NP::Event::npSessionModified: ///< An event generated when the %Np session has been modified. +// app.DebugPrintf("npSessionModified"); +// break; +// case sce::Toolkit::NP::Event::npSessionUpdateResult: ///< An event generated when the %Np session has been updated. +// app.DebugPrintf("npSessionUpdateResult"); +// break; +// case sce::Toolkit::NP::Event::npSessionGetInfoResult: ///< An event generated when the %Np session info has been retrieved. +// app.DebugPrintf("npSessionGetInfoResult"); +// break; +// case sce::Toolkit::NP::Event::npSessionGetInfoListResult: ///< An event generated when the %Np session info has been retrieved. +// app.DebugPrintf("npSessionGetInfoListResult"); +// break; +// case sce::Toolkit::NP::Event::npSessionGetSessionDataResult: ///< An event generated when the %Np session data has been retrieved. +// app.DebugPrintf("npSessionGetSessionDataResult"); +// break; +// case sce::Toolkit::NP::Event::npSessionSearchResult: ///< An event generated when the %Np session search request has been completed. +// app.DebugPrintf("npSessionSearchResult"); +// break; +// case sce::Toolkit::NP::Event::npSessionInviteNotification: ///< An event generated when the %Np session push notification is received. +// app.DebugPrintf("npSessionInviteNotification"); +// break; +// case sce::Toolkit::NP::Event::npSessionInviteGetInfoResult: ///< An event generated when the %Np session info has been retrieved. +// app.DebugPrintf("npSessionInviteGetInfoResult"); +// break; +// case sce::Toolkit::NP::Event::npSessionInviteGetInfoListResult: ///< An event generated when the %Np session info has been retrieved. +// app.DebugPrintf("npSessionInviteGetInfoListResult"); +// break; +// case sce::Toolkit::NP::Event::npSessionInviteGetDataResult: ///< An event generated when the %Np session data has been retrieved. +// app.DebugPrintf("npSessionInviteGetDataResult"); +// break; +// default: +// assert(0); +// break; +// } +// +// } + +void PSVitaNPToolkit::gameCustomDataCallback( const sce::Toolkit::NP::Event& event) +{ +// switch(event.event) +// { +// +// case sce::Toolkit::NP::Event::gameCustomDataItemListResult: +// app.DebugPrintf("gameCustomDataItemListResult"); +// break; +// case sce::Toolkit::NP::Event::gameCustomDataGameDataResult: +// app.DebugPrintf("gameCustomDataGameDataResult"); +// if(m_messageData.hasResult()) +// { +// SQRNetworkManager_Orbis::GetInviteDataAndProcess(m_messageData.get()); +// } +// else +// { +// app.DebugPrintf("gameCustomDataMessageResult error 0x%08x\n", m_messageData.getError()); +// } +// break; +// case sce::Toolkit::NP::Event::gameCustomDataMessageResult: +// app.DebugPrintf("gameCustomDataMessageResult"); +// break; +// case sce::Toolkit::NP::Event::gameCustomDataSetUseFlagResult: +// app.DebugPrintf("gameCustomDataSetUseFlagResult"); +// break; +// case sce::Toolkit::NP::Event::gameCustomDataGameThumbnailResult: +// app.DebugPrintf("gameCustomDataGameThumbnailResult"); +// break; +// case sce::Toolkit::NP::Event::messageError: +// app.DebugPrintf("messageError : 0x%08x\n", event.returnCode); +// assert(0); +// break; +// default: +// assert(0); +// break; +// } +} + +static uint8_t hexCharToUint(char ch) +{ + uint8_t val = 0; + + if ( isdigit(ch) ){ + val = (ch - '0'); + } + else if ( isupper(ch) ){ + val = (ch - 'A' + 10); + } + else{ + val = (ch - 'a' + 10); + } + + return val; +} + +void hexStrToBin( + const char *pHexStr, + uint8_t *pBinBuf, + size_t binBufSize + ) +{ + uint8_t val = 0; + int hexStrLen = strlen(pHexStr); + + int binOffset = 0; + for (int i = 0; i < hexStrLen; i++) { + val |= hexCharToUint(*(pHexStr + i)); + if (i % 2 == 0) { + val <<= 4; + } + else { + if (pBinBuf != NULL && binOffset < binBufSize) { + memcpy(pBinBuf + binOffset, &val, 1); + val = 0; + } + binOffset++; + } + } + + if (val != 0 && pBinBuf != NULL && binOffset < binBufSize) { + memcpy(pBinBuf + binOffset, &val, 1); + } + + return; +} + + +static void npStateCallback(SceNpServiceState state, int retCode, void *userdata) +{ + //CD - Updates the online status of player + switch(state) + { + case SCE_NP_SERVICE_STATE_SIGNED_OUT: + ProfileManager.SetNetworkStatus(false, false); + break; + case SCE_NP_SERVICE_STATE_SIGNED_IN: + ProfileManager.SetNetworkStatus(false, true); + break; + case SCE_NP_SERVICE_STATE_ONLINE: + ProfileManager.SetNetworkStatus(true, true); + break; + default: + break; + } +} + +void PSVitaNPToolkit::init() +{ +// MenuApp menuApp; +// sce::Toolkit::NP::NpTitleId nptTitleId; +// nptTitleId.setTitleSecret(*SQRNetworkManager_Vita::GetSceNpTitleId(), *SQRNetworkManager_Vita::GetSceNpTitleSecret()); + sce::Toolkit::NP::CommunicationId commsIds(s_npCommunicationId, s_npCommunicationPassphrase, s_npCommunicationSignature); + sce::Toolkit::NP::Parameters params(sceNpToolkitCallback,commsIds); + params.m_title.setId(app.GetCommerceCategory()); + + + int ret = sce::Toolkit::NP::Interface::init(params); + if (ret != SCE_OK) + { + app.DebugPrintf("Failed to initialize NP Toolkit Library : 0x%x\n", ret); + assert(0); + } + + + ret = sce::Toolkit::NP::Interface::registerNpCommsId(commsIds, sce::Toolkit::NP::matching); + if (ret < 0) + { + app.DebugPrintf("Failed to register TSS Comms ID : 0x%x\n", ret); + assert(0); + } + +// extern void npStateCallback(SceNpServiceState state, int retCode, void *userdata); + + ret = sceNpRegisterServiceStateCallback(npStateCallback, NULL); + if (ret < 0) + { + app.DebugPrintf("sceNpRegisterServiceStateCallback() failed. ret = 0x%x\n", ret); + } + + + + +// // Register Client ID for Auth +// ret = sce::Toolkit::NP::Interface::registerClientId(CLIENT_ID_FOR_SAMPLE); +// if (ret < 0) +// { +// app.DebugPrintf("Failed to register Auth Client ID : 0x%x\n", ret); +// assert(0); +// } + +} + + + +// void PSVitaNPToolkit::createNPSession() +// { +// #define CURRENT_SESSION_ATTR_NUMS 5 +// #define SESSION_IMAGE_PATH "/app0/orbis/session_image.png" +// #define SESSION_STATUS "Minecraft online game (this text needs defined and localised)" +// #define SESSION_NAME "Minecraft(this text needs defined and localised)" +// +// static const int maxSlots = 8; +// +// SceUserServiceUserId userId = SCE_USER_SERVICE_USER_ID_INVALID; +// int ret = sceUserServiceGetInitialUser(&userId); +// if( ret < 0 ) +// { +// app.DebugPrintf("Couldn't retrieve user ID 0x%x ...\n",ret); +// } +// +// sce::Toolkit::NP::CreateNpSessionRequest createSessionRequest; +// memset(&createSessionRequest,0,sizeof(createSessionRequest)); +// strncpy(createSessionRequest.sessionName,SESSION_NAME,strlen(SESSION_NAME)); +// createSessionRequest.sessionTypeFlag = SCE_TOOLKIT_NP_CREATE_SESSION_TYPE_PUBLIC; +// createSessionRequest.maxSlots = maxSlots; +// strncpy(createSessionRequest.sessionImgPath,SESSION_IMAGE_PATH,strlen(SESSION_IMAGE_PATH)); +// strncpy(createSessionRequest.sessionStatus,SESSION_STATUS,strlen(SESSION_STATUS)); +// createSessionRequest.userInfo.userId = userId; +// char test[3] = {'R','K','B'}; +// createSessionRequest.sessionData= test; +// createSessionRequest.sessionDataSize = 3; +// ret = sce::Toolkit::NP::Sessions::Interface::create(&createSessionRequest,&sm_createJoinFuture); +// } +// +// +// void PSVitaNPToolkit::joinNPSession() +// { +// SceUserServiceUserId userId = SCE_USER_SERVICE_USER_ID_INVALID; +// int ret = sceUserServiceGetInitialUser(&userId); +// if( ret < 0 ) +// { +// app.DebugPrintf("Couldn't retrieve user ID 0x%x ...\n",ret); +// } +// +// sce::Toolkit::NP::JoinNpSessionRequest joinSessionRequest; +// memset(&joinSessionRequest,0,sizeof(joinSessionRequest)); +// // still to sort this out +// ORBIS_STUBBED; +// } +// +// void PSVitaNPToolkit::leaveNPSession() +// { +// +// } +// + + +void PSVitaNPToolkit::getMessageData(SceAppUtilAppEventParam* paramData) +{ + + if (SCE_APPUTIL_APPEVENT_TYPE_NP_INVITE_MESSAGE == paramData->type) + { + sce::Toolkit::NP::Messaging::Interface::retrieveMessageAttachment(paramData,&m_messageData); + } + else if (SCE_APPUTIL_APPEVENT_TYPE_NP_APP_DATA_MESSAGE == paramData->type) + { + sce::Toolkit::NP::Messaging::Interface::retrieveMessageAttachment(paramData,&m_messageData); + } + else if (SCE_APPUTIL_APPEVENT_TYPE_NP_BASIC_JOINABLE_PRESENCE == paramData->type) + { + SceAppUtilNpBasicJoinablePresenceParam joinParam = {0}; + int ret = sceAppUtilAppEventParseNpBasicJoinablePresence(paramData, &joinParam); + if (ret < 0) + { + app.DebugPrintf("sceAppUtilAppEventParseNpBasicJoinablePresence() failed: 0x%x\n", ret); + } + else + { + SQRNetworkManager_Vita::GetJoinablePresenceDataAndProcess(&joinParam); + } + + } + else + { + assert(0); + } + +} \ No newline at end of file diff --git a/Minecraft.Client/PSVita/Network/PSVita_NPToolkit.h b/Minecraft.Client/PSVita/Network/PSVita_NPToolkit.h new file mode 100644 index 00000000..064d837a --- /dev/null +++ b/Minecraft.Client/PSVita/Network/PSVita_NPToolkit.h @@ -0,0 +1,32 @@ +#pragma once + +#include +// #include + + +class PSVitaNPToolkit +{ +public: + static void init(); + static void sceNpToolkitCallback( const sce::Toolkit::NP::Event& event); + static void coreCallback( const sce::Toolkit::NP::Event& event); + static void presenceCallback( const sce::Toolkit::NP::Event& event); + static void profileCallback( const sce::Toolkit::NP::Event& event); + static void messagingCallback( const sce::Toolkit::NP::Event& event); + static void sessionsCallback( const sce::Toolkit::NP::Event& event); + static void gameCustomDataCallback( const sce::Toolkit::NP::Event& event); + +// static void createNPSession(); +// static void destroyNPSession(); +// static void joinNPSession(); +// static void leaveNPSession(); +// static SceNpSessionId* getNPSessionID() { return &m_currentSessionInfo.npSessionId; } + + static void getMessageData(SceAppUtilAppEventParam* paramData); +private: +// static sce::Toolkit::NP::Utilities::Future sm_createJoinFuture; +// static sce::Toolkit::NP::NpSessionInformation m_currentSessionInfo; + static sce::Toolkit::NP::Utilities::Future m_messageData; + +}; + diff --git a/Minecraft.Client/PSVita/Network/SQRNetworkManager_AdHoc_Vita.cpp b/Minecraft.Client/PSVita/Network/SQRNetworkManager_AdHoc_Vita.cpp new file mode 100644 index 00000000..2b2ccec8 --- /dev/null +++ b/Minecraft.Client/PSVita/Network/SQRNetworkManager_AdHoc_Vita.cpp @@ -0,0 +1,3119 @@ +#include "stdafx.h" +#include "SQRNetworkManager_AdHoc_Vita.h" +#include "SonyVoiceChat_Vita.h" +#include "Common/Network/Sony/PlatformNetworkManagerSony.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "PSVita\PSVitaExtras\Conf.h" +#include "Common\Network\Sony\SonyHttp.h" +#include "..\..\..\Minecraft.World\C4JThread.h" + + +#define MATCHING_PORT (1) +#define MATCHING_RXBUFLEN (2048) +#define HELLO_INTERVAL (1 * 1000 * 1000) + +#define KEEPALIVE_INTERVAL (1000 * 1000) +#define INIT_COUNT (10) // seconds before timeout +#define REXMT_INTERVAL (1000 * 1000) +#define MATCHING_EVENT_HANDLER_STACK_SIZE ((4 * 1024) + SCE_NET_ADHOC_MATCHING_POOLSIZE_DEFAULT) + +#define ADHOC_VPORT 4649 +class HelloSyncInfo +{ +public: + SQRNetworkManager::PresenceSyncInfo m_presenceSyncInfo; + GameSessionData m_gameSessionData; +}; + + +static const bool sc_voiceChatEnabled = false; // don't think we'll need voice chat in ad-hoc, will need some work to get the mesh connections if we do. + + +bool g_bNetworkAdHocMode = false; + +int SQRNetworkManager_AdHoc_Vita::m_adhocStatus = false; + + +static unsigned char s_Matching2Pool[SCE_NET_ADHOC_MATCHING_POOLSIZE_DEFAULT]; + +int (* SQRNetworkManager_AdHoc_Vita::s_SignInCompleteCallbackFn)(void *pParam, bool bContinue, int pad) = NULL; +void * SQRNetworkManager_AdHoc_Vita::s_SignInCompleteParam = NULL; +sce::Toolkit::NP::PresenceDetails SQRNetworkManager_AdHoc_Vita::s_lastPresenceInfo; +int SQRNetworkManager_AdHoc_Vita::s_resendPresenceCountdown = 0; +bool SQRNetworkManager_AdHoc_Vita::s_presenceStatusDirty = false; +bool SQRNetworkManager_AdHoc_Vita::s_presenceDataDirty = false; +bool SQRNetworkManager_AdHoc_Vita::s_signInCompleteCallbackIfFailed = false; +HelloSyncInfo SQRNetworkManager_AdHoc_Vita::s_lastPresenceSyncInfo = { 0 }; +HelloSyncInfo SQRNetworkManager_AdHoc_Vita::c_presenceSyncInfoNULL = { 0 }; +//SceNpBasicAttachmentDataId SQRNetworkManager_AdHoc_Vita::s_lastInviteIdToRetry = SCE_NP_BASIC_INVALID_ATTACHMENT_DATA_ID; +long long SQRNetworkManager_AdHoc_Vita::s_roomStartTime = 0; +bool SQRNetworkManager_AdHoc_Vita::b_inviteRecvGUIRunning = false; +// HelloSyncInfo* SQRNetworkManager_AdHoc_Vita::m_gameBootInvite; +// HelloSyncInfo SQRNetworkManager_AdHoc_Vita::m_gameBootInvite_data; + +// static const int sc_UserEventHandle = 0; + +//unsigned int SQRNetworkManager_AdHoc_Vita::RoomSyncData::playerCount = 0; + +SQRNetworkManager_AdHoc_Vita* s_pAdhocVitaManager;// have to use a static var for this as the callback function doesn't take an arg + +// This maps internal to extern states, and needs to match element-by-element the eSQRNetworkManagerInternalState enumerated type +const SQRNetworkManager_AdHoc_Vita::eSQRNetworkManagerState SQRNetworkManager_AdHoc_Vita::m_INTtoEXTStateMappings[SQRNetworkManager_AdHoc_Vita::SNM_INT_STATE_COUNT] = +{ + SNM_STATE_INITIALISING, // SNM_INT_STATE_UNINITIALISED + SNM_STATE_INITIALISING, // SNM_INT_STATE_SIGNING_IN + SNM_STATE_INITIALISING, // SNM_INT_STATE_STARTING_CONTEXT + SNM_STATE_INITIALISE_FAILED, // SNM_INT_STATE_INITIALISE_FAILED + SNM_STATE_IDLE, // SNM_INT_STATE_IDLE + SNM_STATE_IDLE, // SNM_INT_STATE_IDLE_RECREATING_MATCHING_CONTEXT + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_STARTING_MATCHING_CONTEXT + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_SEARCHING_FOR_SERVER + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_SERVER_SEARCH_SERVER_ERROR + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_SERVER_FOUND + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_SERVER_SEARCH_CREATING_CONTEXT + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_SERVER_SEARCH_FAILED + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_CREATE_ROOM_SEARCHING_FOR_WORLD + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_CREATE_ROOM_WORLD_FOUND + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_CREATE_ROOM_CREATING_ROOM + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_CREATE_ROOM_SUCCESS + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_CREATE_ROOM_FAILED + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_CREATE_ROOM_RESTART_MATCHING_CONTEXT + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_WAITING_TO_PLAY + SNM_STATE_JOINING, // SNM_INT_STATE_JOINING_STARTING_MATCHING_CONTEXT + SNM_STATE_JOINING, // SNM_INT_STATE_JOINING_SEARCHING_FOR_SERVER + SNM_STATE_JOINING, // SNM_INT_STATE_JOINING_SERVER_SEARCH_SERVER_ERROR + SNM_STATE_JOINING, // SNM_INT_STATE_JOINING_SERVER_FOUND + SNM_STATE_JOINING, // SNM_INT_STATE_JOINING_SERVER_SEARCH_CREATING_CONTEXT + SNM_STATE_JOINING, // SNM_INT_STATE_JOINING_SERVER_SEARCH_FAILED + SNM_STATE_JOINING, // SNM_INT_STATE_JOINING_JOIN_ROOM + SNM_STATE_JOINING, // SNM_INT_STATE_JOINING_JOIN_ROOM_FAILED + SNM_STATE_JOINING, // SNM_INT_STATE_JOINING_WAITING_FOR_LOCAL_PLAYERS + SNM_STATE_ENDING, // SNM_INT_STATE_SERVER_DELETING_CONTEXT + SNM_STATE_STARTING, // SNM_INT_STATE_STARTING + SNM_STATE_PLAYING, // SNM_INT_STATE_PLAYING + SNM_STATE_LEAVING, // SNM_INT_STATE_LEAVING + SNM_STATE_LEAVING, // SNM_INT_STATE_LEAVING_FAILED + SNM_STATE_ENDING, // SNM_INT_STATE_ENDING +}; + +SQRNetworkManager_AdHoc_Vita::SQRNetworkManager_AdHoc_Vita(ISQRNetworkManagerListener *listener) +{ + m_state = SNM_INT_STATE_UNINITIALISED; + m_stateExternal = SNM_STATE_INITIALISING; + m_nextIdleReasonIsFull = false; + m_friendSearchState = SNM_FRIEND_SEARCH_STATE_IDLE; + m_serverContextValid = false; + m_isHosting = false; + m_currentSmallId = 0; + memset( m_aRoomSlotPlayers, 0, sizeof(m_aRoomSlotPlayers) ); + m_listener = listener; + m_resendExternalRoomDataCountdown = 0; + m_matching2initialised = false; + m_matchingContextClientValid = false; + m_matchingContextServerValid = false; + m_soc = -1; +// m_inviteIndex = 0; +// m_doBootInviteCheck = true; + m_isInSession = false; + m_offlineGame = false; + m_offlineSQR = false; + m_aServerId = NULL; +// m_gameBootInvite = NULL; + m_adhocStatus = false; + m_bLinkDisconnected = false; + m_bIsInitialised=false; + + InitializeCriticalSection(&m_csRoomSyncData); + InitializeCriticalSection(&m_csPlayerState); + InitializeCriticalSection(&m_csStateChangeQueue); + + memset( &m_roomSyncData,0,sizeof(m_roomSyncData)); // MGH - added to fix problem when joining a full room, and the sync data wasn't populated + +// int ret = sceKernelCreateEqueue(&m_basicEventQueue, "SQRNetworkManager_AdHoc_Vita EQ"); +// assert(ret == SCE_OK); +// ret = sceKernelAddUserEvent(m_basicEventQueue, sc_UserEventHandle); +// assert(ret == SCE_OK); +// +// m_basicEventThread = new C4JThread(&BasicEventThreadProc,this,"Basic Event Handler"); +// m_basicEventThread->Run(); +} + +static std::string getIPAddressString(SceNetInAddr add) +{ + char str[32]; + unsigned char *vals = (unsigned char*)&add.s_addr; + sprintf(str, "%d.%d.%d.%d", (int)vals[0], (int)vals[1], (int)vals[2], (int)vals[3]); + return std::string(str); +} + + +// First stage of initialisation. This initialises a few things that don't require the user to be signed in, and then kicks of the network start dialog utility. +// Initialisation continues in InitialiseAfterOnline once this completes. +void SQRNetworkManager_AdHoc_Vita::Initialise() +{ +#define NP_IN_GAME_MESSAGE_POOL_SIZE ( 16 * 1024 ) + + int32_t ret = 0; +// int32_t libCtxId = 0; +// ret = sceNpInGameMessageInitialize(NP_IN_GAME_MESSAGE_POOL_SIZE, NULL); +// assert (ret >= 0); +// libCtxId = ret; + + assert( m_state == SNM_INT_STATE_UNINITIALISED ); + + + //Initialize libnetctl +// ret = sceNetCtlInit(); +// if( ( ret < 0 && ret != SCE_NET_CTL_ERROR_NOT_TERMINATED ) || ForceErrorPoint( SNM_FORCE_ERROR_NET_CTL_INIT ) ) +// { +// SetState(SNM_INT_STATE_INITIALISE_FAILED); +// return; +// } + m_hid=0; + ret = sceNetCtlAdhocRegisterCallback(NetCtlCallback,this,&m_hid); + assert(ret == SCE_OK); + + // Initialise RUDP + const int RUDP_POOL_SIZE = (500 * 1024); // TODO - find out what we need, this size is copied from library reference + uint8_t *rudp_pool = (uint8_t *)malloc(RUDP_POOL_SIZE); + ret = sceRudpInit(rudp_pool, RUDP_POOL_SIZE); + if( ( ret < 0 ) || ForceErrorPoint( SNM_FORCE_ERROR_RUDP_INIT ) ) + { + SetState(SNM_INT_STATE_INITIALISE_FAILED); + return; + } + + SetState(SNM_INT_STATE_SIGNING_IN); + + +// SonyHttp::init(); + +// SceNpCommunicationConfig npConf ; +// npConf.commId = &s_npCommunicationId; +// npConf.commPassphrase = &s_npCommunicationPassphrase; +// npConf.commSignature = &s_npCommunicationSignature; +// ret = sceNpInit(&npConf, NULL); +// if (ret < 0 && ret != SCE_NP_ERROR_ALREADY_INITIALIZED) +// { +// app.DebugPrintf("sceNpInit failed, ret=%x\n", ret); +// assert(0); +// } +// + ret = sceRudpEnableInternalIOThread(RUDP_THREAD_STACK_SIZE, SCE_KERNEL_DEFAULT_PRIORITY); + if(ret < 0) + { + app.DebugPrintf("sceRudpEnableInternalIOThread failed with error code 0x%08x\n", ret); + assert(0); + } + + /* initialize pspnet adhoc */ + ret = sceNetAdhocInit(); + if(ret < 0) + { + app.DebugPrintf("sceNetAdhocInit() failed. ret = 0x%x\n", ret); + //sceNetCtlTerm(); + //sceNetTerm(); + assert(0); + return; + } + + /* initialize pspnet adhoc ctrl */ + SceNetAdhocctlAdhocId adhocId; + memset(&adhocId, 0x00, sizeof(SceNetAdhocctlAdhocId)); + adhocId.type = SCE_NET_ADHOCCTL_ADHOCTYPE_RESERVED; + memcpy(&adhocId.data[0], s_npCommunicationId.data, SCE_NET_ADHOCCTL_ADHOCID_LEN); + ret = sceNetAdhocctlInit(&adhocId); + if(ret < 0) + { + app.DebugPrintf("sceNetAdhocctlInit() failed. ret = 0x%x\n", ret); + assert(0); + } + + OnlineCheck(); + // Already online? the callback won't catch this, so carry on initialising now + if(GetAdhocStatus()) + { + InitialiseAfterOnline(); + } +// if(sc_voiceChatEnabled) +// { +// SonyVoiceChat_Vita::init(); +// } + + m_bIsInitialised=true; +} + +bool SQRNetworkManager_AdHoc_Vita::IsInitialised() +{ + return m_bIsInitialised; +} + +void SQRNetworkManager_AdHoc_Vita::UnInitialise() +{ + int ret; + StopMatchingContext(); + // These can fail if we've not initialised after online + ret = sceNetAdhocMatchingTerm(); + ret = sceRudpEnd(); + ret = sceNpMatching2Term(); + /////////////////////////////////////////////////////// + + ret = sceNetCtlAdhocUnregisterCallback(m_hid); + + //sceNetCtlTerm(); + + ret = sceNetAdhocTerm(); + +// ret = sceNetTerm(); + ret = sceNetAdhocctlTerm(); + + SetState(SNM_INT_STATE_UNINITIALISED); + + m_bIsInitialised=false; +} + +void SQRNetworkManager_AdHoc_Vita::Terminate() +{ + // If playing, attempt to nicely leave the room before shutting down so that our friends won't still think this game is in progress + if( ( m_state == SNM_INT_STATE_HOSTING_CREATE_ROOM_SUCCESS ) || + ( m_state == SNM_INT_STATE_HOSTING_WAITING_TO_PLAY ) || + ( m_state == SNM_INT_STATE_JOINING_WAITING_FOR_LOCAL_PLAYERS ) || + ( m_state == SNM_INT_STATE_PLAYING ) ) + { + if( !m_offlineGame ) + { + LeaveRoom(true); + int count = 200; + do + { + Tick(); + Sleep(10); + count--; + } while( ( count > 0 ) && ( m_state != SNM_INT_STATE_IDLE ) ); + app.DebugPrintf(CMinecraftApp::USER_RR,"Attempted to leave room, %dms used\n",count * 10); + } + } + + int ret = sceRudpEnd(); + ret = sceNpMatching2Term(); + // Terminate event thread by sending it a non-zero value for data +// sceKernelTriggerUserEvent(m_basicEventQueue, sc_UserEventHandle, (void*)1); + + +// do +// { +// Sleep(10); +// } while( m_basicEventThread->isRunning() ); +} + + +SceNpMatching2RoomMemberId getRoomMemberID(SceNetInAddr* addr) +{ + + // adhoc IP address are of the format 169.254.***.*** + // the last 2 digits of the IP address should be unique, so we're using them as a room member ID value + return addr->s_addr >> 16; +} + +void SQRNetworkManager_AdHoc_Vita::StopMatchingContext() +{ + if(m_matchingContextServerValid || m_matchingContextClientValid) + { + int err = sceNetAdhocMatchingStop(m_matchingContext); + assert(err == SCE_OK); + err = sceNetAdhocMatchingDelete(m_matchingContext); + assert(err == SCE_OK); + m_matchingContextServerValid = false; + m_matchingContextClientValid = false; + m_matchingContext = -1; + } +} + + +bool SQRNetworkManager_AdHoc_Vita::CreateMatchingContext(bool bServer /*= false*/) +{ + int matchingMode; + if(bServer) + { + if(m_matchingContextServerValid) + { + SetState(SNM_INT_STATE_IDLE); + return true; + } + matchingMode = SCE_NET_ADHOC_MATCHING_MODE_PARENT; + } + else + { + if(m_matchingContextClientValid) + { + SetState(SNM_INT_STATE_IDLE); + return true; + } + matchingMode = SCE_NET_ADHOC_MATCHING_MODE_CHILD; + } + StopMatchingContext(); + + int ret = sceNetAdhocMatchingCreate(matchingMode, // create this as a client (child) context at first so we can search for other servers (parents) + MAX_ONLINE_PLAYER_COUNT, MATCHING_PORT, MATCHING_RXBUFLEN, + HELLO_INTERVAL, KEEPALIVE_INTERVAL, INIT_COUNT, + REXMT_INTERVAL, MatchingEventHandler); + + s_pAdhocVitaManager = this; + + if( ( ret < 0 ) || ForceErrorPoint( SNM_FORCE_ERROR_CREATE_MATCHING_CONTEXT ) ) + { + SetState(SNM_INT_STATE_INITIALISE_FAILED); + return false; + } + + bool bRet = RegisterCallbacks(); + if( ( !bRet ) || ForceErrorPoint( SNM_FORCE_ERROR_REGISTER_CALLBACKS ) ) + { + SetState(SNM_INT_STATE_INITIALISE_FAILED); + return false; + } + m_matchingContext = ret; + + + + // Free up any external data that we received from the previous search + for( int i = 0; i < m_aFriendSearchResults.size(); i++ ) + { + if(m_aFriendSearchResults[i].m_RoomExtDataReceived) + free(m_aFriendSearchResults[i].m_RoomExtDataReceived); + m_aFriendSearchResults[i].m_RoomExtDataReceived = NULL; + if(m_aFriendSearchResults[i].m_gameSessionData) + free(m_aFriendSearchResults[i].m_gameSessionData); + m_aFriendSearchResults[i].m_gameSessionData = NULL; + } + m_friendCount = 0; + m_aFriendSearchResults.clear(); + + + // Start the context + // Set time-out time to 10 seconds + ret = sceNetAdhocMatchingStart(m_matchingContext, + SCE_KERNEL_DEFAULT_PRIORITY_USER, MATCHING_EVENT_HANDLER_STACK_SIZE, + SCE_KERNEL_THREAD_CPU_AFFINITY_MASK_DEFAULT, + 0, NULL);//sizeof(g_myInfo.name), &g_myInfo.name); + + if( ( ret < 0 ) || ForceErrorPoint( SNM_FORCE_ERROR_CONTEXT_START_ASYNC ) ) + { + SetState(SNM_INT_STATE_INITIALISE_FAILED); + return false; + } + + + + if(bServer) + m_matchingContextServerValid = true; + else + m_matchingContextClientValid = true; + + HandleMatchingContextStart(); + + return true; + + +} + + + +// Second stage of initialisation, that requires NP Manager to be online & the player to be signed in. This kicks of the creation of a context +// for Np Matching 2. Initialisation is finally complete when we get a callback to ContextCallback. The SQRNetworkManager_AdHoc_Vita is then finally moved +// into SNM_INT_STATE_IDLE at this stage. +void SQRNetworkManager_AdHoc_Vita::InitialiseAfterOnline() +{ +// SceNpId npId; +// int option = 0; + + // We should only be doing this if we have come in from an initialisation stage (SQRNetworkManager_AdHoc_Vita::Initialise) or we've had a network disconnect and are coming in from an offline state. + // Don't do anything otherwise - this mainly to catch a bit of a corner case in the initialisation phase where potentially we could register for the callback that would call this with sceNpManagerRegisterCallback. + // and could then really quickly go online so that the there becomes two paths (via the callback or SQRNetworkManager_AdHoc_Vita::Initialise) by which this could be called + if( ( m_state != SNM_INT_STATE_SIGNING_IN ) && !(( m_state == SNM_INT_STATE_IDLE ) && m_offlineSQR) ) + { + // If we aren't going to continue on with this sign-in but are expecting a callback, then let the game know that we have completed the bit we are expecting to do. This + // will happen whilst in game, when we want to be able to sign into PSN, but don't expect the full matching stuff to get set up. + if( s_SignInCompleteCallbackFn ) + { + s_SignInCompleteCallbackFn(s_SignInCompleteParam,true,0); + s_SignInCompleteCallbackFn = NULL; + } + return; + } + + // Initialize matching2 with default settings + int ret = sceNetAdhocMatchingInit(SCE_NET_ADHOC_MATCHING_POOLSIZE_DEFAULT, s_Matching2Pool); + + if( ( ret < 0 ) || ForceErrorPoint( SNM_FORCE_ERROR_MATCHING2_INIT ) ) + { + SetState(SNM_INT_STATE_INITIALISE_FAILED); + return; + } + app.DebugPrintf("SQRNetworkManager::InitialiseAfterOnline - matching context is now valid\n"); + m_matching2initialised = true; + + bool bRet = RegisterCallbacks(); + if( ( !bRet ) || ForceErrorPoint( SNM_FORCE_ERROR_REGISTER_CALLBACKS ) ) + { + SetState(SNM_INT_STATE_INITIALISE_FAILED); + return; + } + + // State should be starting context until the callback that this has been created happens + SetState(SNM_INT_STATE_STARTING_CONTEXT); + if(CreateMatchingContext()) + { + return; + } + + SetState(SNM_INT_STATE_IDLE); +} + + +// General tick function to be called from main game loop - any internal tick functions should be called from here. +void SQRNetworkManager_AdHoc_Vita::Tick() +{ + OnlineCheck(); + int ret; + if((ret = sceNetCtlCheckCallback()) < 0 ) + { + app.DebugPrintf("sceNetCtlCheckCallback error[%d]",ret); + } + updateNetCheckDialog(); //CD - Added this to match the SQRNetworkManager_Vita class + RoomCreateTick(); + FriendSearchTick(); + TickRichPresence(); +// TickInviteGUI(); // TODO + +// if( ( m_gameBootInvite m) && ( s_safeToRespondToGameBootInvite ) ) +// { +// m_listener->HandleInviteReceived( ProfileManager.GetPrimaryPad(), m_gameBootInvite ); +// m_gameBootInvite = NULL; +// } + + ErrorHandlingTick(); + // If we ever fail to send the external room data, we start a countdown so that we attempt to resend. Not sure how likely it is that updating this will fail without the whole network being broken, + // but if in particular we don't update the flag to say that the session is joinable, then nobody is ever going to see this session. + if( m_resendExternalRoomDataCountdown ) + { + if( m_state == SNM_INT_STATE_PLAYING ) + { + m_resendExternalRoomDataCountdown--; + if( m_resendExternalRoomDataCountdown == 0 ) + { + UpdateExternalRoomData(); + } + } + else + { + m_resendExternalRoomDataCountdown = 0; + } + } + + // ProfileManager.SetNetworkStatus(GetOnlineStatus()); + + // Client only - do the final transition to a starting & playing state once we have fully joined the room, And told the game about all the local players so they are also all valid + if( m_state == SNM_INT_STATE_JOINING_WAITING_FOR_LOCAL_PLAYERS ) + { + if( m_localPlayerJoined == m_localPlayerCount ) + { + // Since we're now fully joined, we can update our presence info so that our friends could find us in this game. This data was set up + // at the point that we joined the game (either from search info, or an invitation). + UpdateRichPresenceCustomData(&s_lastPresenceSyncInfo, sizeof(HelloSyncInfo)); + SetState( SNM_INT_STATE_STARTING); + SetState( SNM_INT_STATE_PLAYING ); + } + } + + if( m_state == SNM_INT_STATE_SERVER_DELETING_CONTEXT ) + { + // make sure we've removed all the remote players and killed the udp connections before we bail out +// if(m_RudpCtxToPlayerMap.size() == 0) + ResetToIdle(); + } + + EnterCriticalSection(&m_csStateChangeQueue); + while(m_stateChangeQueue.size() > 0 ) + { + if( m_listener ) + { + m_listener->HandleStateChange(m_stateChangeQueue.front().m_oldState, m_stateChangeQueue.front().m_newState, m_stateChangeQueue.front().m_idleReasonIsSessionFull); + if( m_stateChangeQueue.front().m_newState == SNM_STATE_IDLE ) + { + m_isInSession = false; + } + } + m_stateExternal = m_stateChangeQueue.front().m_newState; + m_stateChangeQueue.pop(); + } + LeaveCriticalSection(&m_csStateChangeQueue); +} + +// Detect any states which reflect internal error states, do anything required, and transition away again +void SQRNetworkManager_AdHoc_Vita::ErrorHandlingTick() +{ + switch( m_state ) + { + case SNM_INT_STATE_INITIALISE_FAILED: + if( s_SignInCompleteCallbackFn ) + { + if( s_signInCompleteCallbackIfFailed ) + { + s_SignInCompleteCallbackFn(s_SignInCompleteParam,false,0); + } + s_SignInCompleteCallbackFn = NULL; + } + app.DebugPrintf("Network error: SNM_INT_STATE_INITIALISE_FAILED\n"); + if( m_isInSession && m_offlineSQR ) + { + // This is a fix for an issue where a player attempts (and fails) to sign in, whilst in an offline game. This was setting the state to idle, which in turn + // sets the game to Not be in a session anymore (but the game wasn't generally aware of, and so keeps playing). Howoever, the game's connections use + // their tick to determine whether to empty their queues or not and so no communications (even though they don't actually use this network manager for local connections) + // were happening. + SetState(SNM_INT_STATE_PLAYING); + } + else + { + m_offlineSQR = true; + SetState(SNM_INT_STATE_IDLE); + } + break; + case SNM_INT_STATE_HOSTING_SERVER_SEARCH_FAILED: + app.DebugPrintf("Network error: SNM_INT_STATE_HOSTING_SERVER_SEARCH_FAILED\n"); + ResetToIdle(); + break; + case SNM_INT_STATE_HOSTING_CREATE_ROOM_FAILED: + app.DebugPrintf("Network error: SNM_INT_STATE_HOSTING_CREATE_ROOM_FAILED\n"); + DeleteServerContext(); + break; + case SNM_INT_STATE_JOINING_SERVER_SEARCH_FAILED: + app.DebugPrintf("Network error: SNM_INT_STATE_JOINING_SERVER_SEARCH_FAILED\n"); + ResetToIdle(); + break; + case SNM_INT_STATE_JOINING_JOIN_ROOM_FAILED: + app.DebugPrintf("Network error: SNM_INT_STATE_JOINING_JOIN_ROOM_FAILED\n"); + DeleteServerContext(); + break; + case SNM_INT_STATE_LEAVING_FAILED: + app.DebugPrintf("Network error: SNM_INT_STATE_LEAVING_FAILED\n"); + DeleteServerContext(); + break; + } + +} + +// Start hosting a game, by creating a room & joining it. We explicity create a server context here (via GetServerContext) as Sony suggest that +// this means we have greater control of representing when players are actually "online". The creation of the room is carried out in a callback +// after that server context is made (ServerContextValidCallback_CreateRoom). +// hostIndex is the index of the user that is hosting the session, and localPlayerMask has bit 0 - 3 set to indicate the full set of local players joining the game. +// extData and extDataSize define the initial state of room data that is externally visible (eg by players searching for rooms, but not in it) +void SQRNetworkManager_AdHoc_Vita::CreateAndJoinRoom(int hostIndex, int localPlayerMask, void *extData, int extDataSize, bool offline) +{ + // hostIndex should always be in the mask + assert( ( ( 1 << hostIndex ) & localPlayerMask ) != 0 ); + + m_isHosting = true; + m_joinExtData = extData; + m_joinExtDataSize = extDataSize; + m_offlineGame = offline; + m_resendExternalRoomDataCountdown = 0; + m_isInSession= true; + + // Default value for room, which we can use for offlinae games + m_room = 0; + + // Initialise room data that will be synchronised. Slot 0 is always reserved for the host. We don't know the + // room member until the room is actually created so this will be set/updated at that point + memset( &m_roomSyncData, 0, sizeof(m_roomSyncData) ); + m_roomSyncData.setPlayerCount(1); + m_roomSyncData.players[0].m_smallId = m_currentSmallId++; + m_roomSyncData.players[0].m_localIdx = hostIndex; + + // Remove the host player that we've already added, then add any other local players specified in the mask + localPlayerMask &= ~( ( 1 << hostIndex ) & localPlayerMask ); + for( int i = 0; i < MAX_LOCAL_PLAYER_COUNT; i++ ) + { + if( localPlayerMask & ( 1 << i ) ) + { + m_roomSyncData.players[m_roomSyncData.getPlayerCount()].m_smallId = m_currentSmallId++; + m_roomSyncData.players[m_roomSyncData.getPlayerCount()].m_localIdx = i; + m_roomSyncData.setPlayerCount(m_roomSyncData.getPlayerCount()+1); + } + } + m_localPlayerCount = m_roomSyncData.getPlayerCount(); + + // For offline games, we can jump straight to the state that says we've just created the room (or would have, for an online game) + if( m_offlineGame ) + { + SetState(SNM_INT_STATE_HOSTING_CREATE_ROOM_SUCCESS); + } + else + { + SetState(SNM_INT_STATE_HOSTING_STARTING_MATCHING_CONTEXT); + // Kick off the sequence of events required for an online game, starting with getting the server context + if(CreateMatchingContext(true)) + { + m_isInSession = true; + SetState(SNM_INT_STATE_HOSTING_CREATE_ROOM_SUCCESS); + } + } +} + +// Updates the externally visible data that was associated with the room when it was created with CreateAndJoinRoom. +void SQRNetworkManager_AdHoc_Vita::UpdateExternalRoomData() +{ + // Update the hello message here + if( m_isHosting ) + { + HelloSyncInfo presenceInfo; + CPlatformNetworkManagerSony::SetSQRPresenceInfoFromExtData( &presenceInfo.m_presenceSyncInfo, m_joinExtData, m_room, m_serverId ); + assert(m_joinExtDataSize == sizeof(GameSessionData)); + memcpy(&presenceInfo.m_gameSessionData, m_joinExtData, sizeof(GameSessionData)); + SQRNetworkManager_AdHoc_Vita::UpdateRichPresenceCustomData(&presenceInfo, sizeof(HelloSyncInfo) ); + // OrbisNPToolkit::createNPSession(); + } +} + +// Determine if the friend room manager is busy. If it isn't busy, then other operations (searching for a friend, reading the found friend's room lists) may safely be performed +bool SQRNetworkManager_AdHoc_Vita::FriendRoomManagerIsBusy() +{ + return (m_friendSearchState != SNM_FRIEND_SEARCH_STATE_IDLE); +} + +// Initiate a search for rooms that the signed in user's friends are in. This is an asynchronous operation, this function returns after it kicks off a search across all game servers +// for any of the player's friends. +bool SQRNetworkManager_AdHoc_Vita::FriendRoomManagerSearch() +{ + if( m_state != SNM_INT_STATE_IDLE ) return false; + + // Don't start another search if we're already searching... + if( m_friendSearchState != SNM_FRIEND_SEARCH_STATE_IDLE ) + { + return false; + } + + + m_friendSearchState = SNM_FRIEND_SEARCH_STATE_GETTING_FRIEND_COUNT; + + // Get friend list - doing this in another thread as it can lock up for a few seconds +// m_getFriendCountThread = new C4JThread(&GetFriendsThreadProc,this,"GetFriendsThreadProc"); +// m_getFriendCountThread->Run(); + + return true; +} + +bool SQRNetworkManager_AdHoc_Vita::FriendRoomManagerSearch2() +{ + m_friendSearchState = SNM_FRIEND_SEARCH_STATE_IDLE; +// if( m_friendCount == 0 ) +// { +// m_friendSearchState = SNM_FRIEND_SEARCH_STATE_IDLE; +// return false; +// } +// +// if( m_aFriendSearchResults.size() > 0 ) +// { +// // If we have some results, then we also want to make sure that we don't have any duplicate rooms here if more than one friend is playing in the same room. +// unordered_set uniqueRooms; +// for( unsigned int i = 0; i < m_aFriendSearchResults.size(); i++ ) +// { +// if(m_aFriendSearchResults[i].m_RoomFound) +// { +// uniqueRooms.insert( m_aFriendSearchResults[i].m_RoomId ); +// } +// } +// +// // Tidy the results up further based on this +// for( unsigned int i = 0; i < m_aFriendSearchResults.size(); ) +// { +// if( uniqueRooms.find(m_aFriendSearchResults[i].m_RoomId) == uniqueRooms.end() ) +// { +// free(m_aFriendSearchResults[i].m_RoomExtDataReceived); +// m_aFriendSearchResults[i] = m_aFriendSearchResults.back(); +// m_aFriendSearchResults.pop_back(); +// } +// else +// { +// uniqueRooms.erase(m_aFriendSearchResults[i].m_RoomId); +// i++; +// } +// } +// } +// m_friendSearchState = SNM_FRIEND_SEARCH_STATE_IDLE; + return true; +} + +void SQRNetworkManager_AdHoc_Vita::FriendSearchTick() +{ + // Move onto next state if we're done getting our friend count + if( m_friendSearchState == SNM_FRIEND_SEARCH_STATE_GETTING_FRIEND_COUNT ) + { +// if( !m_getFriendCountThread->isRunning() ) +// { +// m_friendSearchState = SNM_FRIEND_SEARCH_STATE_GETTING_FRIEND_INFO; +// delete m_getFriendCountThread; +// m_getFriendCountThread = NULL; + FriendRoomManagerSearch2(); +// } + } +} + +// The handler for basic events can't actually get the events themselves, this has to be done on another thread. Instead, we send a sys_event_t to a queue on This thread, +// which has a single data item used which we can use to determine whether to terminate this thread or get a basic event & handle that. +int SQRNetworkManager_AdHoc_Vita::BasicEventThreadProc( void *lpParameter ) +{ + PSVITA_STUBBED; + return 0; +// SQRNetworkManager_AdHoc_Vita *manager = (SQRNetworkManager_AdHoc_Vita *)lpParameter; +// +// int ret = SCE_OK; +// SceKernelEvent event; +// int outEv; +// +// do +// { +// ret = sceKernelWaitEqueue(manager->m_basicEventQueue, &event, 1, &outEv, NULL); +// +// // If the sys_event_t we've sent here from the handler has a non-zero data1 element, this is to signify that we should terminate the thread +// if( event.udata == 0 ) +// { +// // int iEvent; +// // SceNpUserInfo from; +// // uint8_t buffer[SCE_NP_BASIC_MAX_MESSAGE_SIZE]; +// // size_t bufferSize = SCE_NP_BASIC_MAX_MESSAGE_SIZE; +// // int ret = sceNpBasicGetEvent(&iEvent, &from, &buffer, &bufferSize); +// // if( ret == 0 ) +// // { +// // if( iEvent == SCE_NP_BASIC_EVENT_INCOMING_BOOTABLE_INVITATION ) +// // { +// // // 4J Stu - Don't do this here as it can be very disruptive to gameplay. Players can bring this up from LoadOrJoinMenu, PauseMenu and InGameInfoMenu +// // //sceNpBasicRecvMessageCustom(SCE_NP_BASIC_MESSAGE_MAIN_TYPE_INVITE, SCE_NP_BASIC_RECV_MESSAGE_OPTIONS_INCLUDE_BOOTABLE, SYS_MEMORY_CONTAINER_ID_INVALID); +// // } +// // if( iEvent == SCE_NP_BASIC_EVENT_RECV_INVITATION_RESULT ) +// // { +// // SceNpBasicExtendedAttachmentData *result = (SceNpBasicExtendedAttachmentData *)buffer; +// // if(result->userAction == SCE_NP_BASIC_MESSAGE_ACTION_ACCEPT ) +// // { +// // manager->GetInviteDataAndProcess(result->data.id); +// // } +// // } +// // app.DebugPrintf("Incoming basic event of type %d\n",iEvent); +// // } +// } +// +// } while(event.udata == 0 ); +// return 0; +} + +// int SQRNetworkManager_AdHoc_Vita::GetFriendsThreadProc( void* lpParameter ) +// { +// SQRNetworkManager_AdHoc_Vita *manager = (SQRNetworkManager_AdHoc_Vita *)lpParameter; +// +// int ret = 0; +// manager->m_aFriendSearchResults.clear(); +// manager->m_friendCount = 0; +// if(!ProfileManager.IsSignedInLive(ProfileManager.GetPrimaryPad())) +// { +// app.DebugPrintf("getFriendslist failed, not signed into Live! \n"); +// return 0; +// } +// +// +// ret = sceNpBasicGetFriendListEntryCount(&manager->m_friendCount); +// if( ( ret < 0 ) || manager->ForceErrorPoint( SNM_FORCE_ERROR_GET_FRIEND_LIST_ENTRY_COUNT ) ) +// { +// // This is likely when friend list hasn't been received from the server yet - will be returning SCE_NP_BASIC_ERROR_BUSY in this case +// manager->m_friendCount = 0; +// } +// +// +// // There shouldn't ever be more than 100 friends returned but limit here just in case +// if( manager->m_friendCount > 100 ) manager->m_friendCount = 100; +// +// SceNpId* friendIDs = NULL; +// if(manager->m_friendCount > 0) +// { +// // grab all the friend IDs first +// friendIDs = new SceNpId[manager->m_friendCount]; +// SceSize numRecieved; +// ret = sceNpBasicGetFriendListEntries(0, friendIDs, manager->m_friendCount, &numRecieved); +// if (ret < 0) +// { +// app.DebugPrintf("sceNpBasicGetFriendListEntries() failed: ret = 0x%x\n", ret); +// manager->m_friendCount = 0; +// } +// else +// { +// assert(numRecieved == manager->m_friendCount); +// } +// } +// +// +// // It is possible that the size of the friend list might vary from what we just received, so only add in friends that we successfully get an entry for +// for( unsigned int i = 0; i < manager->m_friendCount; i++ ) +// { +// static SceNpBasicGamePresence presenceDetails; +// static SceNpBasicFriendContextState contextState; +// int ret = sceNpBasicGetFriendContextState(&friendIDs[i], &contextState); +// if (ret < 0) +// { +// app.DebugPrintf("sceNpBasicGetFriendContextState() failed: ret = 0x%x\n", ret); +// contextState = SCE_NP_BASIC_FRIEND_CONTEXT_STATE_UNKNOWN; +// } +// if(contextState == SCE_NP_BASIC_FRIEND_CONTEXT_STATE_IN_CONTEXT) // using the same SceNpCommunicationId, so playing Minecraft +// { +// ret = sceNpBasicGetGamePresenceOfFriend(&friendIDs[i], &presenceDetails); +// if( ( ret == 0 ) && ( !manager->ForceErrorPoint( SNM_FORCE_ERROR_GET_FRIEND_LIST_ENTRY ) ) ) +// { +// FriendSearchResult result; +// memcpy(&result.m_NpId, &friendIDs[i], sizeof(SceNpId)); +// result.m_RoomFound = false; +// +// // Only include the friend's game if its the same network id ( this also filters out generally Zeroed HelloSyncInfo, which we do when we aren't in an active game session) +// // if( presenceDetails.size == sizeof(HelloSyncInfo) ) +// { +// HelloSyncInfo *pso = (HelloSyncInfo *)presenceDetails.inGamePresence.data; +// if( pso->netVersion == MINECRAFT_NET_VERSION ) +// { +// if( !pso->inviteOnly ) +// { +// result.m_RoomFound = true; +// result.m_RoomId = pso->m_RoomId; +// result.m_ServerId = pso->m_ServerId; +// +// CPlatformNetworkManagerSony::MallocAndSetExtDataFromSQRPresenceInfo(&result.m_RoomExtDataReceived, pso); +// manager->m_aFriendSearchResults.push_back(result); +// } +// } +// } +// } +// } +// } +// +// if(friendIDs) +// delete friendIDs; +// return 0; +// } + +// Get count of rooms that friends are playing in. Only valid when FriendRoomManagerIsBusy() returns false +int SQRNetworkManager_AdHoc_Vita::FriendRoomManagerGetCount() +{ + assert( m_friendSearchState == SNM_FRIEND_SEARCH_STATE_IDLE ); + return m_aFriendSearchResults.size(); +} + +// Get details of a found session that a friend is playing in. 0 < idx < FriendRoomManagerGetCount(). Only valid when FriendRoomManagerIsBusy() returns false +void SQRNetworkManager_AdHoc_Vita::FriendRoomManagerGetRoomInfo(int idx, SQRNetworkManager_AdHoc_Vita::SessionSearchResult *searchResult) +{ + assert( idx < m_aFriendSearchResults.size() ); + assert( m_friendSearchState == SNM_FRIEND_SEARCH_STATE_IDLE ); + + searchResult->m_NpId = m_aFriendSearchResults[idx].m_NpId; + + ZeroMemory(&searchResult->m_sessionId, sizeof(SQRNetworkManager::SessionID)); + searchResult->m_sessionId.m_RoomId = m_aFriendSearchResults[idx].m_netAddr.s_addr; + //searchResult->m_sessionId.m_ServerId = m_aFriendSearchResults[idx].m_ServerId; + searchResult->m_netAddr = m_aFriendSearchResults[idx].m_netAddr; + searchResult->m_extData = m_aFriendSearchResults[idx].m_RoomExtDataReceived; +} + +// Get overall state of the network manager. +SQRNetworkManager_AdHoc_Vita::eSQRNetworkManagerState SQRNetworkManager_AdHoc_Vita::GetState() +{ + return m_stateExternal;; +} + +bool SQRNetworkManager_AdHoc_Vita::IsHost() +{ + return m_isHosting; +} + +bool SQRNetworkManager_AdHoc_Vita::IsReadyToPlayOrIdle() +{ + return (( m_state == SNM_INT_STATE_HOSTING_WAITING_TO_PLAY ) || ( m_state == SNM_INT_STATE_PLAYING ) || ( m_state == SNM_INT_STATE_IDLE ) ); +} + + +// Consider as "in session" from the moment that a game is created or joined, until the point where the game itself has been told via state change that we are now idle. The +// game code requires IsInSession to return true as soon as it has asked to do one of these things (even if the state P hasn't really caught up with this request yet), and +// it also requires that it is informed of the state changes leading up to not being in the session, before this should report false. +bool SQRNetworkManager_AdHoc_Vita::IsInSession() +{ + return m_isInSession; +} + +// Get count of players currently in the session +int SQRNetworkManager_AdHoc_Vita::GetPlayerCount() +{ + return m_roomSyncData.getPlayerCount(); +} + +// Get count of players who are in the session, but not local to this machine +int SQRNetworkManager_AdHoc_Vita::GetOnlinePlayerCount() +{ + int onlineCount = 0; + for( int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( m_roomSyncData.players[i].m_roomMemberId != m_localMemberId ) + { + onlineCount++; + } + } + return onlineCount; +} + +SQRNetworkPlayer *SQRNetworkManager_AdHoc_Vita::GetPlayerByIndex(int idx) +{ + if( idx < MAX_ONLINE_PLAYER_COUNT ) + { + return GetPlayerIfReady(m_aRoomSlotPlayers[idx]); + } + else + { + return NULL; + } +} + +SQRNetworkPlayer *SQRNetworkManager_AdHoc_Vita::GetPlayerBySmallId(int idx) +{ + EnterCriticalSection(&m_csRoomSyncData); + for( int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( m_roomSyncData.players[i].m_smallId == idx ) + { + SQRNetworkPlayer *player = GetPlayerIfReady(m_aRoomSlotPlayers[i]); + LeaveCriticalSection(&m_csRoomSyncData); + return player; + } + } + LeaveCriticalSection(&m_csRoomSyncData); + return NULL; +} + +SQRNetworkPlayer *SQRNetworkManager_AdHoc_Vita::GetLocalPlayerByUserIndex(int idx) +{ + EnterCriticalSection(&m_csRoomSyncData); + for( int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( ( m_roomSyncData.players[i].m_roomMemberId == m_localMemberId ) && ( m_roomSyncData.players[i].m_localIdx == idx ) ) + { + SQRNetworkPlayer *player = GetPlayerIfReady(m_aRoomSlotPlayers[i]); + LeaveCriticalSection(&m_csRoomSyncData); + return player; + } + } + LeaveCriticalSection(&m_csRoomSyncData); + return NULL; +} + +SQRNetworkPlayer *SQRNetworkManager_AdHoc_Vita::GetHostPlayer() +{ + EnterCriticalSection(&m_csRoomSyncData); + SQRNetworkPlayer *player = GetPlayerIfReady(m_aRoomSlotPlayers[0]); + LeaveCriticalSection(&m_csRoomSyncData); + return player; +} + +SQRNetworkPlayer *SQRNetworkManager_AdHoc_Vita::GetPlayerIfReady(SQRNetworkPlayer *player) +{ + if( player == NULL ) return NULL; + + if( player->IsReady() ) return player; + + return NULL; +} + +// Update state internally +void SQRNetworkManager_AdHoc_Vita::SetState(SQRNetworkManager_AdHoc_Vita::eSQRNetworkManagerInternalState state) +{ + eSQRNetworkManagerState oldState = m_INTtoEXTStateMappings[m_state]; + eSQRNetworkManagerState newState = m_INTtoEXTStateMappings[state]; + bool setIdleReasonSessionFull = false; + if( ( state == SNM_INT_STATE_IDLE ) && m_nextIdleReasonIsFull ) + { + setIdleReasonSessionFull = true; + m_nextIdleReasonIsFull = false; + } + m_state = state; + // Queue any important (ie externally relevant) state changes - we will do a call back for these in our main tick. Don't do it directly here + // as we could be coming from any thread at this stage, with any stack size etc. and so we don't generally want to expect the game to be able to handle itself in such circumstances. + if( ( newState != oldState ) || setIdleReasonSessionFull ) + { + EnterCriticalSection(&m_csStateChangeQueue); + m_stateChangeQueue.push(StateChangeInfo(oldState,newState,setIdleReasonSessionFull)); + LeaveCriticalSection(&m_csStateChangeQueue); + } +} + +void SQRNetworkManager_AdHoc_Vita::ResetToIdle() +{ + app.DebugPrintf("------------------ResetToIdle--------------------\n"); + // If we're the client, remove any networked players properly ( this will destory their rupd context etc.) + if( !m_isHosting ) + { + RemoveNetworkPlayers((1 << MAX_LOCAL_PLAYER_COUNT)-1); + } + else + { + // we don't get signalling back from the matching libs to destroy connections, so we need to kill everything here + std::vector memberIDs; + for(int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + memberIDs.push_back(m_aRoomSlotPlayers[i]->m_roomMemberId); + } + for(int i=0;im_extData, searchResult->m_sessionId.m_RoomId, searchResult->m_sessionId.m_ServerId); + return JoinRoom(searchResult->m_netAddr, localPlayerMask, NULL); +} + +bool SQRNetworkManager_AdHoc_Vita::JoinRoom(SceNpMatching2RoomId roomId, SceNpMatching2ServerId serverId, int localPlayerMask, const PresenceSyncInfo *presence) +{ + assert(0);// only here to match the parent class interface + return false; +} + + +// Join room with a specified roomId. This is used when joining from an invite, as well as by the previous method +bool SQRNetworkManager_AdHoc_Vita::JoinRoom(SceNetInAddr netAddr, int localPlayerMask, const HelloSyncInfo *presence) +{ + // The presence info will be directly passed in if we are joining from an invite, otherwise it has already been set up. This is synchronised out when we have fully joined the game. + if( presence ) + { + memcpy( &s_lastPresenceSyncInfo, presence, sizeof(HelloSyncInfo) ); + } + else + { + for(int i=0;im_type == playerType ) && ( (*it)->m_localPlayerIdx == localPlayerIdx ) ) + { + if( ( playerType != SQRNetworkPlayer::SNP_TYPE_REMOTE ) || ( (*it)->m_roomMemberId == memberId ) ) + { + SQRNetworkPlayer *player = *it; + m_vecTempPlayers.erase(it); + m_aRoomSlotPlayers[ slot ] = player; + return; + } + } + } + // Create the player - non-network players can be considered complete as soon as we create them as we aren't waiting on their network connections becoming complete, so can flag them as such and notify via callback + PlayerUID *pUID = NULL; + PlayerUID localUID; + if( ( playerType == SQRNetworkPlayer::SNP_TYPE_LOCAL ) || + m_isHosting && ( playerType == SQRNetworkPlayer::SNP_TYPE_HOST ) ) + { + // Local players can establish their UID at this point + ProfileManager.GetXUID(localPlayerIdx,&localUID,true); + pUID = &localUID; + } + SQRNetworkPlayer *player = new SQRNetworkPlayer(this, (SQRNetworkPlayer::eSQRNetworkPlayerType)playerType, m_isHosting, memberId, localPlayerIdx, 0, pUID ); + // For offline games, set name directly from gamertag as the PlayerUID will be full of zeroes. + if( m_offlineGame ) + { + player->SetName(ProfileManager.GetGamertag(localPlayerIdx)); + } + NonNetworkPlayerComplete( player, smallId); + m_aRoomSlotPlayers[ slot ] = player; + HandlePlayerJoined( player ); +} + +// For data sending on the local machine, used to send between host and localplayers on the host +void SQRNetworkManager_AdHoc_Vita::LocalDataSend(SQRNetworkPlayer *playerFrom, SQRNetworkPlayer *playerTo, const void *data, unsigned int dataSize) +{ + assert(m_isHosting); + if(m_listener) + { + m_listener->HandleDataReceived( playerFrom, playerTo, (unsigned char *)data, dataSize ); + } +} + +int SQRNetworkManager_AdHoc_Vita::GetSessionIndex(SQRNetworkPlayer *player) +{ + int roomSlotPlayerCount = m_roomSyncData.getPlayerCount(); + for( int i = 0; i < roomSlotPlayerCount; i++ ) + { + if( m_aRoomSlotPlayers[i] == player ) return i; + } + return 0; +} + +// Updates m_aRoomSlotPlayers, based on what is in m_roomSyncData. This needs to be updated when room members join & leave, and when any SQRNetworkPlayer is created externally that this should be mapping to +void SQRNetworkManager_AdHoc_Vita::MapRoomSlotPlayers(int roomSlotPlayerCount/*=-1*/) +{ + EnterCriticalSection(&m_csRoomSyncData); + + // If we pass an explicit roomSlotPlayerCount, it is because we are removing a player, and this is the count of slots that there were *before* the removal. + bool zeroLastSlot = false; + if( roomSlotPlayerCount == -1 ) + { + roomSlotPlayerCount = m_roomSyncData.getPlayerCount(); + } + else + { + zeroLastSlot = true; + } + + if( m_isHosting ) + { + for( int i = 0; i < roomSlotPlayerCount; i++ ) + { + if( m_aRoomSlotPlayers[i] ) + { + // On host, remote players are created and destroyed by the Rudp connections being established and removed, so don't go deleting them here. Other types are managed by this mapping. + // Note that m_vecTempPlayers is used as a pool of players to consider by FindOrCreateNonNetworkPlayer + if( m_aRoomSlotPlayers[i]->m_type != SQRNetworkPlayer::SNP_TYPE_REMOTE ) + { + m_vecTempPlayers.push_back(m_aRoomSlotPlayers[i]); + m_aRoomSlotPlayers[i] = NULL; + } + } + } + for( int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( i == 0 ) + { + // Special case - slot 0 is always the host + FindOrCreateNonNetworkPlayer( i, SQRNetworkPlayer::SNP_TYPE_HOST, m_roomSyncData.players[i].m_roomMemberId, m_roomSyncData.players[i].m_localIdx, m_roomSyncData.players[i].m_smallId); + m_roomSyncData.players[i].m_UID = m_aRoomSlotPlayers[i]->GetUID(); // On host, UIDs flow from player data -> m_roomSyncData + } + else + { + if( m_roomSyncData.players[i].m_roomMemberId == m_localMemberId ) + { + FindOrCreateNonNetworkPlayer( i, SQRNetworkPlayer::SNP_TYPE_LOCAL, m_roomSyncData.players[i].m_roomMemberId, m_roomSyncData.players[i].m_localIdx, m_roomSyncData.players[i].m_smallId); + m_roomSyncData.players[i].m_UID = m_aRoomSlotPlayers[i]->GetUID(); // On host, UIDs flow from player data -> m_roomSyncData + } + else + { + m_aRoomSlotPlayers[i] = GetPlayerFromRoomMemberAndLocalIdx( m_roomSyncData.players[i].m_roomMemberId, m_roomSyncData.players[i].m_localIdx ); + // If we're the host, then we allocated the small id so can flag now if we've got a player to flag... + if( m_aRoomSlotPlayers[i] ) + { + NetworkPlayerSmallIdAllocated(m_aRoomSlotPlayers[i], m_roomSyncData.players[i].m_smallId); + } + } + } + } + + if( zeroLastSlot ) + { + if( roomSlotPlayerCount ) + { + m_aRoomSlotPlayers[ roomSlotPlayerCount - 1 ] = 0; + } + } + + // Also update the externally visible room data for the current slots + if (m_listener ) + { + m_listener->HandleResyncPlayerRequest(m_aRoomSlotPlayers); + } + } + else + { + for( int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( m_aRoomSlotPlayers[i] ) + { + // On clients, local players are created and destroyed by the Rudp connections being established and removed, so don't go deleting them here. Other types are managed by this mapping. + // Note that m_vecTempPlayers is used as a pool of players to consider by FindOrCreateNonNetworkPlayer + if( m_aRoomSlotPlayers[i]->m_type != SQRNetworkPlayer::SNP_TYPE_LOCAL ) + { + m_vecTempPlayers.push_back(m_aRoomSlotPlayers[i]); + m_aRoomSlotPlayers[i] = NULL; + } + } + } + for( int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( i == 0 ) + { + // Special case - slot 0 is always the host + FindOrCreateNonNetworkPlayer( i, SQRNetworkPlayer::SNP_TYPE_HOST, m_roomSyncData.players[i].m_roomMemberId, m_roomSyncData.players[i].m_localIdx, m_roomSyncData.players[i].m_smallId); + m_aRoomSlotPlayers[i]->SetUID(m_roomSyncData.players[i].m_UID); // On client, UIDs flow from m_roomSyncData->player data + } + else + { + if( m_roomSyncData.players[i].m_roomMemberId == m_localMemberId ) + { + // This player is local to this machine - don't bother setting UID from sync data, as it will already have been set accurately when we (locally) made this player + m_aRoomSlotPlayers[i] = GetPlayerFromRoomMemberAndLocalIdx( m_roomSyncData.players[i].m_roomMemberId, m_roomSyncData.players[i].m_localIdx ); + // If we've got the room sync data back from the server, then we've got our smallId. Set flag for this. + if( m_aRoomSlotPlayers[i] ) + { + NetworkPlayerSmallIdAllocated(m_aRoomSlotPlayers[i], m_roomSyncData.players[i].m_smallId); + } + } + else + { + FindOrCreateNonNetworkPlayer( i, SQRNetworkPlayer::SNP_TYPE_REMOTE, m_roomSyncData.players[i].m_roomMemberId, m_roomSyncData.players[i].m_localIdx, m_roomSyncData.players[i].m_smallId); + m_aRoomSlotPlayers[i]->SetUID(m_roomSyncData.players[i].m_UID); // On client, UIDs flow from m_roomSyncData->player data + } + } + } + } + // Clear up any non-network players that are no longer required - this would be a good point to notify of players leaving when we support that + // FindOrCreateNonNetworkPlayer will have pulled any players that we Do need out of m_vecTempPlayers, so the ones that are remaining are no longer in the game + for(AUTO_VAR(it, m_vecTempPlayers.begin()); it != m_vecTempPlayers.end(); it++ ) + { + if( m_listener ) + { + m_listener->HandlePlayerLeaving(*it); + } + delete (*it); + } + m_vecTempPlayers.clear(); + + LeaveCriticalSection(&m_csRoomSyncData); +} + +// On host, update the room sync data with UIDs that are in the players +void SQRNetworkManager_AdHoc_Vita::UpdateRoomSyncUIDsFromPlayers() +{ + EnterCriticalSection(&m_csRoomSyncData); + if( m_isHosting ) + { + for( int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( m_aRoomSlotPlayers[i] ) + { + m_roomSyncData.players[i].m_UID = m_aRoomSlotPlayers[i]->GetUID(); + } + } + } + + LeaveCriticalSection(&m_csRoomSyncData); +} + +// On the client, move UIDs from the room sync data out to the players. +void SQRNetworkManager_AdHoc_Vita::UpdatePlayersFromRoomSyncUIDs() +{ + EnterCriticalSection(&m_csRoomSyncData); + for( int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( m_aRoomSlotPlayers[i] ) + { + if( i == 0 ) + { + // Special case - slot 0 is always the host + m_aRoomSlotPlayers[i]->SetUID(m_roomSyncData.players[i].m_UID); + } + else + { + // Don't sync local players as we already set those up with their UID in the first place... + if( m_roomSyncData.players[i].m_roomMemberId != m_localMemberId ) + { + m_aRoomSlotPlayers[i]->SetUID(m_roomSyncData.players[i].m_UID); + } + } + } + } + LeaveCriticalSection(&m_csRoomSyncData); +} + +// Host only - add remote players to our internal storage of player slots, and synchronise this with other room members. +bool SQRNetworkManager_AdHoc_Vita::AddRemotePlayersAndSync( SceNpMatching2RoomMemberId memberId, int playerMask, bool *isFull/*==NULL*/ ) +{ + assert( m_isHosting ); + + EnterCriticalSection(&m_csRoomSyncData); + + // Establish whether we have enough room to add the players + int addCount = 0; + for( int i = 0; i < MAX_LOCAL_PLAYERS; i++ ) + { + if( playerMask & ( 1 << i ) ) + { + addCount++; + } + } + + if( ( m_roomSyncData.getPlayerCount() + addCount ) > MAX_ONLINE_PLAYER_COUNT ) + { + if( isFull ) + { + *isFull = true; + } + LeaveCriticalSection(&m_csRoomSyncData); + return false; + } + + // We want to keep all players from a particular machine together, so search through the room sync data to see if we can find + // any pre-existing players from this machine. + int firstIdx = -1; + for( int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( m_roomSyncData.players[i].m_roomMemberId == memberId ) + { + firstIdx = i; + break; + } + } + + // We'll just be inserting at the end unless we've got a pre-existing player to insert after. Even then there might be no following + // players. + int insertIdx = m_roomSyncData.getPlayerCount(); + if( firstIdx > -1 ) + { + for( int i = firstIdx; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( m_roomSyncData.players[i].m_roomMemberId != memberId ) + { + insertIdx = i; + break; + } + } + } + + // Add all remote players determined from the player mask to our own slots of active players + for( int i = 0; i < MAX_LOCAL_PLAYER_COUNT; i++ ) + { + if( playerMask & ( 1 << i ) ) + { + // Shift any following players along... + for( int j = m_roomSyncData.getPlayerCount(); j > insertIdx; j-- ) + { + m_roomSyncData.players[j] = m_roomSyncData.players[j-1]; + } + PlayerSyncData *player = &m_roomSyncData.players[ insertIdx ]; + player->m_smallId = m_currentSmallId++; + player->m_roomMemberId = memberId; + player->m_localIdx = i; + m_roomSyncData.setPlayerCount(m_roomSyncData.getPlayerCount()+1); + insertIdx++; + } + } + + // Update mapping from the room slot players to SQRNetworkPlayer instances + MapRoomSlotPlayers(); + + // And then synchronise this out to all other machines + SyncRoomData(); + + LeaveCriticalSection(&m_csRoomSyncData); + + return true; +} + +// Host only - remove all remote players belonging to the supplied memberId, and in the supplied mask, and synchronise this with other room members +void SQRNetworkManager_AdHoc_Vita::RemoveRemotePlayersAndSync( SceNpMatching2RoomMemberId memberId, int mask ) +{ + assert( m_isHosting ); + EnterCriticalSection(&m_csRoomSyncData); + + // Remove any applicable players, keeping remaining players in order + for( int i = 0; i < m_roomSyncData.getPlayerCount(); ) + { + if( ( m_roomSyncData.players[ i ].m_roomMemberId == memberId ) && ( ( 1 << m_roomSyncData.players[ i ].m_localIdx ) & mask ) ) + { + SQRNetworkPlayer *player = GetPlayerFromRoomMemberAndLocalIdx( memberId, m_roomSyncData.players[ i ].m_localIdx ); + if( player ) + { + // Get Rudp context for this player, close that context down ( which will in turn close the socket if required) + int ctx = player->m_rudpCtx; + int err = sceRudpTerminate( ctx ); + assert(err == SCE_OK); + if( m_listener ) + { + m_listener->HandlePlayerLeaving(player); + } + // Delete the player itself and the mapping from context to player map as this context is no longer valid + delete player; + m_RudpCtxToPlayerMap.erase(ctx); + + removePlayerFromVoiceChat(player); + } + m_roomSyncData.setPlayerCount(m_roomSyncData.getPlayerCount()-1); + // Shuffled entries up into the space that we have just created + for( int j = i ; j < m_roomSyncData.getPlayerCount(); j++ ) + { + m_roomSyncData.players[j] = m_roomSyncData.players[j + 1]; + m_aRoomSlotPlayers[j] = m_aRoomSlotPlayers[j + 1]; + } + // Zero last element, that isn't part of the currently sized array anymore + memset(&m_roomSyncData.players[m_roomSyncData.getPlayerCount()],0,sizeof(PlayerSyncData)); + m_aRoomSlotPlayers[m_roomSyncData.getPlayerCount()] = NULL; + } + else + { + i++; + } + } + LeaveCriticalSection(&m_csRoomSyncData); + + // Update mapping from the room slot players to SQRNetworkPlayer instances + MapRoomSlotPlayers(); + + + // And then synchronise this out to all other machines + SyncRoomData(); + + // if(GetOnlinePlayerCount() == 0) + // SonyVoiceChat::shutdown(); +} + +// Client only - remove all network players matching the supplied mask +void SQRNetworkManager_AdHoc_Vita::RemoveNetworkPlayers( int mask ) +{ + assert( !m_isHosting ); + + for(AUTO_VAR(it, m_RudpCtxToPlayerMap.begin()); it != m_RudpCtxToPlayerMap.end(); ) + { + SQRNetworkPlayer *player = it->second; + if( (player->m_roomMemberId == m_localMemberId ) && ( ( 1 << player->m_localPlayerIdx ) & mask ) ) + { + // Get Rudp context for this player, close that context down ( which will in turn close the socket if required) + int ctx = it->first; + int err = sceRudpTerminate( ctx ); + assert(err == SCE_OK); + if( m_listener ) + { + m_listener->HandlePlayerLeaving(player); + } + // Delete any reference to this player from the player mappings + for( int i = 0; i < MAX_ONLINE_PLAYER_COUNT; i++ ) + { + if( m_aRoomSlotPlayers[i] == player ) + { + m_aRoomSlotPlayers[i] = NULL; + } + } + // And delete the reference from the ctx->player map + it = m_RudpCtxToPlayerMap.erase(it); + + removePlayerFromVoiceChat(player); + + // Delete the player itself and the mapping from context to player map as this context is no longer valid + delete player; + } + else + { + it++; + } + } + +} + +// Host only - update the memberId of the local players, and synchronise with other room members +void SQRNetworkManager_AdHoc_Vita::SetLocalPlayersAndSync() +{ + assert( m_isHosting ); + + // Update local IP address + UpdateLocalIPAddress(); + + m_localMemberId = getRoomMemberID(&m_localIPAddr); + if(IsHost()) + m_hostMemberId = m_localMemberId; + + + for( int i = 0; i < m_localPlayerCount; i++ ) + { + m_roomSyncData.players[i].m_roomMemberId = m_localMemberId; + } + + // Update mapping from the room slot players to SQRNetworkPlayer instances + MapRoomSlotPlayers(); + + // And then synchronise this out to all other machines + SyncRoomData(); + +} + +// Host only - sync the room data with other machines +void SQRNetworkManager_AdHoc_Vita::SyncRoomData() +{ + if( m_offlineGame ) return; + + UpdateRoomSyncUIDsFromPlayers(); + + // send the room data packet out to all the clients + for(AUTO_VAR(iter, m_RudpCtxToPlayerMap.begin()); iter != m_RudpCtxToPlayerMap.end(); ++iter) + { + int ctx = iter->first; + SQRNetworkPlayer* pPlayer = GetPlayerFromRudpCtx(ctx); + assert(pPlayer); + sendDataPacket(*GetIPAddrFromRudpCtx(ctx), e_dataTag_RoomSync, &m_roomSyncData, sizeof(m_roomSyncData)); + } +} + + + +void SQRNetworkManager_AdHoc_Vita::MatchingEventHandler(int id, int event, SceNetInAddr* peer, int optlen, void *opt) +{ + SQRNetworkManager_AdHoc_Vita* manager = s_pAdhocVitaManager; + + app.DebugPrintf("MatchingEventHandler_Server : ev : %d, ip addr : %s\n", event, getIPAddressString(*peer).c_str()); + int ret; + + switch (event) + { + case SCE_NET_ADHOC_MATCHING_EVENT_HELLO: + app.DebugPrintf("P2P SCE_NET_ADHOC_MATCHING_EVENT_HELLO Received!!\n"); + + if(manager->m_isHosting) + { + assert(0); // the host should never see the hello message + } + else if(optlen > 0) + { + if(optlen == sizeof(HelloSyncInfo)) + { + FriendSearchResult result; + // memcpy(&result.m_NpId, &friendIDs[i], sizeof(SceNpId)); + result.m_RoomFound = false; + + HelloSyncInfo *pso = (HelloSyncInfo *)opt; + if( pso->m_presenceSyncInfo.netVersion == MINECRAFT_NET_VERSION ) + { + if( !pso->m_presenceSyncInfo.inviteOnly ) + { + result.m_netAddr = *peer; + result.m_RoomFound = true; + memcpy(result.m_NpId.handle.data, pso->m_presenceSyncInfo.hostPlayerUID.getOnlineID(), SCE_NP_ONLINEID_MAX_LENGTH); + // result.m_RoomId = pso->m_RoomId; + // result.m_ServerId = pso->m_ServerId; + + CPlatformNetworkManagerSony::MallocAndSetExtDataFromSQRPresenceInfo(&result.m_RoomExtDataReceived, &pso->m_presenceSyncInfo); + result.m_gameSessionData = malloc(sizeof(GameSessionData)); + memcpy(result.m_gameSessionData, &pso->m_gameSessionData, sizeof(GameSessionData)); + // check we don't have this already + int currIndex = -1; + bool bChanged = false; + for(int i=0; im_aFriendSearchResults.size(); i++) + { + if(manager->m_aFriendSearchResults[i].m_netAddr.s_addr == peer->s_addr) + { + currIndex = i; + if(memcmp(result.m_gameSessionData, manager->m_aFriendSearchResults[i].m_gameSessionData, sizeof(GameSessionData)) != 0) + bChanged = true; + if(memcmp(&result.m_roomSyncData, &manager->m_aFriendSearchResults[i].m_roomSyncData, sizeof(RoomSyncData)) != 0) + bChanged = true; + break; + } + } + if(currIndex>=0 && bChanged) + manager->m_aFriendSearchResults.erase(manager->m_aFriendSearchResults.begin() + currIndex); + if(currIndex<0 || bChanged) + manager->m_aFriendSearchResults.push_back(result); + } + } + else + { + app.DebugPrintf("mismatching net version, expected %d, but got %d !!\n", MINECRAFT_NET_VERSION, pso->m_presenceSyncInfo.netVersion); + } + } + else + { + app.DebugPrintf("Wrong size for HelloSyncInfo, should be %d bytes, but was %d bytes!!\n", sizeof(HelloSyncInfo), optlen); + assert(0); + } + } + break; + + case SCE_NET_ADHOC_MATCHING_EVENT_REQUEST: // A join request was received + app.DebugPrintf("P2P SCE_NET_ADHOC_MATCHING_EVENT_REQUEST Received!!\n"); + if (optlen > 0 && opt != NULL) + { + ret = SCE_OK;// parentRequestAdd(opt); + if (ret != SCE_OK) + { + ret = sceNetAdhocMatchingCancelTarget(manager->m_matchingContext, peer); + if (ret < 0) + { + app.DebugPrintf("sceNetAdhocMatchingCancelTarget error :[%d] [%x]\n",ret,ret) ; + assert(0); + break; + } + } + } + // accept the join request + sceNetAdhocMatchingSelectTarget(manager->m_matchingContext, peer, sizeof(manager->m_roomSyncData), &manager->m_roomSyncData); + break; + + + case SCE_NET_ADHOC_MATCHING_EVENT_ACCEPT: // The join request was accepted + app.DebugPrintf("P2P SCE_NET_ADHOC_MATCHING_EVENT_ACCEPT Received!!\n"); + if( manager->m_isHosting == false ) + { + assert(opt && optlen == sizeof(m_roomSyncData)); + memcpy(&manager->m_roomSyncData, opt, sizeof(manager->m_roomSyncData)); + } + break; + + case SCE_NET_ADHOC_MATCHING_EVENT_ESTABLISHED: // A participation agreement was established + { + app.DebugPrintf("P2P SCE_NET_ADHOC_MATCHING_EVENT_ESTABLISHED Received!!\n"); + + + SceNetInAddr localAddr; + int ret = sceNetCtlAdhocGetInAddr(&localAddr); + assert(ret == SCE_OK); + manager->m_localMemberId = getRoomMemberID(&localAddr); + + if( manager->m_isHosting ) + { + manager->m_hostMemberId = manager->m_localMemberId; + + bool isFull = false; + bool success = manager->AddRemotePlayersAndSync( getRoomMemberID(peer), 1, &isFull ); + if( success ) + { + bool success2 = manager->CreateRudpConnections(*peer); + if(success2) + { + manager->SyncRoomData(); // have to syn the room data again now we have the peer in our list + break; + } + } + // Something has gone wrong adding these players to the room - kick out the player + assert(0); + } + else + { + // Local players can establish their UID at this point + PlayerUID localUID; + ProfileManager.GetXUID(0,&localUID,true); + localUID.setForAdhoc(); + + bool success = manager->CreateRudpConnections(*peer); + manager->SetState(SNM_INT_STATE_JOINING_WAITING_FOR_LOCAL_PLAYERS); + } + + // // If we've created a player, then we want to try and patch up any connections that we should have to it + // manager->MapRoomSlotPlayers(); + // + // SQRNetworkPlayer *player = manager->GetPlayerFromRudpCtx(peer->s_addr); + // if( player ) + // { + // // Flag connection stage as being completed for this player + // manager->NetworkPlayerConnectionComplete(player); + // } + } + + break; + + + case SCE_NET_ADHOC_MATCHING_EVENT_LEAVE: // The participation agreement was canceled by the target player + case SCE_NET_ADHOC_MATCHING_EVENT_DENY: + case SCE_NET_ADHOC_MATCHING_EVENT_CANCEL: // The join request was canceled by the client + case SCE_NET_ADHOC_MATCHING_EVENT_ERROR: // A protocol error occurred + case SCE_NET_ADHOC_MATCHING_EVENT_TIMEOUT: // The participation agreement was canceled because of a Keep Alive timeout + case SCE_NET_ADHOC_MATCHING_EVENT_DATA_TIMEOUT: + app.SetDisconnectReason(DisconnectPacket::eDisconnect_TimeOut); + ret = sceNetAdhocMatchingCancelTarget(manager->m_matchingContext, peer); + if ( ret < 0 ) + { + app.DebugPrintf("sceNetAdhocMatchingCancelTarget error :[%d] [%x]\n",ret,ret) ; + } + // -> + // no break, carry on through to the BYE case -> + // -> + case SCE_NET_ADHOC_MATCHING_EVENT_BYE: // Target participating player has stopped matching + { + app.DebugPrintf("P2P SCE_NET_ADHOC_MATCHING_EVENT_BYE Received!!\n"); + + + SceNpMatching2RoomMemberId peerMemberId = getRoomMemberID(peer); + if( manager->m_isHosting ) + { + // Remove any players associated with this peer + manager->RemoveRemotePlayersAndSync( peerMemberId, 15 ); + } + else if(peerMemberId == manager->m_hostMemberId) + { + // Host has left the game... so its all over for this client too. Finish everything up now, including deleting the server context which belongs to this gaming session + // This also might be a response to a request to leave the game from our end too so don't need to do anything in that case + if( manager->m_state != SNM_INT_STATE_LEAVING ) + { + manager->DeleteServerContext(); + } + } + else + { + if(sc_voiceChatEnabled) + { + // we've lost connection to another client (voice only) so kill the voice connection + // no players left on the remote machine once we remove this one + SQRVoiceConnection* pVoice = SonyVoiceChat_Vita::getVoiceConnectionFromRoomMemberID(peerMemberId); + assert(pVoice); + if(pVoice) + SonyVoiceChat_Vita::disconnectRemoteConnection(pVoice); + } + } + } + + break; + + case SCE_NET_ADHOC_MATCHING_EVENT_DATA: + { + app.DebugPrintf("P2P SCE_NET_ADHOC_MATCHING_EVENT_DATA Received!!\n"); + + if (optlen <= 0 || opt == NULL) + { + assert(0); + break; + } + AdhocDataPacket* pPacket = (AdhocDataPacket*)opt; + unsigned int dataSize = optlen - 4; + + if(pPacket->m_tag == e_dataTag_RoomSync) + { + // room sync data, copy it over + EnterCriticalSection(&manager->m_csRoomSyncData); + assert(dataSize == sizeof(manager->m_roomSyncData)); + memcpy(&manager->m_roomSyncData, pPacket->m_pData, dataSize); + LeaveCriticalSection(&manager->m_csRoomSyncData); + manager->MapRoomSlotPlayers(); + } + else + { + assert(0); + } + } + break ; + + case SCE_NET_ADHOC_MATCHING_EVENT_DATA_ACK: + { + SQRNetworkPlayer *player = manager->GetPlayerFromRudpCtx(peer->s_addr); + // This event signifies that the previous packet has been sent, so we can try to send some more + if( player ) + { + player->SendMoreInternal(); + } + } + break; + + + default: + break; + } +} + + +// Tick the process of creating a room. +void SQRNetworkManager_AdHoc_Vita::RoomCreateTick() +{ + switch( m_state ) + { + case SNM_INT_STATE_HOSTING_CREATE_ROOM_SEARCHING_FOR_WORLD: + break; + case SNM_INT_STATE_HOSTING_CREATE_ROOM_WORLD_FOUND: + break; + case SNM_INT_STATE_HOSTING_CREATE_ROOM_CREATING_ROOM: + break; + case SNM_INT_STATE_HOSTING_CREATE_ROOM_SUCCESS: + SetState(SNM_INT_STATE_HOSTING_WAITING_TO_PLAY); + + // Now we know the local member id we can update our local players + SetLocalPlayersAndSync(); + break; + case SNM_INT_STATE_HOSTING_CREATE_ROOM_FAILED: + break; + default: + break; + } +} + +// For a player using the network to communicate, flag as having its connection complete. This wraps the player's own functionality, so that we can determine if this +// call is transitioning us from not ready to ready, and call a registered callback. +void SQRNetworkManager_AdHoc_Vita::NetworkPlayerConnectionComplete(SQRNetworkPlayer *player) +{ + EnterCriticalSection(&m_csPlayerState); + bool wasReady = player->IsReady(); + bool wasClientReady = player->HasConnectionAndSmallId(); + player->ConnectionComplete(); + bool isReady = player->IsReady(); + bool isClientReady = player->HasConnectionAndSmallId(); + if( !m_isHosting ) + { + // For clients, if we are ready (up the the point of having received our small id) then confirm to the host that this is the case, which makes us now fully ready at this end + if( ( !wasClientReady ) && ( isClientReady ) ) + { + player->ConfirmReady(); + isReady = true; + } + } + LeaveCriticalSection(&m_csPlayerState); + + if( ( !wasReady ) && ( isReady ) ) + { + HandlePlayerJoined( player ); + } +} + +// For a player using the network to communicate, set its small id, thereby flagging it as having one allocated +void SQRNetworkManager_AdHoc_Vita::NetworkPlayerSmallIdAllocated(SQRNetworkPlayer *player, unsigned char smallId) +{ + EnterCriticalSection(&m_csPlayerState); + bool wasReady = player->IsReady(); + bool wasClientReady = player->HasConnectionAndSmallId(); + player->SmallIdAllocated(smallId); + bool isReady = player->IsReady(); + bool isClientReady = player->HasConnectionAndSmallId(); + if( !m_isHosting ) + { + // For clients, if we are ready (up the the point of having received our small id) then confirm to the host that this is the case, which makes us now fully ready at this end + if( ( !wasClientReady ) && ( isClientReady ) ) + { + player->ConfirmReady(); + isReady = true; + } + } + LeaveCriticalSection(&m_csPlayerState); + + if( ( !wasReady ) && ( isReady ) ) + { + HandlePlayerJoined( player ); + } +} + +// On host, for a player using the network to communicate, confirm that its small id has now been received back +void SQRNetworkManager_AdHoc_Vita::NetworkPlayerInitialDataReceived(SQRNetworkPlayer *player, void *data) +{ + EnterCriticalSection(&m_csPlayerState); + SQRNetworkPlayer::InitSendData *ISD = (SQRNetworkPlayer::InitSendData *)data; + bool wasReady = player->IsReady(); + player->InitialDataReceived(ISD); + bool isReady = player->IsReady(); + LeaveCriticalSection(&m_csPlayerState); + // Sync room data back out as we've updated a player's UID here + SyncRoomData(); + + if( ( !wasReady ) && ( isReady ) ) + { + HandlePlayerJoined( player ); + } +} + +// For non-network players, flag that it is complete/ready, and assign its small id. We don't want to call any callbacks for these, as that can be explicitly done when local players are added. +// Also, we dynamically destroy & recreate local players quite a lot when remapping player slots which would create a lot of messages we don't want. +void SQRNetworkManager_AdHoc_Vita::NonNetworkPlayerComplete(SQRNetworkPlayer *player, unsigned char smallId) +{ + player->ConnectionComplete(); + player->SmallIdAllocated(smallId); +} + +void SQRNetworkManager_AdHoc_Vita::HandlePlayerJoined(SQRNetworkPlayer *player) +{ + if( m_listener ) + { + m_listener->HandlePlayerJoined( player ); + } + // On client, keep a count of how many local players we have told the game about. We can only transition to telling the game that we are playing once the room is set up And all the local players are valid to use. + if( !m_isHosting ) + { + if( player->IsLocal() ) + { + m_localPlayerJoined++; + } + } +} + +// Selects a random server from the current list, removes that server so it won't be searched for again, and then kick off an attempt to find out if that particular server is available. +bool SQRNetworkManager_AdHoc_Vita::SelectRandomServer() +{ + assert( (m_state == SNM_INT_STATE_HOSTING_SEARCHING_FOR_SERVER) || (m_state == SNM_INT_STATE_JOINING_SEARCHING_FOR_SERVER) ); + + if( m_serverCount == 0 ) + { + SetState((m_state == SNM_INT_STATE_HOSTING_SEARCHING_FOR_SERVER) ? SNM_INT_STATE_HOSTING_SERVER_SEARCH_FAILED : SNM_INT_STATE_JOINING_SERVER_SEARCH_FAILED); + app.DebugPrintf("SQRNetworkManager::SelectRandomServer - Server count is 0\n"); + return false; + } + + // not really selecting a random server, as we've already been allocated one, but calling this to match PS3 + int serverIdx; + serverIdx = 0; + m_serverCount--; + m_aServerId[serverIdx] = m_aServerId[m_serverCount]; + + // This server is available + SetState((m_state == SNM_INT_STATE_HOSTING_SEARCHING_FOR_SERVER) ? SNM_INT_STATE_HOSTING_SERVER_FOUND : SNM_INT_STATE_JOINING_SERVER_FOUND); + m_serverId = m_aServerId[serverIdx]; + + return true; +} + +// Delete the current server context. Should be called when finished with the current host or client game session. +void SQRNetworkManager_AdHoc_Vita::DeleteServerContext() +{ + // No server context on PS4, so we just set the state, and then we'll check all the UDP connections have shutdown before setting to idle + if(m_serverContextValid) + { + m_serverContextValid = false; + } + SetState(SNM_INT_STATE_SERVER_DELETING_CONTEXT); +} + +// Creates a set of Rudp connections by the "active open" method. This requires that both ends of the connection call cellRudpInitiate to fully create a connection. We +// create one connection per local play on any remote machine. +// +// peerMemberId is the room member Id of the remote end of the connection +// playersMemberId is the room member Id that the players belong to +// ie for the host (when matching incoming connections), these will be the same thing... and for the client, peerMemberId will be the host, whereas playersMemberId will be itself + + + + +bool SQRNetworkManager_AdHoc_Vita::CreateVoiceRudpConnections(SceNpMatching2RoomId roomId, SceNpMatching2RoomMemberId peerMemberId, int playerMask) +{ + PSVITA_STUBBED; + return true; +} + +bool SQRNetworkManager_AdHoc_Vita::CreateSocket() +{ + int ret; + // First get details of the UDPP2P connection that has been established + // int connStatus; + SceNetSockaddrIn sinp2pLocal;//, sinp2pPeer; + + // Local end first... + memset(&sinp2pLocal, 0, sizeof(sinp2pLocal)); + + // Update local IP address + UpdateLocalIPAddress(); + + sinp2pLocal.sin_len = sizeof(sinp2pLocal); + sinp2pLocal.sin_family = SCE_NET_AF_INET; + sinp2pLocal.sin_port = sceNetHtons(SCE_NP_PORT); + sinp2pLocal.sin_vport = sceNetHtons(ADHOC_VPORT) ; + sinp2pLocal.sin_addr = m_localIPAddr; + + // Create socket & bind + ret = sceNetSocket("rupdSocket", SCE_NET_AF_INET, SCE_NET_SOCK_DGRAM_P2P, 0); + assert(ret >= 0); + m_soc = ret; + int optval = 1; + ret = sceNetSetsockopt(m_soc, SCE_NET_SOL_SOCKET, SCE_NET_SO_USECRYPTO, &optval, sizeof(optval)); + if ( ( ret < 0 ) || ForceErrorPoint(SNM_FORCE_ERROR_SETSOCKOPT_0) ) return false; + ret = sceNetSetsockopt(m_soc, SCE_NET_SOL_SOCKET, SCE_NET_SO_USESIGNATURE, &optval, sizeof(optval)); + if ( ( ret < 0 ) || ForceErrorPoint(SNM_FORCE_ERROR_SETSOCKOPT_1) ) return false; + ret = sceNetSetsockopt(m_soc, SCE_NET_SOL_SOCKET, SCE_NET_SO_NBIO, &optval, sizeof(optval)); + if ( ( ret < 0 ) || ForceErrorPoint(SNM_FORCE_ERROR_SETSOCKOPT_2) ) return false; + + ret = sceNetBind(m_soc, &sinp2pLocal, sizeof(sinp2pLocal)); + if ( ( ret < 0 ) || ForceErrorPoint(SNM_FORCE_ERROR_SOCK_BIND) ) return false; + return true; + +} + + +bool SQRNetworkManager_AdHoc_Vita::CreateRudpConnections(SceNetInAddr peer) +{ + // First get details of the UDPP2P connection that has been established + SceNetSockaddrIn sinp2pPeer; + + // get the peer + memset(&sinp2pPeer, 0, sizeof(sinp2pPeer)); + sinp2pPeer.sin_len = sizeof(sinp2pPeer); + sinp2pPeer.sin_family = SCE_NET_AF_INET; + sinp2pPeer.sin_addr = peer; + sinp2pPeer.sin_port = sceNetHtons(SCE_NP_PORT); + + // Set vport + sinp2pPeer.sin_vport = sceNetHtons(ADHOC_VPORT); + + // Create socket & bind, if we don't already have one + if( m_soc == -1 ) + { + if(CreateSocket() == false) + return false; + } + + // Create an Rudp context for each local player that is required. These can be used as individual virtual connections between room members (ie consoles), which are multiplexed + // over the socket we have just made + + int rudpCtx; + + // Socket for the local network node created, now can create an Rupd context. + int ret = sceRudpCreateContext( RudpContextCallback, this, &rudpCtx ); + if ( ( ret < 0 ) || ForceErrorPoint(SNM_FORCE_ERROR_CREATE_RUDP_CONTEXT) ) return false; + if( m_isHosting ) + { + m_RudpCtxToPlayerMap[ rudpCtx ] = new SQRNetworkPlayer( this, SQRNetworkPlayer::SNP_TYPE_REMOTE, true, getRoomMemberID((&peer)), 0, rudpCtx, NULL ); + m_RudpCtxToIPAddrMap[ rudpCtx ] = peer; + } + else + { + // Local players can establish their UID at this point + PlayerUID localUID; + ProfileManager.GetXUID(0,&localUID,true); + localUID.setForAdhoc(); + + m_RudpCtxToPlayerMap[ rudpCtx ] = new SQRNetworkPlayer( this, SQRNetworkPlayer::SNP_TYPE_LOCAL, false, m_localMemberId, 0, rudpCtx, &localUID ); + m_RudpCtxToIPAddrMap[ rudpCtx ] = m_localIPAddr; +} + + // If we've created a player, then we want to try and patch up any connections that we should have to it + MapRoomSlotPlayers(); + + // TODO - set any non-default options for the context. By default, the context is set to have delivery critical and order critical both on + + // Bind the context to the socket we've just created, and initiate. The initiation needs to happen on both client & host sides of the connection to complete. + ret = sceRudpBind( rudpCtx, m_soc , 1 + 0, SCE_RUDP_MUXMODE_P2P ); + if ( ( ret < 0 ) || ForceErrorPoint(SNM_FORCE_ERROR_RUDP_BIND) ) return false; + if(ret < 0) + app.DebugPrintf(" sceRudpBind failed with error 0x%08x\n"); + + ret = sceRudpInitiate( rudpCtx, &sinp2pPeer, sizeof(sinp2pPeer), 0); + if(ret < 0) + app.DebugPrintf(" sceRudpInitiate failed with error 0x%08x\n"); + if ( ( ret < 0 ) || ForceErrorPoint(SNM_FORCE_ERROR_RUDP_INIT2) ) return false; + + return true; +} + + +SQRNetworkPlayer *SQRNetworkManager_AdHoc_Vita::GetPlayerFromRudpCtx(int rudpCtx) +{ + AUTO_VAR(it,m_RudpCtxToPlayerMap.find(rudpCtx)); + if( it != m_RudpCtxToPlayerMap.end() ) + { + return it->second; + } + return NULL; +} + + +SceNetInAddr* SQRNetworkManager_AdHoc_Vita::GetIPAddrFromRudpCtx(int rudpCtx) +{ + AUTO_VAR(it,m_RudpCtxToIPAddrMap.find(rudpCtx)); + if( it != m_RudpCtxToIPAddrMap.end() ) + { + return &it->second; + } + return NULL; +} + + + +SQRNetworkPlayer *SQRNetworkManager_AdHoc_Vita::GetPlayerFromRoomMemberAndLocalIdx(int roomMember, int localIdx) +{ + for(AUTO_VAR(it, m_RudpCtxToPlayerMap.begin()); it != m_RudpCtxToPlayerMap.end(); it++ ) + { + if( (it->second->m_roomMemberId == roomMember ) && ( it->second->m_localPlayerIdx == localIdx ) ) + { + return it->second; + } + } + return NULL; +} + + +// This is called as part of the general initialisation of the network manager, to register any callbacks that the sony libraries require. +// Returns true if all were registered successfully. +bool SQRNetworkManager_AdHoc_Vita::RegisterCallbacks() +{ + // Register RUDP event handler + int ret = sceRudpSetEventHandler(RudpEventCallback, this); + if (ret < 0) + { + app.DebugPrintf("SQRNetworkManager::RegisterCallbacks - cellRudpSetEventHandler failed with code 0x%08x\n", ret); + return false; + } + + // Register the context callback function +// ret = sceNpMatching2RegisterContextCallback(ContextCallback, this); +// if (ret < 0) +// { +// app.DebugPrintf("SQRNetworkManager::RegisterCallbacks - sceNpMatching2RegisterContextCallback failed with code 0x%08x\n", ret); +// return false; +// } +// +// // Register the default request callback & parameters +// SceNpMatching2RequestOptParam optParam; +// +// memset(&optParam, 0, sizeof(optParam)); +// optParam.cbFunc = DefaultRequestCallback; +// optParam.cbFuncArg = this; +// optParam.timeout = (30 * 1000 * 1000); +// optParam.appReqId = 0; +// +// ret = sceNpMatching2SetDefaultRequestOptParam(m_matchingContext, &optParam); +// if (ret < 0) +// { +// app.DebugPrintf("SQRNetworkManager::RegisterCallbacks - sceNpMatching2SetDefaultRequestOptParam failed with code 0x%08x\n", ret); +// return false; +// } +// +// // Register signalling callback +// ret = sceNpMatching2RegisterSignalingCallback(m_matchingContext, SignallingCallback, this); +// if (ret < 0) +// { +// return false; +// } +// +// // Register room event callback +// ret = sceNpMatching2RegisterRoomEventCallback(m_matchingContext, RoomEventCallback, this); +// if (ret < 0) +// { +// app.DebugPrintf("SQRNetworkManager::RegisterCallbacks - sceNpMatching2RegisterRoomEventCallback failed with code 0x%08x\n", ret); +// return false; +// } +// + return true; +} + +extern bool g_bBootedFromInvite; + + + +void SQRNetworkManager_AdHoc_Vita::HandleMatchingContextStart() +{ + // on the standard networking model this happens during a callback signalled from the context starting + // Some special cases to detect when this event is coming in, in case we had to start the matching context because there wasn't a valid context when we went to get a server context. These two + // responses here complete what should then happen to get the server context in each case (for hosting or joining a game) + if( m_state == SNM_INT_STATE_IDLE_RECREATING_MATCHING_CONTEXT ) + { + SetState( SNM_INT_STATE_IDLE ); + GetExtDataForRoom(0, NULL, NULL, NULL); + } + else if( m_state == SNM_INT_STATE_HOSTING_STARTING_MATCHING_CONTEXT ) + { + // no world matching stuff to setup here, we can just signal that we're ready + SetState(SNM_INT_STATE_HOSTING_WAITING_TO_PLAY); + // Now we know the local member id we can update our local players + SetLocalPlayersAndSync(); +// GetServerContext2(); + } + else if( m_state == SNM_INT_STATE_JOINING_STARTING_MATCHING_CONTEXT ) + { + SetState(SNM_INT_STATE_JOINING_SEARCHING_FOR_SERVER); + SelectRandomServer(); + } + else + { + // Normal handling of context starting, from standard initialisation procedure + assert( m_state == SNM_INT_STATE_STARTING_CONTEXT ); + m_offlineSQR = false; + SetState(SNM_INT_STATE_IDLE); + + if(s_SignInCompleteCallbackFn) + { + s_SignInCompleteCallbackFn(s_SignInCompleteParam, true, 0); + s_SignInCompleteCallbackFn = NULL; + } + } +} + + + + + +// Implementation of SceNpBasicEventHandler +int SQRNetworkManager_AdHoc_Vita::BasicEventCallback(int event, int retCode, uint32_t reqId, void *arg) +{ + PSVITA_STUBBED; + // SQRNetworkManager_AdHoc_Vita *manager = (SQRNetworkManager_AdHoc_Vita *)arg; + // // We aren't allowed to actually get the event directly from this callback, so send our own internal event to a thread dedicated to doing this + // sceKernelTriggerUserEvent(m_basicEventQueue, sc_UserEventHandle, NULL); + + return 0; +} + +// Implementation of SceNpManagerCallback +void SQRNetworkManager_AdHoc_Vita::OnlineCheck() +{ + int state; + int ret = sceNetCtlAdhocGetState(&state); + + + bool bConnected = (state == SCE_NET_CTL_STATE_IPOBTAINED); //ProfileManager.IsSignedInLive(ProfileManager.GetPrimaryPad()); + bool oldAdhocStatus = GetAdhocStatus(); + UpdateAdhocStatus(bConnected); + if(oldAdhocStatus == false) + { + if(bConnected) + { + InitialiseAfterOnline(); + } + } +} + +// Implementation of CellSysutilCallback +void SQRNetworkManager_AdHoc_Vita::SysUtilCallback(uint64_t status, uint64_t param, void *userdata) +{ + // SQRNetworkManager_AdHoc_Vita *manager = (SQRNetworkManager_AdHoc_Vita *)userdata; + // struct CellNetCtlNetStartDialogResult netstart_result; + // int ret = 0; + // netstart_result.size = sizeof(netstart_result); + // switch(status) + // { + // case CELL_SYSUTIL_NET_CTL_NETSTART_FINISHED: + // ret = cellNetCtlNetStartDialogUnloadAsync(&netstart_result); + // if(ret < 0) + // { + // manager->SetState(SNM_INT_STATE_INITIALISE_FAILED); + // if( s_SignInCompleteCallbackFn ) + // { + // if( s_signInCompleteCallbackIfFailed ) + // { + // s_SignInCompleteCallbackFn(s_SignInCompleteParam,false,0); + // } + // s_SignInCompleteCallbackFn = NULL; + // } + // return; + // } + // + // if( netstart_result.result != 0 ) + // { + // // Failed, or user may have decided not to sign in - maybe need to differentiate here + // manager->SetState(SNM_INT_STATE_INITIALISE_FAILED); + // if( s_SignInCompleteCallbackFn ) + // { + // if( s_signInCompleteCallbackIfFailed ) + // { + // s_SignInCompleteCallbackFn(s_SignInCompleteParam,false,0); + // } + // s_SignInCompleteCallbackFn = NULL; + // } + // } + // + // break; + // case CELL_SYSUTIL_NET_CTL_NETSTART_UNLOADED: + // break; + // case CELL_SYSUTIL_NP_INVITATION_SELECTED: + // manager->GetInviteDataAndProcess(SCE_NP_BASIC_SELECTED_INVITATION_DATA); + // break; + // default: + // break; + // } +} + +//CD - Added this to match the SQRNetworkManager_Vita class +void SQRNetworkManager_AdHoc_Vita::updateNetCheckDialog() +{ + if(ProfileManager.IsSystemUIDisplayed()) + { + if( sceNetCheckDialogGetStatus() == SCE_COMMON_DIALOG_STATUS_FINISHED ) + { + //Check for errors + SceNetCheckDialogResult netCheckResult; + int ret = sceNetCheckDialogGetResult(&netCheckResult); + app.DebugPrintf("NetCheckDialogResult = 0x%x\n", netCheckResult.result); + ret = sceNetCheckDialogTerm(); + app.DebugPrintf("NetCheckDialogTerm ret = 0x%x\n", ret); + ProfileManager.SetSysUIShowing( false ); + + app.DebugPrintf("------------>>>>>>>> sceNetCheckDialog finished\n"); + + if( netCheckResult.result == SCE_COMMON_DIALOG_RESULT_OK ) + { + if( s_SignInCompleteCallbackFn ) + { + s_SignInCompleteCallbackFn(s_SignInCompleteParam,true,0); + s_SignInCompleteCallbackFn = NULL; + } + } + else + { + // SCE_COMMON_DIALOG_RESULT_USER_CANCELED + // SCE_COMMON_DIALOG_RESULT_ABORTED + + // Failed, or user may have decided not to sign in - maybe need to differentiate here + SetState(SNM_INT_STATE_INITIALISE_FAILED); + if( s_SignInCompleteCallbackFn ) + { + if( s_signInCompleteCallbackIfFailed ) + { + s_SignInCompleteCallbackFn(s_SignInCompleteParam,false,0); + } + s_SignInCompleteCallbackFn = NULL; + } + } + } + } +} + +// Implementation of CellRudpContextEventHandler. This is associate with an Rudp context every time one is created, and can be used to determine the status of each +// Rudp connection. We create one context/connection per local player on the non-hosting consoles. +void SQRNetworkManager_AdHoc_Vita::RudpContextCallback(int ctx_id, int event_id, int error_code, void *arg) +{ + SQRNetworkManager_AdHoc_Vita *manager = (SQRNetworkManager_AdHoc_Vita *)arg; + switch(event_id) + { + case SCE_RUDP_CONTEXT_EVENT_CLOSED: + { + SQRVoiceConnection* pVoice = NULL; + if(sc_voiceChatEnabled) + SonyVoiceChat_Vita::GetVoiceConnectionFromRudpCtx(ctx_id); + + if(pVoice) + { + pVoice->m_bConnected = false; + } + else + { + app.DebugPrintf(CMinecraftApp::USER_RR,"RUDP closed - event error 0x%x\n",error_code); + if( !manager->m_isHosting ) + { + if( manager->m_state == SNM_INT_STATE_JOINING_WAITING_FOR_LOCAL_PLAYERS ) + { + manager->LeaveRoom(true); + } + } + } + } + break; + case SCE_RUDP_CONTEXT_EVENT_ESTABLISHED: + { + SQRNetworkPlayer *player = manager->GetPlayerFromRudpCtx(ctx_id); + if( player ) + { + // Flag connection stage as being completed for this player + manager->NetworkPlayerConnectionComplete(player); + } + else + { + if(sc_voiceChatEnabled) + { + SonyVoiceChat_Vita::setConnected(ctx_id); + } + } + } + break; + case SCE_RUDP_CONTEXT_EVENT_ERROR: + break; + case SCE_RUDP_CONTEXT_EVENT_WRITABLE: + { + SQRNetworkPlayer *player = manager->GetPlayerFromRudpCtx(ctx_id); + // This event signifies that room has opened up in the write buffer, so attempt to send something + if( player ) + { + player->SendMoreInternal(); + } + else + { + if(sc_voiceChatEnabled) + { + SQRVoiceConnection* pVoice = SonyVoiceChat_Vita::GetVoiceConnectionFromRudpCtx(ctx_id); + assert(pVoice); + } + } + } + break; + case SCE_RUDP_CONTEXT_EVENT_READABLE: + if( manager->m_listener ) + { + SQRVoiceConnection* pVoice = NULL; + if(sc_voiceChatEnabled) + { + SonyVoiceChat_Vita::GetVoiceConnectionFromRudpCtx(ctx_id); + } + + if(pVoice) + { + pVoice->readRemoteData(); + } + else + { + unsigned int dataSize = sceRudpGetSizeReadable(ctx_id); + unsigned char* buffer = (unsigned char*)malloc(dataSize); + unsigned int bytesRead = sceRudpRead( ctx_id, buffer, dataSize, 0, NULL ); + assert(bytesRead == dataSize); + + unsigned char* bufferPos = buffer; + unsigned int dataLeft = dataSize; + + // If we're the host, and this player hasn't yet had its small id confirmed, then the first byte sent to us should be this id + if( manager->m_isHosting ) + { + SQRNetworkPlayer *playerFrom = manager->GetPlayerFromRudpCtx( ctx_id ); + if( playerFrom && !playerFrom->HasSmallIdConfirmed() ) + { + if( dataSize >= sizeof(SQRNetworkPlayer::InitSendData) ) + { + SQRNetworkPlayer::InitSendData ISD; + memcpy(&ISD, bufferPos, sizeof(SQRNetworkPlayer::InitSendData)); + manager->NetworkPlayerInitialDataReceived(playerFrom, &ISD); + dataLeft -= sizeof(SQRNetworkPlayer::InitSendData); + bufferPos += sizeof(SQRNetworkPlayer::InitSendData); + } + else + { + assert(false); + } + } + } + + if( dataLeft > 0 ) + { + SQRNetworkPlayer *playerFrom, *playerTo; + if( manager->m_isHosting ) + { + // Data always going from a remote player, to the host + playerFrom = manager->GetPlayerFromRudpCtx( ctx_id ); + playerTo = manager->m_aRoomSlotPlayers[0]; + } + else + { + // Data always going from host player, to a local player + playerFrom = manager->m_aRoomSlotPlayers[0]; + playerTo = manager->GetPlayerFromRudpCtx( ctx_id ); + } + if( ( playerFrom != NULL ) && ( playerTo != NULL ) ) + { + manager->m_listener->HandleDataReceived( playerFrom, playerTo, bufferPos, dataLeft ); + } + } + + delete buffer; + } + } + break; + case SCE_RUDP_CONTEXT_EVENT_FLUSHED: + break; + } +} + +int SQRNetworkManager_AdHoc_Vita::RudpEventCallback(int event_id, int soc, uint8_t const *data, size_t datalen, struct SceNetSockaddr const *addr, SceNetSocklen_t addrlen, void *arg) +{ + SQRNetworkManager_AdHoc_Vita *manager = (SQRNetworkManager_AdHoc_Vita *)arg; + if( event_id == SCE_RUDP_EVENT_SOCKET_RELEASED ) + { + assert( soc == manager->m_soc ); + sceNetSocketClose(soc); + manager->m_soc = -1; + } + return 0; +} + +void SQRNetworkManager_AdHoc_Vita::NetCtlCallback(int eventType, void *arg) +{ + SQRNetworkManager_AdHoc_Vita *manager = (SQRNetworkManager_AdHoc_Vita *)arg; + // Oddly, the disconnect event comes in with a new state of "CELL_NET_CTL_STATE_Connecting"... looks like the event is more important than the state to + // determine what has just happened + if( eventType == SCE_NET_CTL_EVENT_TYPE_DISCONNECTED)// CELL_NET_CTL_EVENT_LINK_DISCONNECTED ) + { + manager->m_bLinkDisconnected = true; + manager->m_listener->HandleDisconnect(false); + } + else //if( event == CELL_NET_CTL_EVENT_ESTABLISH ) + { + manager->m_bLinkDisconnected = false; + } + +} + +// Called when the context has been created, and we are intending to create a room. +void SQRNetworkManager_AdHoc_Vita::ServerContextValid_CreateRoom() +{ + // First find a world + SetState(SNM_INT_STATE_HOSTING_CREATE_ROOM_SEARCHING_FOR_WORLD); + + SceNpMatching2GetWorldInfoListRequest reqParam; + + // Request parameters + memset(&reqParam, 0, sizeof(reqParam)); + reqParam.serverId = m_serverId; + + int ret = -1; + if( !ForceErrorPoint(SNM_FORCE_ERROR_GET_WORLD_INFO_LIST) ) + { + ret = sceNpMatching2GetWorldInfoList( m_matchingContext, &reqParam, NULL, &m_getWorldRequestId); + } + if (ret < 0) + { + SetState(SNM_INT_STATE_HOSTING_CREATE_ROOM_FAILED); + return; + } +} + +// Called when the context has been created, and we are intending to join a pre-existing room. +void SQRNetworkManager_AdHoc_Vita::ServerContextValid_JoinRoom() +{ + // assert( m_state == SNM_INT_STATE_JOINING_SERVER_SEARCH_CREATING_CONTEXT ); + + SetState(SNM_INT_STATE_JOINING_JOIN_ROOM); + + // Join the room, passing the local player mask as initial binary data so that the host knows what local players are here + SceNpMatching2JoinRoomRequest reqParam; + SceNpMatching2BinAttr binAttr; + memset(&reqParam, 0, sizeof(reqParam)); + memset(&binAttr, 0, sizeof(binAttr)); + binAttr.id = SCE_NP_MATCHING2_ROOMMEMBER_BIN_ATTR_INTERNAL_1_ID; + binAttr.ptr = &m_localPlayerJoinMask; + binAttr.size = sizeof(m_localPlayerJoinMask); + + reqParam.roomId = m_roomToJoin; + reqParam.roomMemberBinAttrInternalNum = 1; + reqParam.roomMemberBinAttrInternal = &binAttr; + + int ret = sceNpMatching2JoinRoom( m_matchingContext, &reqParam, NULL, &m_joinRoomRequestId ); + if ( (ret < 0) || ForceErrorPoint(SNM_FORCE_ERROR_JOIN_ROOM) ) + { + if( ret == SCE_NP_MATCHING2_SERVER_ERROR_NAT_TYPE_MISMATCH) + { + app.SetDisconnectReason( DisconnectPacket::eDisconnect_NATMismatch ); + } + SetState(SNM_INT_STATE_JOINING_JOIN_ROOM_FAILED); + } +} + +const SceNpCommunicationId* SQRNetworkManager_AdHoc_Vita::GetSceNpCommsId() +{ + return &s_npCommunicationId; +} + +const SceNpCommunicationSignature* SQRNetworkManager_AdHoc_Vita::GetSceNpCommsSig() +{ + return &s_npCommunicationSignature; +} + +const SceNpTitleId* SQRNetworkManager_AdHoc_Vita::GetSceNpTitleId() +{ + PSVITA_STUBBED; + return NULL; +// return &s_npTitleId; +} + +const SceNpTitleSecret* SQRNetworkManager_AdHoc_Vita::GetSceNpTitleSecret() +{ + PSVITA_STUBBED; + return NULL; +// return &s_npTitleSecret; +} + +int SQRNetworkManager_AdHoc_Vita::GetOldMask(SceNpMatching2RoomMemberId memberId) +{ + int oldMask = 0; + for( int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( m_roomSyncData.players[i].m_roomMemberId == memberId ) + { + oldMask |= (1 << m_roomSyncData.players[i].m_localIdx); + } + } + return oldMask; +} + +int SQRNetworkManager_AdHoc_Vita::GetAddedMask(int newMask, int oldMask) +{ + return newMask & ~oldMask; +} + +int SQRNetworkManager_AdHoc_Vita::GetRemovedMask(int newMask, int oldMask) +{ + return oldMask & ~newMask; +} + + +void SQRNetworkManager_AdHoc_Vita::GetExtDataForRoom( SceNpMatching2RoomId roomId, void *extData, void (* FriendSessionUpdatedFn)(bool success, void *pParam), void *pParam ) +{ + + for(int i=0;i>>>>>>> sceNetCheckDialogInit : Adhoc Mode\n"); + + if( ret < 0 ) + { + if(s_SignInCompleteCallbackFn) // MGH - added after crash on PS4 + { + if( s_signInCompleteCallbackIfFailed ) + { + s_SignInCompleteCallbackFn(s_SignInCompleteParam,false,0); + } + s_SignInCompleteCallbackFn = NULL; + } + } +} + +int SQRNetworkManager_AdHoc_Vita::SetRichPresence(const void *data) +{ + const sce::Toolkit::NP::PresenceDetails *newPresenceInfo = (const sce::Toolkit::NP::PresenceDetails *)data; + + s_lastPresenceInfo.status = newPresenceInfo->status; +// s_lastPresenceInfo.userInfo = newPresenceInfo->userInfo; + s_lastPresenceInfo.presenceType = SCE_NP_BASIC_IN_GAME_PRESENCE_TYPE_DEFAULT; + + s_presenceStatusDirty = true; + SendLastPresenceInfo(); + + // Return as if no error happened no matter what, as we'll be resending ourselves if we need to and don't want the calling system to retry + return 0; +} + +void SQRNetworkManager_AdHoc_Vita::UpdateRichPresenceCustomData(void *data, unsigned int dataBytes) +{ + if(m_isHosting == false) + return; + if(m_matchingContextServerValid) // if the wifi connection has dropped (say after the console has woken from sleep) this can be invalid + { + int err = sceNetAdhocMatchingSetHelloOpt(m_matchingContext, dataBytes, data); + assert(err == SCE_OK); + } + else + { + app.DebugPrintf("UpdateRichPresenceCustomData failed, m_matchingContextServerValid == false\n"); + } + +// C +// +// assert(dataBytes <= SCE_NP_BASIC_IN_GAME_PRESENCE_DATA_SIZE_MAX ); +// memcpy(s_lastPresenceInfo.data, data, dataBytes); +// s_lastPresenceInfo.size = dataBytes; +// +// s_presenceDataDirty = true; +// SendLastPresenceInfo(); +} + +void SQRNetworkManager_AdHoc_Vita::TickRichPresence() +{ + if( s_resendPresenceCountdown ) + { + s_resendPresenceCountdown--; + if( s_resendPresenceCountdown == 0 ) + { + SendLastPresenceInfo(); + } + } +} + +void SQRNetworkManager_AdHoc_Vita::SendLastPresenceInfo() +{ + // Don't attempt to send if we are already waiting to resend + if( s_resendPresenceCountdown ) return; + + // MGH - On Vita, change this to use SCE_NP_BASIC_IN_GAME_PRESENCE_TYPE_GAME_JOINING at some point + + // On PS4 we can't set the status and the data at the same time +// if( s_presenceDataDirty ) +// { +// s_presenceDataDirty = false; +// s_lastPresenceInfo.presenceType = SCE_TOOLKIT_NP_PRESENCE_DATA; +// } +// else if( s_presenceStatusDirty ) +// { +// s_presenceStatusDirty = false; +// s_lastPresenceInfo.presenceType = SCE_TOOLKIT_NP_PRESENCE_STATUS; +// } + + int err = 0; + // check if we're connected to the PSN first + if(ProfileManager.IsSignedInLive(0))//ProfileManager.getQuadrant(s_lastPresenceInfo.userInfo.userId))) + { + err = sce::Toolkit::NP::Presence::Interface::setPresence(&s_lastPresenceInfo); + } + + if( err != SCE_TOOLKIT_NP_SUCCESS ) + { + assert(0); // this should only happen for bad data + } +} + +void SQRNetworkManager_AdHoc_Vita::SetPresenceDataStartHostingGame() +{ + if( m_offlineGame ) + { + SQRNetworkManager_AdHoc_Vita::UpdateRichPresenceCustomData(&c_presenceSyncInfoNULL, sizeof(HelloSyncInfo) ); + } + else + { + HelloSyncInfo presenceInfo; + CPlatformNetworkManagerSony::SetSQRPresenceInfoFromExtData( &presenceInfo.m_presenceSyncInfo, m_joinExtData, m_room, m_serverId ); + assert(m_joinExtDataSize == sizeof(GameSessionData)); + memcpy(&presenceInfo.m_gameSessionData, m_joinExtData, sizeof(GameSessionData)); + SQRNetworkManager_AdHoc_Vita::UpdateRichPresenceCustomData(&presenceInfo, sizeof(HelloSyncInfo) ); + // OrbisNPToolkit::createNPSession(); + } +} + +int SQRNetworkManager_AdHoc_Vita::GetJoiningReadyPercentage() +{ + if ( (m_state == SNM_INT_STATE_HOSTING_SEARCHING_FOR_SERVER) || (m_state == SNM_INT_STATE_JOINING_SEARCHING_FOR_SERVER) ) + { + int completed = ( m_totalServerCount - m_serverCount ) - 1; + int pc = ( completed * 100 ) / m_totalServerCount; + if( pc < 0 ) pc = 0; + if( pc > 100 ) pc = 100; + return pc; + } + else + { + return 100; + } +} + +void SQRNetworkManager_AdHoc_Vita::removePlayerFromVoiceChat( SQRNetworkPlayer* pPlayer ) +{ + if(!sc_voiceChatEnabled) + return; + + + if(pPlayer->IsLocal()) + { + SonyVoiceChat_Vita::disconnectLocalPlayer(pPlayer->GetLocalPlayerIndex()); + } + else + { + int numRemotePlayersLeft = 0; + for( int i = 0; i < MAX_ONLINE_PLAYER_COUNT; i++ ) + { + if( m_aRoomSlotPlayers[i] ) + { + if( m_aRoomSlotPlayers[i] != pPlayer ) + { + if(m_aRoomSlotPlayers[i]->m_roomMemberId == pPlayer->m_roomMemberId) + numRemotePlayersLeft++; + } + } + } + if(numRemotePlayersLeft == 0) + { + // no players left on the remote machine once we remove this one + SQRVoiceConnection* pVoice = SonyVoiceChat_Vita::getVoiceConnectionFromRoomMemberID(pPlayer->m_roomMemberId); + assert(pVoice); + if(pVoice) + SonyVoiceChat_Vita::disconnectRemoteConnection(pVoice); + } + } +} + +int SQRNetworkManager_AdHoc_Vita::sendDataPacket( SceNetInAddr addr, EAdhocDataTag tag, void* data, int dataSize ) +{ + static unsigned char s_dataBuffer[SCE_NET_ADHOC_MATCHING_MAXDATALEN]; + uint32_t* buf = (uint32_t*)s_dataBuffer; + buf[0] = tag; + memcpy(&buf[1], data, dataSize); + int ret = sceNetAdhocMatchingSendData(m_matchingContext, &addr, dataSize+4, s_dataBuffer); +// assert(ret == SCE_OK); + return ret; +} + +int SQRNetworkManager_AdHoc_Vita::sendDataPacket( SceNetInAddr addr, void* data, int dataSize ) +{ + int ret = sceNetAdhocMatchingSendData(m_matchingContext, &addr, dataSize, data); + // assert(ret == SCE_OK); + return ret; +} + +void SQRNetworkManager_AdHoc_Vita::startMatching() +{ + SetState(SNM_INT_STATE_STARTING_CONTEXT); + CreateMatchingContext(); +} + +SQRNetworkPlayer *SQRNetworkManager_AdHoc_Vita::GetPlayerByXuid(PlayerUID xuid) +{ + EnterCriticalSection(&m_csRoomSyncData); + for( int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( m_roomSyncData.players[i].m_UID == xuid ) + { + SQRNetworkPlayer *player = GetPlayerIfReady(m_aRoomSlotPlayers[i]); + LeaveCriticalSection(&m_csRoomSyncData); + return player; + } + } + LeaveCriticalSection(&m_csRoomSyncData); + return NULL; +} + +void SQRNetworkManager_AdHoc_Vita::UpdateLocalIPAddress() +{ + SceNetInAddr localIPAddr; + int ret = sceNetCtlAdhocGetInAddr(&localIPAddr); + + // If we got the IP address, update our cached value + if (ret == SCE_OK) + { + m_localIPAddr = localIPAddr; + } +} \ No newline at end of file diff --git a/Minecraft.Client/PSVita/Network/SQRNetworkManager_AdHoc_Vita.h b/Minecraft.Client/PSVita/Network/SQRNetworkManager_AdHoc_Vita.h new file mode 100644 index 00000000..7d9948c9 --- /dev/null +++ b/Minecraft.Client/PSVita/Network/SQRNetworkManager_AdHoc_Vita.h @@ -0,0 +1,352 @@ +#pragma once +#include +#include +#include +#include + +#include + +#include + +#include "..\..\Common\Network\Sony\SQRNetworkManager.h" +// +class SQRNetworkPlayer; +class ISQRNetworkManagerListener; +class SonyVoiceChat_Vita; +class SQRVoiceConnection; +class C4JThread; + + +class HelloSyncInfo; + +enum EAdhocDataTag +{ + e_dataTag_Normal, + e_dataTag_RoomSync +}; + +class AdhocDataPacket +{ +public: + EAdhocDataTag m_tag; + uint32_t m_pData[1]; +}; + +// This is the lowest level manager for providing network functionality on Sony platforms. This manages various network activities including the players within a gaming session. +// The game shouldn't directly use this class, it is here to provide functionality required by PlatformNetworkManagerSony. + +class SQRNetworkManager_AdHoc_Vita : public SQRNetworkManager +{ + friend class SonyVoiceChat_Vita; + friend class SQRNetworkPlayer; + + static const eSQRNetworkManagerState m_INTtoEXTStateMappings[SNM_INT_STATE_COUNT]; + + + +public: + SQRNetworkManager_AdHoc_Vita(ISQRNetworkManagerListener *listener); + + // General + void Tick(); + void Initialise(); + bool IsInitialised(); + void UnInitialise(); + void Terminate(); + eSQRNetworkManagerState GetState(); + bool IsHost(); + bool IsReadyToPlayOrIdle(); + bool IsInSession(); + + // Session management + void CreateAndJoinRoom(int hostIndex, int localPlayerMask, void *extData, int extDataSize, bool offline); + void UpdateExternalRoomData(); + bool FriendRoomManagerIsBusy(); + bool FriendRoomManagerSearch(); + bool FriendRoomManagerSearch2(); + int FriendRoomManagerGetCount(); + void FriendRoomManagerGetRoomInfo(int idx, SessionSearchResult *searchResult); + bool JoinRoom(SessionSearchResult *searchResult, int localPlayerMask); + bool JoinRoom(SceNetInAddr netAddr, int localPlayerMask, const HelloSyncInfo *presence); + bool JoinRoom(SceNpMatching2RoomId roomId, SceNpMatching2ServerId serverId, int localPlayerMask, const PresenceSyncInfo *presence); + void StartGame(); + void LeaveRoom(bool bActuallyLeaveRoom); + void EndGame(); + bool SessionHasSpace(int spaceRequired); + bool AddLocalPlayerByUserIndex(int idx); + bool RemoveLocalPlayerByUserIndex(int idx); + void SendInviteGUI(); + static void RecvInviteGUI(); + void TickInviteGUI(); + + + + // void GetInviteDataAndProcess(SceNpBasicAttachmentDataId id); + static bool UpdateInviteData(HelloSyncInfo *invite); + void GetExtDataForRoom( SceNpMatching2RoomId roomId, void *extData, void (* FriendSessionUpdatedFn)(bool success, void *pParam), void *pParam ); + + // Player retrieval + int GetPlayerCount(); + int GetOnlinePlayerCount(); + SQRNetworkPlayer *GetPlayerByIndex(int idx); + SQRNetworkPlayer *GetPlayerBySmallId(int idx); + SQRNetworkPlayer *GetLocalPlayerByUserIndex(int idx); + SQRNetworkPlayer *GetPlayerByXuid(PlayerUID xuid); + SQRNetworkPlayer *GetHostPlayer(); + + void removePlayerFromVoiceChat(SQRNetworkPlayer* pPlayer); + // Communication parameter storage + static const SceNpCommunicationId* GetSceNpCommsId(); + static const SceNpCommunicationSignature* GetSceNpCommsSig(); + static const SceNpTitleId* GetSceNpTitleId(); + static const SceNpTitleSecret* GetSceNpTitleSecret(); + + static void GetInviteDataAndProcess(sce::Toolkit::NP::MessageAttachment* pInvite); + static bool GetAdhocStatus() { return m_adhocStatus; } + + + int sendDataPacket(SceNetInAddr addr, EAdhocDataTag tag, void* data, int dataSize); + int sendDataPacket(SceNetInAddr addr, void* data, int dataSize); + +private: + void InitialiseAfterOnline(); + void ErrorHandlingTick(); + void UpdateAdhocStatus(int status) { m_adhocStatus = status; } + void UpdateLocalIPAddress(); + + ISQRNetworkManagerListener *m_listener; + SQRNetworkPlayer *GetPlayerIfReady(SQRNetworkPlayer *player); + + // Internal state + void SetState(eSQRNetworkManagerInternalState state); + void ResetToIdle(); + eSQRNetworkManagerInternalState m_state; + eSQRNetworkManagerState m_stateExternal; + bool m_nextIdleReasonIsFull; + bool m_isHosting; + SceNetInAddr m_localIPAddr; + SceNpMatching2RoomMemberId m_localMemberId; + SceNpMatching2RoomMemberId m_hostMemberId; // if we're not the host + int m_localPlayerCount; + int m_localPlayerJoined; // Client only, keep a count of how many local players we have confirmed as joined to the application + SceNpMatching2RoomId m_room; + unsigned char m_currentSmallId; + int m_soc; + bool m_offlineGame; + bool m_offlineSQR; + int m_resendExternalRoomDataCountdown; + bool m_matching2initialised; +// HelloSyncInfo m_inviteReceived[MAX_SIMULTANEOUS_INVITES]; +// int m_inviteIndex; +// static HelloSyncInfo *m_gameBootInvite; +// static HelloSyncInfo m_gameBootInvite_data; +// bool m_doBootInviteCheck; + bool m_isInSession; + // static SceNpBasicAttachmentDataId s_lastInviteIdToRetry; + static int m_adhocStatus; + bool m_bLinkDisconnected; + +private: + + CRITICAL_SECTION m_csRoomSyncData; + RoomSyncData m_roomSyncData; + void *m_joinExtData; + int m_joinExtDataSize; + + std::vector m_vecTempPlayers; + SQRNetworkPlayer *m_aRoomSlotPlayers[MAX_ONLINE_PLAYER_COUNT]; // Maps from the players in m_roomSyncData, to SQRNetworkPlayers + void FindOrCreateNonNetworkPlayer(int slot, int playerType, SceNpMatching2RoomMemberId memberId, int localPlayerIdx, int smallId); + + void MapRoomSlotPlayers(int roomSlotPlayerCount =-1); + void UpdateRoomSyncUIDsFromPlayers(); + void UpdatePlayersFromRoomSyncUIDs(); + void LocalDataSend(SQRNetworkPlayer *playerFrom, SQRNetworkPlayer *playerTo, const void *data, unsigned int dataSize); + int GetSessionIndex(SQRNetworkPlayer *player); + + bool AddRemotePlayersAndSync( SceNpMatching2RoomMemberId memberId, int playerMask, bool *isFull = NULL ); + void RemoveRemotePlayersAndSync( SceNpMatching2RoomMemberId memberId, int mask ); + void RemoveNetworkPlayers( int mask ); + void SetLocalPlayersAndSync(); + void SyncRoomData(); + SceNpMatching2RequestId m_setRoomDataRequestId; + SceNpMatching2RequestId m_setRoomIntDataRequestId; + SceNpMatching2RequestId m_roomExtDataRequestId; + + // Server context management + bool GetMatchingContext(eSQRNetworkManagerInternalState asyncState); + bool GetServerContext(); + bool GetServerContext_AdHoc(); + bool GetServerContext2(); + bool GetServerContext(SceNpMatching2ServerId serverId); + void DeleteServerContext(); + bool SelectRandomServer(); + void ServerContextTick(); + int m_totalServerCount; + int m_serverCount; + SceNpMatching2ServerId *m_aServerId; + SceNpMatching2ServerId m_serverId; + bool m_serverContextValid; + SceNpMatching2RequestId m_serverSearchRequestId; + SceNpMatching2RequestId m_serverContextRequestId; + + // Room creation management + SceNpMatching2RequestId m_getWorldRequestId; + SceNpMatching2RequestId m_createRoomRequestId; + SceNpMatching2WorldId m_worldId; + void RoomCreateTick(); + + // Room joining management + SceNpMatching2RoomId m_roomToJoin; + int m_localPlayerJoinMask; + SceNpMatching2RequestId m_joinRoomRequestId; + SceNpMatching2RequestId m_kickRequestId; + + // Room leaving management + SceNpMatching2RequestId m_leaveRoomRequestId; + + // Adding extra network players management + SceNpMatching2RequestId m_setRoomMemberInternalDataRequestId; + + // Player state management + void NetworkPlayerConnectionComplete(SQRNetworkPlayer *player); + void NetworkPlayerSmallIdAllocated(SQRNetworkPlayer *player, unsigned char smallId); + void NetworkPlayerInitialDataReceived(SQRNetworkPlayer *player,void *data); + void NonNetworkPlayerComplete(SQRNetworkPlayer *player, unsigned char smallId); + void HandlePlayerJoined(SQRNetworkPlayer *player); + CRITICAL_SECTION m_csPlayerState; + + // State and thread for managing basic event type messages + C4JThread *m_basicEventThread; +// SceKernelEqueue m_basicEventQueue; + static int BasicEventThreadProc( void *lpParameter); + + // State and storage for managing search for friends' games + eSQRNetworkManagerFriendSearchState m_friendSearchState; + SceNpMatching2ContextId m_matchingContext; + bool m_matchingContextServerValid; + bool m_matchingContextClientValid; + SceNpMatching2RequestId m_friendSearchRequestId; + unsigned int m_friendCount; +// C4JThread *m_getFriendCountThread; +// static int GetFriendsThreadProc( void* lpParameter ); + void FriendSearchTick(); + SceNpMatching2RequestId m_roomDataExternalListRequestId; + void (* m_FriendSessionUpdatedFn)(bool success, void *pParam); + void *m_pParamFriendSessionUpdated; + void *m_pExtDataToUpdate; + + // Results from searching for rooms that friends are playing in - 5 matched arrays to store their NpIds, rooms, servers, whether a room was found, and whether the external data had been received for the room. Also a count of how many elements are used in this array. + class FriendSearchResult + { + public: + SceNpId m_NpId; + SceNetInAddr m_netAddr; +// SceNpMatching2RoomId m_RoomId; +// SceNpMatching2ServerId m_ServerId; + bool m_RoomFound; + void *m_RoomExtDataReceived; + void* m_gameSessionData; + RoomSyncData m_roomSyncData; + }; + std::vector m_aFriendSearchResults; + bool m_bFriendsSearchChanged; + + // Rudp management and local players + std::unordered_map m_RudpCtxToPlayerMap; + std::unordered_map m_RudpCtxToIPAddrMap; + + std::unordered_map m_NetAddrToVoiceConnectionMap; + + bool CreateRudpConnections(SceNetInAddr peer); + bool CreateVoiceRudpConnections(SceNpMatching2RoomId roomId, SceNpMatching2RoomMemberId peerMemberId, int playerMask); + bool CreateSocket(); + SQRNetworkPlayer *GetPlayerFromRudpCtx(int rudpCtx); + SceNetInAddr* GetIPAddrFromRudpCtx(int rudpCtx); + SQRVoiceConnection* GetVoiceConnectionFromRudpCtx(int rudpCtx); + + SQRNetworkPlayer *GetPlayerFromRoomMemberAndLocalIdx(int roomMember, int localIdx); + SceNpMatching2RequestId m_roomMemberDataRequestId; + + // Callbacks (for matching) + bool RegisterCallbacks(); + void HandleMatchingContextStart(); + // #ifdef __PS3__ + // static void DefaultRequestCallback(SceNpMatching2ContextId id, SceNpMatching2RequestId reqId, SceNpMatching2Event event, SceNpMatching2EventKey eventKey, int errorCode, size_t dataSize, void *arg); + // static void RoomEventCallback(SceNpMatching2ContextId id, SceNpMatching2RoomId roomId, SceNpMatching2Event event, SceNpMatching2EventKey eventKey, int errorCode, size_t dataSize, void *arg); + // #else +// static void DefaultRequestCallback(SceNpMatching2ContextId id, SceNpMatching2RequestId reqId, SceNpMatching2Event event, int errorCode, const void *data, void *arg); +// static void RoomEventCallback(SceNpMatching2ContextId id, SceNpMatching2RoomId roomId, SceNpMatching2Event event, const void *data, void *arg); + // #endif +// static void SignallingCallback(SceNpMatching2ContextId ctxId, SceNpMatching2RoomId roomId, SceNpMatching2RoomMemberId peerMemberId, SceNpMatching2Event event, int error_code, void *arg); + + // Callback for NpBasic + static int BasicEventCallback(int event, int retCode, uint32_t reqId, void *arg); + + // Callback for NpManager + static void ManagerCallback(int event, int result, void *arg); + + // Callback for sys util + static void SysUtilCallback(uint64_t status, uint64_t param, void *userdata); + void updateNetCheckDialog(); // get the status of the dialog and run any callbacks needed [CD - Added to match SQRNetworkManager_Vita] + + // Callbacks for rudp + static void RudpContextCallback(int ctx_id, int event_id, int error_code, void *arg); + static int RudpEventCallback(int event_id, int soc, uint8_t const *data, size_t datalen, struct SceNetSockaddr const *addr, SceNetSocklen_t addrlen, void *arg); + + // Callback for netctl + static void NetCtlCallback(int eventType, void *arg); + + // Methods to be called when the server context has been created + void ServerContextValid_CreateRoom(); + void ServerContextValid_JoinRoom(); + + // Mask utilities + int GetOldMask(SceNpMatching2RoomMemberId memberId); + int GetAddedMask(int newMask, int oldMask); + int GetRemovedMask(int newMask, int oldMask); + +#ifndef _CONTENT_PACKAGE + static bool aForceError[SNM_FORCE_ERROR_COUNT]; +#endif + bool ForceErrorPoint(eSQRForceError err); + + + static void MatchingEventHandler(int id, int event, SceNetInAddr* peer, int optlen, void *opt); + + +public: + static void AttemptPSNSignIn(int (*SignInCompleteCallbackFn)(void *pParam, bool bContinue, int pad), void *pParam, bool callIfFailed = false); + static int (*s_SignInCompleteCallbackFn)(void *pParam, bool bContinue, int pad); + static bool s_signInCompleteCallbackIfFailed; + static void *s_SignInCompleteParam; + + static int SetRichPresence(const void *data); + void SetPresenceDataStartHostingGame(); + int GetJoiningReadyPercentage(); + + void startMatching(); +private: + void UpdateRichPresenceCustomData(void *data, unsigned int dataBytes); + static void TickRichPresence(); + static void SendLastPresenceInfo(); + void OnlineCheck(); + + bool CreateMatchingContext(bool bServer = false); + void StopMatchingContext(); + + + static sce::Toolkit::NP::PresenceDetails s_lastPresenceInfo; + static int s_resendPresenceCountdown; + static bool s_presenceStatusDirty; + static bool s_presenceDataDirty; + static HelloSyncInfo s_lastPresenceSyncInfo; + static HelloSyncInfo c_presenceSyncInfoNULL; + static bool b_inviteRecvGUIRunning; + // Debug + static long long s_roomStartTime; + + int m_hid; + bool m_bIsInitialised; + +}; + diff --git a/Minecraft.Client/PSVita/Network/SQRNetworkManager_Vita.cpp b/Minecraft.Client/PSVita/Network/SQRNetworkManager_Vita.cpp new file mode 100644 index 00000000..6a4e2c72 --- /dev/null +++ b/Minecraft.Client/PSVita/Network/SQRNetworkManager_Vita.cpp @@ -0,0 +1,4080 @@ +#include "stdafx.h" +#include "SQRNetworkManager_Vita.h" +#include "SonyVoiceChat_Vita.h" +#include "Common/Network/Sony/PlatformNetworkManagerSony.h" + +#include +#include +#include +#include +#include + +#include "PSVita\PSVitaExtras\Conf.h" +#include "Common\Network\Sony\SonyHttp.h" +#include "..\..\..\Minecraft.World\C4JThread.h" + +// image used for the invite gui, filesize must be smaller than SCE_NP_MESSAGE_DIALOG_MAX_INDEX_ICON_SIZE ( 64K ) +#define SESSION_IMAGE_PATH "app0:PSVita/session_image.png" + +int (* SQRNetworkManager_Vita::s_SignInCompleteCallbackFn)(void *pParam, bool bContinue, int pad) = NULL; +void * SQRNetworkManager_Vita::s_SignInCompleteParam = NULL; +sce::Toolkit::NP::PresenceDetails SQRNetworkManager_Vita::s_lastPresenceInfo; +int SQRNetworkManager_Vita::s_resendPresenceCountdown = 0; +bool SQRNetworkManager_Vita::s_presenceStatusDirty = false; +bool SQRNetworkManager_Vita::s_signInCompleteCallbackIfFailed = false; +SQRNetworkManager_Vita::PresenceSyncInfo SQRNetworkManager_Vita::s_lastPresenceSyncInfo = { 0 }; +SQRNetworkManager_Vita::PresenceSyncInfo SQRNetworkManager_Vita::c_presenceSyncInfoNULL = { 0 }; +//SceNpBasicAttachmentDataId SQRNetworkManager_Vita::s_lastInviteIdToRetry = SCE_NP_BASIC_INVALID_ATTACHMENT_DATA_ID; +long long SQRNetworkManager_Vita::s_roomStartTime = 0; +bool SQRNetworkManager_Vita::b_inviteRecvGUIRunning = false; +SQRNetworkManager_Vita::PresenceSyncInfo* SQRNetworkManager_Vita::m_gameBootInvite; +SQRNetworkManager_Vita::PresenceSyncInfo SQRNetworkManager_Vita::m_gameBootInvite_data; +bool SQRNetworkManager_Vita::m_bCallPSNSignInCallback=false; +bool SQRNetworkManager_Vita::m_bJoinablePresenceWaitingForOnline = false; +SceAppUtilNpBasicJoinablePresenceParam SQRNetworkManager_Vita::m_joinablePresenceParam; +bool SQRNetworkManager_Vita::m_bSendingInviteMessage; + +// static const int sc_UserEventHandle = 0; + +//unsigned int SQRNetworkManager_Vita::RoomSyncData::playerCount = 0; + +// This maps internal to extern states, and needs to match element-by-element the eSQRNetworkManagerInternalState enumerated type +const SQRNetworkManager_Vita::eSQRNetworkManagerState SQRNetworkManager_Vita::m_INTtoEXTStateMappings[SQRNetworkManager_Vita::SNM_INT_STATE_COUNT] = +{ + SNM_STATE_INITIALISING, // SNM_INT_STATE_UNINITIALISED + SNM_STATE_INITIALISING, // SNM_INT_STATE_SIGNING_IN + SNM_STATE_INITIALISING, // SNM_INT_STATE_STARTING_CONTEXT + SNM_STATE_INITIALISE_FAILED, // SNM_INT_STATE_INITIALISE_FAILED + SNM_STATE_IDLE, // SNM_INT_STATE_IDLE + SNM_STATE_IDLE, // SNM_INT_STATE_IDLE_RECREATING_MATCHING_CONTEXT + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_STARTING_MATCHING_CONTEXT + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_SEARCHING_FOR_SERVER + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_SERVER_SEARCH_SERVER_ERROR + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_SERVER_FOUND + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_SERVER_SEARCH_CREATING_CONTEXT + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_SERVER_SEARCH_FAILED + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_CREATE_ROOM_SEARCHING_FOR_WORLD + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_CREATE_ROOM_WORLD_FOUND + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_CREATE_ROOM_CREATING_ROOM + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_CREATE_ROOM_SUCCESS + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_CREATE_ROOM_FAILED + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_CREATE_ROOM_RESTART_MATCHING_CONTEXT + SNM_STATE_HOSTING, // SNM_INT_STATE_HOSTING_WAITING_TO_PLAY + SNM_STATE_JOINING, // SNM_INT_STATE_JOINING_STARTING_MATCHING_CONTEXT + SNM_STATE_JOINING, // SNM_INT_STATE_JOINING_SEARCHING_FOR_SERVER + SNM_STATE_JOINING, // SNM_INT_STATE_JOINING_SERVER_SEARCH_SERVER_ERROR + SNM_STATE_JOINING, // SNM_INT_STATE_JOINING_SERVER_FOUND + SNM_STATE_JOINING, // SNM_INT_STATE_JOINING_SERVER_SEARCH_CREATING_CONTEXT + SNM_STATE_JOINING, // SNM_INT_STATE_JOINING_SERVER_SEARCH_FAILED + SNM_STATE_JOINING, // SNM_INT_STATE_JOINING_JOIN_ROOM + SNM_STATE_JOINING, // SNM_INT_STATE_JOINING_JOIN_ROOM_FAILED + SNM_STATE_JOINING, // SNM_INT_STATE_JOINING_WAITING_FOR_LOCAL_PLAYERS + SNM_STATE_ENDING, // SNM_INT_STATE_SERVER_DELETING_CONTEXT + SNM_STATE_STARTING, // SNM_INT_STATE_STARTING + SNM_STATE_PLAYING, // SNM_INT_STATE_PLAYING + SNM_STATE_LEAVING, // SNM_INT_STATE_LEAVING + SNM_STATE_LEAVING, // SNM_INT_STATE_LEAVING_FAILED + SNM_STATE_ENDING, // SNM_INT_STATE_ENDING +}; + +SQRNetworkManager_Vita::SQRNetworkManager_Vita(ISQRNetworkManagerListener *listener) +{ + m_state = SNM_INT_STATE_UNINITIALISED; + m_stateExternal = SNM_STATE_INITIALISING; + m_nextIdleReasonIsFull = false; + m_friendSearchState = SNM_FRIEND_SEARCH_STATE_IDLE; + m_serverContextValid = false; + m_isHosting = false; + m_currentSmallId = 0; + memset( m_aRoomSlotPlayers, 0, sizeof(m_aRoomSlotPlayers) ); + m_listener = listener; + m_soc = -1; + m_resendExternalRoomDataCountdown = 0; + m_matching2initialised = false; + m_matchingContextValid = false; + m_inviteIndex = 0; + m_doBootInviteCheck = true; + m_bSendingInviteMessage = false; + m_isInSession = false; + m_offlineGame = false; + m_offlineSQR = false; + m_aServerId = NULL; + m_gameBootInvite = NULL; + m_onlineStatus = false; + m_bLinkDisconnected = false; + m_bShuttingDown = false; + + InitializeCriticalSection(&m_csRoomSyncData); + InitializeCriticalSection(&m_csPlayerState); + InitializeCriticalSection(&m_csStateChangeQueue); + InitializeCriticalSection(&m_csMatching); + + memset( &m_roomSyncData,0,sizeof(m_roomSyncData)); // MGH - added to fix problem when joining a full room, and the sync data wasn't populated + + // int ret = sceKernelCreateEqueue(&m_basicEventQueue, "SQRNetworkManager_Vita EQ"); + // assert(ret == SCE_OK); + // ret = sceKernelAddUserEvent(m_basicEventQueue, sc_UserEventHandle); + // assert(ret == SCE_OK); + // + // m_basicEventThread = new C4JThread(&BasicEventThreadProc,this,"Basic Event Handler"); + // m_basicEventThread->Run(); +} + +// First stage of initialisation. This initialises a few things that don't require the user to be signed in, and then kicks of the network start dialog utility. +// Initialisation continues in InitialiseAfterOnline once this completes. +void SQRNetworkManager_Vita::Initialise() +{ +#define NP_IN_GAME_MESSAGE_POOL_SIZE ( 16 * 1024 ) + m_bShuttingDown = false; + int32_t ret = 0; + // int32_t libCtxId = 0; + // ret = sceNpInGameMessageInitialize(NP_IN_GAME_MESSAGE_POOL_SIZE, NULL); + // assert (ret >= 0); + // libCtxId = ret; + + assert( m_state == SNM_INT_STATE_UNINITIALISED ); + + + //Initialize libnetctl - already done in SonyHttp_Vita::init + // app.DebugPrintf("sceNetCtlInit\n"); + // ret = sceNetCtlInit(); + // if( ( ret < 0 && ret != SCE_NET_CTL_ERROR_NOT_TERMINATED ) || ForceErrorPoint( SNM_FORCE_ERROR_NET_CTL_INIT ) ) + // { + // SetState(SNM_INT_STATE_INITIALISE_FAILED); + // return; + // } + + m_hid=0; + app.DebugPrintf("sceNetCtlInetRegisterCallback\n"); + ret = sceNetCtlInetRegisterCallback(&NetCtlCallback,this,&m_hid); + assert(ret == SCE_OK); + + // Initialise RUDP + const int RUDP_POOL_SIZE = (500 * 1024); // TODO - find out what we need, this size is copied from library reference + uint8_t *rudp_pool = (uint8_t *)malloc(RUDP_POOL_SIZE); + app.DebugPrintf("sceRudpInit\n"); + ret = sceRudpInit(rudp_pool, RUDP_POOL_SIZE); + if( ( ret < 0 ) || ForceErrorPoint( SNM_FORCE_ERROR_RUDP_INIT ) ) + { + SetState(SNM_INT_STATE_INITIALISE_FAILED); + return; + } + + SetState(SNM_INT_STATE_SIGNING_IN); + // AttemptPSNSignIn(NULL, NULL); + + // SonyHttp::init(); + + // SceNpCommunicationConfig npConf ; + // npConf.commId = &s_npCommunicationId; + // npConf.commPassphrase = &s_npCommunicationPassphrase; + // npConf.commSignature = &s_npCommunicationSignature; + // ret = sceNpInit(&npConf, NULL); + // if (ret < 0 && ret != SCE_NP_ERROR_ALREADY_INITIALIZED) + // { + // app.DebugPrintf("sceNpInit failed, ret=%x\n", ret); + // assert(0); + // } + + app.DebugPrintf("sceRudpEnableInternalIOThread\n"); + ret = sceRudpEnableInternalIOThread(RUDP_THREAD_STACK_SIZE, SCE_KERNEL_DEFAULT_PRIORITY); + if(ret < 0) + { + app.DebugPrintf("sceRudpEnableInternalIOThread failed with error code 0x%08x\n", ret); + assert(0); + } + // Already online? the callback won't catch this, so carry on initialising now + if(ProfileManager.IsSignedInLive(ProfileManager.GetPrimaryPad())) + { + InitialiseAfterOnline(); + } + else + { + // On PS3 we'd be running the netstart dialog here, but we don't want to do that on Vita since we could be switching from ad-hoc mode + SetState(SNM_INT_STATE_INITIALISE_FAILED); + m_offlineSQR = true; + } + SonyVoiceChat_Vita::init(); + + m_bIsInitialised = true; +} + +bool SQRNetworkManager_Vita::IsInitialised() +{ + return m_bIsInitialised; +} +void SQRNetworkManager_Vita::UnInitialise() +{ + int ret; + // on changing to adhoc, we need to shutdown all PSN networking init + + // shutdown voice chat + SonyVoiceChat_Vita::shutdown(); + + app.DebugPrintf("sceNpMatching2ContextStop\n"); + int err = sceNpMatching2ContextStop(m_matchingContext); + + app.DebugPrintf("sceNpMatching2Term\n"); + sceNpMatching2DestroyContext(m_matchingContext); + + app.DebugPrintf("sceNpMatching2Term\n"); + ret = sceNpMatching2Term(); + + app.DebugPrintf("sceRudpEnd\n"); + ret = sceRudpEnd(); + + // shut down NP lib + app.DebugPrintf("sceNetCtlInetUnregisterCallback\n"); + ret = sceNetCtlInetUnregisterCallback(m_hid); + + //app.DebugPrintf("sceNetCtlTerm\n"); + //sceNetCtlTerm(); + + SetState(SNM_INT_STATE_UNINITIALISED); + m_bIsInitialised = false; + m_bShuttingDown = false; + +} + +void SQRNetworkManager_Vita::Terminate() +{ + // If playing, attempt to nicely leave the room before shutting down so that our friends won't still think this game is in progress + if( ( m_state == SNM_INT_STATE_HOSTING_CREATE_ROOM_SUCCESS ) || + ( m_state == SNM_INT_STATE_HOSTING_WAITING_TO_PLAY ) || + ( m_state == SNM_INT_STATE_JOINING_WAITING_FOR_LOCAL_PLAYERS ) || + ( m_state == SNM_INT_STATE_PLAYING ) ) + { + if( !m_offlineGame ) + { + LeaveRoom(true); + int count = 200; + do + { + Tick(); + Sleep(10); + count--; + } while( ( count > 0 ) && ( m_state != SNM_INT_STATE_IDLE ) ); + app.DebugPrintf(CMinecraftApp::USER_RR,"Attempted to leave room, %dms used\n",count * 10); + } + } + + app.DebugPrintf("sceRudpEnd\n"); + int ret = sceRudpEnd(); + app.DebugPrintf("sceNpMatching2Term\n"); + ret = sceNpMatching2Term(); + // Terminate event thread by sending it a non-zero value for data + // sceKernelTriggerUserEvent(m_basicEventQueue, sc_UserEventHandle, (void*)1); + + + // do + // { + // Sleep(10); + // } while( m_basicEventThread->isRunning() ); +} + +// Second stage of initialisation, that requires NP Manager to be online & the player to be signed in. This kicks of the creation of a context +// for Np Matching 2. Initialisation is finally complete when we get a callback to ContextCallback. The SQRNetworkManager_Vita is then finally moved +// into SNM_INT_STATE_IDLE at this stage. +void SQRNetworkManager_Vita::InitialiseAfterOnline() +{ + // SceNpId npId; + // int option = 0; + + // We should only be doing this if we have come in from an initialisation stage (SQRNetworkManager_Vita::Initialise) or we've had a network disconnect and are coming in from an offline state. + // Don't do anything otherwise - this mainly to catch a bit of a corner case in the initialisation phase where potentially we could register for the callback that would call this with sceNpManagerRegisterCallback. + // and could then really quickly go online so that the there becomes two paths (via the callback or SQRNetworkManager_Vita::Initialise) by which this could be called + if( ( m_state != SNM_INT_STATE_SIGNING_IN ) && !(( m_state == SNM_INT_STATE_IDLE ) && m_offlineSQR) ) + { + // If we aren't going to continue on with this sign-in but are expecting a callback, then let the game know that we have completed the bit we are expecting to do. This + // will happen whilst in game, when we want to be able to sign into PSN, but don't expect the full matching stuff to get set up. + if( s_SignInCompleteCallbackFn ) + { + s_SignInCompleteCallbackFn(s_SignInCompleteParam,true,0); + s_SignInCompleteCallbackFn = NULL; + } + return; + } + + // Initialize matching2 with default settings + //int sceNetAdhocMatchingInit(SceSize poolsize,void *poolptr); + + app.DebugPrintf("sceNpMatching2Init\n"); + int ret = sceNpMatching2Init(0, 0, SCE_KERNEL_THREAD_CPU_AFFINITY_MASK_DEFAULT, 0); + + if( ( ret < 0 ) || ForceErrorPoint( SNM_FORCE_ERROR_MATCHING2_INIT ) ) + { + SetState(SNM_INT_STATE_INITIALISE_FAILED); + return; + } + app.DebugPrintf("SQRNetworkManager::InitialiseAfterOnline - matching context is now valid\n"); + m_matching2initialised = true; + + // Get NP ID of the signed-in user + SceNpId npID; + int primaryPad = ProfileManager.GetPrimaryPad(); + if(primaryPad >=0 && ProfileManager.IsSignedInLive(primaryPad)) + { + ProfileManager.GetSceNpId(primaryPad, &npID); + } + else + { + SetState(SNM_INT_STATE_INITIALISE_FAILED); + return; + } + + app.DebugPrintf("sceNpMatching2CreateContext\n"); + ret = sceNpMatching2CreateContext(&npID, GetSceNpCommsId(), &s_npCommunicationPassphrase, &m_matchingContext); + //ret = sceNpMatching2CreateContext(&npID, NULL, NULL, &m_matchingContext); + + if( ( ret < 0 ) || ForceErrorPoint( SNM_FORCE_ERROR_CREATE_MATCHING_CONTEXT ) ) + { + SetState(SNM_INT_STATE_INITIALISE_FAILED); + return; + } + m_matchingContextValid = true; + + bool bRet = RegisterCallbacks(); + if( ( !bRet ) || ForceErrorPoint( SNM_FORCE_ERROR_REGISTER_CALLBACKS ) ) + { + SetState(SNM_INT_STATE_INITIALISE_FAILED); + return; + } + + // State should be starting context until the callback that this has been created happens + SetState(SNM_INT_STATE_STARTING_CONTEXT); + + // Start the context + // Set time-out time to 10 seconds + app.DebugPrintf("sceNpMatching2ContextStart\n"); + ret = sceNpMatching2ContextStart(m_matchingContext, (10*1000*1000)); + + if( ( ret < 0 ) || ForceErrorPoint( SNM_FORCE_ERROR_CONTEXT_START_ASYNC ) ) + { + SetState(SNM_INT_STATE_INITIALISE_FAILED); + } +} + + +// General tick function to be called from main game loop - any internal tick functions should be called from here. +void SQRNetworkManager_Vita::Tick() +{ + OnlineCheck(); + sceNetCtlCheckCallback(); + updateNetCheckDialog(); + ServerContextTick(); + RoomCreateTick(); + FriendSearchTick(); + TickRichPresence(); + TickJoinablePresenceData(); + // TickInviteGUI(); // TODO + + if( ( m_gameBootInvite ) && ( s_safeToRespondToGameBootInvite ) ) + { + m_listener->HandleInviteReceived( ProfileManager.GetPrimaryPad(), m_gameBootInvite ); + m_gameBootInvite = NULL; + } + + ErrorHandlingTick(); + // If we ever fail to send the external room data, we start a countdown so that we attempt to resend. Not sure how likely it is that updating this will fail without the whole network being broken, + // but if in particular we don't update the flag to say that the session is joinable, then nobody is ever going to see this session. + if( m_resendExternalRoomDataCountdown ) + { + if( m_state == SNM_INT_STATE_PLAYING ) + { + m_resendExternalRoomDataCountdown--; + if( m_resendExternalRoomDataCountdown == 0 ) + { + UpdateExternalRoomData(); + } + } + else + { + m_resendExternalRoomDataCountdown = 0; + } + } + + // ProfileManager.SetNetworkStatus(GetOnlineStatus()); + + // Client only - do the final transition to a starting & playing state once we have fully joined the room, And told the game about all the local players so they are also all valid + if( m_state == SNM_INT_STATE_JOINING_WAITING_FOR_LOCAL_PLAYERS ) + { + if( m_localPlayerJoined == m_localPlayerCount ) + { + // Since we're now fully joined, we can update our presence info so that our friends could find us in this game. This data was set up + // at the point that we joined the game (either from search info, or an invitation). + UpdateRichPresenceCustomData(&s_lastPresenceSyncInfo, sizeof(PresenceSyncInfo)); + SetState( SNM_INT_STATE_STARTING); + SetState( SNM_INT_STATE_PLAYING ); + } + } + + if( m_state == SNM_INT_STATE_SERVER_DELETING_CONTEXT ) + { + // make sure we've removed all the remote players and killed the udp connections before we bail out + if(m_RudpCtxToPlayerMap.size() == 0) + ResetToIdle(); + } + + EnterCriticalSection(&m_csStateChangeQueue); + while(m_stateChangeQueue.size() > 0 ) + { + if( m_listener ) + { + m_listener->HandleStateChange(m_stateChangeQueue.front().m_oldState, m_stateChangeQueue.front().m_newState, m_stateChangeQueue.front().m_idleReasonIsSessionFull); + if( m_stateChangeQueue.front().m_newState == SNM_STATE_IDLE ) + { + m_isInSession = false; + } + } + m_stateExternal = m_stateChangeQueue.front().m_newState; + m_stateChangeQueue.pop(); + } + LeaveCriticalSection(&m_csStateChangeQueue); + + // 4J-PB - SQRNetworkManager_PS3::AttemptPSNSignIn was causing crashes in Iggy by calling LoadMovie from a callback, so call it frmo the tick instead + if(m_bCallPSNSignInCallback) + { + m_bCallPSNSignInCallback=false; + if( s_signInCompleteCallbackIfFailed ) + { + s_SignInCompleteCallbackFn(s_SignInCompleteParam,false,0); + s_SignInCompleteCallbackFn = NULL; + } + else if(s_SignInCompleteCallbackFn) + { + s_SignInCompleteCallbackFn(s_SignInCompleteParam, true, 0); + s_SignInCompleteCallbackFn = NULL; + } + } + +} + +// Detect any states which reflect internal error states, do anything required, and transition away again +void SQRNetworkManager_Vita::ErrorHandlingTick() +{ + switch( m_state ) + { + case SNM_INT_STATE_INITIALISE_FAILED: + if( s_SignInCompleteCallbackFn ) + { + if( s_signInCompleteCallbackIfFailed ) + { + s_SignInCompleteCallbackFn(s_SignInCompleteParam,false,0); + } + s_SignInCompleteCallbackFn = NULL; + } + app.DebugPrintf("Network error: SNM_INT_STATE_INITIALISE_FAILED\n"); + if( m_isInSession && m_offlineGame) // m_offlineSQR ) // MGH - changed this to m_offlineGame, as m_offlineSQR can be true when running an online game but the init has failed because the servers are down + { + // This is a fix for an issue where a player attempts (and fails) to sign in, whilst in an offline game. This was setting the state to idle, which in turn + // sets the game to Not be in a session anymore (but the game wasn't generally aware of, and so keeps playing). Howoever, the game's connections use + // their tick to determine whether to empty their queues or not and so no communications (even though they don't actually use this network manager for local connections) + // were happening. + SetState(SNM_INT_STATE_PLAYING); + } + else + { + m_offlineSQR = true; + SetState(SNM_INT_STATE_IDLE); + } + break; + case SNM_INT_STATE_HOSTING_SERVER_SEARCH_FAILED: + app.DebugPrintf("Network error: SNM_INT_STATE_HOSTING_SERVER_SEARCH_FAILED\n"); + ResetToIdle(); + break; + case SNM_INT_STATE_HOSTING_CREATE_ROOM_FAILED: + app.DebugPrintf("Network error: SNM_INT_STATE_HOSTING_CREATE_ROOM_FAILED\n"); + DeleteServerContext(); + break; + case SNM_INT_STATE_JOINING_SERVER_SEARCH_FAILED: + app.DebugPrintf("Network error: SNM_INT_STATE_JOINING_SERVER_SEARCH_FAILED\n"); + ResetToIdle(); + break; + case SNM_INT_STATE_JOINING_JOIN_ROOM_FAILED: + app.DebugPrintf("Network error: SNM_INT_STATE_JOINING_JOIN_ROOM_FAILED\n"); + DeleteServerContext(); + break; + case SNM_INT_STATE_LEAVING_FAILED: + app.DebugPrintf("Network error: SNM_INT_STATE_LEAVING_FAILED\n"); + DeleteServerContext(); + break; + } + +} + +// Start hosting a game, by creating a room & joining it. We explicity create a server context here (via GetServerContext) as Sony suggest that +// this means we have greater control of representing when players are actually "online". The creation of the room is carried out in a callback +// after that server context is made (ServerContextValidCallback_CreateRoom). +// hostIndex is the index of the user that is hosting the session, and localPlayerMask has bit 0 - 3 set to indicate the full set of local players joining the game. +// extData and extDataSize define the initial state of room data that is externally visible (eg by players searching for rooms, but not in it) +void SQRNetworkManager_Vita::CreateAndJoinRoom(int hostIndex, int localPlayerMask, void *extData, int extDataSize, bool offline) +{ + // hostIndex should always be in the mask + assert( ( ( 1 << hostIndex ) & localPlayerMask ) != 0 ); + + m_isHosting = true; + m_joinExtData = extData; + m_joinExtDataSize = extDataSize; + m_offlineGame = offline; + m_resendExternalRoomDataCountdown = 0; + m_isInSession= true; + + // Default value for room, which we can use for offlinae games + m_room = 0; + + // Initialise room data that will be synchronised. Slot 0 is always reserved for the host. We don't know the + // room member until the room is actually created so this will be set/updated at that point + memset( &m_roomSyncData, 0, sizeof(m_roomSyncData) ); + m_roomSyncData.setPlayerCount(1); + m_roomSyncData.players[0].m_smallId = m_currentSmallId++; + m_roomSyncData.players[0].m_localIdx = hostIndex; + + // Remove the host player that we've already added, then add any other local players specified in the mask + localPlayerMask &= ~( ( 1 << hostIndex ) & localPlayerMask ); + for( int i = 0; i < MAX_LOCAL_PLAYER_COUNT; i++ ) + { + if( localPlayerMask & ( 1 << i ) ) + { + m_roomSyncData.players[m_roomSyncData.getPlayerCount()].m_smallId = m_currentSmallId++; + m_roomSyncData.players[m_roomSyncData.getPlayerCount()].m_localIdx = i; + m_roomSyncData.setPlayerCount(m_roomSyncData.getPlayerCount()+1); + } + } + m_localPlayerCount = m_roomSyncData.getPlayerCount(); + + // For offline games, we can jump straight to the state that says we've just created the room (or would have, for an online game) + if( m_offlineGame ) + { + SetState(SNM_INT_STATE_HOSTING_CREATE_ROOM_SUCCESS); + } + else + { + // Kick off the sequence of events required for an online game, starting with getting the server context + m_isInSession = GetServerContext(); + } +} + +// Updates the externally visible data that was associated with the room when it was created with CreateAndJoinRoom. +void SQRNetworkManager_Vita::UpdateExternalRoomData() +{ + if( m_offlineGame ) return; + if( m_isHosting ) + { + SceNpMatching2SetRoomDataExternalRequest reqParam; + memset( &reqParam, 0, sizeof(reqParam) ); + reqParam.roomId = m_room; + SceNpMatching2BinAttr roomBinAttr; + memset(&roomBinAttr, 0, sizeof(roomBinAttr)); + roomBinAttr.id = SCE_NP_MATCHING2_ROOM_BIN_ATTR_EXTERNAL_1_ID; + roomBinAttr.ptr = m_joinExtData; + roomBinAttr.size = m_joinExtDataSize; + reqParam.roomBinAttrExternalNum = 1; + reqParam.roomBinAttrExternal = &roomBinAttr; + + app.DebugPrintf("sceNpMatching2SetRoomDataExternal\n"); + int ret = sceNpMatching2SetRoomDataExternal ( m_matchingContext, &reqParam, NULL, &m_setRoomDataRequestId ); + app.DebugPrintf(CMinecraftApp::USER_RR,"sceNpMatching2SetRoomDataExternal returns 0x%x, number of players %d\n",ret,((char *)m_joinExtData)[174]); + if( ( ret < 0 ) || ForceErrorPoint( SNM_FORCE_ERROR_SET_EXTERNAL_ROOM_DATA ) ) + { + // If we ever fail to send the external room data, we start a countdown so that we attempt to resend. Not sure how likely it is that updating this will fail without the whole network being broken, + // but if in particular we don't update the flag to say that the session is joinable, then nobody is ever going to see this session. + m_resendExternalRoomDataCountdown = 60; + } + } +} + +// Determine if the friend room manager is busy. If it isn't busy, then other operations (searching for a friend, reading the found friend's room lists) may safely be performed +bool SQRNetworkManager_Vita::FriendRoomManagerIsBusy() +{ + return (m_friendSearchState != SNM_FRIEND_SEARCH_STATE_IDLE); +} + +// Initiate a search for rooms that the signed in user's friends are in. This is an asynchronous operation, this function returns after it kicks off a search across all game servers +// for any of the player's friends. +bool SQRNetworkManager_Vita::FriendRoomManagerSearch() +{ + if( m_state != SNM_INT_STATE_IDLE ) return false; + + // Don't start another search if we're already searching... + if( m_friendSearchState != SNM_FRIEND_SEARCH_STATE_IDLE ) + { + return false; + } + + // Free up any external data that we received from the previous search + for( int i = 0; i < m_aFriendSearchResults.size(); i++ ) + { + if(m_aFriendSearchResults[i].m_RoomExtDataReceived) + free(m_aFriendSearchResults[i].m_RoomExtDataReceived); + m_aFriendSearchResults[i].m_RoomExtDataReceived = NULL; + } + + m_friendSearchState = SNM_FRIEND_SEARCH_STATE_GETTING_FRIEND_COUNT; + m_friendCount = 0; + m_aFriendSearchResults.clear(); + + // Get friend list - doing this in another thread as it can lock up for a few seconds + m_getFriendCountThread = new C4JThread(&GetFriendsThreadProc,this,"GetFriendsThreadProc"); + m_getFriendCountThread->Run(); + + return true; +} + +bool SQRNetworkManager_Vita::FriendRoomManagerSearch2() +{ + if( m_friendCount == 0 ) + { + m_friendSearchState = SNM_FRIEND_SEARCH_STATE_IDLE; + return false; + } + + if( m_aFriendSearchResults.size() > 0 ) + { + // If we have some results, then we also want to make sure that we don't have any duplicate rooms here if more than one friend is playing in the same room. + unordered_set uniqueRooms; + for( unsigned int i = 0; i < m_aFriendSearchResults.size(); i++ ) + { + if(m_aFriendSearchResults[i].m_RoomFound) + { + uniqueRooms.insert( m_aFriendSearchResults[i].m_RoomId ); + } + } + + // Tidy the results up further based on this + for( unsigned int i = 0; i < m_aFriendSearchResults.size(); ) + { + if( uniqueRooms.find(m_aFriendSearchResults[i].m_RoomId) == uniqueRooms.end() ) + { + free(m_aFriendSearchResults[i].m_RoomExtDataReceived); + m_aFriendSearchResults[i] = m_aFriendSearchResults.back(); + m_aFriendSearchResults.pop_back(); + } + else + { + uniqueRooms.erase(m_aFriendSearchResults[i].m_RoomId); + i++; + } + } + } + m_friendSearchState = SNM_FRIEND_SEARCH_STATE_IDLE; + return true; +} + +void SQRNetworkManager_Vita::FriendSearchTick() +{ + // Move onto next state if we're done getting our friend count + if( m_friendSearchState == SNM_FRIEND_SEARCH_STATE_GETTING_FRIEND_COUNT ) + { + if( !m_getFriendCountThread->isRunning() ) + { + m_friendSearchState = SNM_FRIEND_SEARCH_STATE_GETTING_FRIEND_INFO; + delete m_getFriendCountThread; + m_getFriendCountThread = NULL; + FriendRoomManagerSearch2(); + } + } +} + +// The handler for basic events can't actually get the events themselves, this has to be done on another thread. Instead, we send a sys_event_t to a queue on This thread, +// which has a single data item used which we can use to determine whether to terminate this thread or get a basic event & handle that. +int SQRNetworkManager_Vita::BasicEventThreadProc( void *lpParameter ) +{ + PSVITA_STUBBED; + return 0; + // SQRNetworkManager_Vita *manager = (SQRNetworkManager_Vita *)lpParameter; + // + // int ret = SCE_OK; + // SceKernelEvent event; + // int outEv; + // + // do + // { + // ret = sceKernelWaitEqueue(manager->m_basicEventQueue, &event, 1, &outEv, NULL); + // + // // If the sys_event_t we've sent here from the handler has a non-zero data1 element, this is to signify that we should terminate the thread + // if( event.udata == 0 ) + // { + // // int iEvent; + // // SceNpUserInfo from; + // // uint8_t buffer[SCE_NP_BASIC_MAX_MESSAGE_SIZE]; + // // size_t bufferSize = SCE_NP_BASIC_MAX_MESSAGE_SIZE; + // // int ret = sceNpBasicGetEvent(&iEvent, &from, &buffer, &bufferSize); + // // if( ret == 0 ) + // // { + // // if( iEvent == SCE_NP_BASIC_EVENT_INCOMING_BOOTABLE_INVITATION ) + // // { + // // // 4J Stu - Don't do this here as it can be very disruptive to gameplay. Players can bring this up from LoadOrJoinMenu, PauseMenu and InGameInfoMenu + // // //sceNpBasicRecvMessageCustom(SCE_NP_BASIC_MESSAGE_MAIN_TYPE_INVITE, SCE_NP_BASIC_RECV_MESSAGE_OPTIONS_INCLUDE_BOOTABLE, SYS_MEMORY_CONTAINER_ID_INVALID); + // // } + // // if( iEvent == SCE_NP_BASIC_EVENT_RECV_INVITATION_RESULT ) + // // { + // // SceNpBasicExtendedAttachmentData *result = (SceNpBasicExtendedAttachmentData *)buffer; + // // if(result->userAction == SCE_NP_BASIC_MESSAGE_ACTION_ACCEPT ) + // // { + // // manager->GetInviteDataAndProcess(result->data.id); + // // } + // // } + // // app.DebugPrintf("Incoming basic event of type %d\n",iEvent); + // // } + // } + // + // } while(event.udata == 0 ); + // return 0; +} + +int SQRNetworkManager_Vita::GetFriendsThreadProc( void* lpParameter ) +{ + SQRNetworkManager_Vita *manager = (SQRNetworkManager_Vita *)lpParameter; + + int ret = 0; + manager->m_aFriendSearchResults.clear(); + manager->m_friendCount = 0; + if(!ProfileManager.IsSignedInLive(ProfileManager.GetPrimaryPad())) + { + app.DebugPrintf("getFriendslist failed, not signed into Live! \n"); + return 0; + } + + + ret = sceNpBasicGetFriendListEntryCount(&manager->m_friendCount); + if( ( ret < 0 ) || manager->ForceErrorPoint( SNM_FORCE_ERROR_GET_FRIEND_LIST_ENTRY_COUNT ) ) + { + // This is likely when friend list hasn't been received from the server yet - will be returning SCE_NP_BASIC_ERROR_BUSY in this case + manager->m_friendCount = 0; + } + + + // There shouldn't ever be more than 100 friends returned but limit here just in case + if( manager->m_friendCount > 100 ) manager->m_friendCount = 100; + + SceNpId* friendIDs = NULL; + if(manager->m_friendCount > 0) + { + // grab all the friend IDs first + friendIDs = new SceNpId[manager->m_friendCount]; + SceSize numRecieved; + ret = sceNpBasicGetFriendListEntries(0, friendIDs, manager->m_friendCount, &numRecieved); + if (ret < 0) + { + app.DebugPrintf("sceNpBasicGetFriendListEntries() failed: ret = 0x%x\n", ret); + manager->m_friendCount = 0; + } + else + { + assert(numRecieved == manager->m_friendCount); + } + } + + + // It is possible that the size of the friend list might vary from what we just received, so only add in friends that we successfully get an entry for + for( unsigned int i = 0; i < manager->m_friendCount; i++ ) + { + static SceNpBasicGamePresence presenceDetails; + static SceNpBasicFriendContextState contextState; + int ret = sceNpBasicGetFriendContextState(&friendIDs[i], &contextState); + if (ret < 0) + { + app.DebugPrintf("sceNpBasicGetFriendContextState() failed: ret = 0x%x\n", ret); + contextState = SCE_NP_BASIC_FRIEND_CONTEXT_STATE_UNKNOWN; + } + if(contextState == SCE_NP_BASIC_FRIEND_CONTEXT_STATE_IN_CONTEXT) // using the same SceNpCommunicationId, so playing Minecraft + { + ret = sceNpBasicGetGamePresenceOfFriend(&friendIDs[i], &presenceDetails); + if( ( ret == 0 ) && ( !manager->ForceErrorPoint( SNM_FORCE_ERROR_GET_FRIEND_LIST_ENTRY ) ) ) + { + FriendSearchResult result; + memcpy(&result.m_NpId, &friendIDs[i], sizeof(SceNpId)); + result.m_RoomFound = false; + + // Only include the friend's game if its the same network id ( this also filters out generally Zeroed PresenceSyncInfo, which we do when we aren't in an active game session) + // if( presenceDetails.size == sizeof(PresenceSyncInfo) ) + { + PresenceSyncInfo *pso = (PresenceSyncInfo *)presenceDetails.inGamePresence.data; + if( pso->netVersion == MINECRAFT_NET_VERSION ) + { + if( !pso->inviteOnly ) + { + result.m_RoomFound = true; + result.m_RoomId = pso->m_RoomId; + result.m_ServerId = pso->m_ServerId; + + CPlatformNetworkManagerSony::MallocAndSetExtDataFromSQRPresenceInfo(&result.m_RoomExtDataReceived, pso); + manager->m_aFriendSearchResults.push_back(result); + } + } + } + } + } + } + + if(friendIDs) + delete friendIDs; + return 0; +} + +// Get count of rooms that friends are playing in. Only valid when FriendRoomManagerIsBusy() returns false +int SQRNetworkManager_Vita::FriendRoomManagerGetCount() +{ + assert( m_friendSearchState == SNM_FRIEND_SEARCH_STATE_IDLE ); + return m_aFriendSearchResults.size(); +} + +// Get details of a found session that a friend is playing in. 0 < idx < FriendRoomManagerGetCount(). Only valid when FriendRoomManagerIsBusy() returns false +void SQRNetworkManager_Vita::FriendRoomManagerGetRoomInfo(int idx, SQRNetworkManager_Vita::SessionSearchResult *searchResult) +{ + assert( idx < m_aFriendSearchResults.size() ); + assert( m_friendSearchState == SNM_FRIEND_SEARCH_STATE_IDLE ); + + searchResult->m_NpId = m_aFriendSearchResults[idx].m_NpId; + searchResult->m_sessionId.m_RoomId = m_aFriendSearchResults[idx].m_RoomId; + searchResult->m_sessionId.m_ServerId = m_aFriendSearchResults[idx].m_ServerId; + searchResult->m_extData = m_aFriendSearchResults[idx].m_RoomExtDataReceived; +} + +// Get overall state of the network manager. +SQRNetworkManager_Vita::eSQRNetworkManagerState SQRNetworkManager_Vita::GetState() +{ + return m_stateExternal;; +} + +bool SQRNetworkManager_Vita::IsHost() +{ + return m_isHosting; +} + +bool SQRNetworkManager_Vita::IsReadyToPlayOrIdle() +{ + return (( m_state == SNM_INT_STATE_HOSTING_WAITING_TO_PLAY ) || ( m_state == SNM_INT_STATE_PLAYING ) || ( m_state == SNM_INT_STATE_IDLE ) ); +} + + +// Consider as "in session" from the moment that a game is created or joined, until the point where the game itself has been told via state change that we are now idle. The +// game code requires IsInSession to return true as soon as it has asked to do one of these things (even if the state system hasn't really caught up with this request yet), and +// it also requires that it is informed of the state changes leading up to not being in the session, before this should report false. +bool SQRNetworkManager_Vita::IsInSession() +{ + return m_isInSession; +} + +// Get count of players currently in the session +int SQRNetworkManager_Vita::GetPlayerCount() +{ + return m_roomSyncData.getPlayerCount(); +} + +// Get count of players who are in the session, but not local to this machine +int SQRNetworkManager_Vita::GetOnlinePlayerCount() +{ + int onlineCount = 0; + for( int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( m_roomSyncData.players[i].m_roomMemberId != m_localMemberId ) + { + onlineCount++; + } + } + return onlineCount; +} + +SQRNetworkPlayer *SQRNetworkManager_Vita::GetPlayerByIndex(int idx) +{ + if( idx < MAX_ONLINE_PLAYER_COUNT ) + { + return GetPlayerIfReady(m_aRoomSlotPlayers[idx]); + } + else + { + return NULL; + } +} + +SQRNetworkPlayer *SQRNetworkManager_Vita::GetPlayerBySmallId(int idx) +{ + EnterCriticalSection(&m_csRoomSyncData); + for( int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( m_roomSyncData.players[i].m_smallId == idx ) + { + SQRNetworkPlayer *player = GetPlayerIfReady(m_aRoomSlotPlayers[i]); + LeaveCriticalSection(&m_csRoomSyncData); + return player; + } + } + LeaveCriticalSection(&m_csRoomSyncData); + return NULL; +} + +SQRNetworkPlayer *SQRNetworkManager_Vita::GetLocalPlayerByUserIndex(int idx) +{ + EnterCriticalSection(&m_csRoomSyncData); + for( int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( ( m_roomSyncData.players[i].m_roomMemberId == m_localMemberId ) && ( m_roomSyncData.players[i].m_localIdx == idx ) ) + { + SQRNetworkPlayer *player = GetPlayerIfReady(m_aRoomSlotPlayers[i]); + LeaveCriticalSection(&m_csRoomSyncData); + return player; + } + } + LeaveCriticalSection(&m_csRoomSyncData); + return NULL; +} + +SQRNetworkPlayer *SQRNetworkManager_Vita::GetHostPlayer() +{ + EnterCriticalSection(&m_csRoomSyncData); + SQRNetworkPlayer *player = GetPlayerIfReady(m_aRoomSlotPlayers[0]); + LeaveCriticalSection(&m_csRoomSyncData); + return player; +} + +SQRNetworkPlayer *SQRNetworkManager_Vita::GetPlayerIfReady(SQRNetworkPlayer *player) +{ + if( player == NULL ) return NULL; + + if( player->IsReady() ) return player; + + return NULL; +} + +// Update state internally + +#ifdef _DEBUG +static const char szNetState[35][60]= +{ + "SNM_INT_STATE_UNINITIALISED", + "SNM_INT_STATE_SIGNING_IN", + "SNM_INT_STATE_STARTING_CONTEXT", + "SNM_INT_STATE_INITIALISE_FAILED", + "SNM_INT_STATE_IDLE", + "SNM_INT_STATE_IDLE_RECREATING_MATCHING_CONTEXT", + "SNM_INT_STATE_HOSTING_STARTING_MATCHING_CONTEXT", + "SNM_INT_STATE_HOSTING_SEARCHING_FOR_SERVER", + "SNM_INT_STATE_HOSTING_SERVER_SEARCH_SERVER_ERROR", + "SNM_INT_STATE_HOSTING_SERVER_FOUND", + "SNM_INT_STATE_HOSTING_SERVER_SEARCH_CREATING_CONTEXT", + "SNM_INT_STATE_HOSTING_SERVER_SEARCH_FAILED", + "SNM_INT_STATE_HOSTING_CREATE_ROOM_SEARCHING_FOR_WORLD", + "SNM_INT_STATE_HOSTING_CREATE_ROOM_WORLD_FOUND", + "SNM_INT_STATE_HOSTING_CREATE_ROOM_CREATING_ROOM", + "SNM_INT_STATE_HOSTING_CREATE_ROOM_SUCCESS", + "SNM_INT_STATE_HOSTING_CREATE_ROOM_FAILED", + "SNM_INT_STATE_HOSTING_CREATE_ROOM_RESTART_MATCHING_CONTEXT", + "SNM_INT_STATE_HOSTING_WAITING_TO_PLAY", + "SNM_INT_STATE_JOINING_STARTING_MATCHING_CONTEXT", + "SNM_INT_STATE_JOINING_SEARCHING_FOR_SERVER", + "SNM_INT_STATE_JOINING_SERVER_SEARCH_SERVER_ERROR", + "SNM_INT_STATE_JOINING_SERVER_FOUND", + "SNM_INT_STATE_JOINING_SERVER_SEARCH_CREATING_CONTEXT", + "SNM_INT_STATE_JOINING_SERVER_SEARCH_FAILED", + "SNM_INT_STATE_JOINING_JOIN_ROOM", + "SNM_INT_STATE_JOINING_JOIN_ROOM_FAILED", + "SNM_INT_STATE_JOINING_WAITING_FOR_LOCAL_PLAYERS", + "SNM_INT_STATE_SERVER_DELETING_CONTEXT", + "SNM_INT_STATE_STARTING", + "SNM_INT_STATE_PLAYING", + "SNM_INT_STATE_LEAVING", + "SNM_INT_STATE_LEAVING_FAILED", + "SNM_INT_STATE_ENDING", + "SNM_INT_STATE_COUNT" +}; + +#endif +void SQRNetworkManager_Vita::SetState(SQRNetworkManager_Vita::eSQRNetworkManagerInternalState state) +{ +#ifdef _DEBUG + app.DebugPrintf("SQRNetworkManager_Vita::SetState [%s]\n",szNetState[state]); +#endif + + eSQRNetworkManagerState oldState = m_INTtoEXTStateMappings[m_state]; + eSQRNetworkManagerState newState = m_INTtoEXTStateMappings[state]; + bool setIdleReasonSessionFull = false; + if( ( state == SNM_INT_STATE_IDLE ) && m_nextIdleReasonIsFull ) + { + setIdleReasonSessionFull = true; + m_nextIdleReasonIsFull = false; + } + m_state = state; + // Queue any important (ie externally relevant) state changes - we will do a call back for these in our main tick. Don't do it directly here + // as we could be coming from any thread at this stage, with any stack size etc. and so we don't generally want to expect the game to be able to handle itself in such circumstances. + if( ( newState != oldState ) || setIdleReasonSessionFull ) + { + EnterCriticalSection(&m_csStateChangeQueue); + m_stateChangeQueue.push(StateChangeInfo(oldState,newState,setIdleReasonSessionFull)); + LeaveCriticalSection(&m_csStateChangeQueue); + } +} + +void SQRNetworkManager_Vita::ResetToIdle() +{ + app.DebugPrintf("------------------ResetToIdle--------------------\n"); + // If we're the client, remove any networked players properly ( this will destory their rupd context etc.) + if( !m_isHosting ) + { + RemoveNetworkPlayers((1 << MAX_LOCAL_PLAYER_COUNT)-1); + } + m_serverContextValid = false; + m_isHosting = false; + m_currentSmallId = 0; + EnterCriticalSection(&m_csRoomSyncData); + for(int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + delete m_aRoomSlotPlayers[i]; + } + memset( m_aRoomSlotPlayers, 0, sizeof(m_aRoomSlotPlayers) ); + memset( &m_roomSyncData,0,sizeof(m_roomSyncData)); + LeaveCriticalSection(&m_csRoomSyncData); + SetState(SNM_INT_STATE_IDLE); + SonyVoiceChat_Vita::checkFinished(); +} + +// Join a room that was found with FriendRoomManagerSearch. 0 < idx < FriendRoomManagerGetCount(). Only valid when FriendRoomManagerIsBusy() returns false +bool SQRNetworkManager_Vita::JoinRoom(SQRNetworkManager_Vita::SessionSearchResult *searchResult, int localPlayerMask) +{ + // Set up the presence info we would like to synchronise out when we have fully joined the game + CPlatformNetworkManagerSony::SetSQRPresenceInfoFromExtData(&s_lastPresenceSyncInfo, searchResult->m_extData, searchResult->m_sessionId.m_RoomId, searchResult->m_sessionId.m_ServerId); + return JoinRoom(searchResult->m_sessionId.m_RoomId, searchResult->m_sessionId.m_ServerId, localPlayerMask, NULL); +} + +// Join room with a specified roomId. This is used when joining from an invite, as well as by the previous method +bool SQRNetworkManager_Vita::JoinRoom(SceNpMatching2RoomId roomId, SceNpMatching2ServerId serverId, int localPlayerMask, const SQRNetworkManager_Vita::PresenceSyncInfo *presence) +{ + // The presence info will be directly passed in if we are joining from an invite, otherwise it has already been set up. This is synchronised out when we have fully joined the game. + if( presence ) + { + memcpy( &s_lastPresenceSyncInfo, presence, sizeof(PresenceSyncInfo) ); + } + + m_isInSession = true; + + m_isHosting = false; + m_offlineGame = false; + m_roomToJoin = roomId; + m_localPlayerJoinMask = localPlayerMask; + m_localPlayerCount = 0; + m_localPlayerJoined = 0; + + for( int i = 0; i < MAX_LOCAL_PLAYER_COUNT; i++ ) + { + if( localPlayerMask & ( 1 << i ) ) m_localPlayerCount++; + } + + return GetServerContext( serverId ); +} + +void SQRNetworkManager_Vita::StartGame() +{ + assert( ( m_state == SNM_INT_STATE_HOSTING_WAITING_TO_PLAY ) || (( m_state == SNM_INT_STATE_IDLE ) && m_offlineSQR) ); + + SetState( SNM_INT_STATE_STARTING); + SetState( SNM_INT_STATE_PLAYING); +} + +void SQRNetworkManager_Vita::LeaveRoom(bool bActuallyLeaveRoom) +{ + if( m_offlineGame ) + { + if( m_state != SNM_INT_STATE_PLAYING ) return; + + SetState(SNM_INT_STATE_LEAVING); + SetState(SNM_INT_STATE_ENDING); + ResetToIdle(); + return; + } + + UpdateRichPresenceCustomData(& c_presenceSyncInfoNULL, sizeof(PresenceSyncInfo) ); + + // SonyVoiceChat::shutdown(); + + // Attempt to leave the room if we are in any of the states we could be in if we have successfully created it + if( bActuallyLeaveRoom ) + { + if( ( m_state == SNM_INT_STATE_HOSTING_CREATE_ROOM_SUCCESS ) || + ( m_state == SNM_INT_STATE_HOSTING_WAITING_TO_PLAY ) || + ( m_state == SNM_INT_STATE_JOINING_WAITING_FOR_LOCAL_PLAYERS ) || + ( m_state == SNM_INT_STATE_PLAYING ) ) + { + SceNpMatching2LeaveRoomRequest reqParam; + memset( &reqParam, 0, sizeof(reqParam) ); + reqParam.roomId = m_room; + + SetState(SNM_INT_STATE_LEAVING); + app.DebugPrintf("sceNpMatching2LeaveRoom\n"); + int ret = sceNpMatching2LeaveRoom( m_matchingContext, &reqParam, NULL, &m_leaveRoomRequestId ); + if( ( ret < 0 ) || ForceErrorPoint(SNM_FORCE_ERROR_LEAVE_ROOM) ) + { + SetState(SNM_INT_STATE_LEAVING_FAILED); + } + } + else if ( m_state == SNM_INT_STATE_HOSTING_CREATE_ROOM_CREATING_ROOM ) + { + // Haven't created the room yet, but will have created the server context so need to recover from that + DeleteServerContext(); + } + else + { + SetState(SNM_INT_STATE_IDLE); + } + } + else + { + // We have created a room but have now had some kind of connection error which means that we've been dropped out of the room and it has been destroyed, so + // no need to leave it again since it doesn't exist anymore. Still need to destroy server context which may be valid + DeleteServerContext(); + } +} + +void SQRNetworkManager_Vita::EndGame() +{ +} + +bool SQRNetworkManager_Vita::SessionHasSpace(int spaceRequired) +{ + return( ( m_roomSyncData.getPlayerCount() + spaceRequired ) <= MAX_ONLINE_PLAYER_COUNT ); +} + +bool SQRNetworkManager_Vita::AddLocalPlayerByUserIndex(int idx) +{ + if( m_isHosting ) + { + if( m_roomSyncData.getPlayerCount() == MAX_ONLINE_PLAYER_COUNT ) return false; + + // Host's players are always at the start of the sync data, so we just need to find the first entry that isn't us to determine what we want to insert before + int insertAtIdx = m_roomSyncData.getPlayerCount(); + for( int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( m_roomSyncData.players[i].m_roomMemberId != m_localMemberId ) + { + insertAtIdx = i; + break; + } + else + { + // Don't add the same local index twice + if( m_roomSyncData.players[i].m_localIdx == idx ) + { + return false; + } + } + } + + // Make room for a new entry... + for( int i = m_roomSyncData.getPlayerCount(); i > insertAtIdx; i-- ) + { + m_roomSyncData.players[i] = m_roomSyncData.players[i-1]; + } + m_roomSyncData.players[insertAtIdx].m_localIdx = idx; + m_roomSyncData.players[insertAtIdx].m_roomMemberId = m_localMemberId; + m_roomSyncData.players[insertAtIdx].m_smallId = m_currentSmallId++; + + m_roomSyncData.setPlayerCount(m_roomSyncData.getPlayerCount()+1); + + // And do any adjusting necessary to the mappings from this room data, to the SQRNetworkPlayers. + // This will also create the required new SQRNetworkPlayer and do all the callbacks that requires etc. + MapRoomSlotPlayers(); + + // Sync this back out to our networked clients... + SyncRoomData(); + + // no connections being made because we're all on the host, so add this player to the existing connections + SonyVoiceChat_Vita::connectPlayerToAll(idx); + return true; + } + else + { + // Don't attempt to join if our client's view of the players indicates that there aren't any free slots + if( m_roomSyncData.getPlayerCount() == MAX_ONLINE_PLAYER_COUNT ) return false; + + // Add the requested player to the mask of local players currently in the game, and update this data - this + // will also then resync with the server which can respond appropriately + int mask = 1 << idx; + if( m_localPlayerJoinMask & mask ) return false; + + m_localPlayerJoinMask |= mask; + + SceNpMatching2SetRoomMemberDataInternalRequest reqParam; + SceNpMatching2BinAttr binAttr; + + memset(&reqParam, 0, sizeof(reqParam)); + memset(&binAttr, 0, sizeof(binAttr)); + + binAttr.id = SCE_NP_MATCHING2_ROOMMEMBER_BIN_ATTR_INTERNAL_1_ID; + binAttr.ptr = &m_localPlayerJoinMask; + binAttr.size = sizeof(m_localPlayerJoinMask); + + reqParam.roomId = m_room; + reqParam.memberId = m_localMemberId; + reqParam.roomMemberBinAttrInternalNum = 1; + reqParam.roomMemberBinAttrInternal = &binAttr; + + app.DebugPrintf("sceNpMatching2SetRoomMemberDataInternal\n"); + int ret = sceNpMatching2SetRoomMemberDataInternal( m_matchingContext, &reqParam, NULL, &m_setRoomMemberInternalDataRequestId ); + + if( ( ret < 0 ) || ForceErrorPoint(SNM_FORCE_ERROR_SET_ROOM_MEMBER_DATA_INTERNAL) ) + { + return false; + } + + // Create the client's end of the rudp connections... note that m_roomSyncData.players[0].m_roomMemberId is always be the host's room member id. + bool rudpOk = CreateRudpConnections(m_room, m_roomSyncData.players[0].m_roomMemberId, mask, m_localMemberId ); + + if( rudpOk ) + { + bool ret = CreateVoiceRudpConnections( m_room, m_roomSyncData.players[0].m_roomMemberId, mask); + assert(ret); + return true; + } + else + { + m_localPlayerJoinMask &= (~mask); + return false; + } + } +} + +bool SQRNetworkManager_Vita::RemoveLocalPlayerByUserIndex(int idx) +{ + if( m_isHosting ) + { + EnterCriticalSection(&m_csRoomSyncData); + + int roomSlotPlayerCount = m_roomSyncData.getPlayerCount(); + + for( int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( ( m_roomSyncData.players[i].m_roomMemberId == m_localMemberId ) && + ( m_roomSyncData.players[i].m_localIdx == idx ) ) + { + // Shuffle all remaining entries up... + m_roomSyncData.setPlayerCount(m_roomSyncData.getPlayerCount()-1); + for( int j = i; j < m_roomSyncData.getPlayerCount(); j++ ) + { + m_roomSyncData.players[j] = m_roomSyncData.players[j+1]; + } + + // Zero last element that isn't part of the currently sized array anymore + memset(&m_roomSyncData.players[m_roomSyncData.getPlayerCount()],0,sizeof(PlayerSyncData)); + + // And do any adjusting necessary to the mappings from this room data, to the SQRNetworkPlayers. + // This will also delete the SQRNetworkPlayer and do all the callbacks that requires etc. + MapRoomSlotPlayers(roomSlotPlayerCount); + m_aRoomSlotPlayers[m_roomSyncData.getPlayerCount()] = NULL; + + // Sync this back out to our networked clients... + SyncRoomData(); + + SonyVoiceChat_Vita::disconnectLocalPlayer(idx); + + LeaveCriticalSection(&m_csRoomSyncData); + return true; + } + } + LeaveCriticalSection(&m_csRoomSyncData); + return false; + } + else + { + // Remove the requested player from the mask of local players currently in the game, and update this data - this + // will also then resync with the server which can respond appropriately + int mask = 1 << idx; + if( ( m_localPlayerJoinMask & mask ) == 0 ) return false; + + m_localPlayerJoinMask &= ~mask; + + SceNpMatching2SetRoomMemberDataInternalRequest reqParam; + SceNpMatching2BinAttr binAttr; + + memset(&reqParam, 0, sizeof(reqParam)); + memset(&binAttr, 0, sizeof(binAttr)); + + binAttr.id = SCE_NP_MATCHING2_ROOMMEMBER_BIN_ATTR_INTERNAL_1_ID; + binAttr.ptr = &m_localPlayerJoinMask; + binAttr.size = sizeof(m_localPlayerJoinMask); + + reqParam.roomId = m_room; + reqParam.memberId = m_localMemberId; + reqParam.roomMemberBinAttrInternalNum = 1; + reqParam.roomMemberBinAttrInternal = &binAttr; + + int ret = sceNpMatching2SetRoomMemberDataInternal( m_matchingContext, &reqParam, NULL, &m_setRoomMemberInternalDataRequestId ); + + if( ( ret < 0 ) || ForceErrorPoint(SNM_FORCE_ERROR_SET_ROOM_MEMBER_DATA_INTERNAL2) ) + { + return false; + } + + RemoveNetworkPlayers( mask ); + + return true; + } +} + + +extern uint8_t *mallocAndCreateUTF8ArrayFromString(int iID); + +// Bring up a Gui to send an invite so a player that the user can select. This invite will contain the room Id so that +void SQRNetworkManager_Vita::SendInviteGUI() +{ + if(ProfileManager.IsSystemUIDisplayed()) + { + app.DebugPrintf("SendInviteGUI failed, SysUI is already up \n"); + return; + } + + //Set invitation information - this is now exactly the same as the presence information that we synchronise out. + + // If we joined a game, we'll have already set s_lastPresenceSyncInfo up (whether we came in from an invite, or joining a game we discovered). If we were hosting, + // then we'll need to set this up now from the external dasta. + if( m_isHosting ) + { + CPlatformNetworkManagerSony::SetSQRPresenceInfoFromExtData(&s_lastPresenceSyncInfo, m_joinExtData, m_room, m_serverId); + } + + sce::Toolkit::NP::MessageData messData; + memset(&messData,0,sizeof(messData)); + + char *subject = (char*)mallocAndCreateUTF8ArrayFromString(IDS_INVITATION_SUBJECT_MAX_18_CHARS); + char *body = (char*)mallocAndCreateUTF8ArrayFromString(IDS_INVITATION_BODY); + messData.attachment = (SceChar8*)&s_lastPresenceSyncInfo;; + messData.attachmentSize = sizeof(PresenceSyncInfo); + messData.body.assign(body); + messData.iconPath.assign(SESSION_IMAGE_PATH); + messData.expireMinutes = 0; + + int ret = sce::Toolkit::NP::Messaging::Interface::sendMessage(&messData, SCE_TOOLKIT_NP_MESSAGE_TYPE_CUSTOM_DATA); + if(ret < SCE_TOOLKIT_NP_SUCCESS ) + { + app.DebugPrintf("Send Message failed 0x%x ...\n",ret); + assert(0); + return; + } + else + { + m_bSendingInviteMessage = true; + ProfileManager.SetSysUIShowing( true ); + } +} + + +void SQRNetworkManager_Vita::RecvInviteGUI() +{ + if(ProfileManager.IsSystemUIDisplayed()) + { + app.DebugPrintf("RecvInviteGUI failed, SysUI is already up \n"); + return; + } + + int ret = sce::Toolkit::NP::Messaging::Interface::displayReceivedMessages(SCE_TOOLKIT_NP_MESSAGE_TYPE_CUSTOM_DATA); + if(ret < SCE_TOOLKIT_NP_SUCCESS ) + { + app.DebugPrintf("displayReceivedMessages 0x%x ...\n",ret); + assert(0); + return; + } + else + { + ProfileManager.SetSysUIShowing( true ); + } + + + // int ret = sceGameCustomDataDialogInitialize(); + // if(ret != SCE_OK) + // { + // app.DebugPrintf("sceGameCustomDataDialogInitialize() failed. ret = 0x%x\n", ret); + // } + // else + // { + // + // SceGameCustomDataDialogParam dialogParam; + // SceGameCustomDataDialogDataParam dataParam; + // + // sceGameCustomDataDialogParamInit( &dialogParam ); + // memset( &dataParam, 0x00, sizeof( SceGameCustomDataDialogDataParam ) ); + // dialogParam.mode = SCE_GAME_CUSTOM_DATA_DIALOG_MODE_RECV; + // dialogParam.dataParam = &dataParam; + // dialogParam.userId = ProfileManager.getUserID(ProfileManager.GetPrimaryPad()); + // ret = sceGameCustomDataDialogOpen( &dialogParam ); + // + // if( SCE_OK != ret ) + // { + // app.DebugPrintf("sceGameCustomDataDialogOpen() failed. ret = 0x%x\n", ret); + // } + // else + // { + // b_inviteRecvGUIRunning = true; + // } + // } +} + + +void SQRNetworkManager_Vita::TickInviteGUI() +{ + PSVITA_STUBBED; + // if(b_inviteRecvGUIRunning) + // { + // SceCommonDialogStatus status = sceGameCustomDataDialogUpdateStatus(); + // + // if( SCE_COMMON_DIALOG_STATUS_FINISHED == status ) + // { + // SceGameCustomDataDialogOnlineIdList sentOnlineIdList; + // memset( &sentOnlineIdList, 0x0, sizeof(SceGameCustomDataDialogOnlineIdList)); + // SceGameCustomDataDialogResult dialogResult; + // memset( &dialogResult, 0x0, sizeof(SceGameCustomDataDialogResult) ); + // dialogResult.sentOnlineIds = &sentOnlineIdList; + // + // int32_t ret = sceGameCustomDataDialogGetResult( &dialogResult ); + // + // if( SCE_OK != ret ) + // { + // app.DebugPrintf( "***** sceGameCustomDataDialogGetResult error:0x%x\n", ret); + // } + // sceGameCustomDataDialogClose(); + // sceGameCustomDataDialogTerminate(); + // b_inviteRecvGUIRunning = false; + // } + // } +} + +// Get the data for an invite into a statically allocated array of invites, and pass a pointer of this back up to the game. Elements in the array are used in a circular fashion, to save any issues with handling freeing of this invite data as the +// qnet equivalent of this seems to just assume that the data persists forever. +void SQRNetworkManager_Vita::GetInviteDataAndProcess(sce::Toolkit::NP::MessageAttachment* pInvite) +{ + + app.DebugPrintf("GameCustomData attachment size : %d\n", pInvite->getAttachmentSize()); + if(pInvite->getAttachmentSize() == sizeof(m_gameBootInvite_data)) + { + memcpy(&m_gameBootInvite_data, pInvite->getAttachmentData(), sizeof(m_gameBootInvite_data)); + m_gameBootInvite = &m_gameBootInvite_data; + } +} + +void SQRNetworkManager_Vita::GetJoinablePresenceDataAndProcess(SceAppUtilNpBasicJoinablePresenceParam* pJoinablePresenceData) +{ + memcpy(&m_joinablePresenceParam, pJoinablePresenceData, sizeof(SceAppUtilNpBasicJoinablePresenceParam)); + if(s_safeToRespondToGameBootInvite && ProfileManager.IsSignedInLive(ProfileManager.GetPrimaryPad())) + { + ProcessJoinablePresenceData(); + } + else + { + m_bJoinablePresenceWaitingForOnline = true; + } +} + +void SQRNetworkManager_Vita::ProcessJoinablePresenceData() +{ + static SceNpBasicGamePresence presenceDetails; + int ret = sceNpBasicGetGamePresenceOfFriend(&m_joinablePresenceParam.npId, &presenceDetails); + if( ret == 0 ) + { + PresenceSyncInfo *pso = (PresenceSyncInfo *)presenceDetails.inGamePresence.data; + memcpy(&m_gameBootInvite_data, pso, sizeof(m_gameBootInvite_data)); + m_gameBootInvite = &m_gameBootInvite_data; + } + m_bJoinablePresenceWaitingForOnline = false; +} + + + + +// This case happens when we were in the main menus when we got an invite, and weren't signed in... now can proceed with the normal flow of code for this situation +// The pair of methods MustSignInReturned_1 & PSNSignInReturned_1 handle this +int MustSignInReturnedPresenceInvite(void *pParam,int iPad,C4JStorage::EMessageResult result) +{ + if(result==C4JStorage::EMessage_ResultAccept) + { + SQRNetworkManager_Vita::AttemptPSNSignIn(&SQRNetworkManager_Vita::PSNSignInReturnedPresenceInvite, pParam,true); + } + return 0; +} + +int SQRNetworkManager_Vita::PSNSignInReturnedPresenceInvite(void* pParam, bool bContinue, int iPad) +{ + INVITE_INFO *inviteInfo = (INVITE_INFO *)pParam; + + // If the invite data isn't set up yet (indicated by it being all zeroes, easiest detected via the net version), then try and get it again... this can happen if we got + // the invite whilst signed out + + if( bContinue ) + { + m_bJoinablePresenceWaitingForOnline = true; + } + return 0; +} + + + +void SQRNetworkManager_Vita::TickJoinablePresenceData() +{ + if(s_safeToRespondToGameBootInvite && m_bJoinablePresenceWaitingForOnline) + { + if(ProfileManager.IsSignedInLive(ProfileManager.GetPrimaryPad())) + ProcessJoinablePresenceData(); + else + { + m_bJoinablePresenceWaitingForOnline = false; // will be set to true again if we sign in succesfully + // Determine why they're not "signed in live" + // MGH - we need to add a new message at some point for connecting when already signed in + // if (ProfileManager.IsSignedInPSN(ProfileManager.GetPrimaryPad())) + // { + // // Signed in to PSN but not connected (no internet access) + // UINT uiIDA[1]; + // uiIDA[0] = IDS_OK; + // ui.RequestMessageBox( IDS_ERROR_NETWORK_TITLE, IDS_ERROR_NETWORK, uiIDA, 1, ProfileManager.GetPrimaryPad(), NULL, NULL, app.GetStringTable()); + // } + // else + { + // Not signed in to PSN + UINT uiIDA[1]; + uiIDA[0] = IDS_PRO_NOTONLINE_ACCEPT; + ui.RequestMessageBox( IDS_PRO_NOTONLINE_TITLE, IDS_PRO_NOTONLINE_TEXT, uiIDA, 1, ProfileManager.GetPrimaryPad(), &MustSignInReturnedPresenceInvite, NULL, app.GetStringTable(), NULL, 0, false); + } + + } + + } +} + +bool SQRNetworkManager_Vita::UpdateInviteData(SQRNetworkManager_Vita::PresenceSyncInfo *invite) +{ + PSVITA_STUBBED; + return false; + + // size_t dataSize = sizeof(SQRNetworkManager_Vita::PresenceSyncInfo); + // int ret = sceNpBasicRecvMessageAttachmentLoad(s_lastInviteIdToRetry, invite, &dataSize); + // return (ret == 0); +} + +// This method is a helper used in MapRoomSlotPlayers - tries to find a player that matches: +// (1) the playerType +// (2) if playerType is remote, memberId +// (3) localPlayerIdx +// The reason we don't care about memberid when the player isn't remote is that it doesn't matter (since we know the player is either on this machine, or it is the host and there's only one of those), +// and there's a period when starting up the host game where it doesn't accurately know the memberId for its own local players +void SQRNetworkManager_Vita::FindOrCreateNonNetworkPlayer(int slot, int playerType, SceNpMatching2RoomMemberId memberId, int localPlayerIdx, int smallId) +{ + for(AUTO_VAR(it, m_vecTempPlayers.begin()); it != m_vecTempPlayers.end(); it++ ) + { + if( ((*it)->m_type == playerType ) && ( (*it)->m_localPlayerIdx == localPlayerIdx ) ) + { + if( ( playerType != SQRNetworkPlayer::SNP_TYPE_REMOTE ) || ( (*it)->m_roomMemberId == memberId ) ) + { + SQRNetworkPlayer *player = *it; + m_vecTempPlayers.erase(it); + m_aRoomSlotPlayers[ slot ] = player; + return; + } + } + } + // Create the player - non-network players can be considered complete as soon as we create them as we aren't waiting on their network connections becoming complete, so can flag them as such and notify via callback + PlayerUID *pUID = NULL; + PlayerUID localUID; + if( ( playerType == SQRNetworkPlayer::SNP_TYPE_LOCAL ) || + m_isHosting && ( playerType == SQRNetworkPlayer::SNP_TYPE_HOST ) ) + { + // Local players can establish their UID at this point + ProfileManager.GetXUID(localPlayerIdx,&localUID,true); + pUID = &localUID; + } + SQRNetworkPlayer *player = new SQRNetworkPlayer(this, (SQRNetworkPlayer::eSQRNetworkPlayerType)playerType, m_isHosting, memberId, localPlayerIdx, 0, pUID ); + // For offline games, set name directly from gamertag as the PlayerUID will be full of zeroes. + if( m_offlineGame ) + { + player->SetName(ProfileManager.GetGamertag(localPlayerIdx)); + } + NonNetworkPlayerComplete( player, smallId); + m_aRoomSlotPlayers[ slot ] = player; + HandlePlayerJoined( player ); +} + +// For data sending on the local machine, used to send between host and localplayers on the host +void SQRNetworkManager_Vita::LocalDataSend(SQRNetworkPlayer *playerFrom, SQRNetworkPlayer *playerTo, const void *data, unsigned int dataSize) +{ + assert(m_isHosting); + if(m_listener) + { + m_listener->HandleDataReceived( playerFrom, playerTo, (unsigned char *)data, dataSize ); + } +} + +int SQRNetworkManager_Vita::GetSessionIndex(SQRNetworkPlayer *player) +{ + int roomSlotPlayerCount = m_roomSyncData.getPlayerCount(); + for( int i = 0; i < roomSlotPlayerCount; i++ ) + { + if( m_aRoomSlotPlayers[i] == player ) return i; + } + return 0; +} + +// Updates m_aRoomSlotPlayers, based on what is in m_roomSyncData. This needs to be updated when room members join & leave, and when any SQRNetworkPlayer is created externally that this should be mapping to +void SQRNetworkManager_Vita::MapRoomSlotPlayers(int roomSlotPlayerCount/*=-1*/) +{ + EnterCriticalSection(&m_csRoomSyncData); + + // If we pass an explicit roomSlotPlayerCount, it is because we are removing a player, and this is the count of slots that there were *before* the removal. + bool zeroLastSlot = false; + if( roomSlotPlayerCount == -1 ) + { + roomSlotPlayerCount = m_roomSyncData.getPlayerCount(); + } + else + { + zeroLastSlot = true; + } + + if( m_isHosting ) + { + for( int i = 0; i < roomSlotPlayerCount; i++ ) + { + if( m_aRoomSlotPlayers[i] ) + { + // On host, remote players are created and destroyed by the Rudp connections being established and removed, so don't go deleting them here. Other types are managed by this mapping. + // Note that m_vecTempPlayers is used as a pool of players to consider by FindOrCreateNonNetworkPlayer + if( m_aRoomSlotPlayers[i]->m_type != SQRNetworkPlayer::SNP_TYPE_REMOTE ) + { + m_vecTempPlayers.push_back(m_aRoomSlotPlayers[i]); + m_aRoomSlotPlayers[i] = NULL; + } + } + } + for( int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( i == 0 ) + { + // Special case - slot 0 is always the host + FindOrCreateNonNetworkPlayer( i, SQRNetworkPlayer::SNP_TYPE_HOST, m_roomSyncData.players[i].m_roomMemberId, m_roomSyncData.players[i].m_localIdx, m_roomSyncData.players[i].m_smallId); + m_roomSyncData.players[i].m_UID = m_aRoomSlotPlayers[i]->GetUID(); // On host, UIDs flow from player data -> m_roomSyncData + } + else + { + if( m_roomSyncData.players[i].m_roomMemberId == m_localMemberId ) + { + FindOrCreateNonNetworkPlayer( i, SQRNetworkPlayer::SNP_TYPE_LOCAL, m_roomSyncData.players[i].m_roomMemberId, m_roomSyncData.players[i].m_localIdx, m_roomSyncData.players[i].m_smallId); + m_roomSyncData.players[i].m_UID = m_aRoomSlotPlayers[i]->GetUID(); // On host, UIDs flow from player data -> m_roomSyncData + } + else + { + m_aRoomSlotPlayers[i] = GetPlayerFromRoomMemberAndLocalIdx( m_roomSyncData.players[i].m_roomMemberId, m_roomSyncData.players[i].m_localIdx ); + // If we're the host, then we allocated the small id so can flag now if we've got a player to flag... + if( m_aRoomSlotPlayers[i] ) + { + NetworkPlayerSmallIdAllocated(m_aRoomSlotPlayers[i], m_roomSyncData.players[i].m_smallId); + } + } + } + } + + if( zeroLastSlot ) + { + if( roomSlotPlayerCount ) + { + m_aRoomSlotPlayers[ roomSlotPlayerCount - 1 ] = 0; + } + } + + // Also update the externally visible room data for the current slots + if (m_listener ) + { + m_listener->HandleResyncPlayerRequest(m_aRoomSlotPlayers); + } + } + else + { + for( int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( m_aRoomSlotPlayers[i] ) + { + // On clients, local players are created and destroyed by the Rudp connections being established and removed, so don't go deleting them here. Other types are managed by this mapping. + // Note that m_vecTempPlayers is used as a pool of players to consider by FindOrCreateNonNetworkPlayer + if( m_aRoomSlotPlayers[i]->m_type != SQRNetworkPlayer::SNP_TYPE_LOCAL ) + { + m_vecTempPlayers.push_back(m_aRoomSlotPlayers[i]); + m_aRoomSlotPlayers[i] = NULL; + } + } + } + for( int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( i == 0 ) + { + // Special case - slot 0 is always the host + FindOrCreateNonNetworkPlayer( i, SQRNetworkPlayer::SNP_TYPE_HOST, m_roomSyncData.players[i].m_roomMemberId, m_roomSyncData.players[i].m_localIdx, m_roomSyncData.players[i].m_smallId); + m_aRoomSlotPlayers[i]->SetUID(m_roomSyncData.players[i].m_UID); // On client, UIDs flow from m_roomSyncData->player data + } + else + { + if( m_roomSyncData.players[i].m_roomMemberId == m_localMemberId ) + { + // This player is local to this machine - don't bother setting UID from sync data, as it will already have been set accurately when we (locally) made this player + m_aRoomSlotPlayers[i] = GetPlayerFromRoomMemberAndLocalIdx( m_roomSyncData.players[i].m_roomMemberId, m_roomSyncData.players[i].m_localIdx ); + // If we've got the room sync data back from the server, then we've got our smallId. Set flag for this. + if( m_aRoomSlotPlayers[i] ) + { + NetworkPlayerSmallIdAllocated(m_aRoomSlotPlayers[i], m_roomSyncData.players[i].m_smallId); + } + } + else + { + FindOrCreateNonNetworkPlayer( i, SQRNetworkPlayer::SNP_TYPE_REMOTE, m_roomSyncData.players[i].m_roomMemberId, m_roomSyncData.players[i].m_localIdx, m_roomSyncData.players[i].m_smallId); + m_aRoomSlotPlayers[i]->SetUID(m_roomSyncData.players[i].m_UID); // On client, UIDs flow from m_roomSyncData->player data + } + } + } + } + // Clear up any non-network players that are no longer required - this would be a good point to notify of players leaving when we support that + // FindOrCreateNonNetworkPlayer will have pulled any players that we Do need out of m_vecTempPlayers, so the ones that are remaining are no longer in the game + for(AUTO_VAR(it, m_vecTempPlayers.begin()); it != m_vecTempPlayers.end(); it++ ) + { + if( m_listener ) + { + m_listener->HandlePlayerLeaving(*it); + } + delete (*it); + } + m_vecTempPlayers.clear(); + + LeaveCriticalSection(&m_csRoomSyncData); +} + +// On host, update the room sync data with UIDs that are in the players +void SQRNetworkManager_Vita::UpdateRoomSyncUIDsFromPlayers() +{ + EnterCriticalSection(&m_csRoomSyncData); + if( m_isHosting ) + { + for( int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( m_aRoomSlotPlayers[i] ) + { + m_roomSyncData.players[i].m_UID = m_aRoomSlotPlayers[i]->GetUID(); + } + } + } + + LeaveCriticalSection(&m_csRoomSyncData); +} + +// On the client, move UIDs from the room sync data out to the players. +void SQRNetworkManager_Vita::UpdatePlayersFromRoomSyncUIDs() +{ + EnterCriticalSection(&m_csRoomSyncData); + for( int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( m_aRoomSlotPlayers[i] ) + { + if( i == 0 ) + { + // Special case - slot 0 is always the host + m_aRoomSlotPlayers[i]->SetUID(m_roomSyncData.players[i].m_UID); + } + else + { + // Don't sync local players as we already set those up with their UID in the first place... + if( m_roomSyncData.players[i].m_roomMemberId != m_localMemberId ) + { + m_aRoomSlotPlayers[i]->SetUID(m_roomSyncData.players[i].m_UID); + } + } + } + } + LeaveCriticalSection(&m_csRoomSyncData); +} + +// Host only - add remote players to our internal storage of player slots, and synchronise this with other room members. +bool SQRNetworkManager_Vita::AddRemotePlayersAndSync( SceNpMatching2RoomMemberId memberId, int playerMask, bool *isFull/*==NULL*/ ) +{ + assert( m_isHosting ); + + EnterCriticalSection(&m_csRoomSyncData); + + // Establish whether we have enough room to add the players + int addCount = 0; + for( int i = 0; i < MAX_LOCAL_PLAYERS; i++ ) + { + if( playerMask & ( 1 << i ) ) + { + addCount++; + } + } + + if( ( m_roomSyncData.getPlayerCount() + addCount ) > MAX_ONLINE_PLAYER_COUNT ) + { + if( isFull ) + { + *isFull = true; + } + LeaveCriticalSection(&m_csRoomSyncData); + return false; + } + + // We want to keep all players from a particular machine together, so search through the room sync data to see if we can find + // any pre-existing players from this machine. + int firstIdx = -1; + for( int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( m_roomSyncData.players[i].m_roomMemberId == memberId ) + { + firstIdx = i; + break; + } + } + + // We'll just be inserting at the end unless we've got a pre-existing player to insert after. Even then there might be no following + // players. + int insertIdx = m_roomSyncData.getPlayerCount(); + if( firstIdx > -1 ) + { + for( int i = firstIdx; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( m_roomSyncData.players[i].m_roomMemberId != memberId ) + { + insertIdx = i; + break; + } + } + } + + // Add all remote players determined from the player mask to our own slots of active players + for( int i = 0; i < MAX_LOCAL_PLAYER_COUNT; i++ ) + { + if( playerMask & ( 1 << i ) ) + { + // Shift any following players along... + for( int j = m_roomSyncData.getPlayerCount(); j > insertIdx; j-- ) + { + m_roomSyncData.players[j] = m_roomSyncData.players[j-1]; + } + PlayerSyncData *player = &m_roomSyncData.players[ insertIdx ]; + player->m_smallId = m_currentSmallId++; + player->m_roomMemberId = memberId; + player->m_localIdx = i; + m_roomSyncData.setPlayerCount(m_roomSyncData.getPlayerCount()+1); + insertIdx++; + } + } + + // Update mapping from the room slot players to SQRNetworkPlayer instances + MapRoomSlotPlayers(); + + // And then synchronise this out to all other machines + SyncRoomData(); + + LeaveCriticalSection(&m_csRoomSyncData); + + return true; +} + +// Host only - remove all remote players belonging to the supplied memberId, and in the supplied mask, and synchronise this with other room members +void SQRNetworkManager_Vita::RemoveRemotePlayersAndSync( SceNpMatching2RoomMemberId memberId, int mask ) +{ + assert( m_isHosting ); + EnterCriticalSection(&m_csRoomSyncData); + + // Remove any applicable players, keeping remaining players in order + for( int i = 0; i < m_roomSyncData.getPlayerCount(); ) + { + if( ( m_roomSyncData.players[ i ].m_roomMemberId == memberId ) && ( ( 1 << m_roomSyncData.players[ i ].m_localIdx ) & mask ) ) + { + SQRNetworkPlayer *player = GetPlayerFromRoomMemberAndLocalIdx( memberId, m_roomSyncData.players[ i ].m_localIdx ); + if( player ) + { + // Get Rudp context for this player, close that context down ( which will in turn close the socket if required) + int ctx = player->m_rudpCtx; + int err = sceRudpTerminate( ctx ); + assert(err == SCE_OK); + if( m_listener ) + { + m_listener->HandlePlayerLeaving(player); + } + // Delete the player itself and the mapping from context to player map as this context is no longer valid + delete player; + m_RudpCtxToPlayerMap.erase(ctx); + + removePlayerFromVoiceChat(player); + } + m_roomSyncData.setPlayerCount(m_roomSyncData.getPlayerCount()-1); + // Shuffled entries up into the space that we have just created + for( int j = i ; j < m_roomSyncData.getPlayerCount(); j++ ) + { + m_roomSyncData.players[j] = m_roomSyncData.players[j + 1]; + m_aRoomSlotPlayers[j] = m_aRoomSlotPlayers[j + 1]; + } + // Zero last element, that isn't part of the currently sized array anymore + memset(&m_roomSyncData.players[m_roomSyncData.getPlayerCount()],0,sizeof(PlayerSyncData)); + m_aRoomSlotPlayers[m_roomSyncData.getPlayerCount()] = NULL; + } + else + { + i++; + } + } + LeaveCriticalSection(&m_csRoomSyncData); + + // Update mapping from the room slot players to SQRNetworkPlayer instances + MapRoomSlotPlayers(); + + + // And then synchronise this out to all other machines + SyncRoomData(); + + // if(GetOnlinePlayerCount() == 0) + // SonyVoiceChat::shutdown(); +} + +// Client only - remove all network players matching the supplied mask +void SQRNetworkManager_Vita::RemoveNetworkPlayers( int mask ) +{ + assert( !m_isHosting ); + + for(AUTO_VAR(it, m_RudpCtxToPlayerMap.begin()); it != m_RudpCtxToPlayerMap.end(); ) + { + SQRNetworkPlayer *player = it->second; + if( (player->m_roomMemberId == m_localMemberId ) && ( ( 1 << player->m_localPlayerIdx ) & mask ) ) + { + // Get Rudp context for this player, close that context down ( which will in turn close the socket if required) + int ctx = it->first; + int err = sceRudpTerminate( ctx ); + assert(err == SCE_OK); + if( m_listener ) + { + m_listener->HandlePlayerLeaving(player); + } + // Delete any reference to this player from the player mappings + for( int i = 0; i < MAX_ONLINE_PLAYER_COUNT; i++ ) + { + if( m_aRoomSlotPlayers[i] == player ) + { + m_aRoomSlotPlayers[i] = NULL; + } + } + // And delete the reference from the ctx->player map + it = m_RudpCtxToPlayerMap.erase(it); + + removePlayerFromVoiceChat(player); + + // Delete the player itself and the mapping from context to player map as this context is no longer valid + delete player; + } + else + { + it++; + } + } + assert(m_RudpCtxToPlayerMap.size() == 0); +} + +// Host only - update the memberId of the local players, and synchronise with other room members +void SQRNetworkManager_Vita::SetLocalPlayersAndSync() +{ + assert( m_isHosting ); + for( int i = 0; i < m_localPlayerCount; i++ ) + { + m_roomSyncData.players[i].m_roomMemberId = m_localMemberId; + } + + // Update mapping from the room slot players to SQRNetworkPlayer instances + MapRoomSlotPlayers(); + + // And then synchronise this out to all other machines + SyncRoomData(); + +} + +// Host only - sync the room data with other machines +void SQRNetworkManager_Vita::SyncRoomData() +{ + if( m_offlineGame ) return; + + UpdateRoomSyncUIDsFromPlayers(); + + SceNpMatching2SetRoomDataInternalRequest reqParam; + memset( &reqParam, 0, sizeof(reqParam) ); + reqParam.roomId = m_room; + SceNpMatching2BinAttr roomBinAttr; + memset(&roomBinAttr, 0, sizeof(roomBinAttr)); + roomBinAttr.id = SCE_NP_MATCHING2_ROOM_BIN_ATTR_INTERNAL_1_ID; + roomBinAttr.ptr = &m_roomSyncData; + roomBinAttr.size = sizeof( m_roomSyncData ); + reqParam.roomBinAttrInternalNum = 1; + reqParam.roomBinAttrInternal = &roomBinAttr; + sceNpMatching2SetRoomDataInternal ( m_matchingContext, &reqParam, NULL, &m_setRoomDataRequestId ); +} + +// Check if the matching context is valid, and if not attempt to create one. If to do this requires starting an asynchronous process, then sets the internal state to the state passed in +// before doing this. +// Returns true on success. +bool SQRNetworkManager_Vita::GetMatchingContext(eSQRNetworkManagerInternalState asyncState) +{ + if( m_matchingContextValid ) return true; + + int ret = 0; + if( !m_matching2initialised) + { + app.DebugPrintf("sceNpMatching2Init\n"); + ret = sceNpMatching2Init(0, 0, SCE_KERNEL_THREAD_CPU_AFFINITY_MASK_DEFAULT, 0); + } + if( ret < 0 ) + { + app.DebugPrintf("SQRNetworkManager::GetMatchingContext - sceNpMatching2Init2 failed with code 0x%08x\n", ret); + return false; + } + m_matching2initialised = true; + + // Get NP ID of the signed-in user + SceNpId npId; + app.DebugPrintf("GetSceNpId\n"); + + /*ret = */ProfileManager.GetSceNpId(ProfileManager.GetPrimaryPad(), &npId); + + + // Create context + app.DebugPrintf("sceNpMatching2CreateContext\n"); + ret = sceNpMatching2CreateContext(&npId, &s_npCommunicationId, &s_npCommunicationPassphrase, &m_matchingContext/*, option*/); + //ret = sceNpMatching2CreateContext(&npId, NULL,NULL, &m_matchingContext/*, option*/); + if( ret < 0 ) + { + app.DebugPrintf("SQRNetworkManager::GetMatchingContext - sceNpMatching2CreateContext failed with code 0x%08x\n", ret); + return false; + } + if( ret < 0 ) return false; + + app.DebugPrintf("RegisterCallbacks\n"); + if( !RegisterCallbacks() ) + { + app.DebugPrintf("SQRNetworkManager::GetMatchingContext - RegisterCallbacks failed\n"); + return false; + } + + // Set internal state & kick off async process that will actually start the context. + SetState(asyncState); + app.DebugPrintf("sceNpMatching2ContextStart\n"); + ret = sceNpMatching2ContextStart(m_matchingContext, (10*1000*1000)); + if( ret < 0 ) + { + // Put state back so that the caller isn't expecting a callback from sceNpMatching2ContextStartAsync completing to happen + SetState(SNM_INT_STATE_IDLE); + app.DebugPrintf("SQRNetworkManager::GetMatchingContext - sceNpMatching2ContextStartAsync failed with code 0x%08x\n", ret); + return false; + } + + app.DebugPrintf("SQRNetworkManager::GetMatchingContext - matching context is now valid\n"); + m_matchingContextValid = true; + return true; +} + +// Starts the process of obtaining a server context. This is an asynchronous operation, at the end of which (if successful), we'll be creating +// a room. General procedure followed here is as suggested by Sony - we get a list of servers, then pick a random one, and see if it is available. +// If not we just cycle round trying other random ones until we either find an available one or fail. +bool SQRNetworkManager_Vita::GetServerContext() +{ + assert(m_state == SNM_INT_STATE_IDLE); + assert(m_serverContextValid == false); + + // Check that the matching context is valid & recreate if necessary + if( !GetMatchingContext(SNM_INT_STATE_HOSTING_STARTING_MATCHING_CONTEXT) ) return false; + // If this caused an async thing to be started up, then we've done as much as we can here - the rest of the code will happen when the async matching 2 context starting completes + // ( event SCE_NP_MATCHING2_CONTEXT_EVENT_Start is received ) + if( m_state == SNM_INT_STATE_HOSTING_STARTING_MATCHING_CONTEXT ) return true; + + return GetServerContext2(); +} + +// Code split out from previous method, so we can also call from creating matching context if required +bool SQRNetworkManager_Vita::GetServerContext2() +{ + + m_aServerId = (SceNpMatching2ServerId *)realloc( m_aServerId, sizeof(SceNpMatching2ServerId) * 1 ); + SceNpMatching2Server server; + app.DebugPrintf("sceNpMatching2GetServerLocal\n"); + int err = sceNpMatching2GetServerLocal(m_matchingContext, &server); + assert(server.status == SCE_NP_MATCHING2_SERVER_STATUS_AVAILABLE); + *m_aServerId = server.serverId; + if(err != SCE_OK) + { + m_serverCount = 0; + assert(0); + } + m_serverCount = 1; + + SetState(SNM_INT_STATE_HOSTING_SEARCHING_FOR_SERVER); + return SelectRandomServer(); +} + +// Overloaded method for (as before) obtaining a server context. This version is so that can also get a server context for a specific server rather than a random one, +// using mainly the same code by making a single element list. This is used when joining an existing room. +bool SQRNetworkManager_Vita::GetServerContext(SceNpMatching2ServerId serverId) +{ + assert(m_state == SNM_INT_STATE_IDLE); + assert(m_serverContextValid == false); + + // Check that the matching context is valid & recreate if necessary + if( !GetMatchingContext(SNM_INT_STATE_JOINING_STARTING_MATCHING_CONTEXT) ) + { + app.DebugPrintf("SQRNetworkManager::GetServerContext - Failed due to no matching context\n"); + return false; + } + + // 4J Stu - If this state is set, then we have successfully created a new context but it won't have started yet + // Therefore the sceNpMatching2GetServerIdListLocal call will fail. If we just skip this check everything should be good. + // if( m_state != SNM_INT_STATE_JOINING_STARTING_MATCHING_CONTEXT ) + // { + // // Get list of server IDs of servers allocated to the application. We don't actually need to do this, but it is as good a way as any to try a matching2 service and check that + // // the context *really* is valid. + // int serverCount = sceNpMatching2GetServerIdListLocal( m_matchingContext, NULL, 0 ); + // // If an error is returned here, we need to destroy and recerate our server - if this goes ok we should come back through this path again + // if( ( serverCount == SCE_NP_MATCHING2_ERROR_CONTEXT_UNAVAILABLE ) || // This error has been seen (occasionally) in a normal working environment + // ( serverCount == SCE_NP_MATCHING2_ERROR_CONTEXT_NOT_STARTED ) ) // Also checking for this as a means of simulating the previous error + // { + // sceNpMatching2DestroyContext(m_matchingContext); + // m_matchingContextValid = false; + // if( !GetMatchingContext(SNM_INT_STATE_JOINING_STARTING_MATCHING_CONTEXT) ) return false; + // } + // } + m_serverCount = 1; + m_totalServerCount = m_serverCount; + m_aServerId = (SceNpMatching2ServerId *)realloc(m_aServerId, sizeof(SceNpMatching2ServerId) * m_serverCount ); + m_aServerId[0] = serverId; + + // If one of the previous GetMatchingContext calls caused an async thing to be started up, then we've done as much as we can here - the rest of the code will happen when the async matching 2 context starting completes + // ( event SCE_NP_MATCHING2_CONTEXT_EVENT_Start is received ) + if( m_state == SNM_INT_STATE_JOINING_STARTING_MATCHING_CONTEXT ) return true; + + SetState(SNM_INT_STATE_JOINING_SEARCHING_FOR_SERVER); + return SelectRandomServer(); +} + +// Tick to update the search for a server which is available, for the creation of a server context. +void SQRNetworkManager_Vita::ServerContextTick() +{ + switch( m_state ) + { + case SNM_INT_STATE_HOSTING_SEARCHING_FOR_SERVER: + case SNM_INT_STATE_JOINING_SEARCHING_FOR_SERVER: + break; + case SNM_INT_STATE_HOSTING_SERVER_SEARCH_SERVER_ERROR: + case SNM_INT_STATE_JOINING_SERVER_SEARCH_SERVER_ERROR: + // Attempt to keep searching if a single server failed + SetState((m_state==SNM_INT_STATE_HOSTING_SERVER_SEARCH_SERVER_ERROR)?SNM_INT_STATE_HOSTING_SEARCHING_FOR_SERVER:SNM_INT_STATE_JOINING_SEARCHING_FOR_SERVER); + if(!SelectRandomServer()) + { + SetState((m_state==SNM_INT_STATE_HOSTING_SERVER_SEARCH_SERVER_ERROR)?SNM_INT_STATE_HOSTING_SERVER_SEARCH_FAILED:SNM_INT_STATE_JOINING_SERVER_SEARCH_FAILED); + } + break; + case SNM_INT_STATE_HOSTING_SERVER_FOUND: + m_serverContextValid = true; + ServerContextValid_CreateRoom(); + break; + + case SNM_INT_STATE_JOINING_SERVER_FOUND: + m_serverContextValid = true; + ServerContextValid_JoinRoom(); + break; + default: + break; + } +} + +// Tick the process of creating a room. +void SQRNetworkManager_Vita::RoomCreateTick() +{ + switch( m_state ) + { + case SNM_INT_STATE_HOSTING_CREATE_ROOM_SEARCHING_FOR_WORLD: + break; + case SNM_INT_STATE_HOSTING_CREATE_ROOM_WORLD_FOUND: + { + SceNpMatching2CreateJoinRoomRequest reqParam; + SceNpMatching2SignalingOptParam optSignalingParam; + SceNpMatching2BinAttr roomBinAttrExt; + SceNpMatching2BinAttr roomBinAttr; + memset(&reqParam, 0, sizeof(reqParam)); + memset(&optSignalingParam, 0, sizeof( optSignalingParam) ); + memset(&roomBinAttr, 0, sizeof(roomBinAttr)); + memset(&roomBinAttrExt, 0, sizeof(roomBinAttrExt)); + + reqParam.worldId = m_worldId; + reqParam.flagAttr = SCE_NP_MATCHING2_ROOM_FLAG_ATTR_NAT_TYPE_RESTRICTION; + reqParam.sigOptParam = &optSignalingParam; + reqParam.maxSlot = MAX_ONLINE_PLAYER_COUNT; + + reqParam.roomBinAttrInternalNum = 1; + reqParam.roomBinAttrInternal = &roomBinAttr; + reqParam.roomBinAttrExternalNum = 1; + reqParam.roomBinAttrExternal = &roomBinAttrExt; + + roomBinAttr.id = SCE_NP_MATCHING2_ROOM_BIN_ATTR_INTERNAL_1_ID; + roomBinAttr.ptr = &m_roomSyncData; + roomBinAttr.size = sizeof( m_roomSyncData ); + + roomBinAttrExt.id = SCE_NP_MATCHING2_ROOM_BIN_ATTR_EXTERNAL_1_ID; + roomBinAttrExt.ptr = m_joinExtData; + roomBinAttrExt.size = m_joinExtDataSize; + + optSignalingParam.type = SCE_NP_MATCHING2_SIGNALING_TYPE_MESH; + optSignalingParam.hubMemberId = 0; // Room owner is the hub of the star + SetState(SNM_INT_STATE_HOSTING_CREATE_ROOM_CREATING_ROOM); + app.DebugPrintf(CMinecraftApp::USER_RR,">> Creating room start\n"); + s_roomStartTime = System::currentTimeMillis(); + app.DebugPrintf("sceNpMatching2CreateJoinRoom\n"); + int ret = sceNpMatching2CreateJoinRoom( m_matchingContext, &reqParam, NULL, &m_createRoomRequestId ); + if ( ( ret < 0 ) || ForceErrorPoint(SNM_FORCE_ERROR_CREATE_JOIN_ROOM) ) + { + SetState(SNM_INT_STATE_HOSTING_CREATE_ROOM_FAILED); + } + } + break; + case SNM_INT_STATE_HOSTING_CREATE_ROOM_CREATING_ROOM: + break; + case SNM_INT_STATE_HOSTING_CREATE_ROOM_SUCCESS: + SetState(SNM_INT_STATE_HOSTING_WAITING_TO_PLAY); + + // Now we know the local member id we can update our local players + SetLocalPlayersAndSync(); + break; + case SNM_INT_STATE_HOSTING_CREATE_ROOM_FAILED: + break; + default: + break; + } +} + +// For a player using the network to communicate, flag as having its connection complete. This wraps the player's own functionality, so that we can determine if this +// call is transitioning us from not ready to ready, and call a registered callback. +void SQRNetworkManager_Vita::NetworkPlayerConnectionComplete(SQRNetworkPlayer *player) +{ + EnterCriticalSection(&m_csPlayerState); + bool wasReady = player->IsReady(); + bool wasClientReady = player->HasConnectionAndSmallId(); + player->ConnectionComplete(); + bool isReady = player->IsReady(); + bool isClientReady = player->HasConnectionAndSmallId(); + if( !m_isHosting ) + { + // For clients, if we are ready (up the the point of having received our small id) then confirm to the host that this is the case, which makes us now fully ready at this end + if( ( !wasClientReady ) && ( isClientReady ) ) + { + player->ConfirmReady(); + isReady = true; + } + } + LeaveCriticalSection(&m_csPlayerState); + + if( ( !wasReady ) && ( isReady ) ) + { + HandlePlayerJoined( player ); + } +} + +// For a player using the network to communicate, set its small id, thereby flagging it as having one allocated +void SQRNetworkManager_Vita::NetworkPlayerSmallIdAllocated(SQRNetworkPlayer *player, unsigned char smallId) +{ + EnterCriticalSection(&m_csPlayerState); + bool wasReady = player->IsReady(); + bool wasClientReady = player->HasConnectionAndSmallId(); + player->SmallIdAllocated(smallId); + bool isReady = player->IsReady(); + bool isClientReady = player->HasConnectionAndSmallId(); + if( !m_isHosting ) + { + // For clients, if we are ready (up the the point of having received our small id) then confirm to the host that this is the case, which makes us now fully ready at this end + if( ( !wasClientReady ) && ( isClientReady ) ) + { + player->ConfirmReady(); + isReady = true; + } + } + LeaveCriticalSection(&m_csPlayerState); + + if( ( !wasReady ) && ( isReady ) ) + { + HandlePlayerJoined( player ); + } +} + +// On host, for a player using the network to communicate, confirm that its small id has now been received back +void SQRNetworkManager_Vita::NetworkPlayerInitialDataReceived(SQRNetworkPlayer *player, void *data) +{ + EnterCriticalSection(&m_csPlayerState); + SQRNetworkPlayer::InitSendData *ISD = (SQRNetworkPlayer::InitSendData *)data; + bool wasReady = player->IsReady(); + player->InitialDataReceived(ISD); + bool isReady = player->IsReady(); + LeaveCriticalSection(&m_csPlayerState); + // Sync room data back out as we've updated a player's UID here + SyncRoomData(); + + if( ( !wasReady ) && ( isReady ) ) + { + HandlePlayerJoined( player ); + } +} + +// For non-network players, flag that it is complete/ready, and assign its small id. We don't want to call any callbacks for these, as that can be explicitly done when local players are added. +// Also, we dynamically destroy & recreate local players quite a lot when remapping player slots which would create a lot of messages we don't want. +void SQRNetworkManager_Vita::NonNetworkPlayerComplete(SQRNetworkPlayer *player, unsigned char smallId) +{ + player->ConnectionComplete(); + player->SmallIdAllocated(smallId); +} + +void SQRNetworkManager_Vita::HandlePlayerJoined(SQRNetworkPlayer *player) +{ + if( m_listener ) + { + m_listener->HandlePlayerJoined( player ); + } + // On client, keep a count of how many local players we have told the game about. We can only transition to telling the game that we are playing once the room is set up And all the local players are valid to use. + if( !m_isHosting ) + { + if( player->IsLocal() ) + { + m_localPlayerJoined++; + } + } +} + +// Selects a random server from the current list, removes that server so it won't be searched for again, and then kick off an attempt to find out if that particular server is available. +bool SQRNetworkManager_Vita::SelectRandomServer() +{ + app.DebugPrintf("SQRNetworkManager_Vita::SelectRandomServer\n"); + + assert( (m_state == SNM_INT_STATE_HOSTING_SEARCHING_FOR_SERVER) || (m_state == SNM_INT_STATE_JOINING_SEARCHING_FOR_SERVER) ); + + if( m_serverCount == 0 ) + { + SetState((m_state == SNM_INT_STATE_HOSTING_SEARCHING_FOR_SERVER) ? SNM_INT_STATE_HOSTING_SERVER_SEARCH_FAILED : SNM_INT_STATE_JOINING_SERVER_SEARCH_FAILED); + app.DebugPrintf("SQRNetworkManager::SelectRandomServer - Server count is 0\n"); + return false; + } + + // not really selecting a random server, as we've already been allocated one, but calling this to match PS3 + int serverIdx; + serverIdx = 0; + m_serverCount--; + m_aServerId[serverIdx] = m_aServerId[m_serverCount]; + + // This server is available + SetState((m_state == SNM_INT_STATE_HOSTING_SEARCHING_FOR_SERVER) ? SNM_INT_STATE_HOSTING_SERVER_FOUND : SNM_INT_STATE_JOINING_SERVER_FOUND); + m_serverId = m_aServerId[serverIdx]; + + return true; +} + +// Delete the current server context. Should be called when finished with the current host or client game session. +void SQRNetworkManager_Vita::DeleteServerContext() +{ + // No server context on PS4, so we just set the state, and then we'll check all the UDP connections have shutdown before setting to idle + if( m_serverContextValid ) + { + m_serverContextValid = false; + SetState(SNM_INT_STATE_SERVER_DELETING_CONTEXT); + } +} + +// Creates a set of Rudp connections by the "active open" method. This requires that both ends of the connection call cellRudpInitiate to fully create a connection. We +// create one connection per local play on any remote machine. +// +// peerMemberId is the room member Id of the remote end of the connection +// playersMemberId is the room member Id that the players belong to +// ie for the host (when matching incoming connections), these will be the same thing... and for the client, peerMemberId will be the host, whereas playersMemberId will be itself + + +static std::string getIPAddressString(SceNetInAddr add) +{ + char str[32]; + unsigned char *vals = (unsigned char*)&add.s_addr; + sprintf(str, "%d.%d.%d.%d", (int)vals[0], (int)vals[1], (int)vals[2], (int)vals[3]); + return std::string(str); +} + +bool SQRNetworkManager_Vita::CreateSocket() +{ + // First get details of the UDPP2P connection that has been established + // int connStatus; + SceNetSockaddrIn sinp2pLocal;//, sinp2pPeer; + SceNpMatching2SignalingNetInfo netInfo; + + // Local end first... + memset(&sinp2pLocal, 0, sizeof(sinp2pLocal)); + memset(&netInfo, 0 , sizeof(netInfo)); + netInfo.size = sizeof(netInfo); + int ret = sceNpMatching2SignalingGetLocalNetInfo(&netInfo); + if( ret < 0 ) return false; + sinp2pLocal.sin_len = sizeof(sinp2pLocal); + sinp2pLocal.sin_family = SCE_NET_AF_INET; + sinp2pLocal.sin_port = sceNetHtons(SCE_NP_PORT); + sinp2pLocal.sin_addr = netInfo.localAddr; + + + // Set vport for both ends of connection + sinp2pLocal.sin_vport = sceNetHtons(1); + + // Create socket & bind + ret = sceNetSocket("rupdSocket", SCE_NET_AF_INET, SCE_NET_SOCK_DGRAM_P2P, 0); + assert(ret >= 0); + m_soc = ret; + int optval = 1; + ret = sceNetSetsockopt(m_soc, SCE_NET_SOL_SOCKET, SCE_NET_SO_USECRYPTO, &optval, sizeof(optval)); + if ( ( ret < 0 ) || ForceErrorPoint(SNM_FORCE_ERROR_SETSOCKOPT_0) ) return false; + ret = sceNetSetsockopt(m_soc, SCE_NET_SOL_SOCKET, SCE_NET_SO_USESIGNATURE, &optval, sizeof(optval)); + if ( ( ret < 0 ) || ForceErrorPoint(SNM_FORCE_ERROR_SETSOCKOPT_1) ) return false; + ret = sceNetSetsockopt(m_soc, SCE_NET_SOL_SOCKET, SCE_NET_SO_NBIO, &optval, sizeof(optval)); + if ( ( ret < 0 ) || ForceErrorPoint(SNM_FORCE_ERROR_SETSOCKOPT_2) ) return false; + + ret = sceNetBind(m_soc, &sinp2pLocal, sizeof(sinp2pLocal)); + if ( ( ret < 0 ) || ForceErrorPoint(SNM_FORCE_ERROR_SOCK_BIND) ) return false; + return true; + +} + + +bool SQRNetworkManager_Vita::CreateVoiceRudpConnections(SceNpMatching2RoomId roomId, SceNpMatching2RoomMemberId peerMemberId, int playerMask) +{ + SceNetSockaddrIn sinp2pPeer; + SceNpMatching2SignalingNetInfo netInfo; + int connStatus; + + memset(&sinp2pPeer, 0, sizeof(sinp2pPeer)); + sinp2pPeer.sin_len = sizeof(sinp2pPeer); + sinp2pPeer.sin_family = SCE_NET_AF_INET; + int ret = sceNpMatching2SignalingGetConnectionStatus(m_matchingContext, roomId, peerMemberId, &connStatus, &sinp2pPeer.sin_addr, &sinp2pPeer.sin_port); + sinp2pPeer.sin_vport = sceNetHtons(1); + + + ret = 0; + // Create socket & bind, if we don't already have one + if( m_soc == -1 ) + { + if(CreateSocket() == false) + return false; + } + + // create this connection if we don't have it already + SQRVoiceConnection* pConnection = SonyVoiceChat_Vita::getVoiceConnectionFromRoomMemberID(peerMemberId); + if(pConnection == NULL) + { + + // Create an Rudp context for the voice connection, this will happen regardless of whether the peer is client or host + int rudpCtx; + ret = sceRudpCreateContext( RudpContextCallback, this, &rudpCtx ); + if(ret < 0){ app.DebugPrintf("sceRudpCreateContext failed : 0x%08x\n", ret); assert(0); } + if ( ( ret < 0 ) || ForceErrorPoint(SNM_FORCE_ERROR_CREATE_RUDP_CONTEXT) ) return false; + + // Bind the context to the socket we've just created, and initiate. The initiation needs to happen on both client & host sides of the connection to complete. + ret = sceRudpBind( rudpCtx, m_soc , 5, SCE_RUDP_MUXMODE_P2P ); + if(ret < 0){ app.DebugPrintf("sceRudpBind failed : 0x%08x\n", ret); assert(0); } + if ( ( ret < 0 ) || ForceErrorPoint(SNM_FORCE_ERROR_RUDP_BIND) ) return false; + + ret = sceRudpInitiate( rudpCtx, (SceNetSockaddr*)&sinp2pPeer, sizeof(sinp2pPeer), 0); + if(ret < 0){ app.DebugPrintf("sceRudpInitiate failed : 0x%08x\n", ret); assert(0); } + if ( ( ret < 0 ) || ForceErrorPoint(SNM_FORCE_ERROR_RUDP_INIT2) ) return false; + + app.DebugPrintf("-----------------------------\n"); + app.DebugPrintf("Voice rudp context created %d connected to %s\n", rudpCtx, getIPAddressString(sinp2pPeer.sin_addr).c_str()); + app.DebugPrintf("-----------------------------\n"); + + pConnection = SonyVoiceChat_Vita::addRemoteConnection(rudpCtx, peerMemberId); + } + + for( int i = 0; i < MAX_LOCAL_PLAYER_COUNT; i++ ) + { + bool bMaskVal = ( playerMask & ( 1 << i ) ); + + if(bMaskVal || GetLocalPlayerByUserIndex(i)) + SonyVoiceChat_Vita::connectPlayer(pConnection, i); + } + return true; +} + + + +bool SQRNetworkManager_Vita::CreateRudpConnections(SceNpMatching2RoomId roomId, SceNpMatching2RoomMemberId peerMemberId, int playerMask, SceNpMatching2RoomMemberId playersMemberId) +{ + // First get details of the UDPP2P connection that has been established + int connStatus; + SceNetSockaddrIn sinp2pPeer; + + // get the peer + memset(&sinp2pPeer, 0, sizeof(sinp2pPeer)); + sinp2pPeer.sin_len = sizeof(sinp2pPeer); + sinp2pPeer.sin_family = SCE_NET_AF_INET; + + int ret = sceNpMatching2SignalingGetConnectionStatus(m_matchingContext, roomId, peerMemberId, &connStatus, &sinp2pPeer.sin_addr, &sinp2pPeer.sin_port); + app.DebugPrintf(CMinecraftApp::USER_RR,"sceNpMatching2SignalingGetConnectionStatus returned 0x%x, connStatus %d peer add:%s peer port:0x%x\n",ret, connStatus,getIPAddressString(sinp2pPeer.sin_addr).c_str(),sinp2pPeer.sin_port); + + // Set vport + sinp2pPeer.sin_vport = sceNetHtons(1); + + // Create socket & bind, if we don't already have one + if( m_soc == -1 ) + { + if(CreateSocket() == false) + return false; + } + + // Create an Rudp context for each local player that is required. These can be used as individual virtual connections between room members (ie consoles), which are multiplexed + // over the socket we have just made + for( int i = 0; i < MAX_LOCAL_PLAYER_COUNT; i++ ) + { + if( ( playerMask & ( 1 << i ) ) == 0 ) continue; + + int rudpCtx; + + // Socket for the local network node created, now can create an Rupd context. + ret = sceRudpCreateContext( RudpContextCallback, this, &rudpCtx ); + if ( ( ret < 0 ) || ForceErrorPoint(SNM_FORCE_ERROR_CREATE_RUDP_CONTEXT) ) return false; + if( m_isHosting ) + { + m_RudpCtxToPlayerMap[ rudpCtx ] = new SQRNetworkPlayer( this, SQRNetworkPlayer::SNP_TYPE_REMOTE, true, playersMemberId, i, rudpCtx, NULL ); + } + else + { + // Local players can establish their UID at this point + PlayerUID localUID; + ProfileManager.GetXUID(i,&localUID,true); + + m_RudpCtxToPlayerMap[ rudpCtx ] = new SQRNetworkPlayer( this, SQRNetworkPlayer::SNP_TYPE_LOCAL, false, m_localMemberId, i, rudpCtx, &localUID ); + } + + // If we've created a player, then we want to try and patch up any connections that we should have to it + MapRoomSlotPlayers(); + + // TODO - set any non-default options for the context. By default, the context is set to have delivery critical and order critical both on + + // Bind the context to the socket we've just created, and initiate. The initiation needs to happen on both client & host sides of the connection to complete. + ret = sceRudpBind( rudpCtx, m_soc , 1 + i, SCE_RUDP_MUXMODE_P2P ); + if ( ( ret < 0 ) || ForceErrorPoint(SNM_FORCE_ERROR_RUDP_BIND) ) return false; + + ret = sceRudpInitiate( rudpCtx, &sinp2pPeer, sizeof(sinp2pPeer), 0); + if ( ( ret < 0 ) || ForceErrorPoint(SNM_FORCE_ERROR_RUDP_INIT2) ) return false; + } + return true; +} + + +SQRNetworkPlayer *SQRNetworkManager_Vita::GetPlayerFromRudpCtx(int rudpCtx) +{ + AUTO_VAR(it,m_RudpCtxToPlayerMap.find(rudpCtx)); + if( it != m_RudpCtxToPlayerMap.end() ) + { + return it->second; + } + return NULL; +} + + + +SQRNetworkPlayer *SQRNetworkManager_Vita::GetPlayerFromRoomMemberAndLocalIdx(int roomMember, int localIdx) +{ + for(AUTO_VAR(it, m_RudpCtxToPlayerMap.begin()); it != m_RudpCtxToPlayerMap.end(); it++ ) + { + if( (it->second->m_roomMemberId == roomMember ) && ( it->second->m_localPlayerIdx == localIdx ) ) + { + return it->second; + } + } + return NULL; +} + + +// This is called as part of the general initialisation of the network manager, to register any callbacks that the sony libraries require. +// Returns true if all were registered successfully. +bool SQRNetworkManager_Vita::RegisterCallbacks() +{ + // Register RUDP event handler + app.DebugPrintf("sceRudpSetEventHandler\n"); + int ret = sceRudpSetEventHandler(RudpEventCallback, this); + if (ret < 0) + { + app.DebugPrintf("SQRNetworkManager::RegisterCallbacks - cellRudpSetEventHandler failed with code 0x%08x\n", ret); + return false; + } + + // Register the context callback function + app.DebugPrintf("sceNpMatching2RegisterContextCallback\n"); + ret = sceNpMatching2RegisterContextCallback(ContextCallback, this); + if (ret < 0) + { + app.DebugPrintf("SQRNetworkManager::RegisterCallbacks - sceNpMatching2RegisterContextCallback failed with code 0x%08x\n", ret); + return false; + } + + // Register the default request callback & parameters + SceNpMatching2RequestOptParam optParam; + + memset(&optParam, 0, sizeof(optParam)); + optParam.cbFunc = DefaultRequestCallback; + optParam.cbFuncArg = this; + optParam.timeout = (30 * 1000 * 1000); + optParam.appReqId = 0; + + app.DebugPrintf("sceNpMatching2SetDefaultRequestOptParam\n"); + ret = sceNpMatching2SetDefaultRequestOptParam(m_matchingContext, &optParam); + if (ret < 0) + { + app.DebugPrintf("SQRNetworkManager::RegisterCallbacks - sceNpMatching2SetDefaultRequestOptParam failed with code 0x%08x\n", ret); + return false; + } + + // Register signalling callback + app.DebugPrintf("sceNpMatching2RegisterSignalingCallback\n"); + ret = sceNpMatching2RegisterSignalingCallback(m_matchingContext, SignallingCallback, this); + if (ret < 0) + { + return false; + } + + // Register room event callback + app.DebugPrintf("sceNpMatching2RegisterRoomEventCallback\n"); + ret = sceNpMatching2RegisterRoomEventCallback(m_matchingContext, RoomEventCallback, this); + if (ret < 0) + { + app.DebugPrintf("SQRNetworkManager::RegisterCallbacks - sceNpMatching2RegisterRoomEventCallback failed with code 0x%08x\n", ret); + return false; + } + + return true; +} + +extern bool g_bBootedFromInvite; + +// This is an implementation of SceNpMatching2ContextCallback. Used to determine whether the matching 2 context is valid or not. +void SQRNetworkManager_Vita::ContextCallback(SceNpMatching2ContextId id, SceNpMatching2Event event, SceNpMatching2EventCause eventCause, int errorCode, void *arg) +{ + if(CGameNetworkManager::usingAdhocMode()) // MGH - added to fix #5772 + return; + + + int ret; + SQRNetworkManager_Vita *manager = (SQRNetworkManager_Vita *)arg; + EnterCriticalSection(&manager->m_csMatching); + if (id != manager->m_matchingContext) + { + LeaveCriticalSection(&manager->m_csMatching); + return; + } + + switch( event ) + { + case SCE_NP_MATCHING2_CONTEXT_EVENT_STARTED: + app.DebugPrintf("SCE_NP_MATCHING2_CONTEXT_EVENT_STARTED\n"); + if(errorCode < 0) + { + if(manager->m_state == SNM_INT_STATE_IDLE_RECREATING_MATCHING_CONTEXT || + manager->m_state == SNM_INT_STATE_HOSTING_STARTING_MATCHING_CONTEXT || + manager->m_state == SNM_INT_STATE_JOINING_STARTING_MATCHING_CONTEXT) + { + // matching context failed to start (this can happen when you block the IP addresses of the matching servers on your router + // agent-0101.ww.sp-int.matching.playstation.net (198.107.157.191) + // static-resource.sp-int.community.playstation.net (203.105.77.140) + manager->SetState(SNM_INT_STATE_INITIALISE_FAILED); + break; + } + } + // Some special cases to detect when this event is coming in, in case we had to start the matching context because there wasn't a valid context when we went to get a server context. These two + // responses here complete what should then happen to get the server context in each case (for hosting or joining a game) + if( manager->m_state == SNM_INT_STATE_IDLE_RECREATING_MATCHING_CONTEXT ) + { + manager->SetState( SNM_INT_STATE_IDLE ); + manager->GetExtDataForRoom(0, NULL, NULL, NULL); + break; + } + + if( manager->m_state == SNM_INT_STATE_HOSTING_STARTING_MATCHING_CONTEXT ) + { + manager->GetServerContext2(); + break; + } + if( manager->m_state == SNM_INT_STATE_JOINING_STARTING_MATCHING_CONTEXT ) + { + manager->SetState(SNM_INT_STATE_JOINING_SEARCHING_FOR_SERVER); + manager->SelectRandomServer(); + break; + } + if ( manager->m_state == SNM_INT_STATE_HOSTING_CREATE_ROOM_RESTART_MATCHING_CONTEXT ) + { + manager->ServerContextValid_CreateRoom(); + break; + } + // Normal handling of context starting, from standard initialisation procedure + assert( manager->m_state == SNM_INT_STATE_STARTING_CONTEXT ); + if (errorCode < 0) + { + manager->SetState(SNM_INT_STATE_INITIALISE_FAILED); + } + else + { + manager->m_offlineSQR = false; + manager->SetState(SNM_INT_STATE_IDLE); + + // 4J-PB - SQRNetworkManager_PS3::AttemptPSNSignIn was causing crashes in Iggy by calling LoadMovie from a callback, so call it from the tick instead + m_bCallPSNSignInCallback=true; + // if(s_SignInCompleteCallbackFn) + // { + // s_SignInCompleteCallbackFn(s_SignInCompleteParam, true, 0); + // s_SignInCompleteCallbackFn = NULL; + // } + + + // Check to see if we were booted from an invite. Only do this once, the first time we have all our networking stuff set up on boot-up + if( manager->m_doBootInviteCheck ) + { + // ORBIS_STUBBED; + // unsigned int type, attributes; + // CellGameContentSize gameSize;` + // char dirName[CELL_GAME_DIRNAME_SIZE]; + // + // if( g_bBootedFromInvite ) + // { + // manager->GetInviteDataAndProcess(SCE_NP_BASIC_SELECTED_INVITATION_DATA); + // manager->m_doBootInviteCheck = false; + // } + } + } + break; + case SCE_NP_MATCHING2_CONTEXT_EVENT_STOPPED: + app.DebugPrintf("SCE_NP_MATCHING2_CONTEXT_EVENT_STOPPED\n"); + // Can happen when we stop the PSN to switch to adhoc mode + //assert(false); + if( manager->m_state == SNM_INT_STATE_HOSTING_CREATE_ROOM_RESTART_MATCHING_CONTEXT ) + { + sceNpMatching2DestroyContext(manager->m_matchingContext); + manager->m_matchingContextValid = false; + if(!manager->GetMatchingContext(SNM_INT_STATE_HOSTING_CREATE_ROOM_RESTART_MATCHING_CONTEXT)) + { + manager->m_offlineSQR = true; + manager->SetState(SNM_INT_STATE_INITIALISE_FAILED); + } + } + break; + case SCE_NP_MATCHING2_CONTEXT_EVENT_START_OVER: + + app.DebugPrintf("SCE_NP_MATCHING2_CONTEXT_EVENT_START_OVER\n"); + app.DebugPrintf("SCE_NP_MATCHING2_CONTEXT_EVENT_START_OVER\n"); + app.DebugPrintf("eventCause=%u, errorCode=0x%08x\n", eventCause, errorCode); + app.DebugPrintf("sceNpMatching2DestroyContext\n"); + sceNpMatching2DestroyContext(manager->m_matchingContext); + if(manager->m_state == SNM_INT_STATE_IDLE_RECREATING_MATCHING_CONTEXT) // MGH - added this to catch when the context start fails when getting GetExtDataForRoom + { + if(manager->m_FriendSessionUpdatedFn) + manager->m_FriendSessionUpdatedFn(false, manager->m_pParamFriendSessionUpdated); + } + manager->m_matchingContextValid = false; + manager->m_offlineSQR = true; + manager->SetState(SNM_INT_STATE_INITIALISE_FAILED); + break; + } + + LeaveCriticalSection(&manager->m_csMatching); +} + +// This is an implementation of SceNpMatching2RequestCallback. This callback is used by default for any matching 2 request functions. +void SQRNetworkManager_Vita::DefaultRequestCallback(SceNpMatching2ContextId id, SceNpMatching2RequestId reqId, SceNpMatching2Event event, int errorCode, const void *data, void *arg) +{ + SQRNetworkManager_Vita *manager = (SQRNetworkManager_Vita *)arg; + EnterCriticalSection(&manager->m_csMatching); + // int ret; + if( id != manager->m_matchingContext ) + { + LeaveCriticalSection(&manager->m_csMatching); + return; + } + + + switch( event ) + { + // This is the response to sceNpMatching2GetWorldInfoList, which is called as part of the process to create a room (which needs a world to be created in). We aren't anticipating + // using worlds in a meaningful way so just getting the first world we find on the server here, and then advancing the state so that the tick can get on with the rest of the process. + case SCE_NP_MATCHING2_REQUEST_EVENT_GET_WORLD_INFO_LIST: + { + app.DebugPrintf("SCE_NP_MATCHING2_REQUEST_EVENT_GET_WORLD_INFO_LIST\n"); + SceNpServiceState serviceState; + int retVal = sceNpGetServiceState(&serviceState); + assert(retVal == 0); + assert(serviceState == SCE_NP_SERVICE_STATE_ONLINE); + + if( errorCode == SCE_NP_MATCHING2_ERROR_NP_SIGNED_OUT ) + { + // If we've already signed out, then we should have detected this already elsewhere and so can silently ignore any errors coming in for pending requests here + break; + } + assert( manager->m_state == SNM_INT_STATE_HOSTING_CREATE_ROOM_SEARCHING_FOR_WORLD ); + if( errorCode == 0 ) + { + if( data != 0 ) + { + // Currently just using first world - this may well be all that we need anyway + SceNpMatching2GetWorldInfoListResponse *pWorldList = (SceNpMatching2GetWorldInfoListResponse *)data; + if( pWorldList->worldNum >= 1 ) + { + manager->m_worldId = pWorldList->world[0].worldId; + manager->SetState(SNM_INT_STATE_HOSTING_CREATE_ROOM_WORLD_FOUND); + break; + } + } + } + // We get this error when starting a new game after a disconnect occurred in a previous game with at least one remote player. Fix by stopping/starting the matching context. + // We stop the context here, which is picked up in the callback, and started again. Then the start event is picked up and reattempts the sceNpMatching2GetWorldInfoList. + if( errorCode == SCE_NET_ERROR_EAGAIN || errorCode == SCE_NET_ERROR_RESOLVER_ETIMEDOUT || errorCode == SCE_NET_CTL_ERROR_WIFI_DISABLED ) + { + sceNpMatching2ContextStop(manager->m_matchingContext); + manager->SetState(SNM_INT_STATE_HOSTING_CREATE_ROOM_RESTART_MATCHING_CONTEXT); + break; + } + app.DebugPrintf("SCE_NP_MATCHING2_REQUEST_EVENT_GET_WORLD_INFO_LIST failed, errorCode 0x%x, data %d\n", errorCode, data); + + manager->SetState(SNM_INT_STATE_HOSTING_CREATE_ROOM_FAILED); + } + break; + // This is the response to sceNpMatching2CreateJoinRoom, which if successful means that we are just about ready to move to an online state as host of a game. The final + // transition actually occurs in the create room tick, on detecting that the state has transitioned to SNM_INT_STATE_HOSTING_CREATE_ROOM_SUCCESS here. + case SCE_NP_MATCHING2_REQUEST_EVENT_CREATE_JOIN_ROOM: + app.DebugPrintf("SCE_NP_MATCHING2_REQUEST_EVENT_CREATE_JOIN_ROOM\n"); + if( errorCode == SCE_NP_MATCHING2_ERROR_NP_SIGNED_OUT ) + { + // If we've already signed out, then we should have detected this already elsewhere and so can silently ignore any errors coming in for pending requests here + break; + } + + if(ProfileManager.IsSignedInLive(ProfileManager.GetPrimaryPad()) == false) + { + // MGH - added to catch a case where the user signed out of PSN, but this still called back with no error + break; + } + + app.DebugPrintf(CMinecraftApp::USER_RR,">> Creating room complete, time taken %d, error 0x%x\n",System::currentTimeMillis()-s_roomStartTime, errorCode); + assert( manager->m_state == SNM_INT_STATE_HOSTING_CREATE_ROOM_CREATING_ROOM ); + if( errorCode == 0 ) + { + if( data != 0 ) + { + SceNpMatching2CreateJoinRoomResponse *roomData = (SceNpMatching2CreateJoinRoomResponse *)data; + manager->m_localMemberId = roomData->roomDataInternal->memberList.me->memberId; + + manager->SetState(SNM_INT_STATE_HOSTING_CREATE_ROOM_SUCCESS); + manager->m_room = roomData->roomDataInternal->roomId; + break; + } + } + manager->SetState(SNM_INT_STATE_HOSTING_CREATE_ROOM_FAILED); + break; + // This is the response to sceNpMatching2JoinRoom, which is called as the final stage of the process started when calling the JoinRoom method. If this is successful, then + // the state can change to SNM_INT_STATE_JOINING_WAITING_FOR_LOCAL_PLAYERS. We can transition out of that state once we have told the application that all the local players + // have joined. + case SCE_NP_MATCHING2_REQUEST_EVENT_JOIN_ROOM: + app.DebugPrintf("SCE_NP_MATCHING2_REQUEST_EVENT_JOIN_ROOM\n"); + assert( manager->m_state == SNM_INT_STATE_JOINING_JOIN_ROOM); + if( errorCode == 0 ) + { + if( data != 0 ) + { + SceNpMatching2JoinRoomResponse *roomData = (SceNpMatching2JoinRoomResponse *)data; + + manager->m_localMemberId = roomData->roomDataInternal->memberList.me->memberId; + manager->m_room = roomData->roomDataInternal->roomId; + // SonyVoiceChat::init(manager); + // Copy over initial room sync data + for( int i = 0; i < roomData->roomDataInternal->roomBinAttrInternalNum; i++ ) + { + if( roomData->roomDataInternal->roomBinAttrInternal[i].data.id == SCE_NP_MATCHING2_ROOM_BIN_ATTR_INTERNAL_1_ID ) + { + assert( roomData->roomDataInternal->roomBinAttrInternal[i].data.size == sizeof( manager->m_roomSyncData ) ); + memcpy( &manager->m_roomSyncData, roomData->roomDataInternal[i].roomBinAttrInternal[0].data.ptr, sizeof( manager->m_roomSyncData ) ); + + // manager->UpdatePlayersFromRoomSyncUIDs(); + // Update mapping from the room slot players to SQRNetworkPlayer instances + manager->MapRoomSlotPlayers(); + break; + } + } + manager->SetState(SNM_INT_STATE_JOINING_WAITING_FOR_LOCAL_PLAYERS); + break; + } + } + manager->SetState(SNM_INT_STATE_JOINING_JOIN_ROOM_FAILED); + if(errorCode == SCE_NP_MATCHING2_SERVER_ERROR_ROOM_FULL) // MGH - added to fix "host has exited" error when 2 players go after the final slot + { + Minecraft::GetInstance()->connectionDisconnected(ProfileManager.GetPrimaryPad(), DisconnectPacket::eDisconnect_ServerFull); + } + break; + // This is the response to sceNpMatching2GetRoomMemberDataInternal.This only happens on the host, as a response to an incoming connection being established, when we + // kick off the request for room member internal data so that we can determine what local players that remote machine is intending to bring into the game. At this point we can + // activate the host end of each of the Rupd connection that this machine requires. We can also update our player slot data (which gets syncronised back out to other room members) at this point. + case SCE_NP_MATCHING2_REQUEST_EVENT_GET_ROOM_MEMBER_DATA_INTERNAL: + app.DebugPrintf("SCE_NP_MATCHING2_REQUEST_EVENT_GET_ROOM_MEMBER_DATA_INTERNAL\n"); + if( manager->m_state == SNM_INT_STATE_INITIALISE_FAILED || + manager->m_state == SNM_INT_STATE_IDLE || + manager->m_state == SNM_INT_STATE_LEAVING || + manager->m_state == SNM_INT_STATE_ENDING ) + { + // MGH - I've caught this being triggered after join has already failed, and then UDP connections are created that aren't killed off + // so just break out here if we're not in the expected state + // fixes devtrack #5807 + app.DebugPrintf("SCE_NP_MATCHING2_REQUEST_EVENT_GET_ROOM_MEMBER_DATA_INTERNAL - manager->GetState() : %d\n", manager->GetState() ); + break; + } + if( errorCode == 0 ) + { + + if( data != 0 ) + { + SceNpMatching2GetRoomMemberDataInternalResponse *pRoomMemberData = (SceNpMatching2GetRoomMemberDataInternalResponse *)data; + assert( pRoomMemberData->roomMemberDataInternal->roomMemberBinAttrInternalNum == 1 ); + + if( manager->m_isHosting ) + { + int playerMask = *((int *)(pRoomMemberData->roomMemberDataInternal->roomMemberBinAttrInternal->data.ptr)); + + bool isFull = false; + bool success1 = manager->AddRemotePlayersAndSync( pRoomMemberData->roomMemberDataInternal->memberId, playerMask, &isFull ); + bool success2; + if( success1 ) + { + success2 = manager->CreateRudpConnections(manager->m_room, pRoomMemberData->roomMemberDataInternal->memberId, playerMask, pRoomMemberData->roomMemberDataInternal->memberId); + if( success2 ) + { + bool ret = manager->CreateVoiceRudpConnections( manager->m_room, pRoomMemberData->roomMemberDataInternal->memberId, 0); + assert(ret == true); + break; + } + } + // Something has gone wrong adding these players to the room - kick out the player + SceNpMatching2KickoutRoomMemberRequest reqParam; + // SceNpMatching2PresenceOptionData optParam; + memset(&reqParam,0,sizeof(reqParam)); + reqParam.roomId = manager->m_room; + reqParam.target = pRoomMemberData->roomMemberDataInternal->memberId; + // Set flag to indicate whether we were kicked for being out of room or not + reqParam.optData.data[0] = isFull ? 1 : 0; + reqParam.optData.len = 1; + int ret = sceNpMatching2KickoutRoomMember(manager->m_matchingContext, &reqParam, NULL, &manager->m_kickRequestId); + app.DebugPrintf(CMinecraftApp::USER_RR,"sceNpMatching2KickoutRoomMember returns error 0x%x\n",ret); + } + else + { + if(pRoomMemberData->roomMemberDataInternal->roomMemberBinAttrInternal->data.ptr == NULL) + { + // the host doesn't send out data, so this must be the host we're connecting to + + // If we are the client, then we locally know what Rupd connections we need (from m_localPlayerJoinMask) and can kick this off. + manager->m_hostMemberId = pRoomMemberData->roomMemberDataInternal->memberId; + bool ret = manager->CreateRudpConnections( manager->m_room, pRoomMemberData->roomMemberDataInternal->memberId, manager->m_localPlayerJoinMask, manager->m_localMemberId); + if( ret == false ) + { + manager->DeleteServerContext(); + } + else + { + bool ret = manager->CreateVoiceRudpConnections( manager->m_room, pRoomMemberData->roomMemberDataInternal->memberId, manager->m_localPlayerJoinMask); + assert(ret == true); + } + } + else + { + // client <-> client + bool ret = manager->CreateVoiceRudpConnections( manager->m_room, pRoomMemberData->roomMemberDataInternal->memberId, manager->m_localPlayerJoinMask); + assert(ret == true); + } + } + + } + } + break; + case SCE_NP_MATCHING2_REQUEST_EVENT_LEAVE_ROOM: + app.DebugPrintf("SCE_NP_MATCHING2_REQUEST_EVENT_LEAVE_ROOM\n"); + // This is the response to sceNpMatching2LeaveRoom - from the Sony docs, this doesn't ever fail so no need to do error checking here + // SonyVoiceChat::signalDisconnected(); + assert(manager->m_state == SNM_INT_STATE_LEAVING ); + manager->DeleteServerContext(); + break; + // This is the response to SceNpMatching2GetRoomDataExternalListRequest, which happens when we request the full details of a room we are interested in joining + case SCE_NP_MATCHING2_REQUEST_EVENT_GET_ROOM_DATA_EXTERNAL_LIST: + app.DebugPrintf("SCE_NP_MATCHING2_REQUEST_EVENT_GET_ROOM_DATA_EXTERNAL_LIST\n"); + if( errorCode == 0 ) + { + if( data != 0 ) + { + SceNpMatching2GetRoomDataExternalListResponse *pExternalData = (SceNpMatching2GetRoomDataExternalListResponse *)data; + SceNpMatching2RoomDataExternal *pRoomExtData = pExternalData->roomDataExternal; + if( pExternalData->roomDataExternalNum == 1 ) + { + if(pRoomExtData->roomBinAttrExternalNum == 1 ) + { + memcpy(manager->m_pExtDataToUpdate, pRoomExtData->roomBinAttrExternal[0].ptr,pRoomExtData->roomBinAttrExternal[0].size); + manager->m_FriendSessionUpdatedFn(true, manager->m_pParamFriendSessionUpdated); + } + else + { + manager->m_FriendSessionUpdatedFn(false, manager->m_pParamFriendSessionUpdated); + } + } + else + { + manager->m_FriendSessionUpdatedFn(false, manager->m_pParamFriendSessionUpdated); + } + } + else + { + manager->m_FriendSessionUpdatedFn(false, manager->m_pParamFriendSessionUpdated); + } + } + else + { + manager->m_FriendSessionUpdatedFn(false, manager->m_pParamFriendSessionUpdated); + } + break; + case SCE_NP_MATCHING2_REQUEST_EVENT_SET_ROOM_DATA_EXTERNAL: + app.DebugPrintf("SCE_NP_MATCHING2_REQUEST_EVENT_SET_ROOM_DATA_EXTERNAL\n"); + if( ( errorCode != 0 ) || manager->ForceErrorPoint(SNM_FORCE_ERROR_SET_ROOM_DATA_CALLBACK) ) + { + app.DebugPrintf(CMinecraftApp::USER_RR,"Error updating external data 0x%x (from SCE_NP_MATCHING2_REQUEST_EVENT_SetRoomDataExternal event in callback)\n",errorCode); + // If we ever fail to send the external room data, we start a countdown so that we attempt to resend. Not sure how likely it is that updating this will fail without the whole network being broken, + // but if in particular we don't update the flag to say that the session is joinable, then nobody is ever going to see this session. + manager->m_resendExternalRoomDataCountdown = 60; + } + break; + }; + + LeaveCriticalSection(&manager->m_csMatching); +} + +void SQRNetworkManager_Vita::RoomEventCallback(SceNpMatching2ContextId id, SceNpMatching2RoomId roomId, SceNpMatching2Event event, const void *data, void *arg) +{ + SQRNetworkManager_Vita *manager = (SQRNetworkManager_Vita *)arg; + + // bool gotEventData = false; + switch( event ) + { + case SCE_NP_MATCHING2_ROOM_EVENT_MEMBER_JOINED: + break; + case SCE_NP_MATCHING2_ROOM_EVENT_MEMBER_LEFT: + break; + case SCE_NP_MATCHING2_ROOM_EVENT_KICKEDOUT: + { + // SonyVoiceChat::signalRoomKickedOut(); + // We've been kicked out. This server has rejected our attempt to join, most likely because there wasn't enough space in the server to have us. There's a flag set + // so we can determine which thing has happened + // assert ( dataSize <= SCE_NP_MATCHING2_EVENT_DATA_MAX_SIZE_RoomUpdateInfo ); + // int ret = sceNpMatching2GetEventData( manager->m_matchingContext, eventKey, manager->cRoomDataUpdateInfo, SCE_NP_MATCHING2_EVENT_DATA_MAX_SIZE_RoomUpdateInfo); + // app.DebugPrintf(CMinecraftApp::USER_RR,"SCE_NP_MATCHING2_ROOM_EVENT_Kickedout, sceNpMatching2GetEventData returning 0x%x\n",ret); + + bool bIsFull = false; + if( ( data ) && !manager->ForceErrorPoint(SNM_FORCE_ERROR_UPDATED_ROOM_DATA) ) + { + // gotEventData = true; + SceNpMatching2RoomUpdateInfo *pUpdateInfo = (SceNpMatching2RoomUpdateInfo *)(data); + if( pUpdateInfo->optData.len == 1 ) + { + if( pUpdateInfo->optData.data[0] == 1 ) + { + bIsFull = true; + } + } + } + app.DebugPrintf(CMinecraftApp::USER_RR,"IsFull determined to be %d\n",bIsFull); + if( bIsFull ) + { + manager->m_nextIdleReasonIsFull = true; + } + manager->ResetToIdle(); + } + break; + case SCE_NP_MATCHING2_ROOM_EVENT_ROOM_DESTROYED: + // SonyVoiceChat::signalRoomDestroyed(); + + { + SceNpMatching2RoomUpdateInfo *pUpdateInfo = (SceNpMatching2RoomUpdateInfo *)data; + app.DebugPrintf("SCE_NP_MATCHING2_ROOM_EVENT_RoomDestroyed\n"); + if( pUpdateInfo ) + { + app.DebugPrintf("Further info: Error 0x%x, cause %d\n",pUpdateInfo->errorCode,pUpdateInfo->eventCause); + } + // If we're hosting, then handle this a bit like a disconnect, in that we will shift the game into an offline game - but don't need to actually leave the room + // since that has been destroyed and so isn't there to be left anymore. Don't do this if we are disconnected though, as we've already handled this. + if( ( manager->m_isHosting ) && !manager->m_bLinkDisconnected ) + { + // MGH - we're not receiving an SCE_NP_MATCHING2_SIGNALING_EVENT_DEAD after this so we have to remove all the remote players + while(manager->m_RudpCtxToPlayerMap.size()) + { + SQRNetworkPlayer* pRemotePlayer = manager->m_RudpCtxToPlayerMap.begin()->second; + manager->RemoveRemotePlayersAndSync( pRemotePlayer->m_roomMemberId, 15 ); + } + + // MGH - added a check for the PSN sign in state, we don't seem to get the signed out matching2 error here + bool bSignedInPSN = ProfileManager.IsSignedInPSN(ProfileManager.GetPrimaryPad()); + bool bSignedOutError = pUpdateInfo && (pUpdateInfo->eventCause==SCE_NP_MATCHING2_EVENT_CAUSE_NP_SIGNED_OUT); + + if(bSignedOutError || (bSignedInPSN == false) ) + { + manager->m_listener->HandleDisconnect(true,true); + } + else + { + manager->m_listener->HandleDisconnect(true); + } + } + } + break; + case SCE_NP_MATCHING2_ROOM_EVENT_ROOM_OWNER_CHANGED: + break; + case SCE_NP_MATCHING2_ROOM_EVENT_UPDATED_ROOM_DATA_INTERNAL: + // We are using the room internal data to synchronise the player data stored in m_roomSyncData from the host to clients. + // The host is the thing creating the internal room data, so it doesn't need to update itself. + if( !manager->m_isHosting ) + { + // assert ( dataSize <= SCE_NP_MATCHING2_EVENT_DATA_MAX_SIZE_RoomDataInternalUpdateInfo ); + // int ret = sceNpMatching2GetEventData( manager->m_matchingContext, eventKey, manager->cRoomDataInternal, SCE_NP_MATCHING2_EVENT_DATA_MAX_SIZE_RoomDataInternalUpdateInfo); + if( ( data) && !manager->ForceErrorPoint(SNM_FORCE_ERROR_UPDATED_ROOM_DATA) ) + { + // gotEventData = true; + SceNpMatching2RoomDataInternalUpdateInfo *pRoomData = (SceNpMatching2RoomDataInternalUpdateInfo *)(data); + for(int i = 0; i < pRoomData->newRoomBinAttrInternalNum; i++) + { + if( pRoomData->newRoomBinAttrInternal[i]->data.id == SCE_NP_MATCHING2_ROOM_BIN_ATTR_INTERNAL_1_ID ) + { + assert( pRoomData->newRoomBinAttrInternal[i]->data.size == sizeof( manager->m_roomSyncData ) ); + memcpy( &manager->m_roomSyncData, pRoomData->newRoomBinAttrInternal[i]->data.ptr, sizeof( manager->m_roomSyncData ) ); + + // manager->UpdatePlayersFromRoomSyncUIDs(); + // Update mapping from the room slot players to SQRNetworkPlayer instances + manager->MapRoomSlotPlayers(); +#if 0 + { + printf("New player sync data arrived\n"); + for(int i = 0; i < manager->m_roomSyncData.getPlayerCount(); i++ ) + { + printf("%d: small %d, machine %d, local %d\n",i, manager->m_roomSyncData.players[i].m_smallId, manager->m_roomSyncData.players[i].m_roomMemberId, manager->m_roomSyncData.players[i].m_localIdx); + } + } +#endif + break; + } + } + break; + } + // TODO - handle error here? What could we do? + } + + break; + case SCE_NP_MATCHING2_ROOM_EVENT_UPDATED_ROOM_MEMBER_DATA_INTERNAL: + if( /*( errorCode == 0 ) && */(!manager->ForceErrorPoint(SNM_FORCE_ERROR_UPDATED_ROOM_MEMBER_DATA_INTERNAL1) ) ) + { + // We'll get this sync'd round all the connected clients, but we only care about it on the host where we can use it to work out if any RUDP connections need to be made or released + if( manager->m_isHosting ) + { + // assert( dataSize <= SCE_NP_MATCHING2_EVENT_DATA_MAX_SIZE_RoomMemberDataInternalUpdateInfo ); + // int ret = sceNpMatching2GetEventData(manager->m_matchingContext, eventKey, (void *)(manager->cRoomMemberDataInternalUpdate), SCE_NP_MATCHING2_EVENT_DATA_MAX_SIZE_RoomMemberDataInternalUpdateInfo); + if( ( data ) && (!manager->ForceErrorPoint(SNM_FORCE_ERROR_UPDATED_ROOM_MEMBER_DATA_INTERNAL2) ) ) + { + // gotEventData = true; + SceNpMatching2RoomMemberDataInternalUpdateInfo *pRoomMemberData = (SceNpMatching2RoomMemberDataInternalUpdateInfo *)(data); + assert( pRoomMemberData->newRoomMemberBinAttrInternalNum == 1 ); + + int playerMask = *((int *)(pRoomMemberData->newRoomMemberBinAttrInternal[0]->data.ptr)); + int oldMask = manager->GetOldMask( pRoomMemberData->newRoomMemberDataInternal->memberId ); + int addedMask = manager->GetAddedMask(playerMask, oldMask ); + int removedMask = manager->GetRemovedMask(playerMask, oldMask ); + + if( addedMask != 0 ) + { + bool success = manager->AddRemotePlayersAndSync( pRoomMemberData->newRoomMemberDataInternal->memberId, addedMask ); + if( success ) + { + success = manager->CreateRudpConnections(manager->m_room, pRoomMemberData->newRoomMemberDataInternal->memberId, addedMask, pRoomMemberData->newRoomMemberDataInternal->memberId); + } + if( ( !success ) || (manager->ForceErrorPoint(SNM_FORCE_ERROR_UPDATED_ROOM_MEMBER_DATA_INTERNAL3) ) ) + { + // Failed for some reason - signal back to the client that this is the case, by updating its internal data back again, rather than have + // it wait for a timeout on its rudp connection initialisation. + SceNpMatching2SetRoomMemberDataInternalRequest reqParam; + SceNpMatching2BinAttr binAttr; + + memset(&reqParam, 0, sizeof(reqParam)); + memset(&binAttr, 0, sizeof(binAttr)); + + binAttr.id = SCE_NP_MATCHING2_ROOMMEMBER_BIN_ATTR_INTERNAL_1_ID; + binAttr.ptr = &oldMask; + binAttr.size = sizeof(oldMask); + + reqParam.roomId = manager->m_room; + reqParam.memberId = pRoomMemberData->newRoomMemberDataInternal->memberId; + reqParam.roomMemberBinAttrInternalNum = 1; + reqParam.roomMemberBinAttrInternal = &binAttr; + + int ret = sceNpMatching2SetRoomMemberDataInternal( manager->m_matchingContext, &reqParam, NULL, &manager->m_setRoomMemberInternalDataRequestId ); + } + else + { + success = manager->CreateVoiceRudpConnections( manager->m_room, pRoomMemberData->newRoomMemberDataInternal->memberId, 0); + assert(success); + } + + } + + if( removedMask != 0 ) + { + manager->RemoveRemotePlayersAndSync( pRoomMemberData->newRoomMemberDataInternal->memberId, removedMask ); + } + + break; + } + } + else + { + // If, as a client, we receive an updated room member data this could be for two reason. + // (1) Another client in the game has updated their own data as someone has joined/left the session + // (2) The server has set someone's data back, due to a failed attempt to join a game + // We're only interested in scenario (2), when the data that has been updated is our own, in which case we know to abandon creating rudp connections etc. for a new player + // assert( dataSize <= SCE_NP_MATCHING2_EVENT_DATA_MAX_SIZE_RoomMemberDataInternalUpdateInfo ); + // int ret = sceNpMatching2GetEventData(manager->m_matchingContext, eventKey, (void *)(manager->cRoomMemberDataInternalUpdate), SCE_NP_MATCHING2_EVENT_DATA_MAX_SIZE_RoomMemberDataInternalUpdateInfo); + if( ( data ) && (!manager->ForceErrorPoint(SNM_FORCE_ERROR_UPDATED_ROOM_MEMBER_DATA_INTERNAL4) ) ) + { + // gotEventData = true; + SceNpMatching2RoomMemberDataInternalUpdateInfo *pRoomMemberData = (SceNpMatching2RoomMemberDataInternalUpdateInfo *)(data); + assert( pRoomMemberData->newRoomMemberBinAttrInternalNum == 1 ); + if( pRoomMemberData->newRoomMemberDataInternal->memberId == manager->m_localMemberId ) + { + int playerMask = *((int *)(pRoomMemberData->newRoomMemberBinAttrInternal[0]->data.ptr)); + if( playerMask != manager->m_localPlayerJoinMask ) + { + int playersToRemove = manager->m_localPlayerJoinMask & (~playerMask); + manager->RemoveNetworkPlayers( playersToRemove ); + if( manager->m_listener ) + { + for( int i = 0; i < MAX_LOCAL_PLAYER_COUNT; i++ ) + { + if( playersToRemove & ( 1 << i ) ) + { + manager->m_listener->HandleAddLocalPlayerFailed(i); + break; + } + } + } + } + } + } + + } + } + break; + case SCE_NP_MATCHING2_ROOM_EVENT_UPDATED_SIGNALING_OPT_PARAM: + break; + }; + + // // If we didn't get the event data, then we need to clear it, or the system even queue will overflow + // if( !gotEventData ) + // { + // sceNpMatching2ClearEventData(manager->m_matchingContext, eventKey); + // } +} + +// This is an implementation of SceNpMatching2SignalingCallback. We configure our too automatically create a star network of connections with the host at the hub, and can respond here to +// the connections being set up to layer sockets and Rudp on top. +void SQRNetworkManager_Vita::SignallingCallback(SceNpMatching2ContextId ctxId, SceNpMatching2RoomId roomId, SceNpMatching2RoomMemberId peerMemberId, SceNpMatching2Event event, int error_code, void *arg) +{ + SQRNetworkManager_Vita *manager = (SQRNetworkManager_Vita *)arg; + + switch( event ) + { + case SCE_NP_MATCHING2_SIGNALING_EVENT_DEAD: + { + if( manager->m_isHosting ) + { + // Remove any players associated with this peer + manager->RemoveRemotePlayersAndSync( peerMemberId, 15 ); + } + else + { + SQRVoiceConnection* pVoice = SonyVoiceChat_Vita::getVoiceConnectionFromRoomMemberID(peerMemberId); + if(pVoice) + { + SonyVoiceChat_Vita::disconnectRemoteConnection(pVoice); + } + if(peerMemberId == manager->m_hostMemberId || pVoice == NULL) // MGH - added check for voice, as we sometime get here before m_hostMemberId has been filled in + { + // Host has left the game... so its all over for this client too. Finish everything up now, including deleting the server context which belongs to this gaming session + // This also might be a response to a request to leave the game from our end too so don't need to do anything in that case + if( manager->m_state != SNM_INT_STATE_LEAVING ) + { + manager->DeleteServerContext(); + manager->ResetToIdle(); + } + } + } + } + case SCE_NP_MATCHING2_SIGNALING_EVENT_ESTABLISHED: + { + + // MGH - changed this to always get the data now, as we need to know if the connecting peer is the host or not + // If we're the host, then we need to get the data associated with the connecting peer to know what connections we should be trying to match. So + // the actual creation of connections happens when the response for this request is processed. + + SceNpMatching2GetRoomMemberDataInternalRequest reqParam; + memset( &reqParam, 0, sizeof(reqParam)); + reqParam.roomId = roomId; + reqParam.memberId = peerMemberId; + SceNpMatching2AttributeId attrs[1] = {SCE_NP_MATCHING2_ROOMMEMBER_BIN_ATTR_INTERNAL_1_ID}; + reqParam.attrId = attrs; + reqParam.attrIdNum = 1; + + sceNpMatching2GetRoomMemberDataInternal( manager->m_matchingContext, &reqParam, NULL, &manager->m_roomMemberDataRequestId); + } + break; + } +} + + +// Implementation of SceNpBasicEventHandler +int SQRNetworkManager_Vita::BasicEventCallback(int event, int retCode, uint32_t reqId, void *arg) +{ + PSVITA_STUBBED; + // SQRNetworkManager_Vita *manager = (SQRNetworkManager_Vita *)arg; + // // We aren't allowed to actually get the event directly from this callback, so send our own internal event to a thread dedicated to doing this + // sceKernelTriggerUserEvent(m_basicEventQueue, sc_UserEventHandle, NULL); + + return 0; +} + +// Implementation of SceNpManagerCallback +void SQRNetworkManager_Vita::OnlineCheck() +{ + bool bSignedIn = ProfileManager.IsSignedInLive(ProfileManager.GetPrimaryPad()); + if(GetOnlineStatus() == false) + { + if(bSignedIn) + { + SceNpServiceState serviceState; + int retVal = sceNpGetServiceState(&serviceState); + assert(retVal == 0); + assert(serviceState == SCE_NP_SERVICE_STATE_ONLINE); + InitialiseAfterOnline(); + } + } + else + { + if(bSignedIn == false) + m_listener->HandleDisconnect(false); + } + UpdateOnlineStatus(bSignedIn); +} + +// Implementation of CellSysutilCallback +void SQRNetworkManager_Vita::SysUtilCallback(uint64_t status, uint64_t param, void *userdata) +{ + // SQRNetworkManager_Vita *manager = (SQRNetworkManager_Vita *)userdata; + // struct CellNetCtlNetStartDialogResult netstart_result; + // int ret = 0; + // netstart_result.size = sizeof(netstart_result); + // switch(status) + // { + // case CELL_SYSUTIL_NET_CTL_NETSTART_FINISHED: + // ret = cellNetCtlNetStartDialogUnloadAsync(&netstart_result); + // if(ret < 0) + // { + // manager->SetState(SNM_INT_STATE_INITIALISE_FAILED); + // if( s_SignInCompleteCallbackFn ) + // { + // if( s_signInCompleteCallbackIfFailed ) + // { + // s_SignInCompleteCallbackFn(s_SignInCompleteParam,false,0); + // } + // s_SignInCompleteCallbackFn = NULL; + // } + // return; + // } + // + // if( netstart_result.result != 0 ) + // { + // // Failed, or user may have decided not to sign in - maybe need to differentiate here + // manager->SetState(SNM_INT_STATE_INITIALISE_FAILED); + // if( s_SignInCompleteCallbackFn ) + // { + // if( s_signInCompleteCallbackIfFailed ) + // { + // s_SignInCompleteCallbackFn(s_SignInCompleteParam,false,0); + // } + // s_SignInCompleteCallbackFn = NULL; + // } + // } + // + // break; + // case CELL_SYSUTIL_NET_CTL_NETSTART_UNLOADED: + // break; + // case CELL_SYSUTIL_NP_INVITATION_SELECTED: + // manager->GetInviteDataAndProcess(SCE_NP_BASIC_SELECTED_INVITATION_DATA); + // break; + // default: + // break; + // } +} + + +void SQRNetworkManager_Vita::updateNetCheckDialog() +{ + if(ProfileManager.IsSystemUIDisplayed()) + { + if( sceNetCheckDialogGetStatus() == SCE_COMMON_DIALOG_STATUS_FINISHED ) + { + //Check for errors + SceNetCheckDialogResult netCheckResult; + int ret = sceNetCheckDialogGetResult(&netCheckResult); + app.DebugPrintf("NetCheckDialogResult = 0x%x\n", netCheckResult.result); + ret = sceNetCheckDialogTerm(); + app.DebugPrintf("NetCheckDialogTerm ret = 0x%x\n", ret); + ProfileManager.SetSysUIShowing( false ); + app.DebugPrintf("------------>>>>>>>> sceNetCheckDialog finished\n"); + + if( netCheckResult.result == SCE_COMMON_DIALOG_RESULT_OK ) + { + if( s_SignInCompleteCallbackFn ) + { + s_SignInCompleteCallbackFn(s_SignInCompleteParam,true,0); + s_SignInCompleteCallbackFn = NULL; + } + } + else + { + // SCE_COMMON_DIALOG_RESULT_USER_CANCELED + // SCE_COMMON_DIALOG_RESULT_ABORTED + + // Failed, or user may have decided not to sign in - maybe need to differentiate here + SetState(SNM_INT_STATE_INITIALISE_FAILED); + if( s_SignInCompleteCallbackFn ) + { + if( s_signInCompleteCallbackIfFailed ) + { + s_SignInCompleteCallbackFn(s_SignInCompleteParam,false,0); + } + s_SignInCompleteCallbackFn = NULL; + } + } + } + } +} + +// Implementation of CellRudpContextEventHandler. This is associate with an Rudp context every time one is created, and can be used to determine the status of each +// Rudp connection. We create one context/connection per local player on the non-hosting consoles. +void SQRNetworkManager_Vita::RudpContextCallback(int ctx_id, int event_id, int error_code, void *arg) +{ + SQRNetworkManager_Vita *manager = (SQRNetworkManager_Vita *)arg; + switch(event_id) + { + case SCE_RUDP_CONTEXT_EVENT_CLOSED: + { + SQRVoiceConnection* pVoice = SonyVoiceChat_Vita::GetVoiceConnectionFromRudpCtx(ctx_id); + if(pVoice) + { + pVoice->m_bConnected = false; + } + else + { + app.DebugPrintf(CMinecraftApp::USER_RR,"RUDP closed - event error 0x%x\n",error_code); + if( !manager->m_isHosting ) + { + if( manager->m_state == SNM_INT_STATE_JOINING_WAITING_FOR_LOCAL_PLAYERS ) + { + manager->LeaveRoom(true); + } + } + } + } + break; + case SCE_RUDP_CONTEXT_EVENT_ESTABLISHED: + { + SQRNetworkPlayer *player = manager->GetPlayerFromRudpCtx(ctx_id); + if( player ) + { + // Flag connection stage as being completed for this player + manager->NetworkPlayerConnectionComplete(player); + } + else + { + SonyVoiceChat_Vita::setConnected(ctx_id); + } + } + break; + case SCE_RUDP_CONTEXT_EVENT_ERROR: + break; + case SCE_RUDP_CONTEXT_EVENT_WRITABLE: + { + SQRNetworkPlayer *player = manager->GetPlayerFromRudpCtx(ctx_id); + // This event signifies that room has opened up in the write buffer, so attempt to send something + if( player ) + { + player->SendMoreInternal(); + } + else + { + SQRVoiceConnection* pVoice = SonyVoiceChat_Vita::GetVoiceConnectionFromRudpCtx(ctx_id); + assert(pVoice); + } + } + break; + case SCE_RUDP_CONTEXT_EVENT_READABLE: + if( manager->m_listener ) + { + SQRVoiceConnection* pVoice = SonyVoiceChat_Vita::GetVoiceConnectionFromRudpCtx(ctx_id); + if(pVoice) + { + pVoice->readRemoteData(); + } + else + { + unsigned int dataSize = sceRudpGetSizeReadable(ctx_id); + // If we're the host, and this player hasn't yet had its small id confirmed, then the first byte sent to us should be this id + if( manager->m_isHosting ) + { + SQRNetworkPlayer *playerFrom = manager->GetPlayerFromRudpCtx( ctx_id ); + if( playerFrom && !playerFrom->HasSmallIdConfirmed() ) + { + if( dataSize >= sizeof(SQRNetworkPlayer::InitSendData) ) + { + SQRNetworkPlayer::InitSendData ISD; + unsigned int bytesRead = sceRudpRead( ctx_id, &ISD, sizeof(SQRNetworkPlayer::InitSendData), 0, NULL ); + if( bytesRead == sizeof(SQRNetworkPlayer::InitSendData) ) + { + manager->NetworkPlayerInitialDataReceived(playerFrom, &ISD); + dataSize -= sizeof(SQRNetworkPlayer::InitSendData); + } + else + { + assert(false); + } + } + else + { + assert(false); + } + } + } + + if( dataSize > 0 ) + { + unsigned char *data = new unsigned char [ dataSize ]; + unsigned int bytesRead = sceRudpRead( ctx_id, data, dataSize, 0, NULL ); + if( bytesRead > 0 ) + { + SQRNetworkPlayer *playerFrom, *playerTo; + if( manager->m_isHosting ) + { + // Data always going from a remote player, to the host + playerFrom = manager->GetPlayerFromRudpCtx( ctx_id ); + playerTo = manager->m_aRoomSlotPlayers[0]; + } + else + { + // Data always going from host player, to a local player + playerFrom = manager->m_aRoomSlotPlayers[0]; + playerTo = manager->GetPlayerFromRudpCtx( ctx_id ); + } + if( ( playerFrom != NULL ) && ( playerTo != NULL ) ) + { + manager->m_listener->HandleDataReceived( playerFrom, playerTo, data, bytesRead ); + } + } + delete [] data; + } + } + } + break; + case SCE_RUDP_CONTEXT_EVENT_FLUSHED: + break; + } +} + +// Implementation of CellRudpEventHandler +#ifdef __PS3__ +int SQRNetworkManager_Vita::RudpEventCallback(int event_id, int soc, uint8_t const *data, size_t datalen, struct sockaddr const *addr, socklen_t addrlen, void *arg) +#else +int SQRNetworkManager_Vita::RudpEventCallback(int event_id, int soc, uint8_t const *data, size_t datalen, struct SceNetSockaddr const *addr, SceNetSocklen_t addrlen, void *arg) +#endif +{ + SQRNetworkManager_Vita *manager = (SQRNetworkManager_Vita *)arg; + if( event_id == SCE_RUDP_EVENT_SOCKET_RELEASED ) + { + assert( soc == manager->m_soc ); + sceNetSocketClose(soc); + manager->m_soc = -1; + } + return 0; +} + +void SQRNetworkManager_Vita::NetCtlCallback(int eventType, void *arg) +{ + SQRNetworkManager_Vita *manager = (SQRNetworkManager_Vita *)arg; + // Oddly, the disconnect event comes in with a new state of "CELL_NET_CTL_STATE_Connecting"... looks like the event is more important than the state to + // determine what has just happened + if( eventType == SCE_NET_CTL_EVENT_TYPE_DISCONNECTED)// CELL_NET_CTL_EVENT_LINK_DISCONNECTED ) + { + manager->m_bLinkDisconnected = true; + manager->m_listener->HandleDisconnect(false); + } + else //if( event == CELL_NET_CTL_EVENT_ESTABLISH ) + { + manager->m_bLinkDisconnected = false; + } + +} + +// Called when the context has been created, and we are intending to create a room. +void SQRNetworkManager_Vita::ServerContextValid_CreateRoom() +{ + // First find a world + SetState(SNM_INT_STATE_HOSTING_CREATE_ROOM_SEARCHING_FOR_WORLD); + + SceNpMatching2GetWorldInfoListRequest reqParam; + + // Request parameters + memset(&reqParam, 0, sizeof(reqParam)); + reqParam.serverId = m_serverId; + + int ret = -1; + if( !ForceErrorPoint(SNM_FORCE_ERROR_GET_WORLD_INFO_LIST) ) + { + app.DebugPrintf("sceNpMatching2GetWorldInfoList\n"); + m_getWorldRequestId=0; + ret = sceNpMatching2GetWorldInfoList( m_matchingContext, &reqParam, NULL, &m_getWorldRequestId); + } + if (ret < 0) + { + SetState(SNM_INT_STATE_HOSTING_CREATE_ROOM_FAILED); + return; + } +} + +// Called when the context has been created, and we are intending to join a pre-existing room. +void SQRNetworkManager_Vita::ServerContextValid_JoinRoom() +{ + // assert( m_state == SNM_INT_STATE_JOINING_SERVER_SEARCH_CREATING_CONTEXT ); + + SetState(SNM_INT_STATE_JOINING_JOIN_ROOM); + + // Join the room, passing the local player mask as initial binary data so that the host knows what local players are here + SceNpMatching2JoinRoomRequest reqParam; + SceNpMatching2BinAttr binAttr; + memset(&reqParam, 0, sizeof(reqParam)); + memset(&binAttr, 0, sizeof(binAttr)); + binAttr.id = SCE_NP_MATCHING2_ROOMMEMBER_BIN_ATTR_INTERNAL_1_ID; + binAttr.ptr = &m_localPlayerJoinMask; + binAttr.size = sizeof(m_localPlayerJoinMask); + + reqParam.roomId = m_roomToJoin; + reqParam.roomMemberBinAttrInternalNum = 1; + reqParam.roomMemberBinAttrInternal = &binAttr; + + int ret = sceNpMatching2JoinRoom( m_matchingContext, &reqParam, NULL, &m_joinRoomRequestId ); + if ( (ret < 0) || ForceErrorPoint(SNM_FORCE_ERROR_JOIN_ROOM) ) + { + if( ret == SCE_NP_MATCHING2_SERVER_ERROR_NAT_TYPE_MISMATCH) + { + app.SetDisconnectReason( DisconnectPacket::eDisconnect_NATMismatch ); + } + SetState(SNM_INT_STATE_JOINING_JOIN_ROOM_FAILED); + } +} + +const SceNpCommunicationId* SQRNetworkManager_Vita::GetSceNpCommsId() +{ + return &s_npCommunicationId; +} + +const SceNpCommunicationSignature* SQRNetworkManager_Vita::GetSceNpCommsSig() +{ + return &s_npCommunicationSignature; +} + +const SceNpTitleId* SQRNetworkManager_Vita::GetSceNpTitleId() +{ + PSVITA_STUBBED; + return NULL; + // return &s_npTitleId; +} + +const SceNpTitleSecret* SQRNetworkManager_Vita::GetSceNpTitleSecret() +{ + PSVITA_STUBBED; + return NULL; + // return &s_npTitleSecret; +} + +int SQRNetworkManager_Vita::GetOldMask(SceNpMatching2RoomMemberId memberId) +{ + int oldMask = 0; + for( int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( m_roomSyncData.players[i].m_roomMemberId == memberId ) + { + oldMask |= (1 << m_roomSyncData.players[i].m_localIdx); + } + } + return oldMask; +} + +int SQRNetworkManager_Vita::GetAddedMask(int newMask, int oldMask) +{ + return newMask & ~oldMask; +} + +int SQRNetworkManager_Vita::GetRemovedMask(int newMask, int oldMask) +{ + return oldMask & ~newMask; +} + + +void SQRNetworkManager_Vita::GetExtDataForRoom( SceNpMatching2RoomId roomId, void *extData, void (* FriendSessionUpdatedFn)(bool success, void *pParam), void *pParam ) +{ + static SceNpMatching2GetRoomDataExternalListRequest reqParam; + static SceNpMatching2RoomId aRoomId[1]; + static SceNpMatching2AttributeId attr[1]; + + // All parameters will be NULL if this is being called a second time, after creating a new matching context via one of the paths below (using GetMatchingContext). + // NULL parameters therefore basically represents an attempt to retry the last sceNpMatching2GetRoomDataExternalList + if( extData != NULL ) + { + aRoomId[0] = roomId; + attr[0] = SCE_NP_MATCHING2_ROOM_BIN_ATTR_EXTERNAL_1_ID; + + memset(&reqParam, 0, sizeof(reqParam)); + reqParam.roomId = aRoomId; + reqParam.roomIdNum = 1; + reqParam.attrIdNum = 1; + reqParam.attrId = attr; + + m_FriendSessionUpdatedFn = FriendSessionUpdatedFn; + m_pParamFriendSessionUpdated = pParam; + m_pExtDataToUpdate = extData; + } + + // Check there's a valid matching context and possibly recreate here + if( !GetMatchingContext(SNM_INT_STATE_IDLE_RECREATING_MATCHING_CONTEXT) ) + { + // No matching context, and failed to try and make one. We're really broken here. + m_FriendSessionUpdatedFn(false, m_pParamFriendSessionUpdated); + return; + } + + // Kicked off an asynchronous thing that will create a matching context, and then call this method back again (with NULL params) once done, so we can reattempt. Don't do anything more now. + if( m_state == SNM_INT_STATE_IDLE_RECREATING_MATCHING_CONTEXT ) + { + app.DebugPrintf("Having to recreate matching context, setting state to SNM_INT_STATE_IDLE_RECREATING_MATCHING_CONTEXT\n"); + return; + } + + int ret = sceNpMatching2GetRoomDataExternalList( m_matchingContext, &reqParam, NULL, &m_roomDataExternalListRequestId ); + + // If we hadn't properly detected that a matching context was unvailable, we might still get an error indicating that it is from the previous call. Handle similarly, but we need + // to destroy the context first. + if( ret == SCE_NP_MATCHING2_ERROR_CONTEXT_NOT_STARTED ) // Also checking for this as a means of simulating the previous error + { + sceNpMatching2DestroyContext(m_matchingContext); + m_matchingContextValid = false; + if( !GetMatchingContext(SNM_INT_STATE_IDLE_RECREATING_MATCHING_CONTEXT) ) + { + // No matching context, and failed to try and make one. We're really broken here. + m_FriendSessionUpdatedFn(false, m_pParamFriendSessionUpdated); + return; + }; + // Kicked off an asynchronous thing that will create a matching context, and then call this method back again (with NULL params) once done, so we can reattempt. Don't do anything more now. + if( m_state == SNM_INT_STATE_IDLE_RECREATING_MATCHING_CONTEXT ) + { + return; + } + } + + if( ret != 0 ) + { + m_FriendSessionUpdatedFn(false, m_pParamFriendSessionUpdated); + } +} + + +#ifdef _CONTENT_PACKAGE +bool SQRNetworkManager_Vita::ForceErrorPoint(eSQRForceError error) +{ + return false; +} +#else +bool SQRNetworkManager_Vita::aForceError[SNM_FORCE_ERROR_COUNT] = +{ + false, // SNM_FORCE_ERROR_NP2_INIT + false, // SNM_FORCE_ERROR_NET_INITIALIZE_NETWORK + false, // SNM_FORCE_ERROR_NET_CTL_INIT + false, // SNM_FORCE_ERROR_RUDP_INIT + false, // SNM_FORCE_ERROR_NET_START_DIALOG + false, // SNM_FORCE_ERROR_MATCHING2_INIT + false, // SNM_FORCE_ERROR_REGISTER_NP_CALLBACK + false, // SNM_FORCE_ERROR_GET_NPID + false, // SNM_FORCE_ERROR_CREATE_MATCHING_CONTEXT + false, // SNM_FORCE_ERROR_REGISTER_CALLBACKS + false, // SNM_FORCE_ERROR_CONTEXT_START_ASYNC + false, // SNM_FORCE_ERROR_SET_EXTERNAL_ROOM_DATA + false, // SNM_FORCE_ERROR_GET_FRIEND_LIST_ENTRY_COUNT + false, // SNM_FORCE_ERROR_GET_FRIEND_LIST_ENTRY + false, // SNM_FORCE_ERROR_GET_USER_INFO_LIST + false, // SNM_FORCE_ERROR_LEAVE_ROOM + false, // SNM_FORCE_ERROR_SET_ROOM_MEMBER_DATA_INTERNAL + false, // SNM_FORCE_ERROR_SET_ROOM_MEMBER_DATA_INTERNAL2 + false, // SNM_FORCE_ERROR_CREATE_SERVER_CONTEXT + false, // SNM_FORCE_ERROR_CREATE_JOIN_ROOM + false, // SNM_FORCE_ERROR_GET_SERVER_INFO + false, // SNM_FORCE_ERROR_DELETE_SERVER_CONTEXT + false, // SNM_FORCE_ERROR_SETSOCKOPT_0 + false, // SNM_FORCE_ERROR_SETSOCKOPT_1 + false, // SNM_FORCE_ERROR_SETSOCKOPT_2 + false, // SNM_FORCE_ERROR_SOCK_BIND + false, // SNM_FORCE_ERROR_CREATE_RUDP_CONTEXT + false, // SNM_FORCE_ERROR_RUDP_BIND + false, // SNM_FORCE_ERROR_RUDP_INIT2 + false, // SNM_FORCE_ERROR_GET_ROOM_EXTERNAL_DATA + false, // SNM_FORCE_ERROR_GET_SERVER_INFO_DATA + false, // SNM_FORCE_ERROR_GET_WORLD_INFO_DATA + false, // SNM_FORCE_ERROR_GET_CREATE_JOIN_ROOM_DATA + false, // SNM_FORCE_ERROR_GET_USER_INFO_LIST_DATA + false, // SNM_FORCE_ERROR_GET_JOIN_ROOM_DATA + false, // SNM_FORCE_ERROR_GET_ROOM_MEMBER_DATA_INTERNAL + false, // SNM_FORCE_ERROR_GET_ROOM_EXTERNAL_DATA2 + false, // SNM_FORCE_ERROR_CREATE_SERVER_CONTEXT_CALLBACK + false, // SNM_FORCE_ERROR_SET_ROOM_DATA_CALLBACK + false, // SNM_FORCE_ERROR_UPDATED_ROOM_DATA + false, // SNM_FORCE_ERROR_UPDATED_ROOM_MEMBER_DATA_INTERNAL1 + false, // SNM_FORCE_ERROR_UPDATED_ROOM_MEMBER_DATA_INTERNAL2 + false, // SNM_FORCE_ERROR_UPDATED_ROOM_MEMBER_DATA_INTERNAL3 + false, // SNM_FORCE_ERROR_UPDATED_ROOM_MEMBER_DATA_INTERNAL4 + false, // SNM_FORCE_ERROR_GET_WORLD_INFO_LIST + false, // SNM_FORCE_ERROR_JOIN_ROOM +}; + +bool SQRNetworkManager_Vita::ForceErrorPoint(eSQRForceError err) +{ + return aForceError[err]; +} +#endif + +void SQRNetworkManager_Vita::AttemptPSNSignIn(int (*SignInCompleteCallbackFn)(void *pParam, bool bContinue, int pad), void *pParam, bool callIfFailed/*=false*/) +{ + if(CGameNetworkManager::usingAdhocMode()) + { + SQRNetworkManager_AdHoc_Vita::AttemptPSNSignIn(SignInCompleteCallbackFn, pParam, callIfFailed); + return; + } + s_SignInCompleteCallbackFn = SignInCompleteCallbackFn; + s_signInCompleteCallbackIfFailed = callIfFailed; + s_SignInCompleteParam = pParam; + + SceNetCheckDialogParam param; + memset(¶m, 0x00, sizeof(param)); + sceNetCheckDialogParamInit(¶m); + param.mode = SCE_NETCHECK_DIALOG_MODE_PSN_ONLINE; + param.defaultAgeRestriction = ProfileManager.GetMinimumAge(); + + //CD - Only add if EU sku, not SCEA or SCEJ + if( app.GetProductSKU() == e_sku_SCEE ) + { + //CD - Added Country age restrictions + SceNetCheckDialogAgeRestriction restrictions[5]; + memset( restrictions, 0x0, sizeof(SceNetCheckDialogAgeRestriction) * 5 ); + //Germany + restrictions[0].age = ProfileManager.GetGermanyMinimumAge(); + memcpy( restrictions[0].countryCode, "de", 2 ); + //Russia + restrictions[1].age = ProfileManager.GetRussiaMinimumAge(); + memcpy( restrictions[1].countryCode, "ru", 2 ); + //Australia + restrictions[2].age = ProfileManager.GetAustraliaMinimumAge(); + memcpy( restrictions[2].countryCode, "au", 2 ); + //Japan + restrictions[3].age = ProfileManager.GetJapanMinimumAge(); + memcpy( restrictions[3].countryCode, "jp", 2 ); + //Korea + restrictions[4].age = ProfileManager.GetKoreaMinimumAge(); + memcpy( restrictions[4].countryCode, "kr", 2 ); + //Set + param.ageRestriction = restrictions; + param.ageRestrictionCount = 5; + } + + memcpy(¶m.npCommunicationId.data, &s_npCommunicationId, sizeof(s_npCommunicationId)); + param.npCommunicationId.term = '\0'; + param.npCommunicationId.num = 0; + + int ret = sceNetCheckDialogInit(¶m); + + ProfileManager.SetSysUIShowing( true ); + app.DebugPrintf("------------>>>>>>>> sceNetCheckDialogInit : PSN Mode\n"); + + if( ret < 0 ) + { + if(s_SignInCompleteCallbackFn) // MGH - added after crash on PS4 + { + if( s_signInCompleteCallbackIfFailed ) + { + s_SignInCompleteCallbackFn(s_SignInCompleteParam,false,0); + } + s_SignInCompleteCallbackFn = NULL; + } + } +} + +int SQRNetworkManager_Vita::SetRichPresence(const void *data) +{ + const sce::Toolkit::NP::PresenceDetails *newPresenceInfo = (const sce::Toolkit::NP::PresenceDetails *)data; + + s_lastPresenceInfo.status = newPresenceInfo->status; + // s_lastPresenceInfo.userInfo = newPresenceInfo->userInfo; + s_lastPresenceInfo.presenceType = SCE_NP_BASIC_IN_GAME_PRESENCE_TYPE_DEFAULT; + + s_presenceStatusDirty = true; + SendLastPresenceInfo(); + + // Return as if no error happened no matter what, as we'll be resending ourselves if we need to and don't want the calling system to retry + return 0; +} + +void SQRNetworkManager_Vita::UpdateRichPresenceCustomData(void *data, unsigned int dataBytes) +{ + assert(dataBytes <= SCE_NP_BASIC_IN_GAME_PRESENCE_DATA_SIZE_MAX ); + memcpy(s_lastPresenceInfo.data, data, dataBytes); + s_lastPresenceInfo.size = dataBytes; + + s_presenceStatusDirty = true; + SendLastPresenceInfo(); +} + +void SQRNetworkManager_Vita::TickRichPresence() +{ + if( s_resendPresenceCountdown ) + { + s_resendPresenceCountdown--; + if( s_resendPresenceCountdown == 0 ) + { + SendLastPresenceInfo(); + } + } +} + +void SQRNetworkManager_Vita::SendLastPresenceInfo() +{ + // Don't attempt to send if we are already waiting to resend + if( s_resendPresenceCountdown ) return; + + // MGH - On Vita, change this to use SCE_NP_BASIC_IN_GAME_PRESENCE_TYPE_GAME_JOINING at some point + + // On PS4 we can't set the status and the data at the same time + if( s_presenceStatusDirty == false) + { + return; // nothing to be done. + } + + int err = 0; + // check if we're connected to the PSN first + if(ProfileManager.IsSignedInLive(0))//ProfileManager.getQuadrant(s_lastPresenceInfo.userInfo.userId))) + { + s_lastPresenceInfo.presenceType = SCE_NP_BASIC_IN_GAME_PRESENCE_TYPE_DEFAULT; + // MGH - if we have data being sent, that means we're in an online game, so let others join from our presence info on the XMB + if(s_lastPresenceInfo.size > 0 ) + { + // make sure it's not an invite only game + PresenceSyncInfo *pso = (PresenceSyncInfo *)s_lastPresenceInfo.data; + if(!pso->inviteOnly) + { + s_lastPresenceInfo.presenceType = SCE_NP_BASIC_IN_GAME_PRESENCE_TYPE_GAME_JOINING; + } + } + err = sce::Toolkit::NP::Presence::Interface::setPresence(&s_lastPresenceInfo); + } + + if( err != SCE_TOOLKIT_NP_SUCCESS ) + { + s_resendPresenceCountdown = (20 * 65); // Bit over a minute before attempting to resend, we should get a new token every minute + } + else + { + s_presenceStatusDirty = false; + } +} + +void SQRNetworkManager_Vita::SetPresenceFailedCallback() +{ + s_presenceStatusDirty = true; + s_resendPresenceCountdown = (20 * 65); // Bit over a minute before attempting to resend, we should get a new token every minute +} + +void SQRNetworkManager_Vita::SetPresenceDataStartHostingGame() +{ + if( m_offlineGame ) + { + SQRNetworkManager_Vita::UpdateRichPresenceCustomData(&c_presenceSyncInfoNULL, sizeof(SQRNetworkManager_Vita::PresenceSyncInfo) ); + } + else + { + SQRNetworkManager_Vita::PresenceSyncInfo presenceInfo; + CPlatformNetworkManagerSony::SetSQRPresenceInfoFromExtData( &presenceInfo, m_joinExtData, m_room, m_serverId ); + SQRNetworkManager_Vita::UpdateRichPresenceCustomData(&presenceInfo, sizeof(SQRNetworkManager_Vita::PresenceSyncInfo) ); + // OrbisNPToolkit::createNPSession(); + } +} + +int SQRNetworkManager_Vita::GetJoiningReadyPercentage() +{ + if ( (m_state == SNM_INT_STATE_HOSTING_SEARCHING_FOR_SERVER) || (m_state == SNM_INT_STATE_JOINING_SEARCHING_FOR_SERVER) ) + { + int completed = ( m_totalServerCount - m_serverCount ) - 1; + int pc = ( completed * 100 ) / m_totalServerCount; + if( pc < 0 ) pc = 0; + if( pc > 100 ) pc = 100; + return pc; + } + else + { + return 100; + } +} + +void SQRNetworkManager_Vita::removePlayerFromVoiceChat( SQRNetworkPlayer* pPlayer ) +{ + if(pPlayer->IsLocal()) + { + SonyVoiceChat_Vita::disconnectLocalPlayer(pPlayer->GetLocalPlayerIndex()); + } + else + { + int numRemotePlayersLeft = 0; + for( int i = 0; i < MAX_ONLINE_PLAYER_COUNT; i++ ) + { + if( m_aRoomSlotPlayers[i] ) + { + if( m_aRoomSlotPlayers[i] != pPlayer ) + { + if(m_aRoomSlotPlayers[i]->m_roomMemberId == pPlayer->m_roomMemberId) + numRemotePlayersLeft++; + } + } + } + if(numRemotePlayersLeft == 0) + { + // no players left on the remote machine once we remove this one + SQRVoiceConnection* pVoice = SonyVoiceChat_Vita::getVoiceConnectionFromRoomMemberID(pPlayer->m_roomMemberId); + if(pVoice) + SonyVoiceChat_Vita::disconnectRemoteConnection(pVoice); + } + } +} + + +SQRNetworkPlayer *SQRNetworkManager_Vita::GetPlayerByXuid(PlayerUID xuid) +{ + EnterCriticalSection(&m_csRoomSyncData); + for( int i = 0; i < m_roomSyncData.getPlayerCount(); i++ ) + { + if( m_roomSyncData.players[i].m_UID == xuid ) + { + SQRNetworkPlayer *player = GetPlayerIfReady(m_aRoomSlotPlayers[i]); + LeaveCriticalSection(&m_csRoomSyncData); + return player; + } + } + LeaveCriticalSection(&m_csRoomSyncData); + return NULL; +} + diff --git a/Minecraft.Client/PSVita/Network/SQRNetworkManager_Vita.h b/Minecraft.Client/PSVita/Network/SQRNetworkManager_Vita.h new file mode 100644 index 00000000..592919d4 --- /dev/null +++ b/Minecraft.Client/PSVita/Network/SQRNetworkManager_Vita.h @@ -0,0 +1,324 @@ +#pragma once +#include +#include +#include +#include + +#include + +#include + +#include "..\..\Common\Network\Sony\SQRNetworkManager.h" + +class SQRNetworkPlayer; +class ISQRNetworkManagerListener; +class SonyVoiceChat_Vita; +class SQRVoiceConnection; +class C4JThread; + +// This is the lowest level manager for providing network functionality on Sony platforms. This manages various network activities including the players within a gaming session. +// The game shouldn't directly use this class, it is here to provide functionality required by PlatformNetworkManagerSony. + +class SQRNetworkManager_Vita : public SQRNetworkManager +{ + friend class SonyVoiceChat_Vita; + friend class SQRNetworkPlayer; + + static const eSQRNetworkManagerState m_INTtoEXTStateMappings[SNM_INT_STATE_COUNT]; + +public: + SQRNetworkManager_Vita(ISQRNetworkManagerListener *listener); + + // General + void Tick(); + void Initialise(); + bool IsInitialised(); + void UnInitialise(); + void Terminate(); + eSQRNetworkManagerState GetState(); + bool IsHost(); + bool IsReadyToPlayOrIdle(); + bool IsInSession(); + + // Session management + void CreateAndJoinRoom(int hostIndex, int localPlayerMask, void *extData, int extDataSize, bool offline); + void UpdateExternalRoomData(); + bool FriendRoomManagerIsBusy(); + bool FriendRoomManagerSearch(); + bool FriendRoomManagerSearch2(); + int FriendRoomManagerGetCount(); + void FriendRoomManagerGetRoomInfo(int idx, SessionSearchResult *searchResult); + bool JoinRoom(SessionSearchResult *searchResult, int localPlayerMask); + bool JoinRoom(SceNpMatching2RoomId roomId, SceNpMatching2ServerId serverId, int localPlayerMask, const SQRNetworkManager_Vita::PresenceSyncInfo *presence); + void StartGame(); + void LeaveRoom(bool bActuallyLeaveRoom); + void EndGame(); + bool SessionHasSpace(int spaceRequired); + bool AddLocalPlayerByUserIndex(int idx); + bool RemoveLocalPlayerByUserIndex(int idx); + void SendInviteGUI(); + static void RecvInviteGUI(); + void TickInviteGUI(); + + + + // void GetInviteDataAndProcess(SceNpBasicAttachmentDataId id); + static bool UpdateInviteData(SQRNetworkManager_Vita::PresenceSyncInfo *invite); + void GetExtDataForRoom( SceNpMatching2RoomId roomId, void *extData, void (* FriendSessionUpdatedFn)(bool success, void *pParam), void *pParam ); + + // Player retrieval + int GetPlayerCount(); + int GetOnlinePlayerCount(); + SQRNetworkPlayer *GetPlayerByIndex(int idx); + SQRNetworkPlayer *GetPlayerBySmallId(int idx); + SQRNetworkPlayer *GetLocalPlayerByUserIndex(int idx); + SQRNetworkPlayer *GetPlayerByXuid(PlayerUID xuid); + SQRNetworkPlayer *GetHostPlayer(); + + void removePlayerFromVoiceChat(SQRNetworkPlayer* pPlayer); + // Communication parameter storage + static const SceNpCommunicationId* GetSceNpCommsId(); + static const SceNpCommunicationSignature* GetSceNpCommsSig(); + static const SceNpTitleId* GetSceNpTitleId(); + static const SceNpTitleSecret* GetSceNpTitleSecret(); + + static void GetInviteDataAndProcess(sce::Toolkit::NP::MessageAttachment* pInvite); + static void GetJoinablePresenceDataAndProcess(SceAppUtilNpBasicJoinablePresenceParam* pJoinablePresenceData); + static void ProcessJoinablePresenceData(); + static void TickJoinablePresenceData(); + + static int PSNSignInReturnedPresenceInvite(void* pParam, bool bContinue, int iPad); + + + static bool m_bJoinablePresenceWaitingForOnline; + static SceAppUtilNpBasicJoinablePresenceParam m_joinablePresenceParam; + static bool m_bSendingInviteMessage; + +private: + void InitialiseAfterOnline(); + void ErrorHandlingTick(); + void UpdateOnlineStatus(int status) { m_onlineStatus = status; } + int GetOnlineStatus() { return m_onlineStatus; } + + ISQRNetworkManagerListener *m_listener; + SQRNetworkPlayer *GetPlayerIfReady(SQRNetworkPlayer *player); + + // Internal state + void SetState(eSQRNetworkManagerInternalState state); + void ResetToIdle(); + eSQRNetworkManagerInternalState m_state; + eSQRNetworkManagerState m_stateExternal; + bool m_nextIdleReasonIsFull; + bool m_isHosting; + SceNpMatching2RoomMemberId m_localMemberId; + SceNpMatching2RoomMemberId m_hostMemberId; // if we're not the host + int m_localPlayerCount; + int m_localPlayerJoined; // Client only, keep a count of how many local players we have confirmed as joined to the application + SceNpMatching2RoomId m_room; + unsigned char m_currentSmallId; + int m_soc; + bool m_offlineGame; + bool m_offlineSQR; + int m_resendExternalRoomDataCountdown; + bool m_matching2initialised; + PresenceSyncInfo m_inviteReceived[MAX_SIMULTANEOUS_INVITES]; + int m_inviteIndex; + static PresenceSyncInfo *m_gameBootInvite; + static PresenceSyncInfo m_gameBootInvite_data; + bool m_doBootInviteCheck; + bool m_isInSession; + // static SceNpBasicAttachmentDataId s_lastInviteIdToRetry; + int m_onlineStatus; + bool m_bLinkDisconnected; + + +private: + + CRITICAL_SECTION m_csRoomSyncData; + RoomSyncData m_roomSyncData; + void *m_joinExtData; + int m_joinExtDataSize; + + std::vector m_vecTempPlayers; + SQRNetworkPlayer *m_aRoomSlotPlayers[MAX_ONLINE_PLAYER_COUNT]; // Maps from the players in m_roomSyncData, to SQRNetworkPlayers + void FindOrCreateNonNetworkPlayer(int slot, int playerType, SceNpMatching2RoomMemberId memberId, int localPlayerIdx, int smallId); + + void MapRoomSlotPlayers(int roomSlotPlayerCount =-1); + void UpdateRoomSyncUIDsFromPlayers(); + void UpdatePlayersFromRoomSyncUIDs(); + void LocalDataSend(SQRNetworkPlayer *playerFrom, SQRNetworkPlayer *playerTo, const void *data, unsigned int dataSize); + int GetSessionIndex(SQRNetworkPlayer *player); + + bool AddRemotePlayersAndSync( SceNpMatching2RoomMemberId memberId, int playerMask, bool *isFull = NULL ); + void RemoveRemotePlayersAndSync( SceNpMatching2RoomMemberId memberId, int mask ); + void RemoveNetworkPlayers( int mask ); + void SetLocalPlayersAndSync(); + void SyncRoomData(); + SceNpMatching2RequestId m_setRoomDataRequestId; + SceNpMatching2RequestId m_setRoomIntDataRequestId; + SceNpMatching2RequestId m_roomExtDataRequestId; + + // Server context management + bool GetMatchingContext(eSQRNetworkManagerInternalState asyncState); + bool GetServerContext(); + bool GetServerContext_AdHoc(); + bool GetServerContext2(); + bool GetServerContext(SceNpMatching2ServerId serverId); + void DeleteServerContext(); + bool SelectRandomServer(); + void ServerContextTick(); + int m_totalServerCount; + int m_serverCount; + SceNpMatching2ServerId *m_aServerId; + SceNpMatching2ServerId m_serverId; + bool m_serverContextValid; + SceNpMatching2RequestId m_serverSearchRequestId; + SceNpMatching2RequestId m_serverContextRequestId; + + // Room creation management + SceNpMatching2RequestId m_getWorldRequestId; + SceNpMatching2RequestId m_createRoomRequestId; + SceNpMatching2WorldId m_worldId; + void RoomCreateTick(); + + // Room joining management + SceNpMatching2RoomId m_roomToJoin; + int m_localPlayerJoinMask; + SceNpMatching2RequestId m_joinRoomRequestId; + SceNpMatching2RequestId m_kickRequestId; + + // Room leaving management + SceNpMatching2RequestId m_leaveRoomRequestId; + + // Adding extra network players management + SceNpMatching2RequestId m_setRoomMemberInternalDataRequestId; + + // Player state management + void NetworkPlayerConnectionComplete(SQRNetworkPlayer *player); + void NetworkPlayerSmallIdAllocated(SQRNetworkPlayer *player, unsigned char smallId); + void NetworkPlayerInitialDataReceived(SQRNetworkPlayer *player,void *data); + void NonNetworkPlayerComplete(SQRNetworkPlayer *player, unsigned char smallId); + void HandlePlayerJoined(SQRNetworkPlayer *player); + CRITICAL_SECTION m_csPlayerState; + + // State and thread for managing basic event type messages + C4JThread *m_basicEventThread; +// SceKernelEqueue m_basicEventQueue; + static int BasicEventThreadProc( void *lpParameter); + + // State and storage for managing search for friends' games + eSQRNetworkManagerFriendSearchState m_friendSearchState; + SceNpMatching2ContextId m_matchingContext; + bool m_matchingContextValid; + SceNpMatching2RequestId m_friendSearchRequestId; + unsigned int m_friendCount; + C4JThread *m_getFriendCountThread; + static int GetFriendsThreadProc( void* lpParameter ); + void FriendSearchTick(); + SceNpMatching2RequestId m_roomDataExternalListRequestId; + void (* m_FriendSessionUpdatedFn)(bool success, void *pParam); + void *m_pParamFriendSessionUpdated; + void *m_pExtDataToUpdate; + + // Results from searching for rooms that friends are playing in - 5 matched arrays to store their NpIds, rooms, servers, whether a room was found, and whether the external data had been received for the room. Also a count of how many elements are used in this array. + class FriendSearchResult + { + public: + SceNpId m_NpId; + SceNpMatching2RoomId m_RoomId; + SceNpMatching2ServerId m_ServerId; + bool m_RoomFound; + void *m_RoomExtDataReceived; + }; + std::vector m_aFriendSearchResults; + + // Rudp management and local players + std::unordered_map m_RudpCtxToPlayerMap; + + std::unordered_map m_NetAddrToVoiceConnectionMap; + + bool CreateRudpConnections(SceNpMatching2RoomId roomId, SceNpMatching2RoomMemberId peerMemberId, int playerMask, SceNpMatching2RoomMemberId playersPeerMemberId); + bool CreateVoiceRudpConnections(SceNpMatching2RoomId roomId, SceNpMatching2RoomMemberId peerMemberId, int playerMask); + bool CreateSocket(); + SQRNetworkPlayer *GetPlayerFromRudpCtx(int rudpCtx); + SQRVoiceConnection* GetVoiceConnectionFromRudpCtx(int rudpCtx); + + SQRNetworkPlayer *GetPlayerFromRoomMemberAndLocalIdx(int roomMember, int localIdx); + SceNpMatching2RequestId m_roomMemberDataRequestId; + + // Callbacks (for matching) + bool RegisterCallbacks(); + static void ContextCallback(SceNpMatching2ContextId id, SceNpMatching2Event event, SceNpMatching2EventCause eventCause, int errorCode, void *arg); + // #ifdef __PS3__ + // static void DefaultRequestCallback(SceNpMatching2ContextId id, SceNpMatching2RequestId reqId, SceNpMatching2Event event, SceNpMatching2EventKey eventKey, int errorCode, size_t dataSize, void *arg); + // static void RoomEventCallback(SceNpMatching2ContextId id, SceNpMatching2RoomId roomId, SceNpMatching2Event event, SceNpMatching2EventKey eventKey, int errorCode, size_t dataSize, void *arg); + // #else + static void DefaultRequestCallback(SceNpMatching2ContextId id, SceNpMatching2RequestId reqId, SceNpMatching2Event event, int errorCode, const void *data, void *arg); + static void RoomEventCallback(SceNpMatching2ContextId id, SceNpMatching2RoomId roomId, SceNpMatching2Event event, const void *data, void *arg); + // #endif + static void SignallingCallback(SceNpMatching2ContextId ctxId, SceNpMatching2RoomId roomId, SceNpMatching2RoomMemberId peerMemberId, SceNpMatching2Event event, int error_code, void *arg); + + // Callback for NpBasic + static int BasicEventCallback(int event, int retCode, uint32_t reqId, void *arg); + + // Callback for NpManager + static void ManagerCallback(int event, int result, void *arg); + + // Callback for sys util + static void SysUtilCallback(uint64_t status, uint64_t param, void *userdata); + void updateNetCheckDialog(); // get the status of the dialog and run any callbacks needed + + // Callbacks for rudp + static void RudpContextCallback(int ctx_id, int event_id, int error_code, void *arg); + static int RudpEventCallback(int event_id, int soc, uint8_t const *data, size_t datalen, struct SceNetSockaddr const *addr, SceNetSocklen_t addrlen, void *arg); + + // Callback for netctl + static void NetCtlCallback(int eventType, void *arg); + + // Methods to be called when the server context has been created + void ServerContextValid_CreateRoom(); + void ServerContextValid_JoinRoom(); + + // Mask utilities + int GetOldMask(SceNpMatching2RoomMemberId memberId); + int GetAddedMask(int newMask, int oldMask); + int GetRemovedMask(int newMask, int oldMask); + +#ifndef _CONTENT_PACKAGE + static bool aForceError[SNM_FORCE_ERROR_COUNT]; +#endif + bool ForceErrorPoint(eSQRForceError err); + +public: + static void AttemptPSNSignIn(int (*SignInCompleteCallbackFn)(void *pParam, bool bContinue, int pad), void *pParam, bool callIfFailed = false); + static int (*s_SignInCompleteCallbackFn)(void *pParam, bool bContinue, int pad); + static bool s_signInCompleteCallbackIfFailed; + static void *s_SignInCompleteParam; + + static int SetRichPresence(const void *data); + void SetPresenceDataStartHostingGame(); + int GetJoiningReadyPercentage(); + static void SetPresenceFailedCallback(); + GameSessionUID GetHostUID() { return s_lastPresenceSyncInfo.hostPlayerUID; } +private: + static void UpdateRichPresenceCustomData(void *data, unsigned int dataBytes); + static void TickRichPresence(); + static void SendLastPresenceInfo(); + void OnlineCheck(); + + static sce::Toolkit::NP::PresenceDetails s_lastPresenceInfo; + static int s_resendPresenceCountdown; + static bool s_presenceStatusDirty; + static PresenceSyncInfo s_lastPresenceSyncInfo; + static PresenceSyncInfo c_presenceSyncInfoNULL; + static bool b_inviteRecvGUIRunning; + // 4J-PB - so we can stop the crash when Iggy's LoadMovie is called from the ContextCallback + static bool m_bCallPSNSignInCallback; + // Debug + static long long s_roomStartTime; + + int m_hid; + bool m_bIsInitialised; + bool m_bShuttingDown; +}; + diff --git a/Minecraft.Client/PSVita/Network/SonyCommerce_Vita.cpp b/Minecraft.Client/PSVita/Network/SonyCommerce_Vita.cpp new file mode 100644 index 00000000..870930f6 --- /dev/null +++ b/Minecraft.Client/PSVita/Network/SonyCommerce_Vita.cpp @@ -0,0 +1,1519 @@ +#include "stdafx.h" + +#include "SonyCommerce_Vita.h" +#include "ShutdownManager.h" +#include +#include +#include + +bool SonyCommerce_Vita::m_bCommerceInitialised = false; +// SceNpCommerce2SessionInfo SonyCommerce_Vita::m_sessionInfo; +SonyCommerce_Vita::State SonyCommerce_Vita::m_state = e_state_noSession; +int SonyCommerce_Vita::m_errorCode = 0; +LPVOID SonyCommerce_Vita::m_callbackParam = NULL; + +void* SonyCommerce_Vita::m_receiveBuffer = NULL; +SonyCommerce_Vita::Event SonyCommerce_Vita::m_event; +std::queue SonyCommerce_Vita::m_messageQueue; +std::vector* SonyCommerce_Vita::m_pProductInfoList = NULL; +SonyCommerce_Vita::ProductInfoDetailed* SonyCommerce_Vita::m_pProductInfoDetailed = NULL; +SonyCommerce_Vita::ProductInfo* SonyCommerce_Vita::m_pProductInfo = NULL; + +SonyCommerce_Vita::CategoryInfo* SonyCommerce_Vita::m_pCategoryInfo = NULL; +const char* SonyCommerce_Vita::m_pProductID = NULL; +char* SonyCommerce_Vita::m_pCategoryID = NULL; +SonyCommerce_Vita::CheckoutInputParams SonyCommerce_Vita::m_checkoutInputParams; +SonyCommerce_Vita::DownloadListInputParams SonyCommerce_Vita::m_downloadInputParams; + +SonyCommerce_Vita::CallbackFunc SonyCommerce_Vita::m_callbackFunc = NULL; +// sys_memory_container_t SonyCommerce_Vita::m_memContainer = SYS_MEMORY_CONTAINER_ID_INVALID; +bool SonyCommerce_Vita::m_bUpgradingTrial = false; + +SonyCommerce_Vita::CallbackFunc SonyCommerce_Vita::m_trialUpgradeCallbackFunc; +LPVOID SonyCommerce_Vita::m_trialUpgradeCallbackParam; + +CRITICAL_SECTION SonyCommerce_Vita::m_queueLock; + +uint32_t SonyCommerce_Vita::m_contextId=0; ///< The npcommerce2 context ID +bool SonyCommerce_Vita::m_contextCreated=false; ///< npcommerce2 context ID created? +SonyCommerce_Vita::Phase SonyCommerce_Vita::m_currentPhase = e_phase_stopped; ///< Current commerce2 util +// char SonyCommerce_Vita::m_commercebuffer[SCE_NP_COMMERCE2_RECV_BUF_SIZE]; + +C4JThread* SonyCommerce_Vita::m_tickThread = NULL; +bool SonyCommerce_Vita::m_bLicenseChecked=false; // Check the trial/full license for the game +bool SonyCommerce_Vita::m_bLicenseInstalled=false; // set to true when the licence has been downloaded and installed (but maybe not checked yet) +bool SonyCommerce_Vita::m_bDownloadsPending=false; // set to true if there are any downloads happening in the background, so we check for them completing, and install when finished +bool SonyCommerce_Vita::m_bDownloadsReady=false; // set to true if there are any downloads ready to install +bool SonyCommerce_Vita::m_bInstallingContent=false; // set to true while new content is being installed, so we don't fire it mulitple times +int SonyCommerce_Vita::m_iClearDLCCountdown=0; // tick for a set number of frames before clearing the DLC, as sometimes it doesn't register as being installed in time +bool SonyCommerce_Vita::m_bPurchasabilityUpdated=false; // set to when any purchase flags change +SonyCommerce_Vita::Message SonyCommerce_Vita::m_lastMessage; + +sce::Toolkit::NP::Utilities::Future > g_productList; +sce::Toolkit::NP::Utilities::Future g_categoryInfo; +sce::Toolkit::NP::Utilities::Future g_detailedProductInfo; + +//sce::Toolkit::NP::Utilities::Future g_bgdlStatus; + + +SonyCommerce_Vita::ProductInfoDetailed s_trialUpgradeProductInfoDetailed; +void SonyCommerce_Vita::Delete() +{ + m_pProductInfoList=NULL; + m_pProductInfoDetailed=NULL; + m_pProductInfo=NULL; + m_pCategoryInfo = NULL; + m_pProductID = NULL; + m_pCategoryID = NULL; +} + +void SonyCommerce_Vita::Init() +{ + assert(m_state == e_state_noSession); + if(!m_bCommerceInitialised) + { + m_bCommerceInitialised = true; + m_pCategoryID=(char *)malloc(sizeof(char) * 100); + InitializeCriticalSection(&m_queueLock); + m_bLicenseInstalled = false; + m_bDownloadsPending = false; + m_bDownloadsReady = false; + + } +} + + + +void SonyCommerce_Vita::CheckForTrialUpgradeKey_Callback(LPVOID param, bool bFullVersion) +{ + ProfileManager.SetFullVersion(bFullVersion); + if(ProfileManager.IsFullVersion()) + { + StorageManager.SetSaveDisabled(false); + ConsoleUIController::handleUnlockFullVersionCallback(); + // licence has been checked, so we're ok to install the trophies now + // ProfileManager.InitialiseTrophies( SQRNetworkManager_Vita::GetSceNpCommsId(), + // SQRNetworkManager_Vita::GetSceNpCommsSig()); + // + } + m_bLicenseChecked=true; + m_bLicenseInstalled = bFullVersion; +} + +bool SonyCommerce_Vita::LicenseChecked() +{ + return m_bLicenseChecked; +} + +void SonyCommerce_Vita::CheckForTrialUpgradeKey() +{ + StorageManager.CheckForTrialUpgradeKey(CheckForTrialUpgradeKey_Callback, NULL); +} + +int SonyCommerce_Vita::Shutdown() +{ + int ret=0; + if (m_contextCreated) + { + m_contextId = 0; + m_contextCreated = false; + } + + m_bCommerceInitialised = false; + delete m_pCategoryID; + DeleteCriticalSection(&m_queueLock); + + return ret; +} + +void SonyCommerce_Vita::InstallContentCallback(LPVOID lpParam,int err) +{ + m_iClearDLCCountdown = 30; + m_bInstallingContent = false; + if(m_bLicenseInstalled && !ProfileManager.IsFullVersion()) + app.GetCommerce()->CheckForTrialUpgradeKey(); +} + +void SonyCommerce_Vita::checkBackgroundDownloadStatus() +{ + if( m_bInstallingContent ) + return; + + Future status; + int ret = sce::Toolkit::NP::Commerce::Interface::getBgdlStatus(&status, false); + if(ret == SCE_OK) + { + bool bInstallContent = false; + // check for the license having been downloaded first + if(!m_bLicenseInstalled && status.get()->licenseReady) + { + m_bLicenseInstalled = true; + bInstallContent = true; + } + + // and now any additional content + m_bDownloadsReady = (status.get()->addcontNumReady > 0); + + if(m_bDownloadsReady) + bInstallContent = true; + // and if there are any downloads still pending, we'll call this function again + m_bDownloadsPending = (status.get()->addcontNumNotReady > 0); + + // install the content + if(bInstallContent) + { + InstallContent(InstallContentCallback, NULL); + } + } +} + +int SonyCommerce_Vita::TickLoop(void* lpParam) +{ + ShutdownManager::HasStarted(ShutdownManager::eCommerceThread); + while( (m_currentPhase != e_phase_stopped) && ShutdownManager::ShouldRun(ShutdownManager::eCommerceThread) ) + { + processEvent(); + processMessage(); + Sleep(16); // sleep for a frame + //((SonyCommerce_Vita*)app.GetCommerce())->Test(); + if(m_bDownloadsPending || m_bDownloadsReady) + { + checkBackgroundDownloadStatus(); + } + if(m_iClearDLCCountdown > 0) // tick for a set number of frames before clearing the DLC, as sometimes it doesn't register as being installed in time + { + m_iClearDLCCountdown--; + if(m_iClearDLCCountdown == 0) + { + app.ClearDLCInstalled(); + ui.HandleDLCInstalled(0); + } + + } + } + ShutdownManager::HasFinished(ShutdownManager::eCommerceThread); + + return 0; +} + +void SonyCommerce_Vita::copyProductList(std::vector* pProductList, std::vector* pNPProductList) +{ + ProductInfo tempInfo; + std::vector tempProductVec; + // Reserve some space + int numProducts = pNPProductList->size(); + tempProductVec.reserve(numProducts); + for(int i=0;iat(i); + + // reset tempInfo + memset(&tempInfo, 0x0, sizeof(tempInfo)); + strncpy(tempInfo.productId, npInfo.productId, SCE_NP_COMMERCE2_PRODUCT_ID_LEN); + strncpy(tempInfo.productName, npInfo.productName, SCE_NP_COMMERCE2_PRODUCT_NAME_LEN); + strncpy(tempInfo.shortDescription, npInfo.shortDescription, SCE_NP_COMMERCE2_PRODUCT_SHORT_DESCRIPTION_LEN); + strcpy(tempInfo.longDescription,"Missing long description"); + strncpy(tempInfo.spName, npInfo.spName, SCE_NP_COMMERCE2_SP_NAME_LEN); + strncpy(tempInfo.imageUrl, npInfo.imageUrl, SCE_NP_COMMERCE2_URL_LEN); + tempInfo.releaseDate = npInfo.releaseDate; + tempInfo.purchasabilityFlag = npInfo.purchasabilityFlag; + m_bPurchasabilityUpdated = true; + // Take out the price. Nicely formatted + // but also keep the price as a value in case it's 0 - we need to show "free" for that + tempInfo.ui32Price = -1;// not available here + strncpy(tempInfo.price, npInfo.price, SCE_TOOLKIT_NP_SKU_PRICE_LEN); + tempProductVec.push_back(tempInfo); + } + pNPProductList->clear(); // clear the vector now we're done, this doesn't happen automatically for the next query + + // Set our result + *pProductList = tempProductVec; +} + +int SonyCommerce_Vita::getProductList(std::vector* productList, char *categoryId) +{ + int ret; + sce::Toolkit::NP::ProductListInputParams params; + int userId = ProfileManager.getUserID(ProfileManager.GetPrimaryPad()); + +// params.userInfo.userId = userId; + strcpy(params.categoryId, categoryId); + params.serviceLabel = 0; + app.DebugPrintf("Getting Product List ...\n"); + + ret = sce::Toolkit::NP::Commerce::Interface::getProductList(&g_productList, params, true); + + app.DebugPrintf(" ----||||---- sce::Toolkit::NP::Commerce::Interface::getProductList : \n \t categoryId %s\n", categoryId); + if (ret < 0) + { + app.DebugPrintf("CommerceInterface::getProductList() error. ret = 0x%x\n", ret); + return ret; + } + + if (g_productList.hasResult()) + { + // result has returned immediately (don't think this should happen, but was handled in the samples + copyProductList(productList, g_productList.get()); + m_event = e_event_commerceGotProductList; + } + return ret; +} + + + +void SonyCommerce_Vita::copyCategoryInfo(CategoryInfo *pInfo, sce::Toolkit::NP::CategoryInfo *pNPInfo) +{ + app.DebugPrintf("copyCategoryInfo %s\n", pNPInfo->current.categoryId); + strcpy(pInfo->current.categoryId, pNPInfo->current.categoryId); + strcpy(pInfo->current.categoryName, pNPInfo->current.categoryName); + strcpy(pInfo->current.categoryDescription, pNPInfo->current.categoryDescription); + strcpy(pInfo->current.imageUrl, pNPInfo->current.imageUrl); + pInfo->countOfProducts = pNPInfo->countOfProducts; + pInfo->countOfSubCategories = pNPInfo->countOfSubCategories; + if(pInfo->countOfSubCategories > 0) + { + std::list::iterator iter = pNPInfo->subCategories.begin(); + std::list::iterator iterEnd = pNPInfo->subCategories.end(); + + while(iter != iterEnd) + { + // For each sub category, obtain information + app.DebugPrintf("copyCategoryInfo subcat - %s\n", iter->categoryId); + + CategoryInfoSub tempSubCatInfo; + strcpy(tempSubCatInfo.categoryId, iter->categoryId); + strcpy(tempSubCatInfo.categoryName, iter->categoryName); + strcpy(tempSubCatInfo.categoryDescription, iter->categoryDescription); + strcpy(tempSubCatInfo.imageUrl, iter->imageUrl); + // Add to the list + pInfo->subCategories.push_back(tempSubCatInfo); + iter++; + } + } +} + +int SonyCommerce_Vita::getCategoryInfo(CategoryInfo *pInfo, char *categoryId) +{ + int ret; + sce::Toolkit::NP::CategoryInfoInputParams params; + int userId = ProfileManager.getUserID(ProfileManager.GetPrimaryPad()); + + params.userInfo.userId = userId; + strcpy(params.categoryId, "");//categoryId); + params.serviceLabel = 0; + + app.DebugPrintf("Getting Category Information...\n"); + + ret = sce::Toolkit::NP::Commerce::Interface::getCategoryInfo(&g_categoryInfo, params, true); + app.DebugPrintf(" ----||||---- sce::Toolkit::NP::Commerce::Interface::getCategoryInfo : \n \t userID %d\n \t categoryId %s\n", userId, categoryId); + if (ret < 0) + { + // error + app.DebugPrintf("Commerce::Interface::getCategoryInfo error: 0x%x\n", ret); + return ret; + } + else if (g_categoryInfo.hasResult()) + { + // result has returned immediately (don't think this should happen, but was handled in the samples + copyCategoryInfo(pInfo, g_categoryInfo.get()); + m_event = e_event_commerceGotCategoryInfo; + } + return ret; +} + +void SonyCommerce_Vita::copyDetailedProductInfo(ProductInfoDetailed *pInfo, sce::Toolkit::NP::ProductInfoDetailed* pNPInfo) +{ + // populate our temp struct + // pInfo->ratingDescriptors = npInfo.ratingSystemId; + strncpy(pInfo->productId, pNPInfo->productId, SCE_NP_COMMERCE2_PRODUCT_ID_LEN); + strncpy(pInfo->productName, pNPInfo->productName, SCE_NP_COMMERCE2_PRODUCT_NAME_LEN); + strncpy(pInfo->shortDescription, pNPInfo->shortDescription, SCE_NP_COMMERCE2_PRODUCT_SHORT_DESCRIPTION_LEN); + strncpy(pInfo->longDescription, pNPInfo->longDescription, SCE_NP_COMMERCE2_PRODUCT_LONG_DESCRIPTION_LEN); + strncpy(pInfo->legalDescription, pNPInfo->legalDescription, SCE_NP_COMMERCE2_PRODUCT_LEGAL_DESCRIPTION_LEN); + strncpy(pInfo->spName, pNPInfo->spName, SCE_NP_COMMERCE2_SP_NAME_LEN); + strncpy(pInfo->imageUrl, pNPInfo->imageUrl, SCE_NP_COMMERCE2_URL_LEN); + pInfo->releaseDate = pNPInfo->releaseDate; + strncpy(pInfo->ratingSystemId, pNPInfo->ratingSystemId, SCE_NP_COMMERCE2_RATING_SYSTEM_ID_LEN); + strncpy(pInfo->ratingImageUrl, pNPInfo->imageUrl, SCE_NP_COMMERCE2_URL_LEN); + strncpy(pInfo->skuId, pNPInfo->skuId, SCE_NP_COMMERCE2_SKU_ID_LEN); + pInfo->purchasabilityFlag = pNPInfo->purchasabilityFlag; + m_bPurchasabilityUpdated = true; + pInfo->ui32Price= pNPInfo->intPrice; + strncpy(pInfo->price, pNPInfo->price, SCE_TOOLKIT_NP_SKU_PRICE_LEN); + +} +int SonyCommerce_Vita::getDetailedProductInfo(ProductInfoDetailed *pInfo, const char *productId, char *categoryId) +{ + int ret; + sce::Toolkit::NP::DetailedProductInfoInputParams params; + int userId = ProfileManager.getUserID(ProfileManager.GetPrimaryPad()); + + //CD - userInfo no longer exists in DetailedProductInfoInputParams struct + //params.userInfo.userId = userId; + strcpy(params.categoryId, categoryId); + strcpy(params.productId, productId); + + + app.DebugPrintf("Getting Detailed Product Information ... \n"); + if(g_detailedProductInfo.get()) // MGH - clear the price out, in case something is hanging around from a previous call + { + g_detailedProductInfo.get()->intPrice = -1; + g_detailedProductInfo.get()->price[0] = 0; + } + ret = sce::Toolkit::NP::Commerce::Interface::getDetailedProductInfo(&g_detailedProductInfo, params, true); + app.DebugPrintf(" ----||||---- sce::Toolkit::NP::Commerce::Interface::getDetailedProductInfo : \n \t userID %d\n \t categoryId %s\n \t productId %s\n", userId, categoryId, productId); + + if (ret < 0) + { + app.DebugPrintf("CommerceInterface::getDetailedProductInfo() error. ret = 0x%x\n", ret); + return ret; + } + + if (g_detailedProductInfo.hasResult()) + { + // result has returned immediately (don't think this should happen, but was handled in the samples + copyDetailedProductInfo(pInfo, g_detailedProductInfo.get()); + m_event = e_event_commerceGotDetailedProductInfo; + } + return ret; +} + +void SonyCommerce_Vita::copyAddDetailedProductInfo(ProductInfo *pInfo, sce::Toolkit::NP::ProductInfoDetailed* pNPInfo) +{ + + // populate our temp struct + // pInfo->ratingDescriptors = npInfo.ratingSystemId; + // strncpy(pInfo->productId, npInfo.productId, SCE_NP_COMMERCE2_PRODUCT_ID_LEN); + // strncpy(pInfo->productName, npInfo.productName, SCE_NP_COMMERCE2_PRODUCT_NAME_LEN); + // strncpy(pInfo->shortDescription, npInfo.shortDescription, SCE_NP_COMMERCE2_PRODUCT_SHORT_DESCRIPTION_LEN); + strncpy(pInfo->longDescription, pNPInfo->longDescription, SCE_NP_COMMERCE2_PRODUCT_LONG_DESCRIPTION_LEN); + // strncpy(pInfo->legalDescription, npInfo.legalDescription, SCE_NP_COMMERCE2_PRODUCT_LEGAL_DESCRIPTION_LEN); + // strncpy(pInfo->spName, npInfo.spName, SCE_NP_COMMERCE2_SP_NAME_LEN); + // strncpy(pInfo->imageUrl, npInfo.imageUrl, SCE_NP_COMMERCE2_URL_LEN); + // pInfo->releaseDate = npInfo.releaseDate; + // strncpy(pInfo->ratingSystemId, npInfo.ratingSystemId, SCE_NP_COMMERCE2_RATING_SYSTEM_ID_LEN); + // strncpy(pInfo->ratingImageUrl, npInfo.imageUrl, SCE_NP_COMMERCE2_URL_LEN); + strncpy(pInfo->skuId, pNPInfo->skuId, SCE_NP_COMMERCE2_SKU_ID_LEN); + pInfo->purchasabilityFlag = pNPInfo->purchasabilityFlag; + m_bPurchasabilityUpdated = true; + pInfo->ui32Price= pNPInfo->intPrice; + strncpy(pInfo->price, pNPInfo->price, SCE_TOOLKIT_NP_SKU_PRICE_LEN); + + app.DebugPrintf(" ---- description - %s\n", pInfo->longDescription); + app.DebugPrintf(" ---- price - %d\n", pInfo->price); + app.DebugPrintf(" ---- hasPurchased %d\n", pInfo->purchasabilityFlag); + +} + +int SonyCommerce_Vita::addDetailedProductInfo(ProductInfo *pInfo, const char *productId, char *categoryId) +{ + int ret; + sce::Toolkit::NP::DetailedProductInfoInputParams params; + int userId = ProfileManager.getUserID(ProfileManager.GetPrimaryPad()); + + //CD - userInfo no longer exists in DetailedProductInfoInputParams struct + //params.userInfo.userId = userId; + strcpy(params.categoryId, categoryId); + strcpy(params.productId, productId); + + + app.DebugPrintf("Getting Detailed Product Information ... \n"); + if(g_detailedProductInfo.get()) // MGH - clear the price out, in case something is hanging around from a previous call + { + g_detailedProductInfo.get()->intPrice = -1; + g_detailedProductInfo.get()->price[0] = 0; + } + ret = sce::Toolkit::NP::Commerce::Interface::getDetailedProductInfo(&g_detailedProductInfo, params, true); + app.DebugPrintf(" ----||||---- sce::Toolkit::NP::Commerce::Interface::getDetailedProductInfo : \n \t userID %d\n \t categoryId %s\n \t productId %s\n", userId, categoryId, productId); + + if (ret < 0) + { + app.DebugPrintf("CommerceInterface::addDetailedProductInfo() error. ret = 0x%x\n", ret); + } + + if (g_detailedProductInfo.hasResult()) + { + // result has returned immediately (don't think this should happen, but was handled in the samples + copyAddDetailedProductInfo(pInfo, g_detailedProductInfo.get()); + m_event = e_event_commerceAddedDetailedProductInfo; + } + return ret; +} + + +int SonyCommerce_Vita::checkout(CheckoutInputParams ¶ms) +{ + int ret; + sce::Toolkit::NP::CheckoutInputParams npParams; + int userId = ProfileManager.getUserID(ProfileManager.GetPrimaryPad()); + + //CD - userInfo no longer exists in CheckoutInputParams struct + //npParams.userInfo.userId = userId; + npParams.serviceLabel = 0; + + std::list::iterator iter = params.skuIds.begin(); + std::list::iterator iterEnd = params.skuIds.end(); + while(iter != iterEnd) + { + npParams.skuIds.push_back((char*)*iter); // have to remove the const here, not sure why the libs pointers aren't const + iter++; + } + + app.DebugPrintf("Starting SonyCommerce_Vita::checkout...\n"); + ret = sce::Toolkit::NP::Commerce::Interface::checkout(npParams, false); + if (ret < 0) + { + app.DebugPrintf("checkout() error. ret = 0x%x\n", ret); + } + return ret; +} + + +int SonyCommerce_Vita::downloadList(DownloadListInputParams ¶ms) +{ + int ret; + sce::Toolkit::NP::DownloadListInputParams npParams; + int userId = ProfileManager.getUserID(ProfileManager.GetPrimaryPad()); + //CD - userInfo no longer exists in DownloadListInputParams struct + //npParams.userInfo.userId = userId; + npParams.serviceLabel = 0; + + std::list::iterator iter = params.skuIds.begin(); + std::list::iterator iterEnd = params.skuIds.end(); + while(iter != iterEnd) + { + npParams.skuIds.push_back((char*)*iter); // have to remove the const here, not sure why the libs pointers aren't const + iter++; + } + + app.DebugPrintf("Starting Store Download List...\n"); + ret = sce::Toolkit::NP::Commerce::Interface::displayDownloadList(npParams, true); + if (ret < 0) + { + app.DebugPrintf("Commerce::Interface::displayDownloadList error: 0x%x\n", ret); + } + return ret; +} + +int SonyCommerce_Vita::checkout_game(CheckoutInputParams ¶ms) +{ + + int ret; + sce::Toolkit::NP::CheckoutInputParams npParams; + npParams.serviceLabel = 0; + + std::list::iterator iter = params.skuIds.begin(); + std::list::iterator iterEnd = params.skuIds.end(); + while(iter != iterEnd) + { + npParams.skuIds.push_back((char*)*iter); // have to remove the const here, not sure why the libs pointers aren't const + iter++; + } + + app.DebugPrintf("Starting Checkout...\n"); + sce::Toolkit::NP::ProductBrowseParams Myparams; + + Myparams.serviceLabel = 0; + strncpy(Myparams.productId, app.GetUpgradeKey(), strlen(app.GetUpgradeKey())); + + ret = sce::Toolkit::NP::Commerce::Interface::productBrowse(Myparams, false); + + //ret = sce::Toolkit::NP::Commerce::Interface::checkout(npParams, false); + if (ret < 0) + { + app.DebugPrintf("Sample menu checkout() error. ret = 0x%x\n", ret); + } + + // we don't seem to get any of the productBrowse completion callbacks on Vita, so just force us into that state next + m_event = e_event_commerceProductBrowseFinished; + + return ret; +} + +int SonyCommerce_Vita::downloadList_game(DownloadListInputParams ¶ms) +{ + + int ret; + sce::Toolkit::NP::DownloadListInputParams npParams; + //memset(&npParams,0,sizeof(sce::Toolkit::NP::DownloadListInputParams)); + npParams.serviceLabel = 0; + npParams.skuIds.clear(); + + std::list::iterator iter = params.skuIds.begin(); + std::list::iterator iterEnd = params.skuIds.end(); + while(iter != iterEnd) + { + npParams.skuIds.push_back((char*)*iter); // have to remove the const here, not sure why the libs pointers aren't const + iter++; + } + + app.DebugPrintf("Starting Store Download List...\n"); + // ret = sce::Toolkit::NP::Commerce::Interface::displayDownloadList(npParams, true); + // if (ret < 0) + // { + // app.DebugPrintf("Commerce::Interface::displayDownloadList error: 0x%x\n", ret); + // } + + sce::Toolkit::NP::ProductBrowseParams Myparams; + + Myparams.serviceLabel = 0; + strncpy(Myparams.productId, "EP4433-PCSB00560_00-MINECRAFTVIT0452", strlen("EP4433-PCSB00560_00-MINECRAFTVIT0452")); + + ret = sce::Toolkit::NP::Commerce::Interface::productBrowse(Myparams, false); + if (ret < 0) + { + // Error handling + app.DebugPrintf("Commerce::Interface::displayDownloadList error: 0x%x\n", ret); + } + + + + // we don't seem to get any of the productBrowse completion callbacks on Vita, so just force us into that state next + m_event = e_event_commerceProductBrowseFinished; + + return ret; +} + +int SonyCommerce_Vita::installContent() +{ + int ret; + ret = sce::Toolkit::NP::Commerce::Interface::installContent(); + return ret; +} + + +void SonyCommerce_Vita::UpgradeTrialCallback2(LPVOID lpParam,int err) +{ + SonyCommerce* pCommerce = (SonyCommerce*)lpParam; + app.DebugPrintf(4,"SonyCommerce_UpgradeTrialCallback2 : err : 0x%08x\n", err); + pCommerce->CheckForTrialUpgradeKey(); + if(err != SCE_OK) + { + UINT uiIDA[1]; + uiIDA[0]=IDS_CONFIRM_OK; + C4JStorage::EMessageResult result = ui.RequestMessageBox( IDS_PRO_UNLOCKGAME_TITLE, IDS_NO_DLCOFFERS, uiIDA,1,ProfileManager.GetPrimaryPad()); + } + m_trialUpgradeCallbackFunc(m_trialUpgradeCallbackParam, m_errorCode); +} + +void SonyCommerce_Vita::UpgradeTrialCallback1(LPVOID lpParam,int err) +{ + SonyCommerce* pCommerce = (SonyCommerce*)lpParam; + app.DebugPrintf(4,"SonyCommerce_UpgradeTrialCallback1 : err : 0x%08x\n", err); + if(err == SCE_OK) + { + char* skuID = s_trialUpgradeProductInfoDetailed.skuId; + if(s_trialUpgradeProductInfoDetailed.purchasabilityFlag == SCE_TOOLKIT_NP_COMMERCE_NOT_PURCHASED) + { + app.DebugPrintf(4,"UpgradeTrialCallback1 - Checkout\n"); + pCommerce->Checkout_Game(UpgradeTrialCallback2, pCommerce, skuID); + } + else + { + app.DebugPrintf(4,"UpgradeTrialCallback1 - DownloadAlreadyPurchased\n"); + pCommerce->DownloadAlreadyPurchased_Game(UpgradeTrialCallback2, pCommerce, skuID); + } + } + else + { + UINT uiIDA[1]; + uiIDA[0]=IDS_CONFIRM_OK; + C4JStorage::EMessageResult result = ui.RequestMessageBox( IDS_PRO_UNLOCKGAME_TITLE, IDS_NO_DLCOFFERS, uiIDA,1,ProfileManager.GetPrimaryPad()); + m_trialUpgradeCallbackFunc(m_trialUpgradeCallbackParam, m_errorCode); + } +} + + + +// global func, so we can call from the profile lib +void SonyCommerce_UpgradeTrial() +{ + // we're now calling the app function here, which manages pending requests + app.UpgradeTrial(); +} + +void SonyCommerce_Vita::UpgradeTrial(CallbackFunc cb, LPVOID lpParam) +{ + m_trialUpgradeCallbackFunc = cb; + m_trialUpgradeCallbackParam = lpParam; + + GetDetailedProductInfo(UpgradeTrialCallback1, this, &s_trialUpgradeProductInfoDetailed, app.GetUpgradeKey(), app.GetCommerceCategory()); +} + + +int SonyCommerce_Vita::createContext() +{ + // SceNpId npId; + // int ret = sceNpManagerGetNpId(&npId); + // if(ret < 0) + // { + // app.DebugPrintf(4,"createContext sceNpManagerGetNpId problem\n"); + // return ret; + // } + // + // if (m_contextCreated) { + // ret = sceNpCommerce2DestroyCtx(m_contextId); + // if (ret < 0) + // { + // app.DebugPrintf(4,"createContext sceNpCommerce2DestroyCtx problem\n"); + // return ret; + // } + // } + // + // // Create commerce2 context + // ret = sceNpCommerce2CreateCtx(SCE_NP_COMMERCE2_VERSION, &npId, commerce2Handler, NULL, &m_contextId); + // if (ret < 0) + // { + // app.DebugPrintf(4,"createContext sceNpCommerce2CreateCtx problem\n"); + // return ret; + // } + + m_contextCreated = true; + + return SCE_OK; +} + +int SonyCommerce_Vita::createSession() +{ + // this does nothing now, we only catch session expired errors now and recreate the session when needed. + int ret = 0; + EnterCriticalSection(&m_queueLock); + m_messageQueue.push(e_message_commerceEnd); + m_event = e_event_commerceSessionCreated; + LeaveCriticalSection(&m_queueLock); + + return ret; +} + +int SonyCommerce_Vita::recreateSession() +{ + int ret = 0; + ret = sce::Toolkit::NP::Commerce::Interface::createSession(); + app.DebugPrintf(" ----||||---- sce::Toolkit::NP::Commerce::Interface::createSession \n"); + + if (ret < 0) + { + return ret; + } + m_currentPhase = e_phase_creatingSessionPhase; + return ret; +} + + + +void SonyCommerce_Vita::commerce2Handler( const sce::Toolkit::NP::Event& event) +{ + // Event reply; + // reply.service = Toolkit::NP::commerce; + // + + // make sure we're initialised + Init(); + + app.DebugPrintf("commerce2Handler returnCode = 0x%08x\n", event.returnCode); + + + EnterCriticalSection(&m_queueLock); + + if(event.returnCode == SCE_NP_COMMERCE2_SERVER_ERROR_SESSION_EXPIRED) + { + // this will happen on the first commerce call, since there is no session, so we create and then queue the request again + m_messageQueue.push(e_message_commerceRecreateSession); + LeaveCriticalSection(&m_queueLock); + return; + } + + + switch (event.event) + { + case sce::Toolkit::NP::Event::UserEvent::commerceNoEntitlements: + app.DebugPrintf("commerce2Handler : commerceNoEntitlements\n"); + StorageManager.EntitlementsCallback(false); + break; + + case sce::Toolkit::NP::Event::UserEvent::commerceGotEntitlementList: + app.DebugPrintf("commerce2Handler : commerceGotEntitlementList\n"); + StorageManager.EntitlementsCallback(true); + break; + + case sce::Toolkit::NP::Event::UserEvent::commerceError: + { + m_messageQueue.push(e_message_commerceEnd); + m_errorCode = event.returnCode; + break; + } + case sce::Toolkit::NP::Event::UserEvent::commerceSessionCreated: + { + // the seesion has been recreated after an error, so queue the old request back up now we're running again + m_messageQueue.push(m_lastMessage); + m_event = e_event_commerceSessionRecreated; + break; + } + case sce::Toolkit::NP::Event::UserEvent::commerceSessionAborted: + { + m_messageQueue.push(e_message_commerceEnd); + m_event = e_event_commerceSessionAborted; + break; + } + case sce::Toolkit::NP::Event::UserEvent::commerceCheckoutStarted: + { + m_currentPhase = e_phase_checkoutPhase; + m_event = e_event_commerceCheckoutStarted; + break; + } + case sce::Toolkit::NP::Event::UserEvent::commerceGotCategoryInfo: + { + // int ret = sce::Toolkit::NP::Commerce::Interface::getBgdlStatus(&status, false); + // if(ret == SCE_OK) + // { + copyCategoryInfo(m_pCategoryInfo, g_categoryInfo.get()); + m_pCategoryInfo = NULL; + m_event = e_event_commerceGotCategoryInfo; + // } + + break; + } + + case sce::Toolkit::NP::Event::UserEvent::commerceGotProductList: + { + copyProductList(m_pProductInfoList, g_productList.get()); + m_pProductInfoDetailed = NULL; + m_event = e_event_commerceGotProductList; + break; + } + + case sce::Toolkit::NP::Event::UserEvent::commerceGotDetailedProductInfo: + { + if(m_pProductInfoDetailed) + { + copyDetailedProductInfo(m_pProductInfoDetailed, g_detailedProductInfo.get()); + m_pProductInfoDetailed = NULL; + } + else + { + copyAddDetailedProductInfo(m_pProductInfo, g_detailedProductInfo.get()); + m_pProductInfo = NULL; + } + m_event = e_event_commerceGotDetailedProductInfo; + break; + } + + + + // case SCE_NP_COMMERCE2_EVENT_DO_CHECKOUT_SUCCESS: + // { + // m_messageQueue.push(e_message_commerceEnd); + // m_event = e_event_commerceCheckoutSuccess; + // break; + // } + // case SCE_NP_COMMERCE2_EVENT_DO_CHECKOUT_BACK: + // { + // m_messageQueue.push(e_message_commerceEnd); + // m_event = e_event_commerceCheckoutAborted; + // break; + // } + case sce::Toolkit::NP::Event::UserEvent::commerceCheckoutFinished: + { + m_messageQueue.push(e_message_commerceEnd); // MGH - fixes an assert when switching to adhoc mode after this + m_event = e_event_commerceCheckoutFinished; + break; + } + case sce::Toolkit::NP::Event::UserEvent::commerceDownloadListStarted: + { + m_currentPhase = e_phase_downloadListPhase; + m_event = e_event_commerceDownloadListStarted; + break; + } + // case SCE_NP_COMMERCE2_EVENT_DO_DL_LIST_SUCCESS: + // { + // m_messageQueue.push(e_message_commerceEnd); + // m_event = e_event_commerceDownloadListSuccess; + // break; + // } + case sce::Toolkit::NP::Event::UserEvent::commerceDownloadListFinished: + { + m_event = e_event_commerceDownloadListFinished; + break; + } + + case sce::Toolkit::NP::Event::UserEvent::commerceProductBrowseStarted: + { + m_currentPhase = e_phase_productBrowsePhase; + m_event = e_event_commerceProductBrowseStarted; + break; + } + case sce::Toolkit::NP::Event::UserEvent::commerceProductBrowseSuccess: + { + m_messageQueue.push(e_message_commerceEnd); + m_event = e_event_commerceProductBrowseSuccess; + break; + } + case sce::Toolkit::NP::Event::UserEvent::commerceProductBrowseAborted: + { + m_messageQueue.push(e_message_commerceEnd); + m_event = e_event_commerceProductBrowseAborted; + break; + } + case sce::Toolkit::NP::Event::UserEvent::commerceProductBrowseFinished: + { + m_event = e_event_commerceProductBrowseFinished; + break; + } + + case sce::Toolkit::NP::Event::UserEvent::commerceInstallStarted: + { + m_event = e_event_commerceInstallContentStarted; + break; + } + case sce::Toolkit::NP::Event::UserEvent::commerceInstallFinished: + { + m_event = e_event_commerceInstallContentFinished; + break; + } + + + // case SCE_NP_COMMERCE2_EVENT_DO_PROD_BROWSE_OPENED: + // break; + // case SCE_NP_COMMERCE2_EVENT_DO_PRODUCT_CODE_STARTED: + // { + // m_currentPhase = e_phase_voucherRedeemPhase; + // m_event = e_event_commerceVoucherInputStarted; + // break; + // } + // case SCE_NP_COMMERCE2_EVENT_DO_PRODUCT_CODE_SUCCESS: + // { + // m_messageQueue.push(e_message_commerceEnd); + // m_event = e_event_commerceVoucherInputSuccess; + // break; + // } + // case SCE_NP_COMMERCE2_EVENT_DO_PRODUCT_CODE_BACK: + // { + // m_messageQueue.push(e_message_commerceEnd); + // m_event = e_event_commerceVoucherInputAborted; + // break; + // } + // case SCE_NP_COMMERCE2_EVENT_DO_PRODUCT_CODE_FINISHED: + // { + // m_event = e_event_commerceVoucherInputFinished; + // break; + // } + default: + break; + }; + + LeaveCriticalSection(&m_queueLock); +} + + + +void SonyCommerce_Vita::processMessage() +{ + EnterCriticalSection(&m_queueLock); + int ret; + if(m_messageQueue.empty()) + { + LeaveCriticalSection(&m_queueLock); + return; + } + Message msg = m_messageQueue.front(); + if(msg != e_message_commerceRecreateSession) + m_lastMessage = msg; + m_messageQueue.pop(); + + switch (msg) + { + + case e_message_commerceCreateSession: + ret = createSession(); + if (ret < 0) + { + m_event = e_event_commerceError; + m_errorCode = ret; + } + break; + + case e_message_commerceRecreateSession: + ret = recreateSession(); + if (ret < 0) + { + m_event = e_event_commerceError; + m_errorCode = ret; + } + break; + + case e_message_commerceGetCategoryInfo: + { + ret = getCategoryInfo(m_pCategoryInfo, m_pCategoryID); + if (ret < 0) + { + m_event = e_event_commerceError; + app.DebugPrintf(4,"ERROR - e_event_commerceGotCategoryInfo - %s\n",m_pCategoryID); + m_errorCode = ret; + } + break; + } + + case e_message_commerceGetProductList: + { + ret = getProductList(m_pProductInfoList, m_pCategoryID); + if (ret < 0) + { + m_event = e_event_commerceError; + } + break; + } + + case e_message_commerceGetDetailedProductInfo: + { + ret = getDetailedProductInfo(m_pProductInfoDetailed, m_pProductID, m_pCategoryID); + if (ret < 0) + { + m_event = e_event_commerceError; + m_errorCode = ret; + } + break; + } + case e_message_commerceAddDetailedProductInfo: + { + ret = addDetailedProductInfo(m_pProductInfo, m_pProductID, m_pCategoryID); + if (ret < 0) + { + m_event = e_event_commerceError; + m_errorCode = ret; + } + break; + } + + // + // case e_message_commerceStoreProductBrowse: + // { + // ret = productBrowse(*(ProductBrowseParams *)msg.inputArgs); + // if (ret < 0) { + // m_event = e_event_commerceError; + // m_errorCode = ret; + // } + // _TOOLKIT_NP_DEL (ProductBrowseParams *)msg.inputArgs; + // break; + // } + // + // case e_message_commerceUpgradeTrial: + // { + // ret = upgradeTrial(); + // if (ret < 0) { + // m_event = e_event_commerceError; + // m_errorCode = ret; + // } + // break; + // } + // + // case e_message_commerceRedeemVoucher: + // { + // ret = voucherCodeInput(*(VoucherInputParams *)msg.inputArgs); + // if (ret < 0) { + // m_event = e_event_commerceError; + // m_errorCode = ret; + // } + // _TOOLKIT_NP_DEL (VoucherInputParams *)msg.inputArgs; + // break; + // } + // + // case e_message_commerceGetEntitlementList: + // { + // Job > tmpJob(static_cast > *>(msg.output)); + // + // int state = 0; + // int ret = sceNpManagerGetStatus(&state); + // + // // We don't want to process this if we are offline + // if (ret < 0 || state != SCE_NP_MANAGER_STATUS_ONLINE) { + // m_event = e_event_commerceError; + // reply.returnCode = SCE_TOOLKIT_NP_OFFLINE; + // tmpJob.setError(SCE_TOOLKIT_NP_OFFLINE); + // } else { + // getEntitlementList(&tmpJob); + // } + // break; + // } + // + // case e_message_commerceConsumeEntitlement: + // { + // int state = 0; + // int ret = sceNpManagerGetStatus(&state); + // + // // We don't want to process this if we are offline + // if (ret < 0 || state != SCE_NP_MANAGER_STATUS_ONLINE) { + // m_event = e_event_commerceError; + // reply.returnCode = SCE_TOOLKIT_NP_OFFLINE; + // } else { + // + // ret = consumeEntitlement(*(EntitlementToConsume *)msg.inputArgs); + // if (ret < 0) { + // m_event = e_event_commerceError; + // m_errorCode = ret; + // } else { + // m_event = e_event_commerceConsumedEntitlement; + // } + // } + // _TOOLKIT_NP_DEL (EntitlementToConsume *)msg.inputArgs; + // + // break; + // } + // + case e_message_commerceCheckout: + { + ret = checkout(m_checkoutInputParams); + if (ret < 0) { + m_event = e_event_commerceError; + m_errorCode = ret; + } + break; + } + + case e_message_commerceDownloadList: + { + ret = downloadList(m_downloadInputParams); + if (ret < 0) { + m_event = e_event_commerceError; + m_errorCode = ret; + } + break; + } + + case e_message_commerceCheckout_Game: + { + ret = checkout_game(m_checkoutInputParams); + if (ret < 0) { + m_event = e_event_commerceError; + m_errorCode = ret; + } + break; + } + + case e_message_commerceDownloadList_Game: + { + ret = downloadList_game(m_downloadInputParams); + if (ret < 0) { + m_event = e_event_commerceError; + m_errorCode = ret; + } + break; + } + + case e_message_commerceInstallContent: + { + ret = installContent(); + if (ret < 0) { + m_event = e_event_commerceError; + m_errorCode = ret; + } + break; + } + + + case e_message_commerceEnd: + app.DebugPrintf("XXX - e_message_commerceEnd!\n"); + ret = commerceEnd(); + if (ret < 0) + { + m_event = e_event_commerceError; + m_errorCode = ret; + } + // 4J-PB - we don't seem to handle the error code here + else if(m_errorCode!=0) + { + m_event = e_event_commerceError; + } + break; + + default: + break; + } + + LeaveCriticalSection(&m_queueLock); +} + + +void SonyCommerce_Vita::processEvent() +{ + int ret = 0; + + switch (m_event) + { + case e_event_none: + break; + case e_event_commerceSessionRecreated: + app.DebugPrintf(4,"Commerce Session Created.\n"); + break; + case e_event_commerceSessionCreated: + app.DebugPrintf(4,"Commerce Session Created.\n"); + runCallback(); + break; + case e_event_commerceSessionAborted: + app.DebugPrintf(4,"Commerce Session aborted.\n"); + runCallback(); + break; + case e_event_commerceGotProductList: + app.DebugPrintf(4,"Got product list.\n"); + runCallback(); + break; + case e_event_commerceGotCategoryInfo: + app.DebugPrintf(4,"Got category info\n"); + runCallback(); + break; + case e_event_commerceGotDetailedProductInfo: + app.DebugPrintf(4,"Got detailed product info.\n"); + runCallback(); + break; + case e_event_commerceAddedDetailedProductInfo: + app.DebugPrintf(4,"Added detailed product info.\n"); + runCallback(); + break; + case e_event_commerceProductBrowseStarted: + break; + case e_event_commerceProductBrowseSuccess: + break; + case e_event_commerceProductBrowseAborted: + break; + case e_event_commerceProductBrowseFinished: + app.DebugPrintf(4,"e_event_commerceProductBrowseFinished succeeded: 0x%x\n", m_errorCode); + if(m_callbackFunc!=NULL) + { + runCallback(); + } + m_bDownloadsPending = true; + +// assert(0); + // ret = sys_memory_container_destroy(s_memContainer); + // if (ret < 0) { + // printf("Failed to destroy memory container"); + // } + // s_memContainer = SYS_MEMORY_CONTAINER_ID_INVALID; + break; + case e_event_commerceVoucherInputStarted: + break; + case e_event_commerceVoucherInputSuccess: + break; + case e_event_commerceVoucherInputAborted: + break; + case e_event_commerceVoucherInputFinished: + assert(0); + // ret = sys_memory_container_destroy(s_memContainer); + // if (ret < 0) { + // printf("Failed to destroy memory container"); + // } + // s_memContainer = SYS_MEMORY_CONTAINER_ID_INVALID; + break; + case e_event_commerceGotEntitlementList: + break; + case e_event_commerceConsumedEntitlement: + break; + case e_event_commerceCheckoutStarted: + app.DebugPrintf(4,"Checkout Started\n"); + ProfileManager.SetSysUIShowing(true); + break; + case e_event_commerceCheckoutSuccess: + app.DebugPrintf(4,"Checkout succeeded: 0x%x\n", m_errorCode); + // clear the DLC installed and check again + ProfileManager.SetSysUIShowing(false); + break; + case e_event_commerceCheckoutAborted: + app.DebugPrintf(4,"Checkout aborted: 0x%x\n", m_errorCode); + ProfileManager.SetSysUIShowing(false); + break; + case e_event_commerceCheckoutFinished: + app.DebugPrintf(4,"Checkout Finished: 0x%x\n", m_errorCode); + if (ret < 0) { + app.DebugPrintf(4,"Failed to destroy memory container"); + } + ProfileManager.SetSysUIShowing(false); + + // 4J-PB - if there's been an error - like dlc already purchased, the runcallback has already happened, and will crash this time + if(m_callbackFunc!=NULL) + { + // get the detailed product info again, to see if the purchase has happened or not + EnterCriticalSection(&m_queueLock); + m_messageQueue.push(e_message_commerceAddDetailedProductInfo); + LeaveCriticalSection(&m_queueLock); + +// runCallback(); + } + m_bDownloadsPending = true; + break; + case e_event_commerceDownloadListStarted: + app.DebugPrintf(4,"Download List Started\n"); + ProfileManager.SetSysUIShowing(true); + break; + case e_event_commerceDownloadListSuccess: + app.DebugPrintf(4,"Download succeeded: 0x%x\n", m_errorCode); + ProfileManager.SetSysUIShowing(false); + m_bDownloadsPending = true; + break; + case e_event_commerceDownloadListFinished: + app.DebugPrintf(4,"Download Finished: 0x%x\n", m_errorCode); + if (ret < 0) { + app.DebugPrintf(4,"Failed to destroy memory container"); + } + ProfileManager.SetSysUIShowing(false); + + // 4J-PB - if there's been an error - like dlc already purchased, the runcallback has already happened, and will crash this time + if(m_callbackFunc!=NULL) + { + runCallback(); + } + m_bDownloadsPending = true; + break; + + case e_event_commerceInstallContentStarted: + app.DebugPrintf(4,"Install content Started\n"); + ProfileManager.SetSysUIShowing(true); + break; + case e_event_commerceInstallContentFinished: + app.DebugPrintf(4,"Install content finished: 0x%x\n", m_errorCode); + ProfileManager.SetSysUIShowing(false); + runCallback(); + break; + + case e_event_commerceError: + app.DebugPrintf(4,"Commerce Error 0x%x\n", m_errorCode); + runCallback(); + break; + default: + break; + } + m_event = e_event_none; +} + + +int SonyCommerce_Vita::commerceEnd() +{ + int ret = 0; + + // if (m_currentPhase == e_phase_voucherRedeemPhase) + // ret = sceNpCommerce2DoProductCodeFinishAsync(m_contextId); + // else if (m_currentPhase == e_phase_productBrowsePhase) + // ret = sceNpCommerce2DoProductBrowseFinishAsync(m_contextId); + // else if (m_currentPhase == e_phase_creatingSessionPhase) + // ret = sceNpCommerce2CreateSessionFinish(m_contextId, &m_sessionInfo); + // else if (m_currentPhase == e_phase_checkoutPhase) + // ret = sceNpCommerce2DoCheckoutFinishAsync(m_contextId); + // else if (m_currentPhase == e_phase_downloadListPhase) + // ret = sceNpCommerce2DoDlListFinishAsync(m_contextId); + + m_currentPhase = e_phase_idle; + + return ret; +} + +void SonyCommerce_Vita::CreateSession( CallbackFunc cb, LPVOID lpParam ) +{ + // 4J-PB - reset any previous error code + // I had this happen when I was offline on Vita, and accepted the PSN sign-in + // the m_errorCode was picked up in the message queue after the commerce init call + if(m_errorCode!=0) + { + app.DebugPrintf("m_errorCode was set!\n"); + m_errorCode=0; + } + Init(); + EnterCriticalSection(&m_queueLock); + setCallback(cb,lpParam); + m_messageQueue.push(e_message_commerceCreateSession); +// m_messageQueue.push(e_message_commerceEnd); +// m_event = e_event_commerceSessionCreated; + + if(m_tickThread && (m_tickThread->isRunning() == false)) + { + delete m_tickThread; + m_tickThread = NULL; + } + if(m_tickThread == NULL) + m_tickThread = new C4JThread(TickLoop, NULL, "SonyCommerce_Vita tick"); + if(m_tickThread->isRunning() == false) + { + m_currentPhase = e_phase_idle; + m_tickThread->Run(); + } + LeaveCriticalSection(&m_queueLock); +} + +void SonyCommerce_Vita::CloseSession() +{ +// assert(m_currentPhase == e_phase_idle); + m_currentPhase = e_phase_stopped; + Shutdown(); +} + +void SonyCommerce_Vita::GetProductList( CallbackFunc cb, LPVOID lpParam, std::vector* productList, const char *categoryId) +{ + EnterCriticalSection(&m_queueLock); + setCallback(cb,lpParam); + m_pProductInfoList = productList; + strcpy(m_pCategoryID,categoryId); + m_messageQueue.push(e_message_commerceGetProductList); + LeaveCriticalSection(&m_queueLock); +} + +void SonyCommerce_Vita::GetDetailedProductInfo( CallbackFunc cb, LPVOID lpParam, ProductInfoDetailed* productInfo, const char *productId, const char *categoryId ) +{ + EnterCriticalSection(&m_queueLock); + setCallback(cb,lpParam); + m_pProductInfoDetailed = productInfo; + m_pProductID = productId; + strcpy(m_pCategoryID,categoryId); + m_messageQueue.push(e_message_commerceGetDetailedProductInfo); + LeaveCriticalSection(&m_queueLock); +} + +// 4J-PB - fill out the long description and the price for the product +void SonyCommerce_Vita::AddDetailedProductInfo( CallbackFunc cb, LPVOID lpParam, ProductInfo* productInfo, const char *productId, const char *categoryId ) +{ + EnterCriticalSection(&m_queueLock); + setCallback(cb,lpParam); + m_pProductInfo = productInfo; + m_pProductID = productId; + strcpy(m_pCategoryID,categoryId); + m_messageQueue.push(e_message_commerceAddDetailedProductInfo); + LeaveCriticalSection(&m_queueLock); +} +void SonyCommerce_Vita::GetCategoryInfo( CallbackFunc cb, LPVOID lpParam, CategoryInfo *info, const char *categoryId ) +{ + EnterCriticalSection(&m_queueLock); + setCallback(cb,lpParam); + m_pCategoryInfo = info; + strcpy(m_pCategoryID,categoryId); + m_messageQueue.push(e_message_commerceGetCategoryInfo); + LeaveCriticalSection(&m_queueLock); +} + +void SonyCommerce_Vita::Checkout( CallbackFunc cb, LPVOID lpParam, ProductInfo* productInfo ) +{ + EnterCriticalSection(&m_queueLock); + setCallback(cb,lpParam); + m_checkoutInputParams.skuIds.clear(); + m_checkoutInputParams.skuIds.push_back(productInfo->skuId); + + m_pProductInfo = productInfo; + m_pProductID = productInfo->productId; + + m_messageQueue.push(e_message_commerceCheckout); + LeaveCriticalSection(&m_queueLock); +} + +void SonyCommerce_Vita::Checkout( CallbackFunc cb, LPVOID lpParam, const char* skuID ) +{ + assert(0); +} + +void SonyCommerce_Vita::DownloadAlreadyPurchased( CallbackFunc cb, LPVOID lpParam, const char* skuID ) +{ + EnterCriticalSection(&m_queueLock); + setCallback(cb,lpParam); + m_downloadInputParams.skuIds.clear(); + m_downloadInputParams.skuIds.push_back(skuID); + m_messageQueue.push(e_message_commerceDownloadList); + LeaveCriticalSection(&m_queueLock); +} + +void SonyCommerce_Vita::Checkout_Game( CallbackFunc cb, LPVOID lpParam, const char* skuID ) +{ + EnterCriticalSection(&m_queueLock); + setCallback(cb,lpParam); + m_checkoutInputParams.skuIds.clear(); + m_checkoutInputParams.skuIds.push_back(skuID); + m_messageQueue.push(e_message_commerceCheckout_Game); + LeaveCriticalSection(&m_queueLock); +} +void SonyCommerce_Vita::DownloadAlreadyPurchased_Game( CallbackFunc cb, LPVOID lpParam, const char* skuID ) +{ + EnterCriticalSection(&m_queueLock); + setCallback(cb,lpParam); + m_downloadInputParams.skuIds.clear(); + m_downloadInputParams.skuIds.push_back(skuID); + m_messageQueue.push(e_message_commerceDownloadList_Game); + LeaveCriticalSection(&m_queueLock); +} + +void SonyCommerce_Vita::InstallContent( CallbackFunc cb, LPVOID lpParam ) +{ + if(m_callbackFunc == NULL && m_messageQueue.size() == 0) // wait till other processes have finished + { + EnterCriticalSection(&m_queueLock); + m_bInstallingContent = true; + setCallback(cb,lpParam); + m_messageQueue.push(e_message_commerceInstallContent); + LeaveCriticalSection(&m_queueLock); + } +} + +bool SonyCommerce_Vita::getPurchasabilityUpdated() +{ + bool retVal = m_bPurchasabilityUpdated; + m_bPurchasabilityUpdated = false; + return retVal; +} + +bool SonyCommerce_Vita::getDLCUpgradePending() +{ + if(m_bDownloadsPending || m_bInstallingContent || (m_iClearDLCCountdown > 0)) + return true; + return false; +} + +/* +bool g_bDoCommerceCreateSession = false; +bool g_bDoCommerceGetProductList = false; +bool g_bDoCommerceGetCategoryInfo = false; +bool g_bDoCommerceGetProductInfoDetailed = false; +bool g_bDoCommerceCheckout = false; +bool g_bDoCommerceCloseSession = false; +const char* g_category = "EP4433-CUSA00265_00"; +const char* g_skuID = "SKINPACK00000001-E001"; +std::vector g_productInfo; +SonyCommerce::CategoryInfo g_categoryInfo2; +SonyCommerce::ProductInfoDetailed g_productInfoDetailed; + +void testCallback(LPVOID lpParam, int error_code) +{ + app.DebugPrintf("Callback hit, error 0x%08x\n", error_code); +} + +void SonyCommerce_Vita::Test() +{ + int err = SCE_OK; + if(g_bDoCommerceCreateSession) + { + CreateSession(testCallback, this); + g_bDoCommerceCreateSession = false; + } + if(g_bDoCommerceGetProductList) + { + GetProductList(testCallback, this, &g_productInfo, g_category); + g_bDoCommerceGetProductList = false; + } + + if(g_bDoCommerceGetCategoryInfo) + { + GetCategoryInfo(testCallback, this, &g_categoryInfo2, g_category); + g_bDoCommerceGetCategoryInfo = false; + } + + if(g_bDoCommerceGetProductInfoDetailed) + { + GetDetailedProductInfo(testCallback, this, &g_productInfoDetailed, g_productInfo[0].productId, g_category); + g_bDoCommerceGetProductInfoDetailed = false; + } + + if(g_bDoCommerceCheckout) + { + //Checkout(testCallback, this, g_skuID);//g_productInfoDetailed.skuId); + Checkout(testCallback, this, g_productInfoDetailed.skuId); + g_bDoCommerceCheckout = false; + } + if(g_bDoCommerceCloseSession) + { + CloseSession(); + g_bDoCommerceCloseSession = false; + } + +} +*/ \ No newline at end of file diff --git a/Minecraft.Client/PSVita/Network/SonyCommerce_Vita.h b/Minecraft.Client/PSVita/Network/SonyCommerce_Vita.h new file mode 100644 index 00000000..ad114796 --- /dev/null +++ b/Minecraft.Client/PSVita/Network/SonyCommerce_Vita.h @@ -0,0 +1,204 @@ +#pragma once + +#include "Common\Network\Sony\SonyCommerce.h" +#include +#include +#include + +class SonyCommerce_Vita : public SonyCommerce +{ + friend class PSVitaNPToolkit; + enum State + { + e_state_noSession, + e_state_creatingSession, + e_state_createSessionDone, + e_state_idle, + + + }; + /// This enum is used to verify the current utility that is running + enum Phase + { + e_phase_stopped = 0, + e_phase_idle, + e_phase_voucherRedeemPhase, + e_phase_productBrowsePhase, + e_phase_creatingSessionPhase, + e_phase_checkoutPhase, + e_phase_downloadListPhase + }; + + enum Message + { + e_message_commerceNone, + e_message_commerceCreateSession, ///< Create a commerce session + e_message_commerceRecreateSession, ///< Recreate a commerce session + e_message_commerceGetCategoryInfo, ///< Information about a category in the Store + e_message_commerceGetProductList, ///< Get a list of products available in the Store + e_message_commerceGetDetailedProductInfo, ///< Get a list of products available in the Store, with additional details + e_message_commerceAddDetailedProductInfo, ///< Add additional details to a ProdcutInfo already retrieved + e_message_commerceStoreProductBrowse, ///< Launches the Store to a specified product + e_message_commerceUpgradeTrial, ///< Upgrade a trial to full game + e_message_commerceRedeemVoucher, ///< Redeem a voucher code + e_message_commerceGetEntitlementList, ///< Get a list of entitlements associated with the current PSN user. + e_message_commerceConsumeEntitlement, ///< Consume an amount from a consumable entitlement. + e_message_commerceCheckout, ///< Launch the Store checkout + e_message_commerceDownloadList, ///< Launch the download list + e_message_commerceCheckout_Game, ///< Launch the Store checkout + e_message_commerceDownloadList_Game, ///< Launch the download list + e_message_commerceInstallContent, ///< Install content that's downloaded from the background download manager + e_message_commerceEnd ///< End commerce2 processing + }; + + enum Event + { + e_event_none, + e_event_commerceSessionCreated, ///< An event generated when a commerce session has successfully been created. + e_event_commerceSessionRecreated, ///< An event generated when a commerce session has successfully been recreated. + e_event_commerceSessionAborted, ///< An event generated when the creation of commerce session has been aborted. + e_event_commerceGotCategoryInfo, ///< An event generated when some category information has been retrieved from the store. + e_event_commerceGotProductList, ///< An event generated when a list of products that are available has been retrieved from the store. + e_event_commerceGotDetailedProductInfo, ///< An event generated when some detailed product information has been retrieved from the store. + e_event_commerceAddedDetailedProductInfo, ///< An event generated when some detailed product information has been retrieved from the store. + e_event_commerceProductBrowseStarted, ///< An event generated when product overlay has started. + e_event_commerceProductBrowseSuccess, ///< An event generated when a product browse was completed successfully, and the user purchased the product. + e_event_commerceProductBrowseAborted, ///< An event generated when a product browse was aborted by the user (the user pressed back). + e_event_commerceProductBrowseFinished, ///< An event generated when a product browse has finished and it is now safe to free memory. + e_event_commerceVoucherInputStarted, ///< An event generated when a voucher code input overlay was started. + e_event_commerceVoucherInputSuccess, ///< An event generated when a voucher code input completed successfully. + e_event_commerceVoucherInputAborted, ///< An event generated when a voucher code input was aborted by the user (user pressed back). + e_event_commerceVoucherInputFinished, ///< An event generated when a voucher code input has finished. It is now safe to free memory. + e_event_commerceGotEntitlementList, ///< An event generated when a the list of entitlements has been received for the current user. + e_event_commerceConsumedEntitlement, ///< An event generated when the has successfully consumed an entitlement. + e_event_commerceCheckoutStarted, ///< An event generated when a store checkout overlay has started. + e_event_commerceCheckoutSuccess, ///< An event generated when user has successfully purchased from the checkout. + e_event_commerceCheckoutAborted, ///< An event generated when the checkout was aborted by the user (user pressed back). + e_event_commerceCheckoutFinished, ///< An event generated when a store checkout overlay has finished. + e_event_commerceDownloadListStarted, ///< An event generated when a download list overlay has started. + e_event_commerceDownloadListSuccess, ///< An event generated when the user has ended the download list. + e_event_commerceDownloadListFinished, ///< An event generated when a download list overlay has finished. + e_event_commerceInstallContentStarted, + e_event_commerceInstallContentFinished, + e_event_commerceError ///< An event generated when a commerce error has occurred. + }; + + static bool m_bLicenseChecked; + static bool m_bCommerceInitialised; +// static SceNpCommerce2SessionInfo m_sessionInfo; + static State m_state; + static int m_errorCode; + static LPVOID m_callbackParam; + static Event m_event; + static Message m_message; + // static uint32_t m_requestID; + static void* m_receiveBuffer; + static std::vector *m_pProductInfoList; + static ProductInfoDetailed *m_pProductInfoDetailed; + static ProductInfo *m_pProductInfo; + static CategoryInfo* m_pCategoryInfo; + static char* m_pCategoryID; + static const char* m_pProductID; + static std::queue m_messageQueue; + static CallbackFunc m_callbackFunc; + static CheckoutInputParams m_checkoutInputParams; + static DownloadListInputParams m_downloadInputParams; +// static sys_memory_container_t m_memContainer; + static bool m_bUpgradingTrial; + static C4JThread* m_tickThread; + static CallbackFunc m_trialUpgradeCallbackFunc; + static LPVOID m_trialUpgradeCallbackParam; + static CRITICAL_SECTION m_queueLock; + static bool m_bLicenseInstalled; + static bool m_bDownloadsPending; + static bool m_bDownloadsReady; + static bool m_bInstallingContent; + static int m_iClearDLCCountdown; + static bool m_bPurchasabilityUpdated; + + static Message m_lastMessage; + static void runCallback() + { + assert(m_callbackFunc); + CallbackFunc func = m_callbackFunc; + m_callbackFunc = NULL; + if(func) + func(m_callbackParam, m_errorCode); + m_errorCode = SCE_OK; + } + static void setCallback(CallbackFunc cb,LPVOID lpParam) + { + assert(m_callbackFunc == NULL); + m_callbackFunc = cb; + m_callbackParam = lpParam; + } + + + static uint32_t m_contextId; ///< The npcommerce2 context ID + static bool m_contextCreated; ///< npcommerce2 context ID created? + static Phase m_currentPhase; ///< Current commerce2 util +// static char m_commercebuffer[SCE_NP_COMMERCE2_RECV_BUF_SIZE]; + + + + static void commerce2Handler( const sce::Toolkit::NP::Event& event); + static void processMessage(); + static void processEvent(); + + static int createContext(); + static int createSession(); + static int recreateSession(); + static void setError(int err) { m_errorCode = err; } + static int getCategoryInfo(CategoryInfo *info, char *categoryId); + static int getProductList(std::vector* productList, char *categoryId); + static int getDetailedProductInfo(ProductInfoDetailed *info, const char *productId, char *categoryId); + static int addDetailedProductInfo(ProductInfo *info, const char *productId, char *categoryId); + static int checkout(CheckoutInputParams ¶ms); + static int downloadList(DownloadListInputParams ¶ms); + static int checkout_game(CheckoutInputParams ¶ms); + static int downloadList_game(DownloadListInputParams ¶ms); + static int installContent(); + static void UpgradeTrialCallback1(LPVOID lpParam,int err); + static void UpgradeTrialCallback2(LPVOID lpParam,int err); + static void Delete(); + static void copyCategoryInfo(CategoryInfo *pInfo, sce::Toolkit::NP::CategoryInfo *pNPInfo); + static void copyProductList(std::vector* pProductList, std::vector* pNPProductList); + static void copyDetailedProductInfo(ProductInfoDetailed *pInfo, sce::Toolkit::NP::ProductInfoDetailed* pNPInfo); + static void copyAddDetailedProductInfo(ProductInfo *pInfo, sce::Toolkit::NP::ProductInfoDetailed* pNPInfo); + static void InstallContentCallback(LPVOID lpParam,int err); + + static int commerceEnd(); + // static int upgradeTrial(); + + static int TickLoop(void* lpParam); + //void Test(); + + static void Init(); + static int Shutdown(); + + static void CheckForTrialUpgradeKey_Callback(LPVOID param, bool bFullVersion); + +public: + static void checkBackgroundDownloadStatus(); + + virtual void CreateSession(CallbackFunc cb, LPVOID lpParam); + virtual void CloseSession(); + + virtual void GetCategoryInfo(CallbackFunc cb, LPVOID lpParam, CategoryInfo *info, const char *categoryId); + virtual void GetProductList(CallbackFunc cb, LPVOID lpParam, std::vector* productList, const char *categoryId); + virtual void GetDetailedProductInfo(CallbackFunc cb, LPVOID lpParam, ProductInfoDetailed* productInfoDetailed, const char *productId, const char *categoryId); + virtual void AddDetailedProductInfo( CallbackFunc cb, LPVOID lpParam, ProductInfo* productInfo, const char *productId, const char *categoryId ); + virtual void Checkout(CallbackFunc cb, LPVOID lpParam, const char* skuID); + virtual void Checkout(CallbackFunc cb, LPVOID lpParam, ProductInfo* productInfo); + virtual void DownloadAlreadyPurchased(CallbackFunc cb, LPVOID lpParam, const char* skuID); + virtual void Checkout_Game(CallbackFunc cb, LPVOID lpParam, const char* skuID); + virtual void DownloadAlreadyPurchased_Game(CallbackFunc cb, LPVOID lpParam, const char* skuID); + static void InstallContent(CallbackFunc cb, LPVOID lpParam); + virtual void UpgradeTrial(CallbackFunc cb, LPVOID lpParam); + virtual void CheckForTrialUpgradeKey(); + virtual bool LicenseChecked(); + + static bool getPurchasabilityUpdated(); + static bool getDLCUpgradePending(); + +}; diff --git a/Minecraft.Client/PSVita/Network/SonyHttp_Vita.cpp b/Minecraft.Client/PSVita/Network/SonyHttp_Vita.cpp new file mode 100644 index 00000000..9110edf5 --- /dev/null +++ b/Minecraft.Client/PSVita/Network/SonyHttp_Vita.cpp @@ -0,0 +1,271 @@ +#include "stdafx.h" +#include "SonyHttp_Vita.h" + +static const int sc_SSLHeapSize = (304 * 1024U); +static const int sc_HTTPHeapSize = (80 * 1024); +static const int sc_NetHeapSize = (16 * 1024); + +#define TEST_USER_AGENT "SimpleSample/1.00" + + +// int SonyHttp_Vita::libnetMemId = 0; +int SonyHttp_Vita::libsslCtxId = 0; +int SonyHttp_Vita::libhttpCtxId = 0; +bool SonyHttp_Vita:: bInitialised = false; + + + + + +bool SonyHttp_Vita::init() +{ +// int ret = sceNetPoolCreate("simple", sc_NetHeapSize, 0); +// assert(ret >= 0); +// libnetMemId = ret; + +// int ret = sceSslInit(sc_SSLHeapSize); +// assert(ret >= 0 || ret == SCE_SSL_ERROR_ALREADY_INITED); +// libsslCtxId = ret; +// +// ret = sceHttpInit(sc_HTTPHeapSize); +// assert(ret >= 0 || ret == SCE_HTTP_ERROR_ALREADY_INITED); +// libhttpCtxId = ret; + + bInitialised = true; + return true; +} + +void SonyHttp_Vita::shutdown() +{ + PSVITA_STUBBED; +// int ret = sceHttpTerm(libhttpCtxId); +// assert(ret == SCE_OK); +// +// ret = sceSslTerm(libsslCtxId); +// assert(ret == SCE_OK); +// +// /* libnet */ +// ret = sceNetPoolDestroy(libnetMemId); +// assert(ret == SCE_OK); + +} +void SonyHttp_Vita::printSslError(SceInt32 sslErr, SceUInt32 sslErrDetail) +{ + switch (sslErr) + { + case (SCE_HTTPS_ERROR_CERT): /* Verify error */ + /* Internal error at verifying certificate*/ + if (sslErrDetail & SCE_HTTPS_ERROR_SSL_INTERNAL){ + app.DebugPrintf("ssl verify error: unexpcted error\n"); + } + /* Error of server certificate or CA certificate */ + if (sslErrDetail & SCE_HTTPS_ERROR_SSL_INVALID_CERT){ + app.DebugPrintf("ssl verify error: invalid server cert or CA cert\n"); + } + /* Server hostname and server certificate are mismatched*/ + if (sslErrDetail & SCE_HTTPS_ERROR_SSL_CN_CHECK){ + app.DebugPrintf("ssl verify error: invalid server hostname\n"); + } + /* Server certificate or CA certificate is expired.*/ + if (sslErrDetail & SCE_HTTPS_ERROR_SSL_NOT_AFTER_CHECK){ + app.DebugPrintf("ssl verify error: server cert or CA cert had expired\n"); + } + /* Server certificate or CA certificate is before validated.*/ + if (sslErrDetail & SCE_HTTPS_ERROR_SSL_NOT_BEFORE_CHECK){ + app.DebugPrintf("ssl verify error: server cert or CA cert isn't validated yet.\n"); + } + /* Unknown CA error */ + if (sslErrDetail & SCE_HTTPS_ERROR_SSL_UNKNOWN_CA){ + app.DebugPrintf("ssl verify error: unknown CA\n"); + } + break; + case (SCE_HTTPS_ERROR_HANDSHAKE): /* fail to ssl-handshake */ + app.DebugPrintf("ssl error: handshake error\n"); + break; + case (SCE_HTTPS_ERROR_IO): /* Error of Socket IO */ + app.DebugPrintf("ssl error: io error\n"); + break; + case (SCE_HTTP_ERROR_OUT_OF_MEMORY): /* Out of memory*/ + app.DebugPrintf("ssl error: out of memory\n"); + break; + case (SCE_HTTPS_ERROR_INTERNAL): /* Unexpected Internal Error*/ + app.DebugPrintf("ssl error: unexpcted error\n"); + break; + default: + break; + } + return; +} + + +void SonyHttp_Vita::printSslCertInfo(SceSslCert *sslCert) +{ + SceInt32 ret; + const SceUChar8 *sboData; + SceSize sboLen, counter; + + ret = sceSslGetSerialNumber(sslCert, &sboData, &sboLen); + if (ret < 0){ + app.DebugPrintf ("sceSslGetSerialNumber() returns 0x%x\n", ret); + } else { + app.DebugPrintf("Serial number="); + for (counter = 0; counter < sboLen; counter++){ + app.DebugPrintf("%02X", sboData[counter]); + } + app.DebugPrintf("\n"); + } +} + + +bool SonyHttp_Vita::getDataFromURL( const char* szURL, void** ppOutData, int* pDataSize) +{ + if(!bInitialised) + return false; + return http_get(szURL, ppOutData, pDataSize); +} + + +int SonyHttp_Vita::sslCallback(SceUInt32 verifyErr, SceSslCert * const sslCert[], SceInt32 certNum, void *userArg) +{ + SceInt32 i; + (void)userArg; + + app.DebugPrintf("Ssl callback:\n"); + app.DebugPrintf("\tbase tmpl[%x]\n", (SceInt32)userArg); + + if (verifyErr != 0){ + printSslError((SceInt32)SCE_HTTPS_ERROR_CERT, verifyErr); + } + for (i = 0; i < certNum; i++){ + printSslCertInfo(sslCert[i]); + } + if (verifyErr == 0){ + return SCE_OK; + } else { + return -1; + } +} + +bool SonyHttp_Vita::http_get_close(bool bOK, SceInt32 tmplId, SceInt32 connId, SceInt32 reqId) +{ + SceInt32 ret; + if (reqId > 0) + { + ret = sceHttpDeleteRequest(reqId); + assert(ret >= 0); + } + if (connId > 0) + { + ret = sceHttpDeleteConnection(connId); + assert(ret >= 0); + } + if (tmplId > 0) + { + ret = sceHttpDeleteTemplate(tmplId); + assert(ret >= 0); + } + assert(bOK); + return bOK; +} + +bool SonyHttp_Vita::http_get(const char *targetUrl, void** ppOutData, int* pDataSize) +{ + SceInt32 ret, tmplId=0, connId=0, reqId=0, statusCode; + SceULong64 contentLength=0; + SceBool finFlag=SCE_FALSE; + SceUChar8* recvBuf; + + ret = sceHttpCreateTemplate(TEST_USER_AGENT, SCE_HTTP_VERSION_1_1, SCE_TRUE); + if (ret < 0) + { + app.DebugPrintf("sceHttpCreateTemplate() error: 0x%08X\n", ret); + return http_get_close(false, tmplId, connId, reqId); + } + tmplId = ret; + + /* Perform http_get without server verification */ + ret = sceHttpsDisableOption(SCE_HTTPS_FLAG_SERVER_VERIFY); + if (ret < 0) + { + app.DebugPrintf("sceHttpsDisableOption() error: 0x%08X\n", ret); + return http_get_close(false, tmplId, connId, reqId); + } + + /* Register SSL callback */ + ret = sceHttpsSetSslCallback(tmplId, sslCallback, (void*)&tmplId); + if (ret < 0) + { + app.DebugPrintf("sceHttpsSetSslCallback() error: 0x%08X\n", ret); + return http_get_close(false, tmplId, connId, reqId); + } + + ret = sceHttpCreateConnectionWithURL(tmplId, targetUrl, SCE_TRUE); + if (ret < 0) + { + app.DebugPrintf("sceHttpCreateConnectionWithURL() error: 0x%08X\n", ret); + return http_get_close(false, tmplId, connId, reqId); + } + connId = ret; + + ret = sceHttpCreateRequestWithURL(connId, SCE_HTTP_METHOD_GET, targetUrl, 0); + if (ret < 0) + { + app.DebugPrintf("sceHttpCreateRequestWithURL() error: 0x%08X\n", ret); + return http_get_close(false, tmplId, connId, reqId); + } + reqId = ret; + + ret = sceHttpSendRequest(reqId, NULL, 0); + if (ret < 0) + { + app.DebugPrintf("sceHttpSendRequest() error: 0x%08X\n", ret); + return http_get_close(false, tmplId, connId, reqId); + } + + ret = sceHttpGetStatusCode(reqId, &statusCode); + if (ret < 0) + { + app.DebugPrintf("sceHttpGetStatusCode() error: 0x%08X\n", ret); + return http_get_close(false, tmplId, connId, reqId); + } + app.DebugPrintf("response code = %d\n", statusCode); + + if(statusCode == 200) + { + ret = sceHttpGetResponseContentLength(reqId, &contentLength); + if(ret < 0) + { + app.DebugPrintf("sceHttpGetContentLength() error: 0x%08X\n", ret); + return http_get_close(false, tmplId, connId, reqId); + } + else + { + app.DebugPrintf("Content-Length = %lu\n", contentLength); + } + recvBuf = new SceUChar8[contentLength+1]; + int bufferLeft = contentLength+1; + SceUChar8* pCurrBuffPos = recvBuf; + int totalBytesRead = 0; + while(finFlag != SCE_TRUE) + { + ret = sceHttpReadData(reqId, pCurrBuffPos, bufferLeft); + if (ret < 0) + { + app.DebugPrintf("\n sceHttpReadData() failed 0x%08X\n", ret); + return http_get_close(false, tmplId, connId, reqId); + } + else if (ret == 0) + { + finFlag = SCE_TRUE; + } + app.DebugPrintf("\n sceHttpReadData() read %d bytes\n", ret); + pCurrBuffPos += ret; + totalBytesRead += ret; + bufferLeft -= ret; + } + } + + *ppOutData = recvBuf; + *pDataSize = contentLength; + return http_get_close(true, tmplId, connId, reqId); +} diff --git a/Minecraft.Client/PSVita/Network/SonyHttp_Vita.h b/Minecraft.Client/PSVita/Network/SonyHttp_Vita.h new file mode 100644 index 00000000..e4244b9e --- /dev/null +++ b/Minecraft.Client/PSVita/Network/SonyHttp_Vita.h @@ -0,0 +1,26 @@ +#pragma once + +#include + +class SonyHttp_Vita +{ + static int sslCallback(SceUInt32 verifyErr, SceSslCert * const sslCert[], SceInt32 certNum, void *userArg); + static bool http_get(const char *targetUrl, void** ppOutData, int* pDataSize); + static bool http_get_close(bool bOK, SceInt32 tmplId, SceInt32 connId, SceInt32 reqId); + + static void printSslError(SceInt32 sslErr, SceUInt32 sslErrDetail); + static void printSslCertInfo(SceSslCert *sslCert); + +// static int libnetMemId; + static int libsslCtxId; + static int libhttpCtxId; + + static bool bInitialised; + +public: + bool init(); + void shutdown(); + bool getDataFromURL(const char* szURL, void** ppOutData, int* pDataSize); + + static int getHTTPContextID() { return libhttpCtxId; } +}; \ No newline at end of file diff --git a/Minecraft.Client/PSVita/Network/SonyRemoteStorage_Vita.cpp b/Minecraft.Client/PSVita/Network/SonyRemoteStorage_Vita.cpp new file mode 100644 index 00000000..3d9483d2 --- /dev/null +++ b/Minecraft.Client/PSVita/Network/SonyRemoteStorage_Vita.cpp @@ -0,0 +1,398 @@ +#include "stdafx.h" + +#include "SonyRemoteStorage_Vita.h" +#include "SonyHttp_Vita.h" +#include +#include +#include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include + + + + +#define AUTH_SCOPE "psn:s2s" +#define CLIENT_ID "969e9d21-527c-4c22-b539-f8e479f690bc" +static SceRemoteStorageData s_getDataOutput; + + +void SonyRemoteStorage_Vita::staticInternalCallback(const SceRemoteStorageEvent event, int32_t retCode, void * userData) +{ + ((SonyRemoteStorage_Vita*)userData)->internalCallback(event, retCode); +} + +void SonyRemoteStorage_Vita::internalCallback(const SceRemoteStorageEvent event, int32_t retCode) +{ + m_lastErrorCode = retCode; + + switch(event) + { + case ERROR_OCCURRED: + app.DebugPrintf("An error occurred with retCode: 0x%x \n", retCode); + m_status = e_error; +// shutdown(); // removed, as the remote storage lib now tries to reconnect if an error has occurred + runCallback(); + m_bTransferStarted = false; + break; + + case GET_DATA_RESULT: + if(retCode >= 0) + { + app.DebugPrintf("Get Data success \n"); + m_status = e_getDataSucceeded; + } + else + { + app.DebugPrintf("An error occurred while Get Data was being processed. retCode: 0x%x \n", retCode); + m_status = e_error; + } + runCallback(); + m_bTransferStarted = false; + break; + + case GET_DATA_PROGRESS: + app.DebugPrintf("Get data progress: %i%%\n", retCode); + m_status = e_getDataInProgress; + m_dataProgress = retCode; + m_startTime = System::currentTimeMillis(); + break; + + case GET_STATUS_RESULT: + if(retCode >= 0) + { + app.DebugPrintf("Get Status success \n"); + app.DebugPrintf("Remaining Syncs for this user: %llu\n", outputGetStatus->remainingSyncs); + app.DebugPrintf("Number of files on the cloud: %d\n", outputGetStatus->numFiles); + for(int i = 0; i < outputGetStatus->numFiles; i++) + { + app.DebugPrintf("\n*** File %d information: ***\n", (i + 1)); + app.DebugPrintf("File name: %s \n", outputGetStatus->data[i].fileName); + app.DebugPrintf("File description: %s \n", outputGetStatus->data[i].fileDescription); + app.DebugPrintf("MD5 Checksum: %s \n", outputGetStatus->data[i].md5Checksum); + app.DebugPrintf("Size of the file: %u bytes \n", outputGetStatus->data[i].fileSize); + app.DebugPrintf("Timestamp: %s \n", outputGetStatus->data[i].timeStamp); + app.DebugPrintf("Visibility: \"%s\" \n", (outputGetStatus->data[i].visibility == 0)?"Private":((outputGetStatus->data[i].visibility == 1)?"Public read only":"Public read and write")); + } + m_status = e_getStatusSucceeded; + } + else + { + app.DebugPrintf("An error occurred while Get Status was being processed. retCode: 0x%x \n", retCode); + m_status = e_error; + } + runCallback(); + break; + + case PSN_SIGN_IN_REQUIRED: + app.DebugPrintf("User's PSN sign-in through web browser is required \n"); + m_status = e_signInRequired; + runCallback(); + break; + + case SET_DATA_RESULT: + if(retCode >= 0) + { + app.DebugPrintf("Set Data success \n"); + m_status = e_setDataSucceeded; + } + else + { + app.DebugPrintf("An error occurred while Set Data was being processed. retCode: 0x%x \n", retCode); + m_status = e_error; + } + runCallback(); + m_bTransferStarted = false; + break; + + case SET_DATA_PROGRESS: + app.DebugPrintf("Set data progress: %i%%\n", retCode); + m_status = e_setDataInProgress; + m_dataProgress = retCode; + + break; + + case USER_ACCOUNT_LINKED: + app.DebugPrintf("User's account has been linked with PSN \n"); + m_bInitialised = true; + m_status = e_accountLinked; + runCallback(); + break; + + case WEB_BROWSER_RESULT: + app.DebugPrintf("This function is not used on PS Vita, as the account will be linked, it is not needed to open a browser to link it \n"); + assert(0); + break; + + default: + app.DebugPrintf("This should never happen \n"); + assert(0); + break; + + } +} + +bool SonyRemoteStorage_Vita::init(CallbackFunc cb, LPVOID lpParam) +{ + int ret = 0; + int reqId = 0; + + m_callbackFunc = cb; + m_callbackParam = lpParam; + m_lastErrorCode = SCE_OK; + + if(m_bInitialised) + { + internalCallback(USER_ACCOUNT_LINKED, 0); + return true; + } + + ret = sceNpAuthInit(); + if(ret < 0 && ret != SCE_NP_AUTH_ERROR_ALREADY_INITIALIZED) + { + app.DebugPrintf("sceNpAuthInit failed 0x%x\n", ret); + return false; + } + + ret = sceNpAuthCreateOAuthRequest(); + if (ret < 0) + { + app.DebugPrintf("Couldn't create auth request 0x%x\n", ret); + return false; + } + + reqId = ret; + + SceNpClientId clientId; + memset(&clientId, 0x0, sizeof(clientId)); + +// SceNpAuthorizationCode authCode; +// memset(&authCode, 0x0, sizeof(authCode)); + + SceNpAuthGetAuthorizationCodeParameter authParams; + memset(&authParams, 0x0, sizeof(authParams)); + + authParams.size = sizeof(authParams); + authParams.pScope = AUTH_SCOPE; + + memcpy(clientId.id, CLIENT_ID, strlen(CLIENT_ID)); + authParams.pClientId = &clientId; + + int issuerId = 0; +// ret = sceNpAuthGetAuthorizationCode(reqId, &authParams, &authCode, &issuerId); +// if (ret < 0) +// { +// app.DebugPrintf("Failed to get auth code 0x%x\n", ret); +// sceNpAuthDeleteOAuthRequest(reqId); +// return false; +// } + + ret = sceNpAuthDeleteOAuthRequest(reqId); + if (ret < 0) + { + app.DebugPrintf("Couldn't delete auth request 0x%x\n", ret); + return false; + } + + SceRemoteStorageInitParams params; + + params.callback = SonyRemoteStorage_Vita::staticInternalCallback; + params.userData = this; + params.thread.threadAffinity = SCE_KERNEL_THREAD_CPU_AFFINITY_MASK_DEFAULT; + params.thread.threadPriority = SCE_KERNEL_DEFAULT_PRIORITY_USER; +// memcpy(params.authCode, authCode.code, SCE_NP_AUTHORIZATION_CODE_MAX_LEN); + strcpy(params.clientId, CLIENT_ID); + params.timeout.connectMs = 30 * 1000; //30 seconds is the default + params.timeout.resolveMs = 30 * 1000; //30 seconds is the default + params.timeout.receiveMs = 120 * 1000; //120 seconds is the default + params.timeout.sendMs = 120 * 1000; //120 seconds is the default + params.pool.memPoolSize = 7 * 1024 * 1024; + if(m_memPoolBuffer == NULL) + m_memPoolBuffer = malloc(params.pool.memPoolSize); + params.pool.memPoolBuffer = m_memPoolBuffer; + + SceRemoteStorageAbortReqParams abortParams; + + ret = sceRemoteStorageInit(params); + if(ret >= 0) + { + abortParams.requestId = ret; + app.DebugPrintf("Session will be created \n"); + } + else if(ret == SCE_REMOTE_STORAGE_ERROR_ALREADY_INITIALISED) + { + app.DebugPrintf("Session already created \n"); + runCallback(); + } + else + { + app.DebugPrintf("Error creating session: 0x%x \n", ret); + return false; + } + return true; +} + + + +bool SonyRemoteStorage_Vita::getRemoteFileInfo(SceRemoteStorageStatus* pInfo, CallbackFunc cb, LPVOID lpParam) +{ + m_callbackFunc = cb; + m_callbackParam = lpParam; + outputGetStatus = pInfo; + + SceRemoteStorageStatusReqParams params; + reqId = sceRemoteStorageGetStatus(params, outputGetStatus); + m_status = e_getStatusInProgress; + + if(reqId >= 0) + { + app.DebugPrintf("Get Status request sent \n"); + return true; + } + else + { + app.DebugPrintf("Error sending Get Status request: 0x%x \n", reqId); + return false; + } +} + +void SonyRemoteStorage_Vita::abort() +{ + m_bAborting = true; + app.DebugPrintf("Aborting...\n"); + if(m_bTransferStarted) + { + app.DebugPrintf("transfer has started so we'll call sceRemoteStorageAbort...\n"); + + SceRemoteStorageAbortReqParams params; + params.requestId = reqId; + int ret = sceRemoteStorageAbort(params); + + if(ret >= 0) + { + app.DebugPrintf("Abort request done \n"); + } + else + { + app.DebugPrintf("Error in Abort request: 0x%x \n", ret); + } + } +} + + + +bool SonyRemoteStorage_Vita::setDataInternal() +{ + // CompressSaveData(); // check if we need to re-save the file compressed first + + snprintf(m_saveFilename, sizeof(m_saveFilename), "%s:%s/GAMEDATA.bin", "savedata0", m_setDataSaveInfo->UTF8SaveFilename); + strcpy(m_saveFileDesc, m_setDataSaveInfo->UTF8SaveTitle); + m_status = e_setDataInProgress; + + + SceRemoteStorageSetDataReqParams params; + params.visibility = PUBLIC_READ_WRITE; + strcpy(params.pathLocation, m_saveFilename); + sprintf(params.fileName, getRemoteSaveFilename()); + + DescriptionData descData; + ZeroMemory(&descData, sizeof(DescriptionData)); + descData.m_platform[0] = SAVE_FILE_PLATFORM_LOCAL & 0xff; + descData.m_platform[1] = (SAVE_FILE_PLATFORM_LOCAL >> 8) & 0xff; + descData.m_platform[2] = (SAVE_FILE_PLATFORM_LOCAL >> 16) & 0xff; + descData.m_platform[3] = (SAVE_FILE_PLATFORM_LOCAL >> 24)& 0xff; + + if(m_thumbnailData) + { + unsigned int uiHostOptions; + bool bHostOptionsRead; + DWORD uiTexturePack; + char seed[22]; + app.GetImageTextData(m_thumbnailData, m_thumbnailDataSize,(unsigned char *)seed, uiHostOptions, bHostOptionsRead, uiTexturePack); + + __int64 iSeed = strtoll(seed,NULL,10); + char seedHex[17]; + sprintf(seedHex,"%016llx",iSeed); + memcpy(descData.m_seed,seedHex,16); // Don't copy null + + // Save the host options that this world was last played with + char hostOptions[9]; + sprintf(hostOptions,"%08x",uiHostOptions); + memcpy(descData.m_hostOptions,hostOptions,8); // Don't copy null + + // Save the texture pack id + char texturePack[9]; + sprintf(texturePack,"%08x",uiTexturePack); + memcpy(descData.m_texturePack,texturePack,8); // Don't copy null + } + + memcpy(descData.m_saveNameUTF8, m_saveFileDesc, strlen(m_saveFileDesc)+1); // plus null + memcpy(params.fileDescription, &descData, sizeof(descData)); + + + if(m_bAborting) + { + runCallback(); + return false; + } + reqId = sceRemoteStorageSetData(params); + + app.DebugPrintf("\n*******************************\n"); + if(reqId >= 0) + { + app.DebugPrintf("Set Data request sent \n"); + m_bTransferStarted = true; + return true; + } + else + { + app.DebugPrintf("Error sending Set Data request: 0x%x \n", reqId); + return false; + } +} + + +bool SonyRemoteStorage_Vita::getData( const char* remotePath, const char* localPath, CallbackFunc cb, LPVOID lpParam ) +{ + m_callbackFunc = cb; + m_callbackParam = lpParam; + + SceRemoteStorageGetDataReqParams params; + sprintf(params.pathLocation, "savedata0:%s/GAMEDATA.bin", localPath); +// strcpy(params.pathLocation, localPath); + // strcpy(params.fileName, "/test/small.txt"); + strcpy(params.fileName, remotePath); + memset(¶ms.psVitaSaveDataSlot, 0, sizeof(params.psVitaSaveDataSlot)); + SceRemoteStorageData s_getDataOutput; + reqId = sceRemoteStorageGetData(params, &s_getDataOutput); + + app.DebugPrintf("\n*******************************\n"); + if(reqId >= 0) + { + app.DebugPrintf("Get Data request sent \n"); + m_bTransferStarted = true; + return true; + } + else + { + app.DebugPrintf("Error sending Get Data request: 0x%x \n", reqId); + return false; + } +} + +void SonyRemoteStorage_Vita::runCallback() +{ + assert(m_callbackFunc); + if(m_callbackFunc) + { + m_callbackFunc(m_callbackParam, m_status, m_lastErrorCode); + } + m_lastErrorCode = SCE_OK; +} diff --git a/Minecraft.Client/PSVita/Network/SonyRemoteStorage_Vita.h b/Minecraft.Client/PSVita/Network/SonyRemoteStorage_Vita.h new file mode 100644 index 00000000..9c636e41 --- /dev/null +++ b/Minecraft.Client/PSVita/Network/SonyRemoteStorage_Vita.h @@ -0,0 +1,43 @@ +#pragma once + + +#include "Common\Network\Sony\SonyRemoteStorage.h" + +class SonyRemoteStorage_Vita : public SonyRemoteStorage +{ +public: + + + virtual bool init(CallbackFunc cb, LPVOID lpParam); + + virtual bool getRemoteFileInfo(SceRemoteStorageStatus* pInfo, CallbackFunc cb, LPVOID lpParam); + virtual bool getData(const char* remotePath, const char* localPath, CallbackFunc cb, LPVOID lpParam); + + virtual void abort(); + virtual bool setDataInternal(); + +private: + int reqId; + void * psnTicket; + size_t psnTicketSize; + bool m_waitingForTicket; + bool initialized; + SceRemoteStorageStatus* outputGetStatus; + SceRemoteStorageData outputGetData; + + int32_t m_lastErrorCode; + int m_getDataProgress; + int m_setDataProgress; + char m_saveFilename[SCE_REMOTE_STORAGE_DATA_NAME_MAX_LEN]; + char m_saveFileDesc[SCE_REMOTE_STORAGE_DATA_DESCRIPTION_MAX_LEN]; + char m_remoteFilename[SCE_REMOTE_STORAGE_DATA_NAME_MAX_LEN]; + + + static void staticInternalCallback(const SceRemoteStorageEvent event, int32_t retCode, void * userData); + void internalCallback(const SceRemoteStorageEvent event, int32_t retCode); + + void runCallback(); + + +}; + diff --git a/Minecraft.Client/PSVita/Network/SonyVoiceChat_Vita.cpp b/Minecraft.Client/PSVita/Network/SonyVoiceChat_Vita.cpp new file mode 100644 index 00000000..842e6b8d --- /dev/null +++ b/Minecraft.Client/PSVita/Network/SonyVoiceChat_Vita.cpp @@ -0,0 +1,1092 @@ +#include "stdafx.h" +#include +#include +#include +#include +#include + +#include "SonyVoiceChat_Vita.h" + +std::vector SonyVoiceChat_Vita::m_remoteConnections; +bool SonyVoiceChat_Vita::m_bVoiceStarted = false; +int SonyVoiceChat_Vita::m_numLocalDevicesConnected = 0; +SQRLocalVoiceDevice SonyVoiceChat_Vita::m_localVoiceDevices[MAX_LOCAL_PLAYER_COUNT]; +uint32_t SonyVoiceChat_Vita::m_voiceOutPort; +bool SonyVoiceChat_Vita::m_forceSendPacket = false; // force a packet across the network, even if there's no data, so we can update flags +RingBuffer SonyVoiceChat_Vita::m_recordRingBuffer(sc_ringBufferSize); +VoicePacket::Flags SonyVoiceChat_Vita::m_localPlayerFlags[MAX_LOCAL_PLAYER_COUNT]; +bool SonyVoiceChat_Vita::m_bInitialised = false; +CRITICAL_SECTION SonyVoiceChat_Vita::m_csRemoteConnections; + +// sample related variables +SceVoiceStartParam startParam; +int32_t playSize = 0; + +static const int sc_thresholdValue = 100; + +static const bool sc_verbose = false; + +// #define _USE_PCM_AUDIO_ +//#define LOOPBACK_TEST + + + +int g_loadedPCMVoiceDataSizes[4]; +int g_loadedPCMVoiceDataPos[4]; +char* g_loadedPCMVoiceData[4]; + +static void CreatePort(uint32_t *portId, const SceVoicePortParam *pArg) +{ +// C4JThread::PushAffinityAllCores(); // PS4 only + + int err = sceVoiceCreatePort( portId, pArg ); + assert(err == SCE_OK); + assert(*portId != SCE_VOICE_INVALID_PORT_ID); +// C4JThread::PopAffinity(); // PS4 only +} + +static void DeletePort(uint32_t& port) +{ + int32_t result; + if (port != SCE_VOICE_INVALID_PORT_ID) + { + result = sceVoiceDeletePort( port ); + if (result != SCE_OK) + { + app.DebugPrintf("sceVoiceDeletePort failed %0x\n", result); + assert(0); + } + port = SCE_VOICE_INVALID_PORT_ID; + } +} + + +void LoadPCMVoiceData() +{ + for(int i=0;i<4;i++) + { + char filename[64]; + sprintf(filename, "voice%d.pcm", i+1); + HANDLE file = CreateFile(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + DWORD dwHigh=0; + g_loadedPCMVoiceDataSizes[i] = GetFileSize(file,&dwHigh); + + if(g_loadedPCMVoiceDataSizes[i]!=0) + { + g_loadedPCMVoiceData[i] = new char[g_loadedPCMVoiceDataSizes[i]]; + DWORD bytesRead; + BOOL bSuccess = ReadFile(file, g_loadedPCMVoiceData[i], g_loadedPCMVoiceDataSizes[i], &bytesRead, NULL); + assert(bSuccess); + } + g_loadedPCMVoiceDataPos[i] = 0; + } +} + + +void SonyVoiceChat_Vita::init() +{ + int returnCode = SCE_OK; + + returnCode = sceSysmoduleLoadModule(SCE_SYSMODULE_VOICE); + if (returnCode < 0) + { + app.DebugPrintf("Error: sceSysmoduleLoadModule(SCE_SYSMODULE_VOICE), ret 0x%08x\n", returnCode); + assert(0); + } + + + SceVoiceInitParam params; + SceVoicePortParam portArgs; + memset( ¶ms, 0, sizeof(params) ); + params.appType = SCEVOICE_APPTYPE_GAME; + params.onEvent = 0; + returnCode = sceVoiceInit( ¶ms , SCEVOICE_VERSION_100); + if (returnCode < 0) + { + app.DebugPrintf("Error: sceVoiceInit(), ret 0x%08x\n", returnCode); + assert(0); + } + +#ifdef _USE_PCM_AUDIO_ + portArgs.portType = SCEVOICE_PORTTYPE_OUT_PCMAUDIO; + portArgs.bMute = false; + portArgs.threshold = 0; + portArgs.volume = 1.0f; + portArgs.pcmaudio.format.dataType = SCEVOICE_PCM_SHORT_LITTLE_ENDIAN; + portArgs.pcmaudio.format.sampleRate = SCE_VOICE_SAMPLINGRATE_16000; + portArgs.pcmaudio.bufSize = 4096; +#else + portArgs.portType = SCEVOICE_PORTTYPE_OUT_VOICE; + portArgs.bMute = false; + portArgs.threshold = 0; + portArgs.volume = 1.0f; + portArgs.voice.bitrate = VOICE_ENCODED_FORMAT; +#endif + CreatePort( &m_voiceOutPort, &portArgs ); + + start(); + m_bInitialised = true; +#ifdef LOOPBACK_TEST + // LoadPCMVoiceData(); + initLocalPlayer(0); + connectPorts(m_localVoiceDevices[0].m_microphonePort, m_localVoiceDevices[0].m_headsetPort); + // SQRVoiceConnection* pConnection = addRemoteConnection(0, 0); + // connectPlayer(pConnection, 0); +#endif + InitializeCriticalSection(&m_csRemoteConnections); +} + +void SonyVoiceChat_Vita::shutdown() +{ + m_bInitialised = false; + int32_t result; + + DeletePort( m_voiceOutPort); + result = sceVoiceStop(); + assert(result == SCE_OK); + result = sceVoiceEnd(); + assert(result == SCE_OK); + + m_bVoiceStarted=false; + sceKernelFreeMemBlock(startParam.container); + + int returnCode = sceSysmoduleUnloadModule(SCE_SYSMODULE_VOICE); + if (returnCode < 0) + { + app.DebugPrintf("Error: sceSysmoduleUnloadModule(SCE_SYSMODULE_VOICE), ret 0x%08x\n", returnCode); + assert(0); + } + + DeleteCriticalSection(&m_csRemoteConnections); +} + + +void SonyVoiceChat_Vita::start() +{ + if( m_bVoiceStarted == false) + { + startParam.container = sceKernelAllocMemBlock("SceUserVoiceEvent", SCE_KERNEL_MEMBLOCK_TYPE_USER_RWDATA, SCE_VOICE_MEMORY_CONTAINER_SIZE, SCE_NULL); + int err; + +// C4JThread::PushAffinityAllCores(); // PS4 only + err = sceVoiceStart(&startParam); + assert(err == SCE_OK); +// C4JThread::PopAffinity(); // PS4 only + + m_bVoiceStarted = true; + } +} + +void SonyVoiceChat_Vita::checkFinished() +{ + EnterCriticalSection(&m_csRemoteConnections); + + for(int i=0;im_bFlaggedForShutdown) + m_remoteConnections[i]->m_bFlaggedForShutdown = true; + } +// assert(m_numLocalDevicesConnected == 0); + + LeaveCriticalSection(&m_csRemoteConnections); +} + +void SonyVoiceChat_Vita::setEnabled( bool bEnabled ) +{ +} + + + +// Internal send function. This attempts to send as many elements in the queue as possible until the write function tells us that we can't send any more. This way, +// we are guaranteed that if there *is* anything more in the queue left to send, we'll get a CELL_RUDP_CONTEXT_EVENT_WRITABLE event when whatever we've managed to +// send here is complete, and can continue on. +void SQRVoiceConnection::SendMoreInternal() +{ + bool keepSending; + do + { + EnterCriticalSection(&m_csQueue); + keepSending = false; + if( m_sendQueue.size() > 0) + { + // Attempt to send the full data in the first element in our queue + unsigned char *data= m_sendQueue.front().current; + int dataSize = m_sendQueue.front().end - m_sendQueue.front().current; + int ret = sceRudpWrite( m_rudpCtx, data, dataSize, 0);//CELL_RUDP_MSG_LATENCY_CRITICAL ); + int wouldBlockFlag = SCE_RUDP_ERROR_WOULDBLOCK; + + if( ret == dataSize ) + { + // Fully sent, remove from queue - will loop in the while loop to see if there's anything else in the queue we could send + delete [] m_sendQueue.front().start; + m_sendQueue.pop(); + if( m_sendQueue.size() ) + { + keepSending = true; + } + } + else if( ( ret >= 0 ) || ( ret == wouldBlockFlag ) ) + { + + + // Things left to send - adjust this element in the queue + int remainingBytes; + if( ret >= 0 ) + { + // Only ret bytes sent so far + remainingBytes = dataSize - ret; + assert(remainingBytes > 0 ); + } + else + { + // Is CELL_RUDP_ERROR_WOULDBLOCK, nothing has yet been sent + remainingBytes = dataSize; + } + m_sendQueue.front().current = m_sendQueue.front().end - remainingBytes; + } + } + LeaveCriticalSection(&m_csQueue); + } while (keepSending); +} + +void SQRVoiceConnection::SendInternal(const void *data, unsigned int dataSize) +{ + EnterCriticalSection(&m_csQueue); + + QueuedSendBlock sendBlock; + + unsigned char *dataCurrent = (unsigned char *)data; + unsigned int dataRemaining = dataSize; + + while( dataRemaining ) + { + int dataSize = dataRemaining; + if( dataSize > SNP_MAX_PAYLOAD ) dataSize = SNP_MAX_PAYLOAD; + sendBlock.start = new unsigned char [dataSize]; + sendBlock.end = sendBlock.start + dataSize; + sendBlock.current = sendBlock.start; + memcpy( sendBlock.start, dataCurrent, dataSize); + m_sendQueue.push(sendBlock); + dataRemaining -= dataSize; + dataCurrent += dataSize; + } + +// app.DebugPrintf("voice sent %d bytes\n", dataSize); + + // Now try and send as much as we can + SendMoreInternal(); + + LeaveCriticalSection(&m_csQueue); +} + +void SQRVoiceConnection::readRemoteData() +{ + unsigned int dataSize = sceRudpGetSizeReadable(m_rudpCtx); + if( dataSize > 0 ) + { + VoicePacket packet; + unsigned int bytesRead = sceRudpRead( m_rudpCtx, &packet, dataSize, 0, NULL ); + unsigned int writeSize; + if( bytesRead > 0 ) + { +// app.DebugPrintf("voice received %d bytes\n", bytesRead); + writeSize = bytesRead; + if(packet.verifyData(bytesRead, 19)) + addPacket(packet); +// m_playRingBuffer.Write((char*)data, writeSize); + + } + } + +} + + + +SQRVoiceConnection::SQRVoiceConnection( int rudpCtx, SceNpMatching2RoomMemberId remoteRoomMemberId ) + : m_rudpCtx(rudpCtx) + , m_remoteRoomMemberId(remoteRoomMemberId) + , m_bConnected(false) + , m_headsetConnectionMask(0) + , m_playRingBuffer(sc_ringBufferSize) +{ + InitializeCriticalSection(&m_csQueue); + InitializeCriticalSection(&m_csPacketQueue); + + SceVoiceInitParam params; + SceVoicePortParam portArgs; +#ifdef _USE_PCM_AUDIO_ + portArgs.portType = SCEVOICE_PORTTYPE_IN_PCMAUDIO; + portArgs.bMute = false; + portArgs.threshold = 100; + portArgs.volume = 1.0f; + portArgs.pcmaudio.format.sampleRate= SCEVOICE_SAMPLINGRATE_16000; + portArgs.pcmaudio.format.dataType = SCEVOICE_PCM_SHORT_LITTLE_ENDIAN; + portArgs.pcmaudio.bufSize = 4096; +#else + portArgs.portType = SCEVOICE_PORTTYPE_IN_VOICE; + portArgs.bMute = false; + portArgs.threshold = sc_thresholdValue; // compensate network jitter + portArgs.volume = 1.0f; + portArgs.voice.bitrate = VOICE_ENCODED_FORMAT; +#endif + CreatePort( &m_voiceInPort, &portArgs ); + m_nextExpectedFrameIndex = 0; + m_bFlaggedForShutdown = false; +} + +SQRVoiceConnection::~SQRVoiceConnection() +{ + DeleteCriticalSection(&m_csQueue); + DeleteCriticalSection(&m_csPacketQueue); + sceRudpTerminate( m_rudpCtx ); + app.DebugPrintf("-----------------------------\n"); + app.DebugPrintf("Voice rudp context deleted %d\n", m_rudpCtx); + app.DebugPrintf("-----------------------------\n"); + + DeletePort(m_voiceInPort); + +} + +bool SQRVoiceConnection::getNextPacket( VoicePacket& packet ) +{ + EnterCriticalSection(&m_csPacketQueue); + bool retVal = false; + if(m_receivedVoicePackets.size() > 0) + { + retVal = true; + packet = m_receivedVoicePackets.front(); + m_receivedVoicePackets.pop(); + } + LeaveCriticalSection(&m_csPacketQueue); + return retVal; +} + +void SQRVoiceConnection::addPacket( VoicePacket& packet ) +{ + EnterCriticalSection(&m_csPacketQueue); + m_receivedVoicePackets.push(packet); + LeaveCriticalSection(&m_csPacketQueue); +} + +int g_frameNum = 0; +bool g_bRecording = false; + + +uint32_t frameSendIndex = 0; +uint32_t lastReadFrameCnt = 0; + + +void PrintAllOutputVoiceStates( std::vector& connections) +{ + for(int rIdx=0;rIdxm_voiceInPort, &portInfo ); + static SceVoicePortState lastPortState = SCEVOICE_PORTSTATE_IDLE; + if(portInfo.state != lastPortState) + { + lastPortState = portInfo.state; + switch(portInfo.state) + { + case SCEVOICE_PORTSTATE_IDLE: + app.DebugPrintf(" ----- SCE_VOICE_PORTSTATE_IDLE\n"); + break; + case SCEVOICE_PORTSTATE_BUFFERING: + app.DebugPrintf(" ----- SCE_VOICE_PORTSTATE_BUFFERING\n"); + break; + case SCEVOICE_PORTSTATE_RUNNING: + app.DebugPrintf(" ----- SCE_VOICE_PORTSTATE_RUNNING\n"); + break; + case SCEVOICE_PORTSTATE_READY: + app.DebugPrintf(" ----- SCE_VOICE_PORTSTATE_READY\n"); + break; + case SCEVOICE_PORTSTATE_NULL: + default: + app.DebugPrintf(" ----- SCE_VOICE_PORTSTATE_NULL\n"); + break; + } + } + } + +} + + +void SonyVoiceChat_Vita::sendPCMMicData() +{ + int32_t result; + uint32_t outputPortBytes; + VoicePacket packetToSend; + uint32_t readSize; + SceVoiceBasePortInfo portInfo; + memset( &portInfo, 0, sizeof(portInfo) ); + uint16_t frameGap = 0; + + DWORD tick = GetTickCount(); + static DWORD lastTick = 0; + int numFrames = ceilf((tick - lastTick)/16.0f); + lastTick = tick; + readSize = 512 * numFrames; + + if(g_loadedPCMVoiceDataPos[0] + readSize < g_loadedPCMVoiceDataSizes[0]) + { + for(int i=0;i (g_loadedPCMVoiceDataSizes[0] + 8192)) + g_loadedPCMVoiceDataPos[0] = 0; + + +} + +void SonyVoiceChat_Vita::sendAllVoiceData() +{ + int32_t result; + uint32_t outputPortBytes; + VoicePacket packetToSend; + uint32_t readSize; + SceVoiceBasePortInfo portInfo; + memset( &portInfo, 0, sizeof(portInfo) ); + uint16_t frameGap = 0; + + VoicePacket::Flags lastPlayerFlags[MAX_LOCAL_PLAYER_COUNT]; + + for(int i=0; isizeof(packetToSend.m_data))?sizeof(packetToSend.m_data):outputPortBytes; + if( outputPortBytes || flagsChanged || m_forceSendPacket) + { + frameSendIndex += lastReadFrameCnt; + if(outputPortBytes) + { + readSize = outputPortBytes; + result = sceVoiceReadFromOPort(m_voiceOutPort, packetToSend.m_data, &readSize ); + if (result != SCE_OK) + { + app.DebugPrintf("sceVoiceReadFromOPort failed %0x\n", result); + assert(0); + return; + } + lastReadFrameCnt = readSize/portInfo.frameSize; + assert(readSize%portInfo.frameSize == 0); + + packetToSend.m_numFrames = lastReadFrameCnt; + packetToSend.m_frameSendIndex = frameSendIndex; + packetToSend.setChecksum(readSize); + } + else + { + readSize = 0; + packetToSend.m_numFrames = 0; + packetToSend.m_frameSendIndex = frameSendIndex; + packetToSend.setChecksum(readSize); + + } + + + int packetSize = packetToSend.getPacketSize(readSize); + + EnterCriticalSection(&m_csRemoteConnections); + + // send this packet out to all our remote connections + for(int rIdx=0;rIdxm_bConnected) + m_remoteConnections[rIdx]->SendInternal(&packetToSend, packetSize); + } + + LeaveCriticalSection(&m_csRemoteConnections); + } + m_forceSendPacket = false; +} + +bool g_bPlaying = false; + +void SonyVoiceChat_Vita::playAllReceivedData() +{ + EnterCriticalSection(&m_csRemoteConnections); + // write all the incoming data from the network to each of the input voices + for(int rIdx=0;rIdxgetNextPacket(packet)) // MGH - changed to a while loop, so all the packets are sent to the voice port, and it can handle delayed packets due to the size of it's internal buffer + { + int frameGap; + if (pVoice->m_nextExpectedFrameIndex == packet.m_frameSendIndex) + { + // no voice frame drop, continuous frames + frameGap = 0; + if(sc_verbose) + app.DebugPrintf("index@%d gets expected frame\n",pVoice->m_nextExpectedFrameIndex); + pVoice->m_nextExpectedFrameIndex = packet.m_frameSendIndex + packet.m_numFrames; + } + else if (pVoice->m_nextExpectedFrameIndex < packet.m_frameSendIndex) + { + // has voice frame drop, dropped forwarding frames + frameGap = packet.m_frameSendIndex - pVoice->m_nextExpectedFrameIndex; + if(sc_verbose) + app.DebugPrintf("index@%d gets dropped forwarding frames %d\n",pVoice->m_nextExpectedFrameIndex, frameGap); + pVoice->m_nextExpectedFrameIndex = packet.m_frameSendIndex + packet.m_numFrames; + } + else if (pVoice->m_nextExpectedFrameIndex > packet.m_frameSendIndex) + { + // has voice frame drop, dropped preceding frames, no reset on pVoice->m_nextExpectedFrameIndex + frameGap = packet.m_frameSendIndex - pVoice->m_nextExpectedFrameIndex; + if(sc_verbose) + app.DebugPrintf("index@%d gets dropped forwarding frames %d\n", pVoice->m_nextExpectedFrameIndex, frameGap); + } + + SceVoiceBasePortInfo portInfo; + int result = sceVoiceGetPortInfo(pVoice->m_voiceInPort, &portInfo ); + if (result != SCE_OK) + { + if(sc_verbose) + app.DebugPrintf("sceVoiceGetPortInfo LoopbackVoiceInPort failed %x\n", result); + assert(0); + LeaveCriticalSection(&m_csRemoteConnections); + return; + } + uint32_t writeSize = packet.m_numFrames * portInfo.frameSize; + int inputPortBytes = portInfo.numByte; + inputPortBytes = (inputPortBytes>writeSize)?writeSize:inputPortBytes; + writeSize = inputPortBytes; + result = sceVoiceWriteToIPort(pVoice->m_voiceInPort, packet.m_data, &writeSize, frameGap); + if (result != SCE_OK) + { + if(sc_verbose) + app.DebugPrintf("sceVoiceWriteToIPort failed %0x\n", result); + assert(0); + LeaveCriticalSection(&m_csRemoteConnections); + return; + } + if (writeSize != inputPortBytes) + { + // libvoice internal voice in port buffer fulls + if(sc_verbose) + app.DebugPrintf("internal voice in port buffer fulls. \n"); + } + packet.m_numFrames = 0; + + // copy the flags + for(int flagIndex=0;flagIndexm_remotePlayerFlags[flagIndex] = packet.m_localPlayerFlags[flagIndex]; + } + } + LeaveCriticalSection(&m_csRemoteConnections); + +} + +void SonyVoiceChat_Vita::tick() +{ + if(m_bInitialised) + { +// DWORD tick = GetTickCount(); +// static DWORD lastTick = 0; +// app.DebugPrintf("Time since last voice tick : %d ms\n", tick - lastTick); +// lastTick = tick; + g_frameNum++; + sendAllVoiceData(); + playAllReceivedData(); + + EnterCriticalSection(&m_csRemoteConnections); + + for(int i=m_remoteConnections.size()-1;i>=0;i--) + { + if(m_remoteConnections[i]->m_bFlaggedForShutdown) + { + delete m_remoteConnections[i]; + m_remoteConnections.erase(m_remoteConnections.begin() + i); + } + } + + LeaveCriticalSection(&m_csRemoteConnections); + + } +} + + + +bool SonyVoiceChat_Vita::hasMicConnected(SQRNetworkPlayer* pNetPlayer) +{ + if(CGameNetworkManager::usingAdhocMode()) // no voice chat in adhoc + return false; + + if(pNetPlayer->IsLocal()) + { + return m_localPlayerFlags[pNetPlayer->GetLocalPlayerIndex()].m_bHasMicConnected; + } + else + { + EnterCriticalSection(&m_csRemoteConnections); + for(int i=0;im_remoteRoomMemberId == pNetPlayer->m_roomMemberId) + { + bool bMicConnected = pVoice->m_remotePlayerFlags[pNetPlayer->GetLocalPlayerIndex()].m_bHasMicConnected; + LeaveCriticalSection(&m_csRemoteConnections); + return bMicConnected; + } + } + LeaveCriticalSection(&m_csRemoteConnections); + } + // if we get here we've not found the player, panic!! + assert(0); + return false; +} + +void SonyVoiceChat_Vita::mute( bool bMute ) +{ +} + +void SonyVoiceChat_Vita::mutePlayer( const SceNpMatching2RoomMemberId member_id, bool bMute ) /*Turn chat audio from a specified player on or off */ +{ +} + +void SonyVoiceChat_Vita::muteLocalPlayer( bool bMute ) /*Turn microphone input on or off */ +{ +} + +bool SonyVoiceChat_Vita::isMuted() +{ + return false; +} + +bool SonyVoiceChat_Vita::isMutedPlayer( const PlayerUID& memberUID) +{ + return false; +} + +bool SonyVoiceChat_Vita::isMutedLocalPlayer() +{ + return false; +} + + +bool SonyVoiceChat_Vita::isTalking(SQRNetworkPlayer* pNetPlayer) +{ + if(CGameNetworkManager::usingAdhocMode()) // no voice chat in adhoc + return false; + + if(pNetPlayer->IsLocal()) + { + return m_localPlayerFlags[pNetPlayer->GetLocalPlayerIndex()].m_bTalking; + } + else + { + EnterCriticalSection(&m_csRemoteConnections); + for(int i=0;im_remoteRoomMemberId == pNetPlayer->m_roomMemberId) + { + bool bTalking = pVoice->m_remotePlayerFlags[pNetPlayer->GetLocalPlayerIndex()].m_bTalking; + LeaveCriticalSection(&m_csRemoteConnections); + return bTalking; + } + } + LeaveCriticalSection(&m_csRemoteConnections); + } + // if we get here we've not found the player, panic!! + assert(0); + return false; +} + + +void SQRLocalVoiceDevice::init(bool bChatRestricted) +{ + SceVoiceInitParam params; + SceVoicePortParam portArgs; + + int returnCode = 0; + m_bChatRestricted = bChatRestricted; + + portArgs.portType = SCEVOICE_PORTTYPE_IN_DEVICE; + portArgs.bMute = false; + portArgs.threshold = 0; + portArgs.volume = 1.0f; + portArgs.device.playerId = 0; +// portArgs.device.type = SCE_AUDIO_IN_TYPE_VOICE; +// portArgs.device.index = 0; + CreatePort( &m_microphonePort, &portArgs ); +// m_micAudioDevicePort = sceAudioInOpen(localUserID, +// SCE_AUDIO_IN_TYPE_VOICE, 0, +// SCE_AUDIO_IN_GRAIN_DEFAULT, +// SCE_AUDIO_IN_FREQ_DEFAULT, +// SCE_AUDIO_IN_PARAM_FORMAT_S16_MONO); +// assert(m_micAudioDevicePort >= 0); + + portArgs.portType = SCEVOICE_PORTTYPE_OUT_DEVICE; + portArgs.bMute = false; + portArgs.threshold = 0; + portArgs.volume = 1.0f; + portArgs.device.playerId = 0; +// portArgs.device.type = SCE_AUDIO_OUT_PORT_TYPE_VOICE; +// portArgs.device.index = 0; + CreatePort( &m_headsetPort, &portArgs ); + + m_bValid = true; + +} + + + +void SQRLocalVoiceDevice::shutdown() +{ + + assert(isValid()); + m_bValid = false; + DeletePort(m_microphonePort); + DeletePort(m_headsetPort); +// int err = sceAudioInClose(m_micAudioDevicePort); +// assert(err == SCE_OK); +// m_micAudioDevicePort = -1; +} + + + +SQRVoiceConnection* SonyVoiceChat_Vita::addRemoteConnection( int RudpCxt, SceNpMatching2RoomMemberId peerMemberId) +{ + EnterCriticalSection(&m_csRemoteConnections); + SQRVoiceConnection* pConn = new SQRVoiceConnection(RudpCxt, peerMemberId); + m_remoteConnections.push_back(pConn); + m_forceSendPacket = true; // new connection, so we'll force a packet through for the flags + LeaveCriticalSection(&m_csRemoteConnections); + + return pConn; +} + +void SonyVoiceChat_Vita::connectPorts(uint32_t inPort, uint32_t outPort) +{ + int returnCode = sceVoiceConnectIPortToOPort(inPort, outPort); + if (returnCode != SCE_OK ) + { + app.DebugPrintf("sceVoiceConnectIPortToOPort failed (0x%08x), inPort 0x%08x, outPort 0x%08x\n", returnCode, inPort, outPort); + assert(0); + } +} +void SonyVoiceChat_Vita::disconnectPorts(uint32_t inPort, uint32_t outPort) +{ + int returnCode = sceVoiceDisconnectIPortFromOPort(inPort, outPort); + if (returnCode != SCE_OK ) + { + app.DebugPrintf("sceVoiceDisconnectIPortFromOPort failed (0x%08x), inPort 0x%08x, outPort 0x%08x\n", returnCode, inPort, outPort); + assert(0); + } +} + + +void SonyVoiceChat_Vita::makeLocalConnections() +{ + // connect all mics to other devices headsets, for local chat + for(int i=0;iisValid()) + { + for(int j=0;jisValid()) + { + if(pConnectFrom->m_localConnections[j] == false) + { + if(pConnectTo->m_bChatRestricted == false && pConnectFrom->m_bChatRestricted == false) + { + connectPorts(pConnectFrom->m_microphonePort, pConnectTo->m_headsetPort); + pConnectFrom->m_localConnections[j] = true; + } + } + } + } + } + } +} + +void SonyVoiceChat_Vita::breakLocalConnections(int playerIdx) +{ + // break any connections with devices that are no longer valid + for(int i=0;im_localConnections[j] == true) + { + SQRLocalVoiceDevice* pConnectedTo = &m_localVoiceDevices[j]; + if(i==playerIdx || j==playerIdx) + { + if(pConnectedTo->m_bChatRestricted == false && pConnectedFrom->m_bChatRestricted == false) + { + disconnectPorts(pConnectedFrom->m_microphonePort, pConnectedTo->m_headsetPort); + pConnectedFrom->m_localConnections[j] = false; + } + } + } + } + } +} + + +void SonyVoiceChat_Vita::initLocalPlayer(int playerIndex) +{ + if(m_localVoiceDevices[playerIndex].isValid() == false) + { + bool chatRestricted = false; + ProfileManager.GetChatAndContentRestrictions(ProfileManager.GetPrimaryPad(),false,&chatRestricted,NULL,NULL); + + // create all device ports required + m_localVoiceDevices[playerIndex].init(chatRestricted); + m_numLocalDevicesConnected++; + if(m_localVoiceDevices[playerIndex].m_bChatRestricted == false) + { + connectPorts(m_localVoiceDevices[playerIndex].m_microphonePort, m_voiceOutPort); + } + m_forceSendPacket = true; // new local device, so we'll force a packet through for the flags + + } + makeLocalConnections(); +} + +void SonyVoiceChat_Vita::connectPlayer(SQRVoiceConnection* pConnection, int playerIndex) +{ + if((pConnection->m_headsetConnectionMask & (1 << playerIndex)) == 0) + { + initLocalPlayer(playerIndex); // added this as we can get a client->client connection coming in first, and the network player hasn't been created yet (so this hasn't been initialised) + if(m_localVoiceDevices[playerIndex].m_bChatRestricted == false) + { + connectPorts(pConnection->m_voiceInPort, m_localVoiceDevices[playerIndex].m_headsetPort); + } + pConnection->m_headsetConnectionMask |= (1 << playerIndex); + app.DebugPrintf("Connecting player %d to rudp context %d\n", playerIndex, pConnection->m_rudpCtx); + m_forceSendPacket = true; // new connection, so we'll force a packet through for the flags + } +} + +SQRVoiceConnection* SonyVoiceChat_Vita::GetVoiceConnectionFromRudpCtx( int RudpCtx ) +{ + for(int i=0;im_rudpCtx == RudpCtx) + return m_remoteConnections[i]; + } + return NULL; +} + +void SonyVoiceChat_Vita::connectPlayerToAll( int playerIndex ) +{ + EnterCriticalSection(&m_csRemoteConnections); + + for(int i=0;im_remoteRoomMemberId == roomMemberID) + { + return m_remoteConnections[i]; + } + } + + return NULL; +} + +void SonyVoiceChat_Vita::disconnectLocalPlayer( int localIdx ) +{ + if(m_localVoiceDevices[localIdx].isValid() == false) + return; + + EnterCriticalSection(&m_csRemoteConnections); + + if(m_localVoiceDevices[localIdx].m_bChatRestricted == false) + { + disconnectPorts(m_localVoiceDevices[localIdx].m_microphonePort, m_voiceOutPort); + + for(int i=0;im_voiceInPort, m_localVoiceDevices[localIdx].m_headsetPort); + m_remoteConnections[i]->m_headsetConnectionMask &= (~(1 << localIdx)); + app.DebugPrintf("disconnecting player %d from rudp context %d\n", localIdx, m_remoteConnections[i]->m_rudpCtx); + } + } + m_numLocalDevicesConnected--; + + if(m_numLocalDevicesConnected == 0) // no more local players, kill all the remote connections + { + for(int i=0;i=0); + if(voiceIdx>=0) + { + m_remoteConnections[voiceIdx]->m_bFlaggedForShutdown = true; + } + + LeaveCriticalSection(&m_csRemoteConnections); + +} + +void SonyVoiceChat_Vita::setConnected( int RudpCtx ) +{ + SQRVoiceConnection* pVoice = GetVoiceConnectionFromRudpCtx(RudpCtx); + if(pVoice) + { + pVoice->m_bConnected = true; + m_forceSendPacket = true; + } + else + { + assert(false); + } +} + + + + +RingBuffer::RingBuffer( int sizeBytes ) +{ + buffer = new char[sizeBytes]; + buf_size = sizeBytes; + buf_full = buf_free = 0; +} + + +int RingBuffer::Write( char* data, int len_ ) +{ + if (len_ <= 0) return len_; + unsigned int len = (unsigned int)len_; + unsigned int data_size = buf_size - (buf_free - buf_full); + if (len > data_size) + len = data_size; + data_size = buf_size - (buf_free % buf_size); + if (data_size > len) + data_size = len; + memcpy(buffer + (buf_free % buf_size), data, data_size); + if (data_size != len) + memcpy(buffer, data + data_size, len - data_size); + buf_free += len; + return len; +} + +int RingBuffer::Read( char* data, int max_bytes_ ) +{ + if (max_bytes_ <= 0) return max_bytes_; + unsigned int max_bytes = (unsigned int)max_bytes_; + unsigned int result = buf_free - buf_full; + if (result > max_bytes) + result = max_bytes; + unsigned int chunk = buf_size - (buf_full % buf_size); + if (chunk > result) + chunk = result; + memcpy(data, buffer + (buf_full % buf_size), chunk); + if (chunk != result) + memcpy(data + chunk, buffer, result - chunk); + buf_full += result; + return result; +} diff --git a/Minecraft.Client/PSVita/Network/SonyVoiceChat_Vita.h b/Minecraft.Client/PSVita/Network/SonyVoiceChat_Vita.h new file mode 100644 index 00000000..6ece603f --- /dev/null +++ b/Minecraft.Client/PSVita/Network/SonyVoiceChat_Vita.h @@ -0,0 +1,222 @@ +#pragma once + +#include +#include +#include +#include +#include "Common/Network/Sony/SQRNetworkPlayer.h" + +static const int sc_maxVoiceDataSize = 2048; + + +class VoicePacket +{ + static const int MAX_LOCAL_PLAYER_COUNT = 1; + +public: + struct Flags + { + bool m_bTalking : 1; + bool m_bHasMicConnected : 1; + }; + + Flags m_localPlayerFlags[MAX_LOCAL_PLAYER_COUNT]; + uint32_t m_frameSendIndex; + uint32_t m_numFrames; + uint32_t m_checkSum; + uint32_t m_playerIndexFlags; + char m_data[sc_maxVoiceDataSize]; + + static int getPacketSize(int dataSize) { return (uint64_t)&((VoicePacket*)0)->m_data[dataSize];} + void setChecksum(int dataSize) + { + m_checkSum = 0; + for(int i=0;i, +// the reader thread changes pointer +class RingBuffer +{ +public: + RingBuffer(int sizeBytes); + ~RingBuffer() { delete buffer; } + void Reset(void) { buf_full = buf_free = 0; } + unsigned int DataSize(void) { return (buf_free - buf_full); } + int Write(char* data, int len_); + int Read(char* data, int max_bytes_); + int getDataSize() { return buf_free - buf_full; } + void ResetByWriter(void) { buf_free = buf_full; } + void ResetByReader(void) { buf_full = buf_free; } + +private: + char* buffer; + unsigned int buf_size; + unsigned int buf_full; + unsigned int buf_free; +}; + + +static const int sc_ringBufferSize = 16384; + +class SQRLocalVoiceDevice +{ +public: + uint32_t m_headsetPort; + uint32_t m_microphonePort; +// int32_t m_micAudioDevicePort; + uint8_t m_localConnections[4]; // connection between this devices mic and other local player's headsets + bool m_bChatRestricted; + + bool m_bValid; + +public: + SQRLocalVoiceDevice() + : m_headsetPort(SCE_VOICE_INVALID_PORT_ID) + , m_microphonePort(SCE_VOICE_INVALID_PORT_ID) + , m_bValid(false) +// , m_micAudioDevicePort(-1) + { + for(int i=0;i<4;i++) + m_localConnections[i] = 0; + } + + void init(bool bChatRestricted); + + void shutdown(); + bool isValid() { return m_bValid; } +// void setBitRate() +// { +// int err = sceVoiceSetBitRate(uint32_t portId, +// SceVoiceBitRate bitrate +// ); +// } +}; + +#define VOICE_ENCODED_FORMAT SCEVOICE_BITRATE_7300 + + + +class SQRVoiceConnection +{ + static const int MAX_LOCAL_PLAYER_COUNT = 1; + + static const int SNP_MAX_PAYLOAD = 1346; // This is the default RUDP payload size - if we want to change this we'll need to use cellRudpSetOption to set something else & adjust segment size + class QueuedSendBlock + { + public: + unsigned char *start; + unsigned char *end; + unsigned char *current; + }; + + std::queue m_sendQueue; + CRITICAL_SECTION m_csQueue; + +public: + int m_rudpCtx; + bool m_bConnected; + uint32_t m_voiceInPort; // 1 input port per connection, incoming UDP packets are written to each of these, and then they're connected out to all headsets + int m_headsetConnectionMask; // 1 bit per player, if the headset connection has been made + RingBuffer m_playRingBuffer; + SceNpMatching2RoomMemberId m_remoteRoomMemberId; // Assigned by Matching2 lib, we can use to indicate which machine this player belongs to (note - 16 bits) + std::queue m_receivedVoicePackets; + CRITICAL_SECTION m_csPacketQueue; + VoicePacket::Flags m_remotePlayerFlags[MAX_LOCAL_PLAYER_COUNT]; + uint32_t m_nextExpectedFrameIndex; + bool m_bFlaggedForShutdown; + SQRVoiceConnection(int rudpCtx, SceNpMatching2RoomMemberId remoteRoomMemberId); + ~SQRVoiceConnection(); + + void SendInternal(const void *data, unsigned int dataSize); + void SendMoreInternal(); + void readRemoteData(); + bool getNextPacket(VoicePacket& packet); + void addPacket(VoicePacket& packet); + + +}; + +class SonyVoiceChat_Vita +{ +public: + + static void init(); + static void start(); + static void shutdown(); + static void tick(); + static void checkFinished(); + static void setEnabled(bool bEnabled); + static bool hasMicConnected(SQRNetworkPlayer* pNetPlayer); + static bool isTalking(SQRNetworkPlayer* pNetPlayer); + static void mute(bool bMute); //Turn chat audio on or off + static void mutePlayer(const SceNpMatching2RoomMemberId member_id, bool bMute); //Turn chat audio from a specified player on or off; + static void muteLocalPlayer(bool bMute); //Turn microphone input on or off; + + static bool isMuted(); + static bool isMutedPlayer(const PlayerUID& memberUID); + static bool isMutedLocalPlayer(); //Turn microphone input on or off; + + static void initLocalPlayer(int playerIndex); + + static SQRVoiceConnection* addRemoteConnection(int RudpCxt, SceNpMatching2RoomMemberId peerMemberId); + static void connectPlayer(SQRVoiceConnection* pConnection, int playerIndex); + static void connectPlayerToAll(int playerIndex); + static void disconnectLocalPlayer(int localIdx); + static void disconnectRemoteConnection( SQRVoiceConnection* pVoice ); + + static void VoiceEventCallback( SceVoiceEventData* pEvent ); + + static std::vector m_remoteConnections; + static void connectPorts(uint32_t inPort, uint32_t outPort); + static void disconnectPorts(uint32_t inPort, uint32_t outPort); + static void makeLocalConnections(); + static void breakLocalConnections(int playerIdx); + + static void sendAllVoiceData(); + static void playAllReceivedData(); + + static void sendPCMMicData(); + static SQRVoiceConnection* getVoiceConnectionFromRoomMemberID(SceNpMatching2RoomMemberId roomMemberID); + + static SQRVoiceConnection* GetVoiceConnectionFromRudpCtx(int RudpCtx); + static void setConnected(int RudpCtx); + +private: + + static const int MAX_LOCAL_PLAYER_COUNT = 1; + + static bool m_bVoiceStarted; + static int m_numLocalDevicesConnected; + static SQRLocalVoiceDevice m_localVoiceDevices[MAX_LOCAL_PLAYER_COUNT]; + + static uint32_t m_voiceOutPort; // single output port that all local devices are mixed to, and then sent out to all other remote machines + + static RingBuffer m_recordRingBuffer; + static RingBuffer m_playRingBuffer; + static VoicePacket::Flags m_localPlayerFlags[MAX_LOCAL_PLAYER_COUNT]; + static bool m_forceSendPacket; // force a packet across the network, even if there's no data, so we can update flags + static bool m_bInitialised; + static CRITICAL_SECTION m_csRemoteConnections; + + +}; \ No newline at end of file -- cgit v1.2.3