Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infrastructure: Don't connect to adhoc unless infra-dns.json tells us to #19897

Merged
merged 6 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Core/Dialog/PSPNetconfDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ int PSPNetconfDialog::Update(int animSpeed) {
else if (state == PSP_NET_APCTL_STATE_DISCONNECTED) {
// When connecting with infrastructure, simulate a connection using the first network configuration entry.
if (connResult < 0) {
// connResult = sceNetApctlConnect(1);
connResult = hleCall(sceNetApctl, int, sceNetApctlConnect, 1);
}
}
Expand Down
8 changes: 4 additions & 4 deletions Core/HLE/proAdhoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1372,15 +1372,15 @@ int friendFinder() {
if (metasocket == (int)INVALID_SOCKET && netAdhocctlInited && isAdhocctlNeedLogin) {
if (g_Config.bEnableWlan) {
if (initNetwork(&product_code) == 0) {
networkInited = true;
g_adhocServerConnected = true;
INFO_LOG(Log::sceNet, "FriendFinder: Network [RE]Initialized");
// At this point we are most-likely not in a Group within the Adhoc Server, so we should probably reset AdhocctlState
adhocctlState = ADHOCCTL_STATE_DISCONNECTED;
netAdhocGameModeEntered = false;
isAdhocctlBusy = false;
}
else {
networkInited = false;
g_adhocServerConnected = false;
shutdown((int)metasocket, SD_BOTH);
closesocket((int)metasocket);
metasocket = (int)INVALID_SOCKET;
Expand All @@ -1390,7 +1390,7 @@ int friendFinder() {
// Prevent retrying to Login again unless it was on demand
isAdhocctlNeedLogin = false;

if (networkInited) {
if (g_adhocServerConnected) {
// Ping Server
now = time_now_d() * 1000000.0; // Use time_now_d()*1000000.0 instead of CoreTiming::GetGlobalTimeUsScaled() if the game gets disconnected from AdhocServer too soon when FPS wasn't stable
// original code : ((sceKernelGetSystemTimeWide() - lastping) >= ADHOCCTL_PING_TIMEOUT)
Expand All @@ -1406,7 +1406,7 @@ int friendFinder() {
if (iResult == SOCKET_ERROR) {
ERROR_LOG(Log::sceNet, "FriendFinder: Socket Error (%i) when sending OPCODE_PING", error);
if (error != EAGAIN && error != EWOULDBLOCK) {
networkInited = false;
g_adhocServerConnected = false;
shutdown((int)metasocket, SD_BOTH);
closesocket((int)metasocket);
metasocket = (int)INVALID_SOCKET;
Expand Down
2 changes: 1 addition & 1 deletion Core/HLE/sceMpeg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1673,7 +1673,7 @@ static u32 sceMpegFinish()
// TODO: Need to properly hook module load/unload for this to work right.
//return ERROR_MPEG_NOT_YET_INIT;
} else {
INFO_LOG(Log::ME, "sceMpegFinish(...)");
INFO_LOG(Log::ME, "sceMpegFinish()");
__VideoPmpShutdown();
}
isMpegInit = false;
Expand Down
51 changes: 35 additions & 16 deletions Core/HLE/sceNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ std::string defaultNetSSID = "Wifi"; // fake AP/hotspot
int netApctlInfoId = 0;
SceNetApctlInfoInternal netApctlInfo;

bool netApctlInited;
bool g_netApctlInited;
u32 netApctlState;
u32 apctlProdCodeAddr = 0;
u32 apctlThreadHackAddr = 0;
Expand Down Expand Up @@ -185,6 +185,7 @@ bool LoadDNSForGameID(std::string_view gameID, InfraDNSConfig *dns) {
dns->revivalTeamURL = revived.getStringOr("url", "");
}
}
dns->connectAdHocForGrouping = def.getBool("connect_adhoc_for_grouping", false);
}

const JsonNode *games = root.getArray("games");
Expand Down Expand Up @@ -242,6 +243,7 @@ bool LoadDNSForGameID(std::string_view gameID, InfraDNSConfig *dns) {
dns->gameName = game.getStringOr("name", "");
dns->dns = game.getStringOr("dns", dns->dns.c_str());
dns->dyn_dns = game.getStringOr("dyn_dns", "");
dns->connectAdHocForGrouping = game.getBool("connect_adhoc_for_grouping", dns->connectAdHocForGrouping);
if (game.hasChild("domains", JSON_OBJECT)) {
const JsonGet domains = game.getDict("domains");
for (auto iter : domains.value_) {
Expand Down Expand Up @@ -332,7 +334,7 @@ int ScheduleApctlState(int event, int newState, int usec, const char* reason) {
}

void __NetApctlInit() {
netApctlInited = false;
g_netApctlInited = false;
netApctlState = PSP_NET_APCTL_STATE_DISCONNECTED;
apctlStateEvent = CoreTiming::RegisterEvent("__ApctlState", __ApctlState);
apctlHandlers.clear();
Expand Down Expand Up @@ -435,11 +437,11 @@ void __NetDoState(PointerWrap &p) {

auto cur_netInited = netInited;
auto cur_netInetInited = netInetInited;
auto cur_netApctlInited = netApctlInited;
auto cur_netApctlInited = g_netApctlInited;

Do(p, netInited);
Do(p, netInetInited);
Do(p, netApctlInited);
Do(p, g_netApctlInited);
Do(p, apctlHandlers);
Do(p, netMallocStat);
if (s < 2) {
Expand Down Expand Up @@ -488,7 +490,7 @@ void __NetDoState(PointerWrap &p) {

if (p.mode == p.MODE_READ) {
// Let's not change "Inited" value when Loading SaveState in the middle of multiplayer to prevent memory & port leaks
netApctlInited = cur_netApctlInited;
g_netApctlInited = cur_netApctlInited;
netInetInited = cur_netInetInited;
netInited = cur_netInited;

Expand Down Expand Up @@ -1006,7 +1008,7 @@ void NetApctl_InitInfo(int confId) {
}

static int sceNetApctlInit(int stackSize, int initPriority) {
if (netApctlInited) {
if (g_netApctlInited) {
return hleLogError(Log::sceNet, ERROR_NET_APCTL_ALREADY_INITIALIZED);
}

Expand Down Expand Up @@ -1038,16 +1040,22 @@ static int sceNetApctlInit(int stackSize, int initPriority) {
memcpy(prodCode->data, discID.c_str(), std::min(ADHOCCTL_ADHOCID_LEN, (int)discID.size()));
}

hleCall(sceNetAdhocctl, int, sceNetAdhocctlInit, stackSize, initPriority, apctlProdCodeAddr);

netApctlInited = true;
// Actually connect sceNetAdhocctl. TODO: Can we move this to sceNetApctlConnect? Would be good because a lot of games call Init but not Connect on startup.
if (g_infraDNSConfig.connectAdHocForGrouping) {
hleCall(sceNetAdhocctl, int, sceNetAdhocctlInit, stackSize, initPriority, apctlProdCodeAddr);
}

g_netApctlInited = true;
return hleLogSuccessInfoI(Log::sceNet, 0);
}

int NetApctl_Term() {
// Note: Since we're borrowing AdhocServer for Grouping purpose, we should cleanup too
NetAdhocctl_Term();
// Should we check the g_infraDNSConfig.connectAdHocForGrouping flag here?
if (g_netApctlInited) {
hleCall(sceNetAdhocctl, int, sceNetAdhocctlTerm);
}

if (apctlProdCodeAddr != 0) {
userMemory.Free(apctlProdCodeAddr);
apctlProdCodeAddr = 0;
Expand All @@ -1061,9 +1069,8 @@ int NetApctl_Term() {
apctlThreadID = 0;
}

netApctlInited = false;
g_netApctlInited = false;
netApctlState = PSP_NET_APCTL_STATE_DISCONNECTED;

return 0;
}

Expand Down Expand Up @@ -1275,12 +1282,18 @@ int sceNetApctlConnect(int confId) {

// Is this confId is the index to the scanning's result data or sceNetApctlGetBSSDescIDListUser result?
netApctlInfoId = confId;

// Note: We're borrowing AdhocServer for Grouping purpose, so we can simulate Broadcast over the internet just like Adhoc's pro-online implementation
int ret = hleCall(sceNetAdhocctl, int, sceNetAdhocctlConnect, "INFRA");
int ret = 0;
if (g_infraDNSConfig.connectAdHocForGrouping) {
ret = hleCall(sceNetAdhocctl, int, sceNetAdhocctlConnect, "INFRA");
}

if (netApctlState == PSP_NET_APCTL_STATE_DISCONNECTED)
if (netApctlState == PSP_NET_APCTL_STATE_DISCONNECTED) {
__UpdateApctlHandlers(0, PSP_NET_APCTL_STATE_JOINING, PSP_NET_APCTL_EVENT_CONNECT_REQUEST, 0);
//hleDelayResult(0, "give time to init/cleanup", adhocEventDelayMS * 1000);
}

// hleDelayResult(0, "give time to init/cleanup", adhocEventDelayMS * 1000);
// TODO: Blocks current thread and wait for a state change to prevent user-triggered connection attempt from causing events to piles up
return hleLogSuccessInfoI(Log::sceNet, 0, "connect = %i", ret);
}
Expand All @@ -1289,7 +1302,9 @@ int sceNetApctlDisconnect() {
// Like its 'sister' function sceNetAdhocctlDisconnect, we need to alert Apctl handlers that a disconnect took place
// or else games like Phantasy Star Portable 2 will hang at certain points (e.g. returning to the main menu after trying to connect to PSN).
// Note: Since we're borrowing AdhocServer for Grouping purpose, we should disconnect too
hleCall(sceNetAdhocctl, int, sceNetAdhocctlDisconnect);
if (g_infraDNSConfig.connectAdHocForGrouping) {
hleCall(sceNetAdhocctl, int, sceNetAdhocctlDisconnect);
}

// Discards any pending events so we can disconnect immediately
apctlEvents.clear();
Expand All @@ -1302,6 +1317,10 @@ int NetApctl_GetState() {
return netApctlState;
}

bool __NetApctlConnected() {
return netApctlState >= PSP_NET_APCTL_STATE_GOT_IP;
}

static int sceNetApctlGetState(u32 pStateAddr) {
//if (!netApctlInited) return hleLogError(Log::sceNet, ERROR_NET_APCTL_NOT_IN_BSS, "apctl not in bss");

Expand Down
6 changes: 5 additions & 1 deletion Core/HLE/sceNet.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class AfterApctlMipsCall : public PSPAction {
};

extern bool netInited;
extern bool netApctlInited;
extern bool g_netApctlInited;
extern u32 netApctlState;
extern SceNetApctlInfoInternal netApctlInfo;
extern std::string defaultNetConfigName;
Expand All @@ -127,6 +127,8 @@ void __NetDoState(PointerWrap &p);

int NetApctl_GetState();

bool __NetApctlConnected();

int sceNetApctlConnect(int connIndex);

// These return false if allowed to be consistent with the similar function for achievements.
Expand Down Expand Up @@ -154,6 +156,8 @@ struct InfraDNSConfig {

std::string revivalTeam;
std::string revivalTeamURL;

bool connectAdHocForGrouping;
};

extern InfraDNSConfig g_infraDNSConfig;
12 changes: 6 additions & 6 deletions Core/HLE/sceNetAdhoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
// TODO: Make accessor functions instead, and throw all this state in a struct.
bool netAdhocInited;
bool netAdhocctlInited;
bool networkInited = false;
bool g_adhocServerConnected = false;

#define DISCOVER_DURATION_US 2000000 // 2 seconds is probably the normal time it takes for PSP to connect to a group (ie. similar to NetconfigDialog time)
u64 netAdhocDiscoverStartTime = 0;
Expand Down Expand Up @@ -128,7 +128,6 @@ void ZeroNetAdhocMatchingThreads();
void SaveNetAdhocMatchingInited();
void RestoreNetAdhocMatchingInited();


bool __NetAdhocConnected() {
return netAdhocInited && netAdhocctlInited && (adhocctlState == ADHOCCTL_STATE_CONNECTED || adhocctlState == ADHOCCTL_STATE_GAMEMODE);
}
Expand Down Expand Up @@ -371,7 +370,7 @@ static void __AdhocctlNotify(u64 userdata, int cyclesLate) {
}

// Retry until successfully sent. Login packet sent after successfully connected to Adhoc Server (indicated by networkInited), so we're not sending Login again here
if ((req.opcode == OPCODE_LOGIN && !networkInited) || (ret == SOCKET_ERROR && (sockerr == EAGAIN || sockerr == EWOULDBLOCK))) {
if ((req.opcode == OPCODE_LOGIN && !g_adhocServerConnected) || (ret == SOCKET_ERROR && (sockerr == EAGAIN || sockerr == EWOULDBLOCK))) {
u64 now = (u64)(time_now_d() * 1000000.0);
if (now - adhocctlStartTime <= static_cast<u64>(adhocDefaultTimeout) + 500) {
// Try again in another 0.5ms until timedout.
Expand Down Expand Up @@ -1278,8 +1277,9 @@ int sceNetAdhocctlInit(int stackSize, int prio, u32 productAddr) {
// FIXME: Returning 0x8002013a (SCE_KERNEL_ERROR_LIBRARY_NOT_YET_LINKED) without adhoc module loaded first?
// FIXME: Sometimes returning 0x80410601 (ERROR_NET_ADHOC_AUTH_ALREADY_INITIALIZED / Library module is already initialized ?) when AdhocctlTerm is not fully done?

if (netAdhocctlInited)
if (netAdhocctlInited) {
return hleLogError(Log::sceNet, ERROR_NET_ADHOCCTL_ALREADY_INITIALIZED);
}

auto product = PSPPointer<SceNetAdhocctlAdhocId>::Create(productAddr);
if (product.IsValid()) {
Expand All @@ -1306,7 +1306,7 @@ int sceNetAdhocctlInit(int stackSize, int prio, u32 productAddr) {

// Need to make sure to be connected to Adhoc Server (indicated by networkInited) before returning to prevent GTA VCS failed to create/join a group and unable to see any game room
int us = adhocDefaultDelay;
if (g_Config.bEnableWlan && !networkInited) {
if (g_Config.bEnableWlan && !g_adhocServerConnected) {
AdhocctlRequest dummyreq = { OPCODE_LOGIN, {0} };
return hleLogSuccessOrWarn(Log::sceNet, WaitBlockingAdhocctlSocket(dummyreq, us, "adhocctl init"));
}
Expand Down Expand Up @@ -2582,7 +2582,7 @@ int NetAdhocctl_Term() {
//May also need to clear Handlers
adhocctlHandlers.clear();
// Free stuff here
networkInited = false;
g_adhocServerConnected = false;
shutdown((int)metasocket, SD_BOTH);
closesocket((int)metasocket);
metasocket = (int)INVALID_SOCKET;
Expand Down
5 changes: 3 additions & 2 deletions Core/HLE/sceNetAdhoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void __UpdateAdhocctlHandlers(u32 flags, u32 error);

bool __NetAdhocConnected();

// Called from netdialog
// Called from netdialog (and from sceNetApctl)
// NOTE: use hleCall for sceNet* ones!
int sceNetAdhocctlGetState(u32 ptrToStatus);
int sceNetAdhocctlCreate(const char * groupName);
Expand All @@ -116,6 +116,7 @@ int sceNetAdhocctlScan();
int sceNetAdhocctlGetScanInfo(u32 sizeAddr, u32 bufAddr);
int sceNetAdhocctlDisconnect();
int sceNetAdhocctlInit(int stackSize, int prio, u32 productAddr);
int sceNetAdhocctlTerm();

int NetAdhocctl_Term();
int NetAdhocctl_GetState();
Expand All @@ -125,7 +126,7 @@ int NetAdhoc_Term();
// May need to use these from sceNet.cpp
extern bool netAdhocInited;
extern bool netAdhocctlInited;
extern bool networkInited;
extern bool g_adhocServerConnected;
extern bool netAdhocGameModeEntered;
extern int netAdhocEnterGameModeTimeout;
extern int adhocDefaultTimeout; //3000000 usec
Expand Down
2 changes: 1 addition & 1 deletion Core/HLE/sceNetInet.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class PointerWrap;

extern bool netInited;
extern bool netInetInited;
extern bool netApctlInited;
extern bool g_netApctlInited;
extern u32 netApctlState;
extern SceNetApctlInfoInternal netApctlInfo;
extern std::string defaultNetConfigName;
Expand Down
6 changes: 1 addition & 5 deletions Core/HLE/sceNetResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ static std::mutex g_netResolversLock; // Do we really need this?
static bool g_netResolverInitialized = true;

static int sceNetResolverInit() {
// TODO: Move this to infra-jns.config! This isn't ok.
// However obviously don't remove the mHostToAlias mechanism.
g_Config.mHostToAlias["socomftb2.psp.online.scea.com"] = "67.222.156.250";
g_Config.mHostToAlias["socompsp-prod.muis.pdonline.scea.com"] = "67.222.156.250";

// Hardcoded mHostToAlias entries here have been moved to the infra-dns.json file.
return hleLogSuccessInfoI(Log::sceNet, 0);
}

Expand Down
10 changes: 8 additions & 2 deletions UI/PauseScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "Core/HLE/sceUmd.h"
#include "Core/HLE/sceNet.h"
#include "Core/HLE/sceNetInet.h"
#include "Core/HLE/sceNetAdhoc.h"

#include "GPU/GPUCommon.h"
#include "GPU/GPUState.h"
Expand Down Expand Up @@ -277,11 +278,12 @@ void GamePauseScreen::update() {
finishNextFrame_ = false;
}

if (netInited != lastNetInited_ || netInetInited != lastNetInetInited_) {
if (netInited != lastNetInited_ || netInetInited != lastNetInetInited_ || lastAdhocServerConnected_ != g_adhocServerConnected) {
INFO_LOG(Log::sceNet, "Network status changed, recreating views");
RecreateViews();
lastNetInetInited_ = netInetInited;
lastNetInited_ = netInited;
lastAdhocServerConnected_ = g_adhocServerConnected;
}

bool mustRunBehind = MustRunBehind();
Expand Down Expand Up @@ -384,7 +386,7 @@ void GamePauseScreen::CreateViews() {
}
}

if (netInited) {
if (netInited || g_adhocServerConnected) {
leftColumnItems->Add(new NoticeView(NoticeLevel::INFO, nw->T("Network connected"), ""));

if (!g_infraDNSConfig.revivalTeam.empty() && netInetInited) {
Expand All @@ -399,6 +401,10 @@ void GamePauseScreen::CreateViews() {
});
}
}

if (g_adhocServerConnected) {
leftColumnItems->Add(new TextView(std::string(nw->T("AdHoc Server")) + ": " + std::string(nw->T("Connected"))));
}
}

bool achievementsAllowSavestates = !Achievements::HardcoreModeActive() || g_Config.bAchievementsSaveStateInHardcoreMode;
Expand Down
1 change: 1 addition & 0 deletions UI/PauseScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,5 @@ class GamePauseScreen : public UIDialogScreenWithGameBackground {
UI::Button *playButton_ = nullptr;
bool lastNetInited_ = false;
bool lastNetInetInited_ = false;
bool lastAdhocServerConnected_ = false;
};
Loading
Loading