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

[Adhoc] Fix the flickering loop when playing multiplayer on Samurai Dou Portable/Way of the Samurai Portable #14072

Merged
merged 6 commits into from
Feb 7, 2021
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
26 changes: 8 additions & 18 deletions Core/HLE/proAdhoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1139,18 +1139,18 @@ void AfterMatchingMipsCall::SetData(int ContextID, int eventId, u32_le BufAddr)

bool SetMatchingInCallback(SceNetAdhocMatchingContext* context, bool IsInCB) {
if (context == NULL) return false;
context->eventlock->lock(); //peerlock.lock();
peerlock.lock();
context->IsMatchingInCB = IsInCB;
context->eventlock->unlock(); //peerlock.unlock();
peerlock.unlock();
return IsInCB;
}

bool IsMatchingInCallback(SceNetAdhocMatchingContext* context) {
bool inCB = false;
if (context == NULL) return inCB;
context->eventlock->lock(); //peerlock.lock();
peerlock.lock();
inCB = (context->IsMatchingInCB);
context->eventlock->unlock(); //peerlock.unlock();
peerlock.unlock();
return inCB;
}

Expand Down Expand Up @@ -1908,20 +1908,10 @@ u_long getAvailToRecv(int sock, int udpBufferSize) {
return 0;

if (udpBufferSize > 0 && n > 0) {
// Cap number of bytes of full DGRAM message(s?) up to buffer size
static int lastUdpBufSize = 0;
static char* buf = NULL;
if (udpBufferSize > lastUdpBufSize) {
// Reusing temp buffer to prevent causing too many fragmentation due to repeated alloc -> free (was getting out of memory issue)
char *tmp = (char*)realloc(buf, udpBufferSize);
if (tmp) {
buf = tmp;
lastUdpBufSize = udpBufferSize;
}
}
// Does each recv can only received one message?
err = recvfrom(sock, buf, udpBufferSize, MSG_PEEK | MSG_NOSIGNAL | MSG_TRUNC, NULL, NULL);
//free(buf); // Repeated alloc -> free seems to cause too many fragmentation and ended getting out of memory due to too many alloc -> free
// TODO: Cap number of bytes of full DGRAM message(s) up to buffer size
char buf[8];
// Each recv can only received one message, not sure how to read the next pending msg without actually receiving the first one
err = recvfrom(sock, buf, sizeof(buf), MSG_PEEK | MSG_NOSIGNAL | MSG_TRUNC, NULL, NULL);
if (err >= 0)
return (u_long)err;
}
Expand Down
2 changes: 1 addition & 1 deletion Core/HLE/sceNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ u32 Net_Term() {
}

static u32 sceNetTerm() {
WARN_LOG(SCENET, "sceNetTerm()");
WARN_LOG(SCENET, "sceNetTerm() at %08x", currentMIPS->pc);
int retval = Net_Term();

// Give time to make sure everything are cleaned up
Expand Down
36 changes: 22 additions & 14 deletions Core/HLE/sceNetAdhoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ int PollAdhocSocket(SceNetAdhocPollSd* sds, int count, int timeout, int nonblock
int FlushPtpSocket(int socketId);
int NetAdhocGameMode_DeleteMaster();
int NetAdhocctl_ExitGameMode();
int NetAdhoc_PtpConnect(int id, int timeout, int flag, bool allowForcedConnect = true);
int NetAdhocPtp_Connect(int id, int timeout, int flag, bool allowForcedConnect = true);
static int sceNetAdhocPdpSend(int id, const char* mac, u32 port, void* data, int len, int timeout, int flag);
static int sceNetAdhocPdpRecv(int id, void* addr, void* port, void* buf, void* dataLength, u32 timeout, int flag);

Expand Down Expand Up @@ -1808,14 +1808,16 @@ int NetAdhoc_SetSocketAlert(int id, s32_le flag) {
adhocSockets[id - 1]->flags = flg;
adhocSockets[id - 1]->alerted_flags = 0;

return hleDelayResult(0, "set socket alert delay", 1000);
return 0;
}

// Flags seems to be bitmasks of ADHOC_F_ALERT... (need more games to test this)
int sceNetAdhocSetSocketAlert(int id, int flag) {
WARN_LOG_REPORT_ONCE(sceNetAdhocSetSocketAlert, SCENET, "UNTESTED sceNetAdhocSetSocketAlert(%d, %08x) at %08x", id, flag, currentMIPS->pc);

return hleLogDebug(SCENET, NetAdhoc_SetSocketAlert(id, flag), "");
int retval = NetAdhoc_SetSocketAlert(id, flag);
hleDelayResult(retval, "set socket alert delay", 1000);
return hleLogDebug(SCENET, retval, "");
}

int PollAdhocSocket(SceNetAdhocPollSd* sds, int count, int timeout, int nonblock) {
Expand Down Expand Up @@ -2361,7 +2363,7 @@ u32 NetAdhocctl_Disconnect() {
}

// Return Success, some games might ignore returned value and always treat it as success, otherwise repeatedly calling this function
return hleDelayResult(0, "disconnect delay", us);
return 0;
}

// Library uninitialized
Expand All @@ -2384,7 +2386,7 @@ static u32 sceNetAdhocctlDelHandler(u32 handlerID) {

if (adhocctlHandlers.find(handlerID) != adhocctlHandlers.end()) {
adhocctlHandlers.erase(handlerID);
INFO_LOG(SCENET, "sceNetAdhocctlDelHandler(%d)", handlerID);
INFO_LOG(SCENET, "sceNetAdhocctlDelHandler(%d) at %08x", handlerID, currentMIPS->pc);
} else {
WARN_LOG(SCENET, "sceNetAdhocctlDelHandler(%d): Invalid Handler ID", handlerID);
}
Expand All @@ -2395,6 +2397,7 @@ static u32 sceNetAdhocctlDelHandler(u32 handlerID) {
int NetAdhocctl_Term() {
if (netAdhocctlInited) {
if (adhocctlState != ADHOCCTL_STATE_DISCONNECTED) {
// Note: This might block current thread if the first attempt to send OPCODE_DISCONNECT to AdhocServer failed with EAGAIN error
if (netAdhocGameModeEntered)
NetAdhocctl_ExitGameMode();
else
Expand All @@ -2407,6 +2410,8 @@ int NetAdhocctl_Term() {
friendFinderThread.join();
}

// TODO: May need to block current thread to make sure all Adhocctl callbacks have been fully executed before terminating Adhoc PSPThread (ie. threadAdhocID).

// Clear GameMode resources
NetAdhocGameMode_DeleteMaster();
deleteAllGMB();
Expand Down Expand Up @@ -2441,7 +2446,7 @@ int NetAdhocctl_Term() {

int sceNetAdhocctlTerm() {
// WLAN might be disabled in the middle of successfull multiplayer, but we still need to cleanup right?
INFO_LOG(SCENET, "sceNetAdhocctlTerm()");
INFO_LOG(SCENET, "sceNetAdhocctlTerm() at %08x", currentMIPS->pc);

//if (netAdhocMatchingInited) NetAdhocMatching_Term();
int retval = NetAdhocctl_Term();
Expand Down Expand Up @@ -2659,7 +2664,7 @@ int NetAdhocctl_Create(const char* groupName) {
hleEatMicro(us);
// Return Success
// FIXME: When tested using JPCSP + official prx files it seems sceNetAdhocctlCreate switching to a different thread for at least 100ms after returning success and before executing the next line.
return hleDelayResult(0, "create/connect/join delay", adhocEventPollDelay);
return 0;
}

// Connected State
Expand Down Expand Up @@ -3210,7 +3215,7 @@ static int sceNetAdhocPtpOpen(const char *srcmac, int sport, const char *dstmac,
changeBlockingMode(tcpsocket, 1);

// Initiate PtpConnect (ie. The Warrior seems to try to PtpSend right after PtpOpen without trying to PtpConnect first)
NetAdhoc_PtpConnect(i + 1, rexmt_int, 1, false);
NetAdhocPtp_Connect(i + 1, rexmt_int, 1, false);

// Return PTP Socket Pointer
return hleLogDebug(SCENET, i + 1, "success");
Expand Down Expand Up @@ -3459,7 +3464,7 @@ static int sceNetAdhocPtpAccept(int id, u32 peerMacAddrPtr, u32 peerPortPtr, int
return hleLogSuccessVerboseI(SCENET, ERROR_NET_ADHOC_NOT_INITIALIZED, "not initialized");
}

int NetAdhoc_PtpConnect(int id, int timeout, int flag, bool allowForcedConnect) {
int NetAdhocPtp_Connect(int id, int timeout, int flag, bool allowForcedConnect) {
// Library is initialized
if (netAdhocInited)
{
Expand Down Expand Up @@ -3578,7 +3583,7 @@ static int sceNetAdhocPtpConnect(int id, int timeout, int flag) {
return -1;
}

return NetAdhoc_PtpConnect(id, timeout, flag);
return NetAdhocPtp_Connect(id, timeout, flag);
}

int NetAdhocPtp_Close(int id, int unknown) {
Expand Down Expand Up @@ -4693,8 +4698,7 @@ int NetAdhocMatching_Start(int matchingId, int evthPri, int evthPartitionId, int
// Multithreading Unlock
peerlock.unlock();

// Give a little time to make sure matching Threads are ready before the game use the next sceNet functions, should've checked for status instead of guessing the time?
return hleDelayResult(0, "give some time", adhocMatchingEventDelay);
return 0;
}

#define KERNEL_PARTITION_ID 1
Expand All @@ -4706,7 +4710,9 @@ static int sceNetAdhocMatchingStart(int matchingId, int evthPri, int evthStack,
if (!g_Config.bEnableWlan)
return -1;

return NetAdhocMatching_Start(matchingId, evthPri, USER_PARTITION_ID, evthStack, inthPri, USER_PARTITION_ID, inthStack, optLen, optDataAddr);
int retval = NetAdhocMatching_Start(matchingId, evthPri, USER_PARTITION_ID, evthStack, inthPri, USER_PARTITION_ID, inthStack, optLen, optDataAddr);
// Give a little time to make sure matching Threads are ready before the game use the next sceNet functions, should've checked for status instead of guessing the time?
return hleDelayResult(retval, "give some time", adhocMatchingEventDelay);
}

// With params for Partition ID for the event & input handler stack
Expand All @@ -4715,7 +4721,9 @@ static int sceNetAdhocMatchingStart2(int matchingId, int evthPri, int evthPartit
if (!g_Config.bEnableWlan)
return -1;

return NetAdhocMatching_Start(matchingId, evthPri, evthPartitionId, evthStack, inthPri, inthPartitionId, inthStack, optLen, optDataAddr);
int retval = NetAdhocMatching_Start(matchingId, evthPri, evthPartitionId, evthStack, inthPri, inthPartitionId, inthStack, optLen, optDataAddr);
// Give a little time to make sure matching Threads are ready before the game use the next sceNet functions, should've checked for status instead of guessing the time?
return hleDelayResult(retval, "give some time", adhocMatchingEventDelay);
}


Expand Down