diff --git a/Core/HLE/proAdhoc.cpp b/Core/HLE/proAdhoc.cpp index 4f952f6432..34fafd6f4a 100644 --- a/Core/HLE/proAdhoc.cpp +++ b/Core/HLE/proAdhoc.cpp @@ -123,12 +123,15 @@ bool isPDPPortInUse(uint16_t port) { return false; } -bool isPTPPortInUse(uint16_t port) { +bool isPTPPortInUse(uint16_t port, bool forListen) { // Iterate Sockets for (int i = 0; i < MAX_SOCKET; i++) { auto sock = adhocSockets[i]; if (sock != NULL && sock->type == SOCK_PTP) - if (sock->data.ptp.lport == port) + // It's allowed to Listen and Open the same PTP port, But it's not allowed to Listen or Open the same PTP port twice. + if (sock->data.ptp.lport == port && + ((forListen && sock->data.ptp.state == ADHOC_PTP_STATE_LISTEN) || + (!forListen && sock->data.ptp.state != ADHOC_PTP_STATE_LISTEN))) return true; } // Unused Port diff --git a/Core/HLE/proAdhoc.h b/Core/HLE/proAdhoc.h index 0fecf3dddd..5f434d981b 100644 --- a/Core/HLE/proAdhoc.h +++ b/Core/HLE/proAdhoc.h @@ -942,12 +942,14 @@ bool isLocalMAC(const SceNetEtherAddr * addr); bool isPDPPortInUse(uint16_t port); /** - * Check whether PTP Port is in use or not + * Check whether PTP Port is in use or not (only sockets with non-Listening state will be considered as in use) * @param port To-be-checked Port Number + * @param forListen to check for listening or non-listening port * @return 1 if in use or... 0 */ -bool isPTPPortInUse(uint16_t port); +bool isPTPPortInUse(uint16_t port, bool forListen); +// Convert MAC address to string std::string mac2str(SceNetEtherAddr* mac); /* diff --git a/Core/HLE/sceNetAdhoc.cpp b/Core/HLE/sceNetAdhoc.cpp index 821bca74b8..fdb7b7ac95 100644 --- a/Core/HLE/sceNetAdhoc.cpp +++ b/Core/HLE/sceNetAdhoc.cpp @@ -2863,6 +2863,10 @@ static int sceNetAdhocPtpOpen(const char *srcmac, int sport, const char *dstmac, } // Valid Addresses if (saddr != NULL && isLocalMAC(saddr) && daddr != NULL && !isBroadcastMAC(daddr)) { + // Dissidia 012 will try to reOpen the port without Closing the old one first when PtpConnect failed to try again. + if (isPTPPortInUse(sport, false)) + return hleLogDebug(SCENET, ERROR_NET_ADHOC_PORT_IN_USE, "port in use"); + // Random Port required if (sport == 0) { isClient = true; @@ -2957,7 +2961,7 @@ static int sceNetAdhocPtpOpen(const char *srcmac, int sport, const char *dstmac, changeBlockingMode(tcpsocket, 1); // Return PTP Socket Pointer - return i + 1; + return hleLogDebug(SCENET, i + 1, "success"); } // Free Memory @@ -3368,7 +3372,7 @@ static int sceNetAdhocPtpClose(int id, int unknown) { static int sceNetAdhocPtpListen(const char *srcmac, int sport, int bufsize, int rexmt_int, int rexmt_cnt, int backlog, int flag) { INFO_LOG(SCENET, "sceNetAdhocPtpListen(%s, %d, %d, %d, %d, %d, %d) at %08x", mac2str((SceNetEtherAddr*)srcmac).c_str(), sport,bufsize,rexmt_int,rexmt_cnt,backlog,flag, currentMIPS->pc); if (!g_Config.bEnableWlan) { - return 0; + return -1; } // Library is initialized SceNetEtherAddr * saddr = (SceNetEtherAddr *)srcmac; @@ -3379,6 +3383,10 @@ static int sceNetAdhocPtpListen(const char *srcmac, int sport, int bufsize, int } // Valid Address if (saddr != NULL && isLocalMAC(saddr)) { + // It's allowed to Listen and Open the same PTP port, But it's not allowed to Listen or Open the same PTP port twice. + if (isPTPPortInUse(sport, true)) + return hleLogDebug(SCENET, ERROR_NET_ADHOC_PORT_IN_USE, "port in use"); + // Random Port required if (sport == 0) { //sport 0 should be shifted back to 0 when using offset Phantasy Star Portable 2 use this @@ -3474,7 +3482,7 @@ static int sceNetAdhocPtpListen(const char *srcmac, int sport, int bufsize, int changeBlockingMode(tcpsocket, 1); // Return PTP Socket Pointer - return i + 1; + return hleLogDebug(SCENET, i + 1, "success"); } // Free Memory @@ -3496,23 +3504,23 @@ static int sceNetAdhocPtpListen(const char *srcmac, int sport, int bufsize, int closesocket(tcpsocket); // Port not available (exclusively in use?) - return ERROR_NET_ADHOC_INVALID_PORT; //ERROR_NET_ADHOC_PORT_IN_USE; // ERROR_NET_ADHOC_PORT_NOT_AVAIL; + return hleLogDebug(SCENET, ERROR_NET_ADHOC_PORT_NOT_AVAIL, "port not available"); //ERROR_NET_ADHOC_PORT_IN_USE; // ERROR_NET_ADHOC_INVALID_PORT; } // Socket not available - return ERROR_NET_ADHOC_SOCKET_ID_NOT_AVAIL; + return hleLogDebug(SCENET, ERROR_NET_ADHOC_SOCKET_ID_NOT_AVAIL, "socket id not available"); } // Invalid Arguments - return ERROR_NET_ADHOC_INVALID_ARG; + return hleLogDebug(SCENET, ERROR_NET_ADHOC_INVALID_ARG, "invalid arg"); } // Invalid Addresses - return ERROR_NET_ADHOC_INVALID_ADDR; + return hleLogDebug(SCENET, ERROR_NET_ADHOC_INVALID_ADDR, "invalid address"); } // Library is uninitialized - return ERROR_NET_ADHOC_NOT_INITIALIZED; + return hleLogDebug(SCENET, ERROR_NET_ADHOC_NOT_INITIALIZED, "adhoc not initialized"); } /**