Skip to content

Commit

Permalink
Stashed infrastructure codes that need to be separated into smaller …
Browse files Browse the repository at this point in the history
…commits/PRs.
  • Loading branch information
anr2me committed Jan 11, 2024
1 parent ec9c877 commit bfa42b8
Show file tree
Hide file tree
Showing 21 changed files with 3,269 additions and 315 deletions.
1 change: 1 addition & 0 deletions Common/Net/HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ constexpr const char *HTTP_VERSION = "1.1";

Client::Client() {
userAgent_ = DEFAULT_USERAGENT;
httpVersion_ = HTTP_VERSION;
}

Client::~Client() {
Expand Down
1 change: 1 addition & 0 deletions Common/Net/HTTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class Client : public net::Connection {

protected:
std::string userAgent_;
const char* httpVersion_;
double dataTimeout_ = 900.0;
};

Expand Down
2 changes: 2 additions & 0 deletions Core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,8 @@ static const ConfigSetting networkSettings[] = {
ConfigSetting("EnableAdhocServer", &g_Config.bEnableAdhocServer, false, CfgFlag::PER_GAME),
ConfigSetting("proAdhocServer", &g_Config.proAdhocServer, "socom.cc", CfgFlag::PER_GAME),
ConfigSetting("PortOffset", &g_Config.iPortOffset, 10000, CfgFlag::PER_GAME),
ConfigSetting("PrimaryDNSServer", &g_Config.primaryDNSServer, "135.148.144.253", CfgFlag::PER_GAME),
ConfigSetting("SecondaryDNSServer", &g_Config.secondaryDNSServer, "0.0.0.0", CfgFlag::PER_GAME),
ConfigSetting("MinTimeout", &g_Config.iMinTimeout, 0, CfgFlag::PER_GAME),
ConfigSetting("ForcedFirstConnect", &g_Config.bForcedFirstConnect, false, CfgFlag::PER_GAME),
ConfigSetting("EnableUPnP", &g_Config.bEnableUPnP, false, CfgFlag::PER_GAME),
Expand Down
2 changes: 2 additions & 0 deletions Core/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@ struct Config {

// Networking
std::string proAdhocServer;
std::string primaryDNSServer;
std::string secondaryDNSServer;
bool bEnableWlan;
bool bEnableAdhocServer;
bool bEnableUPnP;
Expand Down
2 changes: 1 addition & 1 deletion Core/Dialog/PSPMsgDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static const float FONT_SCALE = 0.65f;

// These are rough, it seems to take a long time to init, and probably depends on threads.
// TODO: This takes like 700ms on a PSP but that's annoyingly long.
const static int MSG_INIT_DELAY_US = 300000;
const static int MSG_INIT_DELAY_US = 350000;
const static int MSG_SHUTDOWN_DELAY_US = 26000;

PSPMsgDialog::PSPMsgDialog(UtilityDialogType type) : PSPDialog(type) {
Expand Down
102 changes: 76 additions & 26 deletions Core/Dialog/PSPNetconfDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static const float FONT_SCALE = 0.65f;

// Needs testing.
const static int NET_INIT_DELAY_US = 200000;
const static int NET_SHUTDOWN_DELAY_US = 200000;
const static int NET_SHUTDOWN_DELAY_US = 501000;
const static int NET_CONNECT_TIMEOUT = 15000000; // Using 15 secs to match the timeout on Adhoc Server side (SERVER_USER_TIMEOUT)

struct ScanInfos {
Expand Down Expand Up @@ -79,6 +79,7 @@ int PSPNetconfDialog::Init(u32 paramAddr) {
connResult = -1;
scanInfosAddr = 0;
scanStep = 0;
canceling = false;
startTime = (u64)(time_now_d() * 1000000.0);

StartFade(true);
Expand Down Expand Up @@ -244,6 +245,7 @@ int PSPNetconfDialog::Update(int animSpeed) {
// It seems JPCSP doesn't check for NETCONF_STATUS_APNET
if (request.netAction == NETCONF_CONNECT_APNET || request.netAction == NETCONF_STATUS_APNET || request.netAction == NETCONF_CONNECT_APNET_LAST) {
int state = NetApctl_GetState();
bool timedout = (state == PSP_NET_APCTL_STATE_DISCONNECTED && now - startTime > NET_CONNECT_TIMEOUT);

UpdateFade(animSpeed);
StartDraw();
Expand Down Expand Up @@ -285,33 +287,69 @@ int PSPNetconfDialog::Update(int animSpeed) {
DrawBanner();
DrawIndicator();

if (state == PSP_NET_APCTL_STATE_GOT_IP || state == PSP_NET_APCTL_STATE_GETTING_IP) {
DisplayMessage(di->T("ObtainingIP", "Obtaining IP address.\nPlease wait..."), di->T("ConnectionName", "Connection Name"), netApctlInfo.name, di->T("SSID"), netApctlInfo.ssid);
if (!g_Config.bEnableWlan) {
DisplayMessage(di->T("WlanIsOff", "A connection error has occurred.\nThe WIRELESS switch on the PPSSPP system is not turned on."), di->T("ConnectionName", "Connection Name"), netApctlInfo.name, di->T("SSID"), netApctlInfo.ssid);
DisplayButtons(DS_BUTTON_CANCEL, di->T("Back"));
}
else if (timedout) {
// FIXME: Do we need to show error message on timeout?
DisplayMessage(di->T("InternalError", "An internal error has occurred.") + StringFromFormat("\n(%08X)", connResult));
DisplayButtons(DS_BUTTON_CANCEL, di->T("Back"));
}
else if (canceling) {
DisplayMessage(di->T("CancelingPleaseWait", "Canceling.\nPlease wait..."), di->T("ConnectionName", "Connection Name"), netApctlInfo.name, di->T("SSID"), netApctlInfo.ssid);
}
else {
// Skipping the Select Connection screen since we only have 1 fake profile
DisplayMessage(di->T("ConnectingAP", "Connecting to the access point.\nPlease wait..."), di->T("ConnectionName", "Connection Name"), netApctlInfo.name, di->T("SSID"), netApctlInfo.ssid);

if (state == PSP_NET_APCTL_STATE_GOT_IP || state == PSP_NET_APCTL_STATE_GETTING_IP) {
DisplayMessage(di->T("ObtainingIP", "Obtaining IP address.\nPlease wait..."), di->T("ConnectionName", "Connection Name"), netApctlInfo.name, di->T("SSID"), netApctlInfo.ssid);
}
else {
// Skipping the Select Connection screen since we only have 1 fake profile
DisplayMessage(di->T("ConnectingAP", "Connecting to the access point.\nPlease wait..."), di->T("ConnectionName", "Connection Name"), netApctlInfo.name, di->T("SSID"), netApctlInfo.ssid);
DisplayButtons(DS_BUTTON_CANCEL, di->T("Cancel"));
}

// The Netconf dialog stays visible until the network reaches the state PSP_NET_APCTL_STATE_GOT_IP.
if (state == PSP_NET_APCTL_STATE_GOT_IP) {
if (pendingStatus != SCE_UTILITY_STATUS_FINISHED) {
StartFade(false);
ChangeStatus(SCE_UTILITY_STATUS_FINISHED, NET_SHUTDOWN_DELAY_US);
}
}

else if (state == PSP_NET_APCTL_STATE_JOINING) {
// Switch to the next (Obtaining IP) message animation
// TODO: Should be animating the top and bottom lines widening-from-the-middle animation instead of fading-out and in
//StartFade(true);
}

else if (state == PSP_NET_APCTL_STATE_DISCONNECTED) {
// FIXME: Based on JPCSP+official prx, sceNetApctl* calls supposed to runs on SceDialogmainGraphics thread instead of current thread where Update is being called (ie. Main), thus animation smoothness won't be affected by blocking syscalls
// When connecting with infrastructure, simulate a connection using the first network configuration entry.
if (connResult < 0 && (now - startTime > 2000000)) {
connResult = sceNetApctlConnect(1);
}
}
}
DisplayButtons(DS_BUTTON_CANCEL, di->T("Cancel"));

// The Netconf dialog stays visible until the network reaches the state PSP_NET_APCTL_STATE_GOT_IP.
// The Netconf dialog stays visible until the network reaches the state PSP_NET_APCTL_STATE_GOT_IP.
if (state == PSP_NET_APCTL_STATE_GOT_IP) {
// Checking pendingStatus to make sure ChangeStatus not to continously extending the delay ticks on every call for eternity
if (pendingStatus != SCE_UTILITY_STATUS_FINISHED) {
StartFade(false);
ChangeStatus(SCE_UTILITY_STATUS_FINISHED, NET_SHUTDOWN_DELAY_US);
}
}

else if (state == PSP_NET_APCTL_STATE_JOINING) {
// Switch to the next message
StartFade(true);
}

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);
if (!canceling && state != PSP_NET_APCTL_STATE_GOT_IP && state != PSP_NET_APCTL_STATE_GETTING_IP && IsButtonPressed(cancelButtonFlag)) {
if (state != PSP_NET_APCTL_STATE_DISCONNECTED) {
canceling = true;
sceNetApctlDisconnect();
}
StartFade(false);
ChangeStatus(SCE_UTILITY_STATUS_FINISHED, NET_SHUTDOWN_DELAY_US);
request.common.result = SCE_UTILITY_DIALOG_RESULT_ABORT;
}
}

Expand All @@ -320,23 +358,29 @@ int PSPNetconfDialog::Update(int animSpeed) {
else if (request.netAction == NETCONF_CONNECT_ADHOC || request.netAction == NETCONF_CREATE_ADHOC || request.netAction == NETCONF_JOIN_ADHOC) {
int state = NetAdhocctl_GetState();
bool timedout = (state == ADHOCCTL_STATE_DISCONNECTED && now - startTime > NET_CONNECT_TIMEOUT);
std::string channel = std::to_string(g_Config.iWlanAdhocChannel);
if (g_Config.iWlanAdhocChannel == PSP_SYSTEMPARAM_ADHOC_CHANNEL_AUTOMATIC)
channel = "Automatic";

UpdateFade(animSpeed);
StartDraw();
PPGeDrawRect(0, 0, 480, 272, CalcFadedColor(0xC0C8B2AC));
DrawBanner();
DrawIndicator();

if (timedout) {
// FIXME: Do we need to show error message?
if (!g_Config.bEnableWlan) {
DisplayMessage(di->T("WlanIsOff", "A connection error has occurred.\nThe WIRELESS switch on the PPSSPP system is not turned on."));
DisplayButtons(DS_BUTTON_CANCEL, di->T("Back"));
}
else if (timedout) {
// FIXME: Do we need to show error message on timeout?
DisplayMessage(di->T("InternalError", "An internal error has occurred.") + StringFromFormat("\n(%08X)", connResult));
DisplayButtons(DS_BUTTON_CANCEL, di->T("Back"));
}
else if (canceling) {
DisplayMessage(di->T("CancelingPleaseWait", "Canceling.\nPlease wait..."), di->T("Channel:") + std::string(" ") + di->T(channel));
}
else {
std::string channel = std::to_string(g_Config.iWlanAdhocChannel);
if (g_Config.iWlanAdhocChannel == PSP_SYSTEMPARAM_ADHOC_CHANNEL_AUTOMATIC)
channel = "Automatic";

DisplayMessage(di->T("ConnectingPleaseWait", "Connecting.\nPlease wait..."), di->T("Channel:") + std::string(" ") + di->T(channel));

// Only Join mode is showing Cancel button on KHBBS and the button will fade out before the dialog is fading out, probably because it's already connected thus can't be canceled anymore
Expand All @@ -345,6 +389,7 @@ int PSPNetconfDialog::Update(int animSpeed) {

// KHBBS will first enter the arena using NETCONF_CONNECT_ADHOC (auto-create group when not exist yet?), but when the event started the event's creator use NETCONF_CREATE_ADHOC while the joining players use NETCONF_JOIN_ADHOC
if (request.NetconfData.IsValid()) {
// FIXME: Based on JPCSP+official prx, sceNetAdhocctl* calls supposed to runs on SceDialogmainGraphics thread instead of current thread where Update is being called (ie. Main), thus animation smoothness won't be affected by blocking syscalls
if (state == ADHOCCTL_STATE_DISCONNECTED) {
switch (request.netAction)
{
Expand Down Expand Up @@ -441,7 +486,11 @@ int PSPNetconfDialog::Update(int animSpeed) {
scanInfosAddr = 0;
}

if ((request.netAction == NETCONF_JOIN_ADHOC || timedout) && IsButtonPressed(cancelButtonFlag)) {
if ((request.netAction == NETCONF_JOIN_ADHOC || timedout || !g_Config.bEnableWlan) && IsButtonPressed(cancelButtonFlag)) {
if (state != ADHOCCTL_STATE_DISCONNECTED) {
canceling = true;
sceNetAdhocctlDisconnect();
}
StartFade(false);
ChangeStatus(SCE_UTILITY_STATUS_FINISHED, NET_SHUTDOWN_DELAY_US);
request.common.result = SCE_UTILITY_DIALOG_RESULT_ABORT;
Expand All @@ -454,9 +503,6 @@ int PSPNetconfDialog::Update(int animSpeed) {
EndDraw();
}

if (ReadStatus() == SCE_UTILITY_STATUS_FINISHED || pendingStatus == SCE_UTILITY_STATUS_FINISHED)
Memory::Memcpy(requestAddr, &request, request.common.size, "NetConfDialogParam");

return 0;
}

Expand All @@ -469,6 +515,10 @@ int PSPNetconfDialog::Shutdown(bool force) {
ChangeStatusShutdown(NET_SHUTDOWN_DELAY_US);
}

// FIXME: This should probably be done within FinishShutdown? since FinishShutdown is not overrideable, Shutdown is the closes method
if (Memory::IsValidAddress(requestAddr)) // Need to validate first to prevent Invalid address when the game is being Shutdown/Exited to menu
Memory::Memcpy(requestAddr, &request, request.common.size, "NetConfDialogParam");

return 0;
}

Expand Down
1 change: 1 addition & 0 deletions Core/Dialog/PSPNetconfDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class PSPNetconfDialog: public PSPDialog {
u32 requestAddr = 0;
int connResult = -1;
bool hideNotice = false;
bool canceling = false;

int yesnoChoice = 0;
float scrollPos_ = 0.0f;
Expand Down
20 changes: 20 additions & 0 deletions Core/HLE/FunctionWrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,11 @@ template<int func(int, int, int, u32, int)> void WrapI_IIIUI() {
RETURN(retval);
}

template<int func(int, int, int, u32, u32)> void WrapI_IIIUU() {
int retval = func(PARAM(0), PARAM(1), PARAM(2), PARAM(3), PARAM(4));
RETURN(retval);
}

template<int func(int, u32, u32, int, int)> void WrapI_IUUII() {
int retval = func(PARAM(0), PARAM(1), PARAM(2), PARAM(3), PARAM(4));
RETURN(retval);
Expand Down Expand Up @@ -498,6 +503,11 @@ template<int func(const char *, u32)> void WrapI_CU() {
RETURN(retval);
}

template<int func(int, const char*, u32)> void WrapI_ICU() {
int retval = func(PARAM(0), Memory::GetCharPointer(PARAM(1)), PARAM(2));
RETURN(retval);
}

template<int func(const char *, u32, int)> void WrapI_CUI() {
int retval = func(Memory::GetCharPointer(PARAM(0)), PARAM(1), PARAM(2));
RETURN(retval);
Expand Down Expand Up @@ -770,6 +780,16 @@ template<int func(int, u32, u32, u32)> void WrapI_IUUU() {
RETURN(retval);
}

template<int func(int, u32, int, int, u32, u32)> void WrapI_IUIIUU() {
int retval = func(PARAM(0), PARAM(1), PARAM(2), PARAM(3), PARAM(4), PARAM(5));
RETURN(retval);
}

template<int func(int, u32, int, int, u32, int)> void WrapI_IUIIUI() {
int retval = func(PARAM(0), PARAM(1), PARAM(2), PARAM(3), PARAM(4), PARAM(5));
RETURN(retval);
}

template<int func(int, u32, u32)> void WrapI_IUU() {
int retval = func(PARAM(0), PARAM(1), PARAM(2));
RETURN(retval);
Expand Down
3 changes: 3 additions & 0 deletions Core/HLE/HLETables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include "sceNet.h"
#include "sceNetAdhoc.h"
#include "sceNp.h"
#include "sceNp2.h"
#include "sceMpeg.h"
#include "sceOpenPSID.h"
#include "sceP3da.h"
Expand Down Expand Up @@ -307,6 +308,8 @@ void RegisterAllModules() {
Register_sceDdrdb();
Register_mp4msv();
Register_InterruptManagerForKernel();
Register_sceNpMatching2();
Register_sceNetApctl_internal_user();
// add new modules here.
}

30 changes: 23 additions & 7 deletions Core/HLE/proAdhoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@

uint16_t portOffset;
uint32_t minSocketTimeoutUS;
uint32_t fakePoolSize = 0;
SceNetMallocStat netAdhocPoolStat = {};
SceNetAdhocMatchingContext * contexts = NULL;
char* dummyPeekBuf64k = NULL;
int dummyPeekBuf64kSize = 65536;
Expand Down Expand Up @@ -1496,8 +1496,8 @@ int friendFinder(){
SceNetAdhocctlConnectBSSIDPacketS2C* packet = (SceNetAdhocctlConnectBSSIDPacketS2C*)rx;

INFO_LOG(SCENET, "FriendFinder: Incoming OPCODE_CONNECT_BSSID [%s]", mac2str(&packet->mac).c_str());
// Update User BSSID
parameter.bssid.mac_addr = packet->mac; // This packet seems to contains Adhoc Group Creator's BSSID (similar to AP's BSSID) so it shouldn't get mixed up with local MAC address
// Update Group BSSID
parameter.bssid.mac_addr = packet->mac; // This packet seems to contains Adhoc Group Creator's BSSID (similar to AP's BSSID) so it shouldn't get mixed up with local MAC address. Note: On JPCSP + prx files params.bssid is hardcoded to "Jpcsp\0" and doesn't match to any of player's mac

// From JPCSP: Some games have problems when the PSP_ADHOCCTL_EVENT_CONNECTED is sent too quickly after connecting to a network. The connection will be set CONNECTED with a small delay (200ms or 200us?)
// Notify Event Handlers
Expand Down Expand Up @@ -1915,10 +1915,22 @@ bool isPrivateIP(uint32_t ip) {
return false;
}

bool isAPIPA(uint32_t ip) {
return (((uint8_t*)&ip)[0] == 169 && ((uint8_t*)&ip)[1] == 254);
}

bool isLoopbackIP(uint32_t ip) {
return ((uint8_t*)&ip)[0] == 0x7f;
}

bool isMulticastIP(uint32_t ip) {
return ((ip & 0xF0) == 0xE0);
}

bool isBroadcastIP(uint32_t ip, const uint32_t subnetmask) {
return (ip == (ip | (~subnetmask)));
}

void getLocalMac(SceNetEtherAddr * addr){
// Read MAC Address from config
uint8_t mac[ETHER_ADDR_LEN] = {0};
Expand Down Expand Up @@ -1972,14 +1984,18 @@ int getSockMaxSize(int udpsock) {
}

int getSockBufferSize(int sock, int opt) { // opt = SO_RCVBUF/SO_SNDBUF
int n = PSP_ADHOC_PDP_MFS; // 16384;
int n = PSP_ADHOC_PDP_MFS*2; // 16384; // The value might be twice of the value being set using setsockopt
socklen_t m = sizeof(n);
getsockopt(sock, SOL_SOCKET, opt, (char *)&n, &m); // in linux the value is twice of the value being set using setsockopt
return (n/2);
getsockopt(sock, SOL_SOCKET, opt, (char *)&n, &m);
return (n);
}

int setSockBufferSize(int sock, int opt, int size) { // opt = SO_RCVBUF/SO_SNDBUF
int n = size; // 8192; //16384
int n = size; // 8192;
switch (opt) {
case SO_RCVBUF: n = std::max(size, 128); break; // FIXME: The minimum (doubled) value for SO_RCVBUF is 256 ? (2048+MTU+padding on newer OS? TCP_SKB_MIN_TRUESIZE)
case SO_SNDBUF: n = std::max(size, 1024); break; // FIXME: The minimum (doubled) value for SO_SNDBUF is 2048 ? (twice the minimum of SO_RCVBUF on newer OS? TCP_SKB_MIN_TRUESIZE * 2)
}
return setsockopt(sock, SOL_SOCKET, opt, (char *)&n, sizeof(n));
}

Expand Down
Loading

0 comments on commit bfa42b8

Please sign in to comment.