From 9f4f0a4e6a6733217a47cc58b6d284b6d5abdda3 Mon Sep 17 00:00:00 2001 From: Nemoumbra Date: Sat, 4 Jan 2025 19:55:50 +0300 Subject: [PATCH 1/8] Brace codestyle fix --- Core/HLE/sceNetAdhocMatching.cpp | 432 +++++++++++-------------------- 1 file changed, 144 insertions(+), 288 deletions(-) diff --git a/Core/HLE/sceNetAdhocMatching.cpp b/Core/HLE/sceNetAdhocMatching.cpp index 58a02c5abc..73172f0c55 100644 --- a/Core/HLE/sceNetAdhocMatching.cpp +++ b/Core/HLE/sceNetAdhocMatching.cpp @@ -124,8 +124,7 @@ void deleteMatchingEvents(const int matchingId = -1) { * Broadcast Ping Message to other Matching Users * @param context Matching Context Pointer */ -void broadcastPingMessage(SceNetAdhocMatchingContext * context) -{ +void broadcastPingMessage(SceNetAdhocMatchingContext * context) { // Ping Opcode uint8_t ping = PSP_ADHOC_MATCHING_PACKET_PING; @@ -154,8 +153,7 @@ void broadcastPingMessage(SceNetAdhocMatchingContext * context) * Broadcast Hello Message to other Matching Users * @param context Matching Context Pointer */ -void broadcastHelloMessage(SceNetAdhocMatchingContext * context) -{ +void broadcastHelloMessage(SceNetAdhocMatchingContext * context) { static uint8_t * hello = NULL; static int32_t len = -5; @@ -169,8 +167,7 @@ void broadcastHelloMessage(SceNetAdhocMatchingContext * context) } // Allocated Hello Message Buffer - if (hello != NULL) - { + if (hello != NULL) { // Hello Opcode hello[0] = PSP_ADHOC_MATCHING_PACKET_HELLO; @@ -219,8 +216,7 @@ void broadcastHelloMessage(SceNetAdhocMatchingContext * context) * @param optlen Optional Data Length * @param opt Optional Data */ -void sendAcceptPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac, int optlen, void * opt) -{ +void sendAcceptPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac, int optlen, void * opt) { // Lock the peer std::lock_guard peer_guard(peerlock); @@ -228,8 +224,7 @@ void sendAcceptPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * ma SceNetAdhocMatchingMemberInternal * peer = findPeer(context, mac); // Found Peer - if (peer != NULL && (peer->state == PSP_ADHOC_MATCHING_PEER_CHILD || peer->state == PSP_ADHOC_MATCHING_PEER_P2P)) - { + if (peer != NULL && (peer->state == PSP_ADHOC_MATCHING_PEER_CHILD || peer->state == PSP_ADHOC_MATCHING_PEER_P2P)) { // Required Sibling Buffer uint32_t siblingbuflen = 0; @@ -243,8 +238,7 @@ void sendAcceptPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * ma uint8_t * accept = (uint8_t *)malloc(9LL + optlen + siblingbuflen); // Allocated Accept Message Buffer - if (accept != NULL) - { + if (accept != NULL) { // Accept Opcode accept[0] = PSP_ADHOC_MATCHING_PACKET_ACCEPT; @@ -258,8 +252,7 @@ void sendAcceptPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * ma if (optlen > 0) memcpy(accept + 9, opt, optlen); // Parent Mode Extra Data required - if (context->mode == PSP_ADHOC_MATCHING_MODE_PARENT && siblingcount > 0) - { + if (context->mode == PSP_ADHOC_MATCHING_MODE_PARENT && siblingcount > 0) { // Create MAC Array Pointer uint8_t * siblingmacs = (uint8_t *)(accept + 9 + optlen); @@ -268,14 +261,12 @@ void sendAcceptPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * ma // Iterate Peer List SceNetAdhocMatchingMemberInternal * item = context->peerlist; - for (; item != NULL; item = item->next) - { + for (; item != NULL; item = item->next) { // Ignore Target if (item == peer) continue; // Copy Child MAC - if (item->state == PSP_ADHOC_MATCHING_PEER_CHILD) - { + if (item->state == PSP_ADHOC_MATCHING_PEER_CHILD) { // Clone MAC the stupid memcpy way to shut up PSP CPU memcpy(siblingmacs + sizeof(SceNetEtherAddr) * i++, &item->mac, sizeof(SceNetEtherAddr)); } @@ -303,8 +294,7 @@ void sendAcceptPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * ma * @param optlen Optional Data Length * @param opt Optional Data */ -void sendJoinPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac, int optlen, void * opt) -{ +void sendJoinPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac, int optlen, void * opt) { // Lock the peer std::lock_guard peer_guard(peerlock); @@ -312,14 +302,12 @@ void sendJoinPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac, SceNetAdhocMatchingMemberInternal * peer = findPeer(context, mac); // Valid Peer - if (peer != NULL && peer->state == PSP_ADHOC_MATCHING_PEER_OUTGOING_REQUEST) - { + if (peer != NULL && peer->state == PSP_ADHOC_MATCHING_PEER_OUTGOING_REQUEST) { // Allocate Join Message Buffer uint8_t * join = (uint8_t *)malloc(5LL + optlen); // Allocated Join Message Buffer - if (join != NULL) - { + if (join != NULL) { // Join Opcode join[0] = PSP_ADHOC_MATCHING_PACKET_JOIN; @@ -347,8 +335,7 @@ void sendJoinPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac, * @param optlen Optional Data Length * @param opt Optional Data */ -void sendCancelPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac, int optlen, void * opt) -{ +void sendCancelPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac, int optlen, void * opt) { // Lock the peer std::lock_guard peer_guard(peerlock); @@ -356,8 +343,7 @@ void sendCancelPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * ma uint8_t * cancel = (uint8_t *)malloc(5LL + optlen); // Allocated Cancel Message Buffer - if (cancel != NULL) - { + if (cancel != NULL) { // Cancel Opcode cancel[0] = PSP_ADHOC_MATCHING_PACKET_CANCEL; @@ -380,11 +366,9 @@ void sendCancelPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * ma SceNetAdhocMatchingMemberInternal * peer = findPeer(context, mac); // Found Peer - if (peer != NULL) - { + if (peer != NULL) { // Child Mode Fallback - Delete All - if (context->mode == PSP_ADHOC_MATCHING_MODE_CHILD) - { + if (context->mode == PSP_ADHOC_MATCHING_MODE_CHILD) { // Delete Peer List clearPeerList(context); } @@ -404,8 +388,7 @@ void sendCancelPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * ma * @param datalen Data Length * @param data Data */ -void sendBulkDataPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac, int datalen, void * data) -{ +void sendBulkDataPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac, int datalen, void * data) { // Lock the peer std::lock_guard peer_guard(peerlock); @@ -413,8 +396,7 @@ void sendBulkDataPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * SceNetAdhocMatchingMemberInternal * peer = findPeer(context, mac); // Valid Peer (rest is already checked in send.c) - if (peer != NULL) - { + if (peer != NULL) { // Don't send if it's aborted //if (peer->sending == 0) return; @@ -422,8 +404,7 @@ void sendBulkDataPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * uint8_t * send = (uint8_t *)malloc(5LL + datalen); // Allocated Send Message Buffer - if (send != NULL) - { + if (send != NULL) { // Send Opcode send[0] = PSP_ADHOC_MATCHING_PACKET_BULK; @@ -455,8 +436,7 @@ void sendBulkDataPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * * @param context Matching Context Pointer * @param mac New Child's MAC */ -void sendBirthPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac) -{ +void sendBirthPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac) { // Lock the peer std::lock_guard peer_guard(peerlock); @@ -464,8 +444,7 @@ void sendBirthPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac SceNetAdhocMatchingMemberInternal * newborn = findPeer(context, mac); // Found Newborn Child - if (newborn != NULL) - { + if (newborn != NULL) { // Packet Buffer uint8_t packet[7]; @@ -477,14 +456,12 @@ void sendBirthPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac // Iterate Peers SceNetAdhocMatchingMemberInternal * peer = context->peerlist; - for (; peer != NULL; peer = peer->next) - { + for (; peer != NULL; peer = peer->next) { // Skip Newborn Child if (peer == newborn) continue; // Send only to children - if (peer->state == PSP_ADHOC_MATCHING_PEER_CHILD) - { + if (peer->state == PSP_ADHOC_MATCHING_PEER_CHILD) { // Send Packet context->socketlock->lock(); int sent = sceNetAdhocPdpSend(context->socket, (const char*)&peer->mac, (*context->peerPort)[peer->mac], packet, sizeof(packet), 0, ADHOC_F_NONBLOCK); @@ -505,8 +482,7 @@ void sendBirthPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac * @param context Matching Context Pointer * @param mac Dead Child's MAC */ -void sendDeathPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac) -{ +void sendDeathPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac) { // Lock the peer std::lock_guard peer_guard(peerlock); @@ -514,8 +490,7 @@ void sendDeathPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac SceNetAdhocMatchingMemberInternal * deadkid = findPeer(context, mac); // Found abandoned Child - if (deadkid != NULL) - { + if (deadkid != NULL) { // Packet Buffer uint8_t packet[7]; @@ -524,8 +499,7 @@ void sendDeathPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac // Iterate Peers SceNetAdhocMatchingMemberInternal * peer = context->peerlist; - for (; peer != NULL; peer = peer->next) - { + for (; peer != NULL; peer = peer->next) { // Skip dead Child? Or May be we should also tells the disconnected Child, that they have been disconnected from the Host (in the case they were disconnected because they went to PPSSPP settings for too long) if (peer == deadkid) { // Set Opcode @@ -538,8 +512,7 @@ void sendDeathPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac } else // Send to other children - if (peer->state == PSP_ADHOC_MATCHING_PEER_CHILD) - { + if (peer->state == PSP_ADHOC_MATCHING_PEER_CHILD) { // Set Opcode packet[0] = PSP_ADHOC_MATCHING_PACKET_DEATH; @@ -559,18 +532,15 @@ void sendDeathPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac * Tell Established Peers that we're shutting the Networking Layer down * @param context Matching Context Pointer */ -void sendByePacket(SceNetAdhocMatchingContext * context) -{ +void sendByePacket(SceNetAdhocMatchingContext * context) { // Lock the peer std::lock_guard peer_guard(peerlock); // Iterate Peers SceNetAdhocMatchingMemberInternal * peer = context->peerlist; - for (; peer != NULL; peer = peer->next) - { + for (; peer != NULL; peer = peer->next) { // Peer of Interest - if (peer->state == PSP_ADHOC_MATCHING_PEER_PARENT || peer->state == PSP_ADHOC_MATCHING_PEER_CHILD || peer->state == PSP_ADHOC_MATCHING_PEER_P2P || peer->state == PSP_ADHOC_MATCHING_PEER_CANCEL_IN_PROGRESS) - { + if (peer->state == PSP_ADHOC_MATCHING_PEER_PARENT || peer->state == PSP_ADHOC_MATCHING_PEER_CHILD || peer->state == PSP_ADHOC_MATCHING_PEER_P2P || peer->state == PSP_ADHOC_MATCHING_PEER_CANCEL_IN_PROGRESS) { // Bye Opcode uint8_t opcode = PSP_ADHOC_MATCHING_PACKET_BYE; @@ -587,14 +557,12 @@ void sendByePacket(SceNetAdhocMatchingContext * context) * @param context Matching Context Pointer * @param sendermac Packet Sender MAC */ -void actOnPingPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sendermac) -{ +void actOnPingPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sendermac) { // Find Peer SceNetAdhocMatchingMemberInternal * peer = findPeer(context, sendermac); // Found Peer - if (peer != NULL) - { + if (peer != NULL) { // Update Receive Timer peer->lastping = CoreTiming::GetGlobalTimeUsScaled(); //time_now_d()*1000000.0; } @@ -606,20 +574,16 @@ void actOnPingPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sen * @param sendermac Packet Sender MAC * @param length Packet Length */ -void actOnHelloPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sendermac, int32_t length) -{ +void actOnHelloPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sendermac, int32_t length) { // Interested in Hello Data - if ((context->mode == PSP_ADHOC_MATCHING_MODE_CHILD && findParent(context) == NULL) || (context->mode == PSP_ADHOC_MATCHING_MODE_P2P && findP2P(context) == NULL)) - { + if ((context->mode == PSP_ADHOC_MATCHING_MODE_CHILD && findParent(context) == NULL) || (context->mode == PSP_ADHOC_MATCHING_MODE_P2P && findP2P(context) == NULL)) { // Complete Packet Header available - if (length >= 5) - { + if (length >= 5) { // Extract Optional Data Length int optlen = 0; memcpy(&optlen, context->rxbuf + 1, sizeof(optlen)); // Complete Valid Packet available - if (optlen >= 0 && length >= (5 + optlen)) - { + if (optlen >= 0 && length >= (5 + optlen)) { // Set Default Null Data void * opt = NULL; @@ -630,14 +594,12 @@ void actOnHelloPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * se SceNetAdhocMatchingMemberInternal * peer = findPeer(context, sendermac); // Peer not found - if (peer == NULL) - { + if (peer == NULL) { // Allocate Memory peer = (SceNetAdhocMatchingMemberInternal *)malloc(sizeof(SceNetAdhocMatchingMemberInternal)); // Allocated Memory - if (peer != NULL) - { + if (peer != NULL) { // Clear Memory memset(peer, 0, sizeof(SceNetAdhocMatchingMemberInternal)); @@ -659,8 +621,7 @@ void actOnHelloPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * se } // Peer available now - if (peer != NULL && peer->state != PSP_ADHOC_MATCHING_PEER_OUTGOING_REQUEST && peer->state != PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST) - { + if (peer != NULL && peer->state != PSP_ADHOC_MATCHING_PEER_OUTGOING_REQUEST && peer->state != PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST) { std::string hellohex; DataToHexString(10, 0, (u8*)opt, optlen, &hellohex); DEBUG_LOG(Log::sceNet, "HELLO Dump (%d bytes):\n%s", optlen, hellohex.c_str()); @@ -679,23 +640,18 @@ void actOnHelloPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * se * @param sendermac Packet Sender MAC * @param length Packet Length */ -void actOnJoinPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sendermac, int32_t length) -{ +void actOnJoinPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sendermac, int32_t length) { // Not a child mode context - if (context->mode != PSP_ADHOC_MATCHING_MODE_CHILD) - { + if (context->mode != PSP_ADHOC_MATCHING_MODE_CHILD) { // We still got a unoccupied slot in our room (Parent / P2P) - if ((context->mode == PSP_ADHOC_MATCHING_MODE_PARENT && countChildren(context) < (context->maxpeers - 1)) || (context->mode == PSP_ADHOC_MATCHING_MODE_P2P && findP2P(context) == NULL)) - { + if ((context->mode == PSP_ADHOC_MATCHING_MODE_PARENT && countChildren(context) < (context->maxpeers - 1)) || (context->mode == PSP_ADHOC_MATCHING_MODE_P2P && findP2P(context) == NULL)) { // Complete Packet Header available - if (length >= 5) - { + if (length >= 5) { // Extract Optional Data Length int optlen = 0; memcpy(&optlen, context->rxbuf + 1, sizeof(optlen)); // Complete Valid Packet available - if (optlen >= 0 && length >= (5 + optlen)) - { + if (optlen >= 0 && length >= (5 + optlen)) { // Set Default Null Data void * opt = NULL; @@ -714,14 +670,12 @@ void actOnJoinPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sen } // New Peer - if (peer == NULL) - { + if (peer == NULL) { // Allocate Memory peer = (SceNetAdhocMatchingMemberInternal *)malloc(sizeof(SceNetAdhocMatchingMemberInternal)); // Allocated Memory - if (peer != NULL) - { + if (peer != NULL) { // Clear Memory memset(peer, 0, sizeof(SceNetAdhocMatchingMemberInternal)); @@ -749,8 +703,7 @@ void actOnJoinPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sen } // Existing Peer (this case is only reachable for P2P mode) - else - { + else { // Set Peer State peer->state = PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST; @@ -778,17 +731,13 @@ void actOnJoinPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sen * @param sendermac Packet Sender MAC * @param length Packet Length */ -void actOnAcceptPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sendermac, uint32_t length) -{ +void actOnAcceptPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sendermac, uint32_t length) { // Not a parent context - if (context->mode != PSP_ADHOC_MATCHING_MODE_PARENT) - { + if (context->mode != PSP_ADHOC_MATCHING_MODE_PARENT) { // Don't have a master yet - if ((context->mode == PSP_ADHOC_MATCHING_MODE_CHILD && findParent(context) == NULL) || (context->mode == PSP_ADHOC_MATCHING_MODE_P2P && findP2P(context) == NULL)) - { + if ((context->mode == PSP_ADHOC_MATCHING_MODE_CHILD && findParent(context) == NULL) || (context->mode == PSP_ADHOC_MATCHING_MODE_P2P && findP2P(context) == NULL)) { // Complete Packet Header available - if (length >= 9) - { + if (length >= 9) { // Extract Optional Data Length int optlen = 0; memcpy(&optlen, context->rxbuf + 1, sizeof(optlen)); @@ -796,8 +745,7 @@ void actOnAcceptPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * s int siblingcount = 0; memcpy(&siblingcount, context->rxbuf + 5, sizeof(siblingcount)); // Complete Valid Packet available - if (optlen >= 0 && length >= (9LL + optlen + static_cast(sizeof(SceNetEtherAddr)) * siblingcount)) - { + if (optlen >= 0 && length >= (9LL + optlen + static_cast(sizeof(SceNetEtherAddr)) * siblingcount)) { // Set Default Null Data void * opt = NULL; @@ -814,14 +762,12 @@ void actOnAcceptPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * s SceNetAdhocMatchingMemberInternal * request = findOutgoingRequest(context); // We are waiting for a answer to our request... - if (request != NULL) - { + if (request != NULL) { // Find Peer SceNetAdhocMatchingMemberInternal * peer = findPeer(context, sendermac); // It's the answer we wanted! - if (request == peer) - { + if (request == peer) { // Change Peer State peer->state = (context->mode == PSP_ADHOC_MATCHING_MODE_CHILD) ? (PSP_ADHOC_MATCHING_PEER_PARENT) : (PSP_ADHOC_MATCHING_PEER_P2P); @@ -859,23 +805,19 @@ void actOnAcceptPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * s * @param sendermac Packet Sender MAC * @param length Packet Length */ -void actOnCancelPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sendermac, int32_t length) -{ +void actOnCancelPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sendermac, int32_t length) { // Find Peer SceNetAdhocMatchingMemberInternal * peer = findPeer(context, sendermac); // Interest Condition fulfilled - if (peer != NULL) - { + if (peer != NULL) { // Complete Packet Header available - if (length >= 5) - { + if (length >= 5) { // Extract Optional Data Length int optlen = 0; memcpy(&optlen, context->rxbuf + 1, sizeof(optlen)); // Complete Valid Packet available - if (optlen >= 0 && length >= (5 + optlen)) - { + if (optlen >= 0 && length >= (5 + optlen)) { // Set Default Null Data void * opt = NULL; @@ -886,14 +828,12 @@ void actOnCancelPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * s SceNetAdhocMatchingMemberInternal* request = findOutgoingRequest(context); // Child Mode - if (context->mode == PSP_ADHOC_MATCHING_MODE_CHILD) - { + if (context->mode == PSP_ADHOC_MATCHING_MODE_CHILD) { // Get Parent SceNetAdhocMatchingMemberInternal* parent = findParent(context); // Join Request denied - if (request == peer) - { + if (request == peer) { // Spawn Deny Event spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_DENY, sendermac, optlen, opt); @@ -903,15 +843,12 @@ void actOnCancelPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * s } // Kicked from Room - else if (parent == peer) - { + else if (parent == peer) { // Iterate Peers SceNetAdhocMatchingMemberInternal * item = context->peerlist; - for (; item != NULL; item = item->next) - { + for (; item != NULL; item = item->next) { // Established Peer - if (item->state == PSP_ADHOC_MATCHING_PEER_CHILD || item->state == PSP_ADHOC_MATCHING_PEER_PARENT) - { + if (item->state == PSP_ADHOC_MATCHING_PEER_CHILD || item->state == PSP_ADHOC_MATCHING_PEER_PARENT) { // Spawn Leave / Kick Event spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_LEAVE, &item->mac, optlen, opt); } @@ -923,11 +860,9 @@ void actOnCancelPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * s } // Parent Mode - else if (context->mode == PSP_ADHOC_MATCHING_MODE_PARENT) - { + else if (context->mode == PSP_ADHOC_MATCHING_MODE_PARENT) { // Cancel Join Request - if (peer->state == PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST) - { + if (peer->state == PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST) { // Spawn Request Cancel Event spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_CANCEL, sendermac, optlen, opt); @@ -937,8 +872,7 @@ void actOnCancelPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * s } // Leave Room - else if (peer->state == PSP_ADHOC_MATCHING_PEER_CHILD) - { + else if (peer->state == PSP_ADHOC_MATCHING_PEER_CHILD) { // Spawn Leave / Kick Event spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_LEAVE, sendermac, optlen, opt); @@ -949,14 +883,12 @@ void actOnCancelPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * s } // P2P Mode - else - { + else { // Get P2P Partner SceNetAdhocMatchingMemberInternal* p2p = findP2P(context); // Join Request denied - if (request == peer) - { + if (request == peer) { // Spawn Deny Event spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_DENY, sendermac, optlen, opt); @@ -966,8 +898,7 @@ void actOnCancelPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * s } // Kicked from Room - else if (p2p == peer) - { + else if (p2p == peer) { // Spawn Leave / Kick Event spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_LEAVE, sendermac, optlen, opt); @@ -977,8 +908,7 @@ void actOnCancelPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * s } // Cancel Join Request - else if (peer->state == PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST) - { + else if (peer->state == PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST) { // Spawn Request Cancel Event spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_CANCEL, sendermac, optlen, opt); @@ -998,8 +928,7 @@ void actOnCancelPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * s * @param sendermac Packet Sender MAC * @param length Packet Length */ -void actOnBulkDataPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sendermac, int32_t length) -{ +void actOnBulkDataPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sendermac, int32_t length) { // Find Peer SceNetAdhocMatchingMemberInternal * peer = findPeer(context, sendermac); @@ -1010,14 +939,12 @@ void actOnBulkDataPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * (context->mode == PSP_ADHOC_MATCHING_MODE_P2P && peer->state == PSP_ADHOC_MATCHING_PEER_P2P))) { // Complete Packet Header available - if (length > 5) - { + if (length > 5) { // Extract Data Length int datalen = 0; memcpy(&datalen, context->rxbuf + 1, sizeof(datalen)); // Complete Valid Packet available - if (datalen > 0 && length >= (5 + datalen)) - { + if (datalen > 0 && length >= (5 + datalen)) { // Extract Data void * data = context->rxbuf + 5; @@ -1034,17 +961,14 @@ void actOnBulkDataPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * * @param sendermac Packet Sender MAC * @param length Packet Length */ -void actOnBirthPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sendermac, uint32_t length) -{ +void actOnBirthPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sendermac, uint32_t length) { // Find Peer SceNetAdhocMatchingMemberInternal * peer = findPeer(context, sendermac); // Valid Circumstances - if (peer != NULL && context->mode == PSP_ADHOC_MATCHING_MODE_CHILD && peer == findParent(context)) - { + if (peer != NULL && context->mode == PSP_ADHOC_MATCHING_MODE_CHILD && peer == findParent(context)) { // Complete Packet available - if (length >= (1 + sizeof(SceNetEtherAddr))) - { + if (length >= (1 + sizeof(SceNetEtherAddr))) { // Extract Child MAC SceNetEtherAddr mac; memcpy(&mac, context->rxbuf + 1, sizeof(SceNetEtherAddr)); @@ -1053,8 +977,7 @@ void actOnBirthPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * se SceNetAdhocMatchingMemberInternal * sibling = (SceNetAdhocMatchingMemberInternal *)malloc(sizeof(SceNetAdhocMatchingMemberInternal)); // Allocated Memory - if (sibling != NULL) - { + if (sibling != NULL) { // Clear Memory memset(sibling, 0, sizeof(SceNetAdhocMatchingMemberInternal)); @@ -1088,17 +1011,14 @@ void actOnBirthPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * se * @param sendermac Packet Sender MAC * @param length Packet Length */ -void actOnDeathPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sendermac, uint32_t length) -{ +void actOnDeathPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sendermac, uint32_t length) { // Find Peer SceNetAdhocMatchingMemberInternal * peer = findPeer(context, sendermac); // Valid Circumstances - if (peer != NULL && context->mode == PSP_ADHOC_MATCHING_MODE_CHILD && peer == findParent(context)) - { + if (peer != NULL && context->mode == PSP_ADHOC_MATCHING_MODE_CHILD && peer == findParent(context)) { // Complete Packet available - if (length >= (1 + sizeof(SceNetEtherAddr))) - { + if (length >= (1 + sizeof(SceNetEtherAddr))) { // Extract Child MAC SceNetEtherAddr mac; memcpy(&mac, context->rxbuf + 1, sizeof(SceNetEtherAddr)); @@ -1107,8 +1027,7 @@ void actOnDeathPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * se SceNetAdhocMatchingMemberInternal * deadkid = findPeer(context, &mac); // Valid Sibling - if (deadkid->state == PSP_ADHOC_MATCHING_PEER_CHILD) - { + if (deadkid->state == PSP_ADHOC_MATCHING_PEER_CHILD) { // Spawn Leave Event spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_LEAVE, &mac, 0, NULL); @@ -1124,14 +1043,12 @@ void actOnDeathPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * se * @param context Matching Context Pointer * @param sendermac Packet Sender MAC */ -void actOnByePacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sendermac) -{ +void actOnByePacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sendermac) { // Find Peer SceNetAdhocMatchingMemberInternal * peer = findPeer(context, sendermac); // We know this guy - if (peer != NULL) - { + if (peer != NULL) { // P2P or Child Bye. FIXME: Should we allow BYE Event to intervene joining process of Parent-Child too just like P2P Mode? (ie. Crazy Taxi uses P2P Mode) if ((context->mode == PSP_ADHOC_MATCHING_MODE_PARENT && peer->state == PSP_ADHOC_MATCHING_PEER_CHILD) || (context->mode == PSP_ADHOC_MATCHING_MODE_CHILD && peer->state == PSP_ADHOC_MATCHING_PEER_CHILD) || @@ -1150,8 +1067,7 @@ void actOnByePacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * send } // Parent Bye - else if (context->mode == PSP_ADHOC_MATCHING_MODE_CHILD && peer->state == PSP_ADHOC_MATCHING_PEER_PARENT) - { + else if (context->mode == PSP_ADHOC_MATCHING_MODE_CHILD && peer->state == PSP_ADHOC_MATCHING_PEER_PARENT) { // Spawn Leave / Kick Event. FIXME: DISCONNECT event should only be triggered on Parent/P2P mode and for Parent/P2P peer? spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_BYE, sendermac, 0, NULL); @@ -1168,8 +1084,7 @@ void actOnByePacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * send * @param argp SceNetAdhocMatchingContext * * @return Exit Point is never reached... */ -int matchingEventThread(int matchingId) -{ +int matchingEventThread(int matchingId) { SetCurrentThreadName("MatchingEvent"); // Multithreading Lock peerlock.lock(); @@ -1187,8 +1102,7 @@ int matchingEventThread(int matchingId) u32 bufAddr = 0; //= userMemory.Alloc(bufLen); //context->rxbuf; u32_le * args = context->handlerArgs; //MatchingArgs - while (contexts != NULL && context->eventRunning) - { + while (contexts != NULL && context->eventRunning) { // Multithreading Lock peerlock.lock(); // Cast Context @@ -1197,15 +1111,13 @@ int matchingEventThread(int matchingId) peerlock.unlock(); // Messages on Stack ready for processing - while (context != NULL && context->event_stack != NULL) - { + while (context != NULL && context->event_stack != NULL) { // Claim Stack context->eventlock->lock(); // Iterate Message List ThreadMessage * msg = context->event_stack; - if (msg != NULL) - { + if (msg != NULL) { // Default Optional Data void* opt = NULL; @@ -1248,16 +1160,14 @@ int matchingEventThread(int matchingId) } // Process Last Messages - if (contexts != NULL && context->event_stack != NULL) - { + if (contexts != NULL && context->event_stack != NULL) { // Claim Stack context->eventlock->lock(); // Iterate Message List int msg_count = 0; ThreadMessage * msg = context->event_stack; - for (; msg != NULL; msg = msg->next) - { + for (; msg != NULL; msg = msg->next) { // Default Optional Data void * opt = NULL; @@ -1303,8 +1213,7 @@ int matchingEventThread(int matchingId) * @param argp SceNetAdhocMatchingContext * * @return Exit Point is never reached... */ -int matchingInputThread(int matchingId) // TODO: The MatchingInput thread is using sceNetAdhocPdpRecv & sceNetAdhocPdpSend functions so it might be better to run this on PSP thread instead of real thread -{ +int matchingInputThread(int matchingId) { // TODO: The MatchingInput thread is using sceNetAdhocPdpRecv & sceNetAdhocPdpSend functions so it might be better to run this on PSP thread instead of real thread SetCurrentThreadName("MatchingInput"); auto n = GetI18NCategory(I18NCat::NETWORKING); // Multithreading Lock @@ -1331,8 +1240,7 @@ int matchingInputThread(int matchingId) // TODO: The MatchingInput thread is usi // Run while needed... if (context != NULL) { - while (contexts != NULL && context->inputRunning) - { + while (contexts != NULL && context->inputRunning) { // Multithreading Lock peerlock.lock(); // Cast Context @@ -1344,12 +1252,10 @@ int matchingInputThread(int matchingId) // TODO: The MatchingInput thread is usi now = CoreTiming::GetGlobalTimeUsScaled(); //time_now_d()*1000000.0; // Hello Message Sending Context with unoccupied Slots - if ((context->mode == PSP_ADHOC_MATCHING_MODE_PARENT && (countChildren(context) < (context->maxpeers - 1))) || (context->mode == PSP_ADHOC_MATCHING_MODE_P2P && findP2P(context) == NULL)) - { + if ((context->mode == PSP_ADHOC_MATCHING_MODE_PARENT && (countChildren(context) < (context->maxpeers - 1))) || (context->mode == PSP_ADHOC_MATCHING_MODE_P2P && findP2P(context) == NULL)) { // Hello Message Broadcast necessary because of Hello Interval if (context->hello_int > 0) - if (static_cast(now - lasthello) >= static_cast(context->hello_int)) - { + if (static_cast(now - lasthello) >= static_cast(context->hello_int)) { // Broadcast Hello Message broadcastHelloMessage(context); @@ -1360,8 +1266,7 @@ int matchingInputThread(int matchingId) // TODO: The MatchingInput thread is usi // Ping Required if (context->keepalive_int > 0) { - if (static_cast(now - lastping) >= static_cast(context->keepalive_int)) - { + if (static_cast(now - lastping) >= static_cast(context->keepalive_int)) { // Handle Peer Timeouts handleTimeout(context); @@ -1378,15 +1283,13 @@ int matchingInputThread(int matchingId) // TODO: The MatchingInput thread is usi } // Messages on Stack ready for processing - if (context->input_stack != NULL) - { + if (context->input_stack != NULL) { // Claim Stack context->inputlock->lock(); // Iterate Message List ThreadMessage* msg = context->input_stack; - while (msg != NULL) - { + while (msg != NULL) { // Default Optional Data void* opt = NULL; @@ -1441,8 +1344,7 @@ int matchingInputThread(int matchingId) // TODO: The MatchingInput thread is usi // Received Data from a Sender that interests us // Note: There are cases where the sender port might be re-mapped by router or ISP, so we shouldn't check the source port. - if (recvresult == 0 && rxbuflen > 0) - { + if (recvresult == 0 && rxbuflen > 0) { // Log Receive Success if (context->rxbuf[0] > 1) { INFO_LOG(Log::sceNet, "InputLoop[%d]: Received %d Bytes (Opcode[%d]=%s)", matchingId, rxbuflen, context->rxbuf[0], getMatchingOpcodeStr(context->rxbuf[0])); @@ -1520,16 +1422,14 @@ int matchingInputThread(int matchingId) // TODO: The MatchingInput thread is usi if (contexts != NULL) { // Process Last Messages - if (context->input_stack != NULL) - { + if (context->input_stack != NULL) { // Claim Stack context->inputlock->lock(); // Iterate Message List int msg_count = 0; ThreadMessage* msg = context->input_stack; - while (msg != NULL) - { + while (msg != NULL) { // Default Optional Data void* opt = NULL; @@ -1989,36 +1889,29 @@ static int sceNetAdhocMatchingSelectTarget(int matchingId, const char *macAddres return -1; // Initialized Library - if (netAdhocMatchingInited) - { + if (netAdhocMatchingInited) { // Valid Arguments - if (macAddress != NULL) - { + if (macAddress != NULL) { SceNetEtherAddr * target = (SceNetEtherAddr *)macAddress; // Find Matching Context for ID SceNetAdhocMatchingContext * context = findMatchingContext(matchingId); // Found Matching Context - if (context != NULL) - { + if (context != NULL) { // Running Context - if (context->running) - { + if (context->running) { // Search Result SceNetAdhocMatchingMemberInternal * peer = findPeer(context, (SceNetEtherAddr *)target); // Found Peer in List - if (peer != NULL) - { + if (peer != NULL) { // Valid Optional Data Length - if ((optLen == 0) || (optLen > 0 && optDataPtr != 0)) - { + if ((optLen == 0) || (optLen > 0 && optDataPtr != 0)) { void * opt = NULL; if (Memory::IsValidAddress(optDataPtr)) opt = Memory::GetPointerWriteUnchecked(optDataPtr); // Host Mode - if (context->mode == PSP_ADHOC_MATCHING_MODE_PARENT) - { + if (context->mode == PSP_ADHOC_MATCHING_MODE_PARENT) { // Already Connected if (peer->state == PSP_ADHOC_MATCHING_PEER_CHILD) return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_ALREADY_ESTABLISHED, "adhocmatching already established"); @@ -2026,8 +1919,7 @@ static int sceNetAdhocMatchingSelectTarget(int matchingId, const char *macAddres if (countChildren(context) == (context->maxpeers - 1)) return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_EXCEED_MAXNUM, "adhocmatching exceed maxnum"); // Requesting Peer - if (peer->state == PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST) - { + if (peer->state == PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST) { // Accept Peer in Group peer->state = PSP_ADHOC_MATCHING_PEER_CHILD; @@ -2048,8 +1940,7 @@ static int sceNetAdhocMatchingSelectTarget(int matchingId, const char *macAddres } // Client Mode - else if (context->mode == PSP_ADHOC_MATCHING_MODE_CHILD) - { + else if (context->mode == PSP_ADHOC_MATCHING_MODE_CHILD) { // Already connected if (findParent(context) != NULL) return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_ALREADY_ESTABLISHED, "adhocmatching already established"); @@ -2057,8 +1948,7 @@ static int sceNetAdhocMatchingSelectTarget(int matchingId, const char *macAddres if (findOutgoingRequest(context) != NULL) return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_REQUEST_IN_PROGRESS, "adhocmatching request in progress"); // Valid Offer - if (peer->state == PSP_ADHOC_MATCHING_PEER_OFFER) - { + if (peer->state == PSP_ADHOC_MATCHING_PEER_OFFER) { // Switch into Join Request Mode peer->state = PSP_ADHOC_MATCHING_PEER_OUTGOING_REQUEST; @@ -2071,8 +1961,7 @@ static int sceNetAdhocMatchingSelectTarget(int matchingId, const char *macAddres } // P2P Mode - else - { + else { // Already connected if (findP2P(context) != NULL) return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_ALREADY_ESTABLISHED, "adhocmatching already established"); @@ -2080,8 +1969,7 @@ static int sceNetAdhocMatchingSelectTarget(int matchingId, const char *macAddres if (findOutgoingRequest(context) != NULL) return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_REQUEST_IN_PROGRESS, "adhocmatching request in progress"); // Join Request Mode - if (peer->state == PSP_ADHOC_MATCHING_PEER_OFFER) - { + if (peer->state == PSP_ADHOC_MATCHING_PEER_OFFER) { // Switch into Join Request Mode peer->state = PSP_ADHOC_MATCHING_PEER_OUTGOING_REQUEST; @@ -2093,8 +1981,7 @@ static int sceNetAdhocMatchingSelectTarget(int matchingId, const char *macAddres } // Requesting Peer - else if (peer->state == PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST) - { + else if (peer->state == PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST) { // Accept Peer in Group peer->state = PSP_ADHOC_MATCHING_PEER_P2P; @@ -2138,38 +2025,32 @@ static int sceNetAdhocMatchingSelectTarget(int matchingId, const char *macAddres int NetAdhocMatching_CancelTargetWithOpt(int matchingId, const char* macAddress, int optLen, u32 optDataPtr) { // Initialized Library - if (netAdhocMatchingInited) - { + if (netAdhocMatchingInited) { SceNetEtherAddr* target = (SceNetEtherAddr*)macAddress; void* opt = NULL; if (Memory::IsValidAddress(optDataPtr)) opt = Memory::GetPointerWriteUnchecked(optDataPtr); // Valid Arguments - if (target != NULL && ((optLen == 0) || (optLen > 0 && opt != NULL))) - { + if (target != NULL && ((optLen == 0) || (optLen > 0 && opt != NULL))) { // Find Matching Context SceNetAdhocMatchingContext* context = findMatchingContext(matchingId); // Found Matching Context - if (context != NULL) - { + if (context != NULL) { // Running Context - if (context->running) - { + if (context->running) { // Find Peer SceNetAdhocMatchingMemberInternal* peer = findPeer(context, (SceNetEtherAddr*)target); // Found Peer - if (peer != NULL) - { + if (peer != NULL) { // Valid Peer Mode if ((context->mode == PSP_ADHOC_MATCHING_MODE_CHILD && (peer->state == PSP_ADHOC_MATCHING_PEER_PARENT || peer->state == PSP_ADHOC_MATCHING_PEER_OUTGOING_REQUEST)) || (context->mode == PSP_ADHOC_MATCHING_MODE_PARENT && (peer->state == PSP_ADHOC_MATCHING_PEER_CHILD || peer->state == PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST)) || (context->mode == PSP_ADHOC_MATCHING_MODE_P2P && (peer->state == PSP_ADHOC_MATCHING_PEER_P2P || peer->state == PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST))) { // Notify other Children of Death - if (context->mode == PSP_ADHOC_MATCHING_MODE_PARENT && peer->state == PSP_ADHOC_MATCHING_PEER_CHILD && countConnectedPeers(context) > 1) - { + if (context->mode == PSP_ADHOC_MATCHING_MODE_PARENT && peer->state == PSP_ADHOC_MATCHING_PEER_CHILD && countConnectedPeers(context) > 1) { // Send Death Message sendDeathMessage(context, peer); } @@ -2298,8 +2179,7 @@ int sceNetAdhocMatchingSetHelloOpt(int matchingId, int optLenAddr, u32 optDataAd //free(hello); // Allocation Required - if (optLenAddr > 0) - { + if (optLenAddr > 0) { // Allocate Memory if (optLenAddr > context->hellolen) { hello = realloc(hello, optLenAddr); @@ -2319,8 +2199,7 @@ int sceNetAdhocMatchingSetHelloOpt(int matchingId, int optLenAddr, u32 optDataAd context->hellolen = optLenAddr; context->helloAddr = optDataAddr; } - else - { + else { // Delete Hello Data context->hellolen = 0; context->helloAddr = 0; @@ -2377,16 +2256,14 @@ static int sceNetAdhocMatchingGetMembers(int matchingId, u32 sizeAddr, u32 buf) int available = sizeof(SceNetAdhocMatchingMemberInfoEmu) * peercount; // Length Returner Mode - if (buf == 0) - { + if (buf == 0) { // Get Connected Peer Count *buflen = available; DEBUG_LOG(Log::sceNet, "MemberList [Connected: %i]", peercount); } // Normal Mode - else - { + else { // Fix Negative Length if ((*buflen) < 0) *buflen = 0; @@ -2402,8 +2279,7 @@ static int sceNetAdhocMatchingGetMembers(int matchingId, u32 sizeAddr, u32 buf) // Filled Request Counter int filledpeers = 0; - if (requestedpeers > 0) - { + if (requestedpeers > 0) { // Add Self-Peer first, unless if there is existing Parent/P2P peer if (peercount == 1 || context->mode != PSP_ADHOC_MATCHING_MODE_CHILD) { // Add Local MAC @@ -2413,17 +2289,14 @@ static int sceNetAdhocMatchingGetMembers(int matchingId, u32 sizeAddr, u32 buf) } // Room for more than local peer - if (requestedpeers > 1) - { + if (requestedpeers > 1) { // P2P Mode - if (context->mode == PSP_ADHOC_MATCHING_MODE_P2P) - { + if (context->mode == PSP_ADHOC_MATCHING_MODE_P2P) { // Find P2P Brother SceNetAdhocMatchingMemberInternal* p2p = findP2P(context, excludeTimedout); // P2P Brother found - if (p2p != NULL) - { + if (p2p != NULL) { // Faking lastping auto friendpeer = findFriend(&p2p->mac); if (p2p->lastping != 0 && friendpeer != NULL && friendpeer->last_recv != 0) @@ -2439,8 +2312,7 @@ static int sceNetAdhocMatchingGetMembers(int matchingId, u32 sizeAddr, u32 buf) } // Parent or Child Mode - else - { + else { // Add Parent first SceNetAdhocMatchingMemberInternal* parentpeer = findParent(context); if (parentpeer != NULL) { @@ -2462,8 +2334,7 @@ static int sceNetAdhocMatchingGetMembers(int matchingId, u32 sizeAddr, u32 buf) // Iterate Peer List SceNetAdhocMatchingMemberInternal* peer = context->peerlist; - for (; peer != NULL && filledpeers < requestedpeers; peer = peer->next) - { + for (; peer != NULL && filledpeers < requestedpeers; peer = peer->next) { // Should we exclude timedout members? if (!excludeTimedout || peer->lastping != 0) { // Faking lastping @@ -2515,8 +2386,7 @@ static int sceNetAdhocMatchingGetMembers(int matchingId, u32 sizeAddr, u32 buf) } // Link Result List - for (int i = 0; i < filledpeers - 1; i++) - { + for (int i = 0; i < filledpeers - 1; i++) { // Link Next Element //buf2[i].next = &buf2[i + 1]; buf2[i].next = buf + (sizeof(SceNetAdhocMatchingMemberInfoEmu) * (i + 1LL)); @@ -2541,20 +2411,16 @@ int sceNetAdhocMatchingSendData(int matchingId, const char *mac, int dataLen, u3 return -1; // Initialized Library - if (netAdhocMatchingInited) - { + if (netAdhocMatchingInited) { // Valid Arguments - if (mac != NULL) - { + if (mac != NULL) { // Find Matching Context SceNetAdhocMatchingContext * context = findMatchingContext(matchingId); // Found Context - if (context != NULL) - { + if (context != NULL) { // Running Context - if (context->running) - { + if (context->running) { // Invalid Data Length if (dataLen <= 0 || dataAddr == 0) // Invalid Data Length @@ -2570,11 +2436,9 @@ int sceNetAdhocMatchingSendData(int matchingId, const char *mac, int dataLen, u3 SceNetAdhocMatchingMemberInternal* peer = findPeer(context, (SceNetEtherAddr*)mac); // Found Peer - if (peer != NULL) - { + if (peer != NULL) { // Valid Peer Connection State - if (peer->state == PSP_ADHOC_MATCHING_PEER_PARENT || peer->state == PSP_ADHOC_MATCHING_PEER_CHILD || peer->state == PSP_ADHOC_MATCHING_PEER_P2P) - { + if (peer->state == PSP_ADHOC_MATCHING_PEER_PARENT || peer->state == PSP_ADHOC_MATCHING_PEER_CHILD || peer->state == PSP_ADHOC_MATCHING_PEER_P2P) { // Send in Progress if (peer->sending) return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_DATA_BUSY, "data busy"); @@ -2619,29 +2483,23 @@ int sceNetAdhocMatchingAbortSendData(int matchingId, const char *mac) { return -1; // Initialized Library - if (netAdhocMatchingInited) - { + if (netAdhocMatchingInited) { // Valid Arguments - if (mac != NULL) - { + if (mac != NULL) { // Find Matching Context SceNetAdhocMatchingContext * context = findMatchingContext(matchingId); // Found Context - if (context != NULL) - { + if (context != NULL) { // Running Context - if (context->running) - { + if (context->running) { // Find Target Peer SceNetAdhocMatchingMemberInternal * peer = findPeer(context, (SceNetEtherAddr *)mac); // Found Peer - if (peer != NULL) - { + if (peer != NULL) { // Peer is sending - if (peer->sending) - { + if (peer->sending) { // Set Peer as Bulk Idle peer->sending = 0; @@ -2689,14 +2547,12 @@ int sceNetAdhocMatchingGetPoolStat(u32 poolstatPtr) { return -1; // Initialized Library - if (netAdhocMatchingInited) - { + if (netAdhocMatchingInited) { SceNetMallocStat * poolstat = NULL; if (Memory::IsValidAddress(poolstatPtr)) poolstat = (SceNetMallocStat *)Memory::GetPointer(poolstatPtr); // Valid Argument - if (poolstat != NULL) - { + if (poolstat != NULL) { // Fill Poolstat with Fake Data poolstat->pool = fakePoolSize; poolstat->maximum = fakePoolSize / 2; // Max usage faked to halt the pool From 9f94e035a78041b0624fdf3bf431cbe7147501d8 Mon Sep 17 00:00:00 2001 From: Nemoumbra Date: Sat, 4 Jan 2025 20:29:39 +0300 Subject: [PATCH 2/8] Added early returns for send{X}Packet funcs --- Core/HLE/sceNetAdhocMatching.cpp | 361 ++++++++++++++++--------------- 1 file changed, 191 insertions(+), 170 deletions(-) diff --git a/Core/HLE/sceNetAdhocMatching.cpp b/Core/HLE/sceNetAdhocMatching.cpp index 73172f0c55..14d3c165ef 100644 --- a/Core/HLE/sceNetAdhocMatching.cpp +++ b/Core/HLE/sceNetAdhocMatching.cpp @@ -223,68 +223,72 @@ void sendAcceptPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * ma // Find Peer SceNetAdhocMatchingMemberInternal * peer = findPeer(context, mac); - // Found Peer - if (peer != NULL && (peer->state == PSP_ADHOC_MATCHING_PEER_CHILD || peer->state == PSP_ADHOC_MATCHING_PEER_P2P)) { - // Required Sibling Buffer - uint32_t siblingbuflen = 0; + if (peer == NULL || (peer->state != PSP_ADHOC_MATCHING_PEER_CHILD && peer->state != PSP_ADHOC_MATCHING_PEER_P2P)) { + // Not found + return; + } - // Parent Mode - if (context->mode == PSP_ADHOC_MATCHING_MODE_PARENT) siblingbuflen = (u32)sizeof(SceNetEtherAddr) * (countConnectedPeers(context) - 2); + // Required Sibling Buffer + uint32_t siblingbuflen = 0; - // Sibling Count - int siblingcount = siblingbuflen / sizeof(SceNetEtherAddr); + // Parent Mode + if (context->mode == PSP_ADHOC_MATCHING_MODE_PARENT) siblingbuflen = (u32)sizeof(SceNetEtherAddr) * (countConnectedPeers(context) - 2); - // Allocate Accept Message Buffer - uint8_t * accept = (uint8_t *)malloc(9LL + optlen + siblingbuflen); + // Sibling Count + int siblingcount = siblingbuflen / sizeof(SceNetEtherAddr); - // Allocated Accept Message Buffer - if (accept != NULL) { - // Accept Opcode - accept[0] = PSP_ADHOC_MATCHING_PACKET_ACCEPT; + // Allocate Accept Message Buffer + uint8_t * accept = (uint8_t *)malloc(9LL + optlen + siblingbuflen); - // Optional Data Length - memcpy(accept + 1, &optlen, sizeof(optlen)); + if (accept == NULL) { + // Failed to allocate the Accept Message Buffer + return; + } - // Sibling Count - memcpy(accept + 5, &siblingcount, sizeof(siblingcount)); + // Accept Opcode + accept[0] = PSP_ADHOC_MATCHING_PACKET_ACCEPT; - // Copy Optional Data - if (optlen > 0) memcpy(accept + 9, opt, optlen); + // Optional Data Length + memcpy(accept + 1, &optlen, sizeof(optlen)); - // Parent Mode Extra Data required - if (context->mode == PSP_ADHOC_MATCHING_MODE_PARENT && siblingcount > 0) { - // Create MAC Array Pointer - uint8_t * siblingmacs = (uint8_t *)(accept + 9 + optlen); + // Sibling Count + memcpy(accept + 5, &siblingcount, sizeof(siblingcount)); - // MAC Writing Pointer - int i = 0; + // Copy Optional Data + if (optlen > 0) memcpy(accept + 9, opt, optlen); - // Iterate Peer List - SceNetAdhocMatchingMemberInternal * item = context->peerlist; - for (; item != NULL; item = item->next) { - // Ignore Target - if (item == peer) continue; + // Parent Mode Extra Data required + if (context->mode == PSP_ADHOC_MATCHING_MODE_PARENT && siblingcount > 0) { + // Create MAC Array Pointer + uint8_t * siblingmacs = (uint8_t *)(accept + 9 + optlen); - // Copy Child MAC - if (item->state == PSP_ADHOC_MATCHING_PEER_CHILD) { - // Clone MAC the stupid memcpy way to shut up PSP CPU - memcpy(siblingmacs + sizeof(SceNetEtherAddr) * i++, &item->mac, sizeof(SceNetEtherAddr)); - } - } + // MAC Writing Pointer + int i = 0; + + // Iterate Peer List + SceNetAdhocMatchingMemberInternal * item = context->peerlist; + for (; item != NULL; item = item->next) { + // Ignore Target + if (item == peer) continue; + + // Copy Child MAC + if (item->state == PSP_ADHOC_MATCHING_PEER_CHILD) { + // Clone MAC the stupid memcpy way to shut up PSP CPU + memcpy(siblingmacs + sizeof(SceNetEtherAddr) * i++, &item->mac, sizeof(SceNetEtherAddr)); } - - // Send Data - context->socketlock->lock(); - sceNetAdhocPdpSend(context->socket, (const char*)mac, (*context->peerPort)[*mac], accept, 9 + optlen + siblingbuflen, 0, ADHOC_F_NONBLOCK); - context->socketlock->unlock(); - - // Free Memory - free(accept); - - // Spawn Local Established Event - spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_ESTABLISHED, mac, 0, NULL); } } + + // Send Data + context->socketlock->lock(); + sceNetAdhocPdpSend(context->socket, (const char*)mac, (*context->peerPort)[*mac], accept, 9 + optlen + siblingbuflen, 0, ADHOC_F_NONBLOCK); + context->socketlock->unlock(); + + // Free Memory + free(accept); + + // Spawn Local Established Event + spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_ESTABLISHED, mac, 0, NULL); } /** @@ -301,31 +305,35 @@ void sendJoinPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac, // Find Peer SceNetAdhocMatchingMemberInternal * peer = findPeer(context, mac); - // Valid Peer - if (peer != NULL && peer->state == PSP_ADHOC_MATCHING_PEER_OUTGOING_REQUEST) { - // Allocate Join Message Buffer - uint8_t * join = (uint8_t *)malloc(5LL + optlen); - - // Allocated Join Message Buffer - if (join != NULL) { - // Join Opcode - join[0] = PSP_ADHOC_MATCHING_PACKET_JOIN; - - // Optional Data Length - memcpy(join + 1, &optlen, sizeof(optlen)); - - // Copy Optional Data - if (optlen > 0) memcpy(join + 5, opt, optlen); - - // Send Data - context->socketlock->lock(); - sceNetAdhocPdpSend(context->socket, (const char*)mac, (*context->peerPort)[*mac], join, 5 + optlen, 0, ADHOC_F_NONBLOCK); - context->socketlock->unlock(); - - // Free Memory - free(join); - } + if (peer == NULL || peer->state != PSP_ADHOC_MATCHING_PEER_OUTGOING_REQUEST) { + // Valid peer not found + return; } + + // Allocate Join Message Buffer + uint8_t * join = (uint8_t *)malloc(5LL + optlen); + + if (join == NULL) { + // Failed to allocate the Join Message Buffer + return; + } + + // Join Opcode + join[0] = PSP_ADHOC_MATCHING_PACKET_JOIN; + + // Optional Data Length + memcpy(join + 1, &optlen, sizeof(optlen)); + + // Copy Optional Data + if (optlen > 0) memcpy(join + 5, opt, optlen); + + // Send Data + context->socketlock->lock(); + sceNetAdhocPdpSend(context->socket, (const char*)mac, (*context->peerPort)[*mac], join, 5 + optlen, 0, ADHOC_F_NONBLOCK); + context->socketlock->unlock(); + + // Free Memory + free(join); } /** @@ -365,19 +373,21 @@ void sendCancelPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * ma // Find Peer SceNetAdhocMatchingMemberInternal * peer = findPeer(context, mac); - // Found Peer - if (peer != NULL) { - // Child Mode Fallback - Delete All - if (context->mode == PSP_ADHOC_MATCHING_MODE_CHILD) { - // Delete Peer List - clearPeerList(context); - } + if (peer == NULL) { + // Peer not found + return; + } - // Delete Peer - else { - // Instead of removing peer immediately, We should give a little time before removing the peer and let it timed out? so it can send the BYE packet when stopping AdhocMatching after Canceling it - peer->lastping = CoreTiming::GetGlobalTimeUsScaled(); - } + // Child Mode Fallback - Delete All + if (context->mode == PSP_ADHOC_MATCHING_MODE_CHILD) { + // Delete Peer List + clearPeerList(context); + } + + // Delete Peer + else { + // Instead of removing peer immediately, We should give a little time before removing the peer and let it timed out? so it can send the BYE packet when stopping AdhocMatching after Canceling it + peer->lastping = CoreTiming::GetGlobalTimeUsScaled(); } } @@ -395,40 +405,44 @@ void sendBulkDataPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * // Find Peer SceNetAdhocMatchingMemberInternal * peer = findPeer(context, mac); - // Valid Peer (rest is already checked in send.c) - if (peer != NULL) { - // Don't send if it's aborted - //if (peer->sending == 0) return; - - // Allocate Send Message Buffer - uint8_t * send = (uint8_t *)malloc(5LL + datalen); - - // Allocated Send Message Buffer - if (send != NULL) { - // Send Opcode - send[0] = PSP_ADHOC_MATCHING_PACKET_BULK; - - // Data Length - memcpy(send + 1, &datalen, sizeof(datalen)); - - // Copy Data - memcpy(send + 5, data, datalen); - - // Send Data - context->socketlock->lock(); - sceNetAdhocPdpSend(context->socket, (const char*)mac, (*context->peerPort)[*mac], send, 5 + datalen, 0, ADHOC_F_NONBLOCK); - context->socketlock->unlock(); - - // Free Memory - free(send); - - // Remove Busy Bit from Peer - peer->sending = 0; - - // Spawn Data Event - spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_DATA_ACK, mac, 0, NULL); - } + if (peer == NULL) { + // Invalid Peer + return; } + + // Don't send if it's aborted + //if (peer->sending == 0) return; + + // Allocate Send Message Buffer + uint8_t * send = (uint8_t *)malloc(5LL + datalen); + + if (send == NULL) { + // Failed to allocate the Send Message Buffer + return; + } + + // Send Opcode + send[0] = PSP_ADHOC_MATCHING_PACKET_BULK; + + // Data Length + memcpy(send + 1, &datalen, sizeof(datalen)); + + // Copy Data + memcpy(send + 5, data, datalen); + + // Send Data + context->socketlock->lock(); + sceNetAdhocPdpSend(context->socket, (const char*)mac, (*context->peerPort)[*mac], send, 5 + datalen, 0, ADHOC_F_NONBLOCK); + context->socketlock->unlock(); + + // Free Memory + free(send); + + // Remove Busy Bit from Peer + peer->sending = 0; + + // Spawn Data Event + spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_DATA_ACK, mac, 0, NULL); } /** @@ -443,37 +457,41 @@ void sendBirthPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac // Find Newborn Child SceNetAdhocMatchingMemberInternal * newborn = findPeer(context, mac); - // Found Newborn Child - if (newborn != NULL) { - // Packet Buffer - uint8_t packet[7]; + if (newborn == NULL) { + // Did not find Newborn Child + return; + } - // Set Opcode - packet[0] = PSP_ADHOC_MATCHING_PACKET_BIRTH; + // Packet Buffer + uint8_t packet[7]; - // Set Newborn MAC - memcpy(packet + 1, mac, sizeof(SceNetEtherAddr)); + // Set Opcode + packet[0] = PSP_ADHOC_MATCHING_PACKET_BIRTH; - // Iterate Peers - SceNetAdhocMatchingMemberInternal * peer = context->peerlist; - for (; peer != NULL; peer = peer->next) { - // Skip Newborn Child - if (peer == newborn) continue; + // Set Newborn MAC + memcpy(packet + 1, mac, sizeof(SceNetEtherAddr)); - // Send only to children - if (peer->state == PSP_ADHOC_MATCHING_PEER_CHILD) { - // Send Packet - context->socketlock->lock(); - int sent = sceNetAdhocPdpSend(context->socket, (const char*)&peer->mac, (*context->peerPort)[peer->mac], packet, sizeof(packet), 0, ADHOC_F_NONBLOCK); - context->socketlock->unlock(); + // Iterate Peers + SceNetAdhocMatchingMemberInternal * peer = context->peerlist; + for (; peer != NULL; peer = peer->next) { + // Skip Newborn Child + if (peer == newborn) continue; - // Log Send Success - if (sent >= 0) - INFO_LOG(Log::sceNet, "InputLoop: Sending BIRTH [%s] to %s", mac2str(mac).c_str(), mac2str(&peer->mac).c_str()); - else - WARN_LOG(Log::sceNet, "InputLoop: Failed to Send BIRTH [%s] to %s", mac2str(mac).c_str(), mac2str(&peer->mac).c_str()); - } + // Send only to children + if (peer->state != PSP_ADHOC_MATCHING_PEER_CHILD) { + continue; } + + // Send Packet + context->socketlock->lock(); + int sent = sceNetAdhocPdpSend(context->socket, (const char*)&peer->mac, (*context->peerPort)[peer->mac], packet, sizeof(packet), 0, ADHOC_F_NONBLOCK); + context->socketlock->unlock(); + + // Log Send Success + if (sent >= 0) + INFO_LOG(Log::sceNet, "InputLoop: Sending BIRTH [%s] to %s", mac2str(mac).c_str(), mac2str(&peer->mac).c_str()); + else + WARN_LOG(Log::sceNet, "InputLoop: Failed to Send BIRTH [%s] to %s", mac2str(mac).c_str(), mac2str(&peer->mac).c_str()); } } @@ -489,43 +507,46 @@ void sendDeathPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac // Find abandoned Child SceNetAdhocMatchingMemberInternal * deadkid = findPeer(context, mac); - // Found abandoned Child - if (deadkid != NULL) { - // Packet Buffer - uint8_t packet[7]; + if (deadkid == NULL) { + // Did not find abandoned Child + return; + } - // Set abandoned Child MAC - memcpy(packet + 1, mac, sizeof(SceNetEtherAddr)); + // Packet Buffer + uint8_t packet[7]; - // Iterate Peers - SceNetAdhocMatchingMemberInternal * peer = context->peerlist; - for (; peer != NULL; peer = peer->next) { - // Skip dead Child? Or May be we should also tells the disconnected Child, that they have been disconnected from the Host (in the case they were disconnected because they went to PPSSPP settings for too long) - if (peer == deadkid) { + // Set abandoned Child MAC + memcpy(packet + 1, mac, sizeof(SceNetEtherAddr)); + + // Iterate Peers + SceNetAdhocMatchingMemberInternal * peer = context->peerlist; + for (; peer != NULL; peer = peer->next) { + // Skip dead Child? Or May be we should also tells the disconnected Child, that they have been disconnected from the Host (in the case they were disconnected because they went to PPSSPP settings for too long) + if (peer == deadkid) { + // Set Opcode + packet[0] = PSP_ADHOC_MATCHING_PACKET_BYE; + + // Send Bye Packet + context->socketlock->lock(); + sceNetAdhocPdpSend(context->socket, (const char*)&peer->mac, (*context->peerPort)[peer->mac], packet, sizeof(packet[0]), 0, ADHOC_F_NONBLOCK); + context->socketlock->unlock(); + } + else { + // Send to other children + if (peer->state == PSP_ADHOC_MATCHING_PEER_CHILD) { // Set Opcode - packet[0] = PSP_ADHOC_MATCHING_PACKET_BYE; + packet[0] = PSP_ADHOC_MATCHING_PACKET_DEATH; - // Send Bye Packet + // Send Death Packet context->socketlock->lock(); - sceNetAdhocPdpSend(context->socket, (const char*)&peer->mac, (*context->peerPort)[peer->mac], packet, sizeof(packet[0]), 0, ADHOC_F_NONBLOCK); + sceNetAdhocPdpSend(context->socket, (const char*)&peer->mac, (*context->peerPort)[peer->mac], packet, sizeof(packet), 0, ADHOC_F_NONBLOCK); context->socketlock->unlock(); } - else - // Send to other children - if (peer->state == PSP_ADHOC_MATCHING_PEER_CHILD) { - // Set Opcode - packet[0] = PSP_ADHOC_MATCHING_PACKET_DEATH; - - // Send Death Packet - context->socketlock->lock(); - sceNetAdhocPdpSend(context->socket, (const char*)&peer->mac, (*context->peerPort)[peer->mac], packet, sizeof(packet), 0, ADHOC_F_NONBLOCK); - context->socketlock->unlock(); - } } - - // Delete Peer - deletePeer(context, deadkid); } + + // Delete Peer + deletePeer(context, deadkid); } /** From 110799f09d5c673753f82193d83d90d437579bc1 Mon Sep 17 00:00:00 2001 From: Nemoumbra Date: Sat, 4 Jan 2025 21:08:31 +0300 Subject: [PATCH 3/8] Cleaned up broadcastHelloMessage and broadcastPingMessage --- Core/HLE/sceNetAdhocMatching.cpp | 84 ++++++++++++++++---------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/Core/HLE/sceNetAdhocMatching.cpp b/Core/HLE/sceNetAdhocMatching.cpp index 14d3c165ef..47d748c252 100644 --- a/Core/HLE/sceNetAdhocMatching.cpp +++ b/Core/HLE/sceNetAdhocMatching.cpp @@ -128,9 +128,11 @@ void broadcastPingMessage(SceNetAdhocMatchingContext * context) { // Ping Opcode uint8_t ping = PSP_ADHOC_MATCHING_PACKET_PING; + // Lock the peer + std::lock_guard peer_guard(peerlock); + // Send Broadcast // FIXME: Not sure whether this PING supposed to be sent only to AdhocMatching members or to everyone in Adhocctl Group, since we already pinging the AdhocServer to avoid getting kicked out of Adhocctl Group - peerlock.lock(); auto peer = friends; // Use context->peerlist if only need to send to AdhocMatching members for (; peer != NULL; peer = peer->next) { // Skipping soon to be removed peer @@ -146,7 +148,6 @@ void broadcastPingMessage(SceNetAdhocMatchingContext * context) { sceNetAdhocPdpSend(context->socket, (const char*)&peer->mac_addr, port, &ping, sizeof(ping), 0, ADHOC_F_NONBLOCK); context->socketlock->unlock(); } - peerlock.unlock(); } /** @@ -166,47 +167,46 @@ void broadcastHelloMessage(SceNetAdhocMatchingContext * context) { } } - // Allocated Hello Message Buffer - if (hello != NULL) { - // Hello Opcode - hello[0] = PSP_ADHOC_MATCHING_PACKET_HELLO; - - // Hello Data Length (have to memcpy this to avoid cpu alignment crash) - memcpy(hello + 1, &context->hellolen, sizeof(context->hellolen)); - - // FIXME: When using JPCSP + prx files the data being sent have a header of 12 bytes instead of 5 bytes: - // [01(always 1? size of the next data? or combined with next byte as U16_BE opcode?) 01(matching opcode, or combined with previous byte as U16_BE opcode?) 01 E0(size of next data + hello data in big-endian/U16_BE) 00 0F 42 40(U32_BE? time?) 00 0F 42 40(U32_BE? time?)], - // followed by hello data (0x1D8 bytes of opt data, based on Ys vs. Sora no Kiseki), and followed by 16 bytes of (optional?) footer [01 00 00 .. 00 00](footer doesn't exist if the size after opcode is 00 00) - - // Copy Hello Data - if (context->hellolen > 0) memcpy(hello + 5, context->hello, context->hellolen); - - std::string hellohex; - DataToHexString(10, 0, context->hello, context->hellolen, &hellohex); - DEBUG_LOG(Log::sceNet, "HELLO Dump (%d bytes):\n%s", context->hellolen, hellohex.c_str()); - - // Send Broadcast, so everyone know we have a room here - peerlock.lock(); - SceNetAdhocctlPeerInfo* peer = friends; - for (; peer != NULL; peer = peer->next) { - // Skipping soon to be removed peer - if (peer->last_recv == 0) - continue; - - u16_le port = context->port; - auto it = (*context->peerPort).find(peer->mac_addr); - if (it != (*context->peerPort).end()) - port = it->second; - - context->socketlock->lock(); - sceNetAdhocPdpSend(context->socket, (const char*)&peer->mac_addr, port, hello, 5 + context->hellolen, 0, ADHOC_F_NONBLOCK); - context->socketlock->unlock(); - } - peerlock.unlock(); - - // Free Memory, not needed since it may be reused again later - //free(hello); + if (hello == NULL) { + // Failed to allocate the Hello Message Buffer + return; } + + // Hello Opcode + hello[0] = PSP_ADHOC_MATCHING_PACKET_HELLO; + + // Hello Data Length (have to memcpy this to avoid cpu alignment crash) + memcpy(hello + 1, &context->hellolen, sizeof(context->hellolen)); + + // FIXME: When using JPCSP + prx files the data being sent have a header of 12 bytes instead of 5 bytes: + // [01(always 1? size of the next data? or combined with next byte as U16_BE opcode?) 01(matching opcode, or combined with previous byte as U16_BE opcode?) 01 E0(size of next data + hello data in big-endian/U16_BE) 00 0F 42 40(U32_BE? time?) 00 0F 42 40(U32_BE? time?)], + // followed by hello data (0x1D8 bytes of opt data, based on Ys vs. Sora no Kiseki), and followed by 16 bytes of (optional?) footer [01 00 00 .. 00 00](footer doesn't exist if the size after opcode is 00 00) + + // Copy Hello Data + if (context->hellolen > 0) memcpy(hello + 5, context->hello, context->hellolen); + + std::string hellohex; + DataToHexString(10, 0, context->hello, context->hellolen, &hellohex); + DEBUG_LOG(Log::sceNet, "HELLO Dump (%d bytes):\n%s", context->hellolen, hellohex.c_str()); + + // Send Broadcast, so everyone know we have a room here + peerlock.lock(); + SceNetAdhocctlPeerInfo* peer = friends; + for (; peer != NULL; peer = peer->next) { + // Skipping soon to be removed peer + if (peer->last_recv == 0) + continue; + + u16_le port = context->port; + auto it = (*context->peerPort).find(peer->mac_addr); + if (it != (*context->peerPort).end()) + port = it->second; + + context->socketlock->lock(); + sceNetAdhocPdpSend(context->socket, (const char*)&peer->mac_addr, port, hello, 5 + context->hellolen, 0, ADHOC_F_NONBLOCK); + context->socketlock->unlock(); + } + peerlock.unlock(); } /** From 4f623db57ab3b849fc07864b58d0cd3bce96aa48 Mon Sep 17 00:00:00 2001 From: Nemoumbra Date: Sat, 4 Jan 2025 23:42:39 +0300 Subject: [PATCH 4/8] Added early returns for actOn{X}Packet funcs --- Core/HLE/sceNetAdhocMatching.cpp | 708 ++++++++++++++++--------------- 1 file changed, 372 insertions(+), 336 deletions(-) diff --git a/Core/HLE/sceNetAdhocMatching.cpp b/Core/HLE/sceNetAdhocMatching.cpp index 47d748c252..6567373141 100644 --- a/Core/HLE/sceNetAdhocMatching.cpp +++ b/Core/HLE/sceNetAdhocMatching.cpp @@ -598,6 +598,81 @@ void actOnPingPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sen void actOnHelloPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sendermac, int32_t length) { // Interested in Hello Data if ((context->mode == PSP_ADHOC_MATCHING_MODE_CHILD && findParent(context) == NULL) || (context->mode == PSP_ADHOC_MATCHING_MODE_P2P && findP2P(context) == NULL)) { + if (length < 5) { + // Incomplete Packet Header + return; + } + + // Extract Optional Data Length + int optlen = 0; memcpy(&optlen, context->rxbuf + 1, sizeof(optlen)); + + if (optlen < 0 || length < (5 + optlen)) { + // Invalid packet + return; + } + + // Set Default Null Data + void * opt = NULL; + + // Extract Optional Data Pointer + if (optlen > 0) opt = context->rxbuf + 5; + + // Find Peer + SceNetAdhocMatchingMemberInternal * peer = findPeer(context, sendermac); + + // Peer not found + if (peer == NULL) { + // Allocate Memory + peer = (SceNetAdhocMatchingMemberInternal *)malloc(sizeof(SceNetAdhocMatchingMemberInternal)); + + // Allocated Memory + if (peer != NULL) { + // Clear Memory + memset(peer, 0, sizeof(SceNetAdhocMatchingMemberInternal)); + + // Copy Sender MAC + peer->mac = *sendermac; + + // Set Peer State + peer->state = PSP_ADHOC_MATCHING_PEER_OFFER; + + // Initialize Ping Timer + peer->lastping = CoreTiming::GetGlobalTimeUsScaled(); //time_now_d()*1000000.0; + + peerlock.lock(); + // Link Peer into List + peer->next = context->peerlist; + context->peerlist = peer; + peerlock.unlock(); + } + } + + // Peer available now + if (peer != NULL && peer->state != PSP_ADHOC_MATCHING_PEER_OUTGOING_REQUEST && peer->state != PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST) { + std::string hellohex; + DataToHexString(10, 0, (u8*)opt, optlen, &hellohex); + DEBUG_LOG(Log::sceNet, "HELLO Dump (%d bytes):\n%s", optlen, hellohex.c_str()); + + // Spawn Hello Event. FIXME: HELLO event should not be triggered in the middle of joining? This will cause Bleach 7 to Cancel the join request + spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_HELLO, sendermac, optlen, opt); + } + } +} + +/** +* Handle Join Packet +* @param context Matching Context Pointer +* @param sendermac Packet Sender MAC +* @param length Packet Length +*/ +void actOnJoinPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sendermac, int32_t length) { + if (context->mode == PSP_ADHOC_MATCHING_MODE_CHILD) { + // Child mode context + return; + } + + // We still got a unoccupied slot in our room (Parent / P2P) + if ((context->mode == PSP_ADHOC_MATCHING_MODE_PARENT && countChildren(context) < (context->maxpeers - 1)) || (context->mode == PSP_ADHOC_MATCHING_MODE_P2P && findP2P(context) == NULL)) { // Complete Packet Header available if (length >= 5) { // Extract Optional Data Length @@ -614,7 +689,15 @@ void actOnHelloPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * se // Find Peer SceNetAdhocMatchingMemberInternal * peer = findPeer(context, sendermac); - // Peer not found + // If we got the peer in the table already and are a parent, there is nothing left to be done. + // This is because the only way a parent can know of a child is via a join request... + // If we thus know of a possible child, then we already had a previous join request thus no need for double tapping. + if (peer != NULL && peer->lastping != 0 && context->mode == PSP_ADHOC_MATCHING_MODE_PARENT) { + WARN_LOG(Log::sceNet, "Join Event(2) Ignored"); + return; + } + + // New Peer if (peer == NULL) { // Allocate Memory peer = (SceNetAdhocMatchingMemberInternal *)malloc(sizeof(SceNetAdhocMatchingMemberInternal)); @@ -628,7 +711,7 @@ void actOnHelloPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * se peer->mac = *sendermac; // Set Peer State - peer->state = PSP_ADHOC_MATCHING_PEER_OFFER; + peer->state = PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST; // Initialize Ping Timer peer->lastping = CoreTiming::GetGlobalTimeUsScaled(); //time_now_d()*1000000.0; @@ -638,98 +721,6 @@ void actOnHelloPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * se peer->next = context->peerlist; context->peerlist = peer; peerlock.unlock(); - } - } - - // Peer available now - if (peer != NULL && peer->state != PSP_ADHOC_MATCHING_PEER_OUTGOING_REQUEST && peer->state != PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST) { - std::string hellohex; - DataToHexString(10, 0, (u8*)opt, optlen, &hellohex); - DEBUG_LOG(Log::sceNet, "HELLO Dump (%d bytes):\n%s", optlen, hellohex.c_str()); - - // Spawn Hello Event. FIXME: HELLO event should not be triggered in the middle of joining? This will cause Bleach 7 to Cancel the join request - spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_HELLO, sendermac, optlen, opt); - } - } - } - } -} - -/** -* Handle Join Packet -* @param context Matching Context Pointer -* @param sendermac Packet Sender MAC -* @param length Packet Length -*/ -void actOnJoinPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sendermac, int32_t length) { - // Not a child mode context - if (context->mode != PSP_ADHOC_MATCHING_MODE_CHILD) { - // We still got a unoccupied slot in our room (Parent / P2P) - if ((context->mode == PSP_ADHOC_MATCHING_MODE_PARENT && countChildren(context) < (context->maxpeers - 1)) || (context->mode == PSP_ADHOC_MATCHING_MODE_P2P && findP2P(context) == NULL)) { - // Complete Packet Header available - if (length >= 5) { - // Extract Optional Data Length - int optlen = 0; memcpy(&optlen, context->rxbuf + 1, sizeof(optlen)); - - // Complete Valid Packet available - if (optlen >= 0 && length >= (5 + optlen)) { - // Set Default Null Data - void * opt = NULL; - - // Extract Optional Data Pointer - if (optlen > 0) opt = context->rxbuf + 5; - - // Find Peer - SceNetAdhocMatchingMemberInternal * peer = findPeer(context, sendermac); - - // If we got the peer in the table already and are a parent, there is nothing left to be done. - // This is because the only way a parent can know of a child is via a join request... - // If we thus know of a possible child, then we already had a previous join request thus no need for double tapping. - if (peer != NULL && peer->lastping != 0 && context->mode == PSP_ADHOC_MATCHING_MODE_PARENT) { - WARN_LOG(Log::sceNet, "Join Event(2) Ignored"); - return; - } - - // New Peer - if (peer == NULL) { - // Allocate Memory - peer = (SceNetAdhocMatchingMemberInternal *)malloc(sizeof(SceNetAdhocMatchingMemberInternal)); - - // Allocated Memory - if (peer != NULL) { - // Clear Memory - memset(peer, 0, sizeof(SceNetAdhocMatchingMemberInternal)); - - // Copy Sender MAC - peer->mac = *sendermac; - - // Set Peer State - peer->state = PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST; - - // Initialize Ping Timer - peer->lastping = CoreTiming::GetGlobalTimeUsScaled(); //time_now_d()*1000000.0; - - peerlock.lock(); - // Link Peer into List - peer->next = context->peerlist; - context->peerlist = peer; - peerlock.unlock(); - - // Spawn Request Event - spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_REQUEST, sendermac, optlen, opt); - - // Return Success - return; - } - } - - // Existing Peer (this case is only reachable for P2P mode) - else { - // Set Peer State - peer->state = PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST; - - // Initialize Ping Timer - peer->lastping = CoreTiming::GetGlobalTimeUsScaled(); // Spawn Request Event spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_REQUEST, sendermac, optlen, opt); @@ -738,12 +729,28 @@ void actOnJoinPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sen return; } } + + // Existing Peer (this case is only reachable for P2P mode) + else { + // Set Peer State + peer->state = PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST; + + // Initialize Ping Timer + peer->lastping = CoreTiming::GetGlobalTimeUsScaled(); + + // Spawn Request Event + spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_REQUEST, sendermac, optlen, opt); + + // Return Success + return; + } } } - WARN_LOG(Log::sceNet, "Join Event(2) Rejected"); - // Auto-Reject Player - sendCancelPacket(context, sendermac, 0, NULL); } + + WARN_LOG(Log::sceNet, "Join Event(2) Rejected"); + // Auto-Reject Player + sendCancelPacket(context, sendermac, 0, NULL); } /** @@ -753,70 +760,80 @@ void actOnJoinPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sen * @param length Packet Length */ void actOnAcceptPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * sendermac, uint32_t length) { - // Not a parent context - if (context->mode != PSP_ADHOC_MATCHING_MODE_PARENT) { - // Don't have a master yet - if ((context->mode == PSP_ADHOC_MATCHING_MODE_CHILD && findParent(context) == NULL) || (context->mode == PSP_ADHOC_MATCHING_MODE_P2P && findP2P(context) == NULL)) { - // Complete Packet Header available - if (length >= 9) { - // Extract Optional Data Length - int optlen = 0; memcpy(&optlen, context->rxbuf + 1, sizeof(optlen)); + if (context->mode == PSP_ADHOC_MATCHING_MODE_PARENT) { + // Parent context + return; + } - // Extract Sibling Count - int siblingcount = 0; memcpy(&siblingcount, context->rxbuf + 5, sizeof(siblingcount)); - - // Complete Valid Packet available - if (optlen >= 0 && length >= (9LL + optlen + static_cast(sizeof(SceNetEtherAddr)) * siblingcount)) { - // Set Default Null Data - void * opt = NULL; - - // Extract Optional Data Pointer - if (optlen > 0) opt = context->rxbuf + 9; - - // Sibling MAC Array Null Data - SceNetEtherAddr * siblings = NULL; - - // Extract Optional Sibling MAC Array - if (siblingcount > 0) siblings = (SceNetEtherAddr *)(context->rxbuf + 9 + optlen); - - // Find Outgoing Request - SceNetAdhocMatchingMemberInternal * request = findOutgoingRequest(context); - - // We are waiting for a answer to our request... - if (request != NULL) { - // Find Peer - SceNetAdhocMatchingMemberInternal * peer = findPeer(context, sendermac); - - // It's the answer we wanted! - if (request == peer) { - // Change Peer State - peer->state = (context->mode == PSP_ADHOC_MATCHING_MODE_CHILD) ? (PSP_ADHOC_MATCHING_PEER_PARENT) : (PSP_ADHOC_MATCHING_PEER_P2P); - - // Remove Unneeded Peer Information - postAcceptCleanPeerList(context); - - // Add Sibling Peers - if (context->mode == PSP_ADHOC_MATCHING_MODE_CHILD) { - // Add existing siblings - postAcceptAddSiblings(context, siblingcount, siblings); - - // Add Self Peer to the following position (using peer->state = 0 to identify as Self) - addMember(context, &context->mac); - } - - // IMPORTANT! The Event Order here is ok! - // Internally the Event Stack appends to the front, so the order will be switched around. - - // Spawn Established Event - spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_ESTABLISHED, sendermac, 0, NULL); - - // Spawn Accept Event - spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_ACCEPT, sendermac, optlen, opt); - } - } - } - } + // Don't have a master yet + if ((context->mode == PSP_ADHOC_MATCHING_MODE_CHILD && findParent(context) == NULL) || (context->mode == PSP_ADHOC_MATCHING_MODE_P2P && findP2P(context) == NULL)) { + if (length < 9) { + // Incomplete Packet Header + return; } + + // Extract Optional Data Length + int optlen = 0; memcpy(&optlen, context->rxbuf + 1, sizeof(optlen)); + + // Extract Sibling Count + int siblingcount = 0; memcpy(&siblingcount, context->rxbuf + 5, sizeof(siblingcount)); + + if (optlen < 0 || length < (9LL + optlen + static_cast(sizeof(SceNetEtherAddr)) * siblingcount)) { + // Invalid packet + return; + } + + // Set Default Null Data + void * opt = NULL; + + // Extract Optional Data Pointer + if (optlen > 0) opt = context->rxbuf + 9; + + // Sibling MAC Array Null Data + SceNetEtherAddr * siblings = NULL; + + // Extract Optional Sibling MAC Array + if (siblingcount > 0) siblings = (SceNetEtherAddr *)(context->rxbuf + 9 + optlen); + + // Find Outgoing Request + SceNetAdhocMatchingMemberInternal * request = findOutgoingRequest(context); + + // We are waiting for an answer to our request... + if (request == NULL) { + return; + } + + // Find Peer + SceNetAdhocMatchingMemberInternal * peer = findPeer(context, sendermac); + + if (request != peer) { + // It's not the answer we wanted! + return; + } + + // Change Peer State + peer->state = (context->mode == PSP_ADHOC_MATCHING_MODE_CHILD) ? (PSP_ADHOC_MATCHING_PEER_PARENT) : (PSP_ADHOC_MATCHING_PEER_P2P); + + // Remove Unneeded Peer Information + postAcceptCleanPeerList(context); + + // Add Sibling Peers + if (context->mode == PSP_ADHOC_MATCHING_MODE_CHILD) { + // Add existing siblings + postAcceptAddSiblings(context, siblingcount, siblings); + + // Add Self Peer to the following position (using peer->state = 0 to identify as Self) + addMember(context, &context->mac); + } + + // IMPORTANT! The Event Order here is ok! + // Internally the Event Stack appends to the front, so the order will be switched around. + + // Spawn Established Event + spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_ESTABLISHED, sendermac, 0, NULL); + + // Spawn Accept Event + spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_ACCEPT, sendermac, optlen, opt); } } @@ -830,115 +847,121 @@ void actOnCancelPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * s // Find Peer SceNetAdhocMatchingMemberInternal * peer = findPeer(context, sendermac); - // Interest Condition fulfilled - if (peer != NULL) { - // Complete Packet Header available - if (length >= 5) { - // Extract Optional Data Length - int optlen = 0; memcpy(&optlen, context->rxbuf + 1, sizeof(optlen)); + if (peer == NULL) { + // Interest Condition not fulfilled + return; + } - // Complete Valid Packet available - if (optlen >= 0 && length >= (5 + optlen)) { - // Set Default Null Data - void * opt = NULL; + if (length < 5) { + // Incomplete Packet Header + return; + } - // Extract Optional Data Pointer - if (optlen > 0) opt = context->rxbuf + 5; + // Extract Optional Data Length + int optlen = 0; memcpy(&optlen, context->rxbuf + 1, sizeof(optlen)); - // Get Outgoing Join Request - SceNetAdhocMatchingMemberInternal* request = findOutgoingRequest(context); + if (optlen < 0 || length < (5 + optlen)) { + // Invalid packet + return; + } - // Child Mode - if (context->mode == PSP_ADHOC_MATCHING_MODE_CHILD) { - // Get Parent - SceNetAdhocMatchingMemberInternal* parent = findParent(context); + // Set Default Null Data + void * opt = NULL; - // Join Request denied - if (request == peer) { - // Spawn Deny Event - spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_DENY, sendermac, optlen, opt); + // Extract Optional Data Pointer + if (optlen > 0) opt = context->rxbuf + 5; - // Delete Peer from List - //deletePeer(context, peer); - peer->lastping = 0; - } + // Get Outgoing Join Request + SceNetAdhocMatchingMemberInternal* request = findOutgoingRequest(context); - // Kicked from Room - else if (parent == peer) { - // Iterate Peers - SceNetAdhocMatchingMemberInternal * item = context->peerlist; - for (; item != NULL; item = item->next) { - // Established Peer - if (item->state == PSP_ADHOC_MATCHING_PEER_CHILD || item->state == PSP_ADHOC_MATCHING_PEER_PARENT) { - // Spawn Leave / Kick Event - spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_LEAVE, &item->mac, optlen, opt); - } - } + // Child Mode + if (context->mode == PSP_ADHOC_MATCHING_MODE_CHILD) { + // Get Parent + SceNetAdhocMatchingMemberInternal* parent = findParent(context); - // Delete Peer from List - clearPeerList(context); - } - } + // Join Request denied + if (request == peer) { + // Spawn Deny Event + spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_DENY, sendermac, optlen, opt); - // Parent Mode - else if (context->mode == PSP_ADHOC_MATCHING_MODE_PARENT) { - // Cancel Join Request - if (peer->state == PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST) { - // Spawn Request Cancel Event - spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_CANCEL, sendermac, optlen, opt); + // Delete Peer from List + //deletePeer(context, peer); + peer->lastping = 0; + } - // Delete Peer from List - //deletePeer(context, peer); - peer->lastping = 0; - } - - // Leave Room - else if (peer->state == PSP_ADHOC_MATCHING_PEER_CHILD) { - // Spawn Leave / Kick Event - spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_LEAVE, sendermac, optlen, opt); - - // Delete Peer from List - //deletePeer(context, peer); - peer->lastping = 0; - } - } - - // P2P Mode - else { - // Get P2P Partner - SceNetAdhocMatchingMemberInternal* p2p = findP2P(context); - - // Join Request denied - if (request == peer) { - // Spawn Deny Event - spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_DENY, sendermac, optlen, opt); - - // FIXME: Delete Peer from List? - // Instead of removing the peer immediately, we should let it timedout, otherwise inviter in Crazy Taxi will wait forever without getting timedout, since handleTimeout need the peer data to exist. - peer->lastping = 0; - } - - // Kicked from Room - else if (p2p == peer) { - // Spawn Leave / Kick Event - spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_LEAVE, sendermac, optlen, opt); - - // Delete Peer from List - //deletePeer(context, peer); - peer->lastping = 0; - } - - // Cancel Join Request - else if (peer->state == PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST) { - // Spawn Request Cancel Event - spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_CANCEL, sendermac, optlen, opt); - - // Delete Peer from List - //deletePeer(context, peer); - peer->lastping = 0; - } + // Kicked from Room + else if (parent == peer) { + // Iterate Peers + SceNetAdhocMatchingMemberInternal * item = context->peerlist; + for (; item != NULL; item = item->next) { + // Established Peer + if (item->state == PSP_ADHOC_MATCHING_PEER_CHILD || item->state == PSP_ADHOC_MATCHING_PEER_PARENT) { + // Spawn Leave / Kick Event + spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_LEAVE, &item->mac, optlen, opt); } } + + // Delete Peer from List + clearPeerList(context); + } + } + + // Parent Mode + else if (context->mode == PSP_ADHOC_MATCHING_MODE_PARENT) { + // Cancel Join Request + if (peer->state == PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST) { + // Spawn Request Cancel Event + spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_CANCEL, sendermac, optlen, opt); + + // Delete Peer from List + //deletePeer(context, peer); + peer->lastping = 0; + } + + // Leave Room + else if (peer->state == PSP_ADHOC_MATCHING_PEER_CHILD) { + // Spawn Leave / Kick Event + spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_LEAVE, sendermac, optlen, opt); + + // Delete Peer from List + //deletePeer(context, peer); + peer->lastping = 0; + } + } + + // P2P Mode + else { + // Get P2P Partner + SceNetAdhocMatchingMemberInternal* p2p = findP2P(context); + + // Join Request denied + if (request == peer) { + // Spawn Deny Event + spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_DENY, sendermac, optlen, opt); + + // FIXME: Delete Peer from List? + // Instead of removing the peer immediately, we should let it timedout, otherwise inviter in Crazy Taxi will wait forever without getting timedout, since handleTimeout need the peer data to exist. + peer->lastping = 0; + } + + // Kicked from Room + else if (p2p == peer) { + // Spawn Leave / Kick Event + spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_LEAVE, sendermac, optlen, opt); + + // Delete Peer from List + //deletePeer(context, peer); + peer->lastping = 0; + } + + // Cancel Join Request + else if (peer->state == PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST) { + // Spawn Request Cancel Event + spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_CANCEL, sendermac, optlen, opt); + + // Delete Peer from List + //deletePeer(context, peer); + peer->lastping = 0; } } } @@ -986,44 +1009,49 @@ void actOnBirthPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * se // Find Peer SceNetAdhocMatchingMemberInternal * peer = findPeer(context, sendermac); - // Valid Circumstances - if (peer != NULL && context->mode == PSP_ADHOC_MATCHING_MODE_CHILD && peer == findParent(context)) { - // Complete Packet available - if (length >= (1 + sizeof(SceNetEtherAddr))) { - // Extract Child MAC - SceNetEtherAddr mac; - memcpy(&mac, context->rxbuf + 1, sizeof(SceNetEtherAddr)); - - // Allocate Memory - SceNetAdhocMatchingMemberInternal * sibling = (SceNetAdhocMatchingMemberInternal *)malloc(sizeof(SceNetAdhocMatchingMemberInternal)); - - // Allocated Memory - if (sibling != NULL) { - // Clear Memory - memset(sibling, 0, sizeof(SceNetAdhocMatchingMemberInternal)); - - // Save MAC Address - sibling->mac = mac; - - // Set Peer State - sibling->state = PSP_ADHOC_MATCHING_PEER_CHILD; - - // Initialize Ping Timer - sibling->lastping = CoreTiming::GetGlobalTimeUsScaled(); //time_now_d()*1000000.0; - - peerlock.lock(); - - // Link Peer - sibling->next = context->peerlist; - context->peerlist = sibling; - - peerlock.unlock(); - - // Spawn Established Event. FIXME: ESTABLISHED event should only be triggered for Parent/P2P peer? - //spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_ESTABLISHED, &sibling->mac, 0, NULL); - } - } + if (peer == NULL || context->mode != PSP_ADHOC_MATCHING_MODE_CHILD || peer != findParent(context)) { + // Invalid Circumstances + return; } + if (length < (1 + sizeof(SceNetEtherAddr))) { + // Incomplete packet + return; + } + + // Extract Child MAC + SceNetEtherAddr mac; + memcpy(&mac, context->rxbuf + 1, sizeof(SceNetEtherAddr)); + + // Allocate Memory + SceNetAdhocMatchingMemberInternal * sibling = (SceNetAdhocMatchingMemberInternal *)malloc(sizeof(SceNetAdhocMatchingMemberInternal)); + + if (sibling == NULL) { + // Failed to allocate memory + return; + } + + // Clear Memory + memset(sibling, 0, sizeof(SceNetAdhocMatchingMemberInternal)); + + // Save MAC Address + sibling->mac = mac; + + // Set Peer State + sibling->state = PSP_ADHOC_MATCHING_PEER_CHILD; + + // Initialize Ping Timer + sibling->lastping = CoreTiming::GetGlobalTimeUsScaled(); //time_now_d()*1000000.0; + + peerlock.lock(); + + // Link Peer + sibling->next = context->peerlist; + context->peerlist = sibling; + + peerlock.unlock(); + + // Spawn Established Event. FIXME: ESTABLISHED event should only be triggered for Parent/P2P peer? + //spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_ESTABLISHED, &sibling->mac, 0, NULL); } /** @@ -1036,27 +1064,33 @@ void actOnDeathPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * se // Find Peer SceNetAdhocMatchingMemberInternal * peer = findPeer(context, sendermac); - // Valid Circumstances - if (peer != NULL && context->mode == PSP_ADHOC_MATCHING_MODE_CHILD && peer == findParent(context)) { - // Complete Packet available - if (length >= (1 + sizeof(SceNetEtherAddr))) { - // Extract Child MAC - SceNetEtherAddr mac; - memcpy(&mac, context->rxbuf + 1, sizeof(SceNetEtherAddr)); - - // Find Peer - SceNetAdhocMatchingMemberInternal * deadkid = findPeer(context, &mac); - - // Valid Sibling - if (deadkid->state == PSP_ADHOC_MATCHING_PEER_CHILD) { - // Spawn Leave Event - spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_LEAVE, &mac, 0, NULL); - - // Delete Peer - deletePeer(context, deadkid); - } - } + if (peer == NULL || context->mode != PSP_ADHOC_MATCHING_MODE_CHILD || peer != findParent(context)) { + // Invalid Circumstances + return; } + + if (length < (1 + sizeof(SceNetEtherAddr))) { + // Incomplete packet + return; + } + + // Extract Child MAC + SceNetEtherAddr mac; + memcpy(&mac, context->rxbuf + 1, sizeof(SceNetEtherAddr)); + + // Find Peer + SceNetAdhocMatchingMemberInternal * deadkid = findPeer(context, &mac); + + if (deadkid->state != PSP_ADHOC_MATCHING_PEER_CHILD) { + // Invalid Sibling + return; + } + + // Spawn Leave Event + spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_LEAVE, &mac, 0, NULL); + + // Delete Peer + deletePeer(context, deadkid); } /** @@ -1068,33 +1102,35 @@ void actOnByePacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * send // Find Peer SceNetAdhocMatchingMemberInternal * peer = findPeer(context, sendermac); - // We know this guy - if (peer != NULL) { - // P2P or Child Bye. FIXME: Should we allow BYE Event to intervene joining process of Parent-Child too just like P2P Mode? (ie. Crazy Taxi uses P2P Mode) - if ((context->mode == PSP_ADHOC_MATCHING_MODE_PARENT && peer->state == PSP_ADHOC_MATCHING_PEER_CHILD) || - (context->mode == PSP_ADHOC_MATCHING_MODE_CHILD && peer->state == PSP_ADHOC_MATCHING_PEER_CHILD) || - (context->mode == PSP_ADHOC_MATCHING_MODE_P2P && - (peer->state == PSP_ADHOC_MATCHING_PEER_P2P || peer->state == PSP_ADHOC_MATCHING_PEER_OFFER || peer->state == PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST || peer->state == PSP_ADHOC_MATCHING_PEER_OUTGOING_REQUEST || peer->state == PSP_ADHOC_MATCHING_PEER_CANCEL_IN_PROGRESS))) - { - if (context->mode != PSP_ADHOC_MATCHING_MODE_CHILD) { - // Spawn Leave / Kick Event. FIXME: DISCONNECT event should only be triggered on Parent/P2P mode and for Parent/P2P peer? - spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_BYE, sendermac, 0, NULL); - } + if (peer == NULL) { + // We don't know this guy + return; + } - // Delete Peer - deletePeer(context, peer); - // Instead of removing peer immediately, We should give a little time before removing the peer and let it timed out? just in case the game is in the middle of communicating with the peer on another thread so it won't recognize it as Unknown peer - //peer->lastping = CoreTiming::GetGlobalTimeUsScaled(); - } - - // Parent Bye - else if (context->mode == PSP_ADHOC_MATCHING_MODE_CHILD && peer->state == PSP_ADHOC_MATCHING_PEER_PARENT) { + // P2P or Child Bye. FIXME: Should we allow BYE Event to intervene joining process of Parent-Child too just like P2P Mode? (ie. Crazy Taxi uses P2P Mode) + if ((context->mode == PSP_ADHOC_MATCHING_MODE_PARENT && peer->state == PSP_ADHOC_MATCHING_PEER_CHILD) || + (context->mode == PSP_ADHOC_MATCHING_MODE_CHILD && peer->state == PSP_ADHOC_MATCHING_PEER_CHILD) || + (context->mode == PSP_ADHOC_MATCHING_MODE_P2P && + (peer->state == PSP_ADHOC_MATCHING_PEER_P2P || peer->state == PSP_ADHOC_MATCHING_PEER_OFFER || peer->state == PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST || peer->state == PSP_ADHOC_MATCHING_PEER_OUTGOING_REQUEST || peer->state == PSP_ADHOC_MATCHING_PEER_CANCEL_IN_PROGRESS))) + { + if (context->mode != PSP_ADHOC_MATCHING_MODE_CHILD) { // Spawn Leave / Kick Event. FIXME: DISCONNECT event should only be triggered on Parent/P2P mode and for Parent/P2P peer? spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_BYE, sendermac, 0, NULL); - - // Delete Peer from List - clearPeerList(context); } + + // Delete Peer + deletePeer(context, peer); + // Instead of removing peer immediately, We should give a little time before removing the peer and let it timed out? just in case the game is in the middle of communicating with the peer on another thread so it won't recognize it as Unknown peer + //peer->lastping = CoreTiming::GetGlobalTimeUsScaled(); + } + + // Parent Bye + else if (context->mode == PSP_ADHOC_MATCHING_MODE_CHILD && peer->state == PSP_ADHOC_MATCHING_PEER_PARENT) { + // Spawn Leave / Kick Event. FIXME: DISCONNECT event should only be triggered on Parent/P2P mode and for Parent/P2P peer? + spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_BYE, sendermac, 0, NULL); + + // Delete Peer from List + clearPeerList(context); } } From f7289ff22a13bb544bb90585b20da8308fc64082 Mon Sep 17 00:00:00 2001 From: Nemoumbra Date: Sat, 4 Jan 2025 23:52:31 +0300 Subject: [PATCH 5/8] Simplified 2 more matching funcs --- Core/HLE/sceNetAdhocMatching.cpp | 91 +++++++++++++++----------------- 1 file changed, 44 insertions(+), 47 deletions(-) diff --git a/Core/HLE/sceNetAdhocMatching.cpp b/Core/HLE/sceNetAdhocMatching.cpp index 6567373141..39fee7454a 100644 --- a/Core/HLE/sceNetAdhocMatching.cpp +++ b/Core/HLE/sceNetAdhocMatching.cpp @@ -1561,49 +1561,49 @@ void netAdhocMatchingValidateLoopMemory() { int NetAdhocMatching_Stop(int matchingId) { SceNetAdhocMatchingContext* item = findMatchingContext(matchingId); - - if (item != NULL) { - // This will cause using PdpRecv on this socket to return ERROR_NET_ADHOC_SOCKET_ALERTED (Based on Ys vs. Sora no Kiseki when tested with JPCSP + prx files). Is this used to abort inprogress socket activity? - NetAdhoc_SetSocketAlert(item->socket, ADHOC_F_ALERTRECV); - - item->inputRunning = false; - if (item->inputThread.joinable()) { - item->inputThread.join(); - } - - item->eventRunning = false; - if (item->eventThread.joinable()) { - item->eventThread.join(); - } - - // Stop fake PSP Thread. - // kernelObjects may already been cleared early during a Shutdown, thus trying to access it may generates Warning/Error in the log - if (matchingThreads[item->matching_thid] > 0 && strcmp(__KernelGetThreadName(matchingThreads[item->matching_thid]), "ERROR") != 0) { - __KernelStopThread(matchingThreads[item->matching_thid], SCE_KERNEL_ERROR_THREAD_TERMINATED, "AdhocMatching stopped"); - __KernelDeleteThread(matchingThreads[item->matching_thid], SCE_KERNEL_ERROR_THREAD_TERMINATED, "AdhocMatching deleted"); - } - matchingThreads[item->matching_thid] = 0; - - // Make sure nobody locking/using the socket - item->socketlock->lock(); - // Delete the socket - NetAdhocPdp_Delete(item->socket, 0); // item->connected = (sceNetAdhocPdpDelete(item->socket, 0) < 0); - item->socketlock->unlock(); - - // Multithreading Lock - peerlock.lock(); - - // Remove your own MAC, or All members, or don't remove at all or we should do this on MatchingDelete ? - clearPeerList(item); //deleteAllMembers(item); - - item->running = 0; - netAdhocMatchingStarted--; - - // Multithreading Unlock - peerlock.unlock(); - + if (item == NULL) { + return 0; } + // This will cause using PdpRecv on this socket to return ERROR_NET_ADHOC_SOCKET_ALERTED (Based on Ys vs. Sora no Kiseki when tested with JPCSP + prx files). Is this used to abort inprogress socket activity? + NetAdhoc_SetSocketAlert(item->socket, ADHOC_F_ALERTRECV); + + item->inputRunning = false; + if (item->inputThread.joinable()) { + item->inputThread.join(); + } + + item->eventRunning = false; + if (item->eventThread.joinable()) { + item->eventThread.join(); + } + + // Stop fake PSP Thread. + // kernelObjects may already been cleared early during a Shutdown, thus trying to access it may generates Warning/Error in the log + if (matchingThreads[item->matching_thid] > 0 && strcmp(__KernelGetThreadName(matchingThreads[item->matching_thid]), "ERROR") != 0) { + __KernelStopThread(matchingThreads[item->matching_thid], SCE_KERNEL_ERROR_THREAD_TERMINATED, "AdhocMatching stopped"); + __KernelDeleteThread(matchingThreads[item->matching_thid], SCE_KERNEL_ERROR_THREAD_TERMINATED, "AdhocMatching deleted"); + } + matchingThreads[item->matching_thid] = 0; + + // Make sure nobody locking/using the socket + item->socketlock->lock(); + // Delete the socket + NetAdhocPdp_Delete(item->socket, 0); // item->connected = (sceNetAdhocPdpDelete(item->socket, 0) < 0); + item->socketlock->unlock(); + + // Multithreading Lock + peerlock.lock(); + + // Remove your own MAC, or All members, or don't remove at all or we should do this on MatchingDelete ? + clearPeerList(item); //deleteAllMembers(item); + + item->running = 0; + netAdhocMatchingStarted--; + + // Multithreading Unlock + peerlock.unlock(); + return 0; } @@ -1614,12 +1614,12 @@ int sceNetAdhocMatchingStop(int matchingId) { } int NetAdhocMatching_Delete(int matchingId) { + // Multithreading Lock + std::lock_guard peer_guard(peerlock); + // Previous Context Reference SceNetAdhocMatchingContext* prev = NULL; - // Multithreading Lock - peerlock.lock(); //contextlock.lock(); - // Context Pointer SceNetAdhocMatchingContext* item = contexts; @@ -1672,9 +1672,6 @@ int NetAdhocMatching_Delete(int matchingId) { prev = item; } - // Multithreading Unlock - peerlock.unlock(); //contextlock.unlock(); - return 0; } From 3497ccacb50d4c25856553136d88b829a298992b Mon Sep 17 00:00:00 2001 From: Nemoumbra Date: Sun, 5 Jan 2025 00:08:20 +0300 Subject: [PATCH 6/8] Hadooken codestyle fix + cleaned up NetAdhocMatching_Start --- Core/HLE/sceNetAdhocMatching.cpp | 294 +++++++++++++++---------------- 1 file changed, 144 insertions(+), 150 deletions(-) diff --git a/Core/HLE/sceNetAdhocMatching.cpp b/Core/HLE/sceNetAdhocMatching.cpp index 39fee7454a..3f2e861ae8 100644 --- a/Core/HLE/sceNetAdhocMatching.cpp +++ b/Core/HLE/sceNetAdhocMatching.cpp @@ -1743,169 +1743,163 @@ static int sceNetAdhocMatchingCreate(int mode, int maxnum, int port, int rxbufle SceNetAdhocMatchingHandler handler; handler.entryPoint = callbackAddr; - // Library initialized - if (netAdhocMatchingInited) { - // Valid Member Limit - if (maxnum > 1 && maxnum <= 16) { - // Valid Receive Buffer size - if (rxbuflen >= 1) { //1024 //200 on DBZ Shin Budokai 2 - // Valid Arguments - if (mode >= 1 && mode <= 3) { - - // Iterate Matching Contexts - SceNetAdhocMatchingContext * item = contexts; - for (; item != NULL; item = item->next) { - // Port Match found - if (item->port == port) - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_PORT_IN_USE, "adhoc matching port in use"); - } - - // Allocate Context Memory - SceNetAdhocMatchingContext * context = (SceNetAdhocMatchingContext *)malloc(sizeof(SceNetAdhocMatchingContext)); - - // Allocated Memory - if (context != NULL) { - // Create PDP Socket - SceNetEtherAddr localmac; - getLocalMac(&localmac); - - // Clear Memory - memset(context, 0, sizeof(SceNetAdhocMatchingContext)); - - // Allocate Receive Buffer - context->rxbuf = (uint8_t*)malloc(rxbuflen); - - // Allocated Memory - if (context->rxbuf != NULL) { - // Clear Memory - memset(context->rxbuf, 0, rxbuflen); - - // Fill in Context Data - context->id = findFreeMatchingID(); - context->mode = mode; - context->maxpeers = maxnum; - context->port = port; - context->rxbuflen = rxbuflen; - context->resendcounter = init_count; - context->resend_int = rexmt_int; // used as ack timeout on lost packet (ie. not receiving anything after sending)? - context->hello_int = hello_int; // client might set this to 0 - if (keepalive_int < 1) context->keepalive_int = PSP_ADHOCCTL_PING_TIMEOUT; else context->keepalive_int = keepalive_int; // client might set this to 0 - context->keepalivecounter = init_count; // used to multiply keepalive_int as timeout - context->timeout = (((u64)(keepalive_int)+(u64)rexmt_int) * (u64)init_count); - context->timeout += 500000; // For internet play we need higher timeout than what the game wanted - context->handler = handler; - context->peerPort = new std::map(); - - // Fill in Selfpeer - context->mac = localmac; - - // Create locks - context->socketlock = new std::recursive_mutex; - context->eventlock = new std::recursive_mutex; - context->inputlock = new std::recursive_mutex; - - // Multithreading Lock - peerlock.lock(); //contextlock.lock(); - - // Add Callback Handler - context->handler.entryPoint = callbackAddr; - context->matching_thid = static_cast(matchingThreads.size()); - matchingThreads.push_back(0); - - // Link Context - //context->connected = true; - context->next = contexts; - contexts = context; - - // Multithreading UnLock - peerlock.unlock(); //contextlock.unlock(); - - // Just to make sure Adhoc is already connected - //hleDelayResult(context->id, "give time to init/cleanup", adhocEventDelayMS * 1000); - - // Return Matching ID - return hleLogDebug(Log::sceNet, context->id, "success"); - } - - // Free Memory - free(context); - } - - // Out of Memory - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_NO_SPACE, "adhoc matching no space"); - } - - // InvalidERROR_NET_Arguments - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_INVALID_ARG, "adhoc matching invalid arg"); - } - - // Invalid Receive Buffer Size - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_RXBUF_TOO_SHORT, "adhoc matching rxbuf too short"); - } + if (!netAdhocMatchingInited) { + // Uninitialized Library + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_NOT_INITIALIZED, "adhoc matching not initialized"); + } + if (maxnum <= 1 || maxnum > 16) { // Invalid Member Limit return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_INVALID_MAXNUM, "adhoc matching invalid maxnum"); } - // Uninitialized Library - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_NOT_INITIALIZED, "adhoc matching not initialized"); + + if (rxbuflen < 1) { + // Invalid Receive Buffer Size + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_RXBUF_TOO_SHORT, "adhoc matching rxbuf too short"); + } + + if (mode < 1 || mode > 3) { + // InvalidERROR_NET_Arguments + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_INVALID_ARG, "adhoc matching invalid arg"); + } + + // Iterate Matching Contexts + SceNetAdhocMatchingContext * item = contexts; + for (; item != NULL; item = item->next) { + // Port Match found + if (item->port == port) + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_PORT_IN_USE, "adhoc matching port in use"); + } + + // Allocate Context Memory + SceNetAdhocMatchingContext * context = (SceNetAdhocMatchingContext *)malloc(sizeof(SceNetAdhocMatchingContext)); + + // Allocated Memory + if (context != NULL) { + // Create PDP Socket + SceNetEtherAddr localmac; + getLocalMac(&localmac); + + // Clear Memory + memset(context, 0, sizeof(SceNetAdhocMatchingContext)); + + // Allocate Receive Buffer + context->rxbuf = (uint8_t*)malloc(rxbuflen); + + // Allocated Memory + if (context->rxbuf != NULL) { + // Clear Memory + memset(context->rxbuf, 0, rxbuflen); + + // Fill in Context Data + context->id = findFreeMatchingID(); + context->mode = mode; + context->maxpeers = maxnum; + context->port = port; + context->rxbuflen = rxbuflen; + context->resendcounter = init_count; + context->resend_int = rexmt_int; // used as ack timeout on lost packet (ie. not receiving anything after sending)? + context->hello_int = hello_int; // client might set this to 0 + if (keepalive_int < 1) context->keepalive_int = PSP_ADHOCCTL_PING_TIMEOUT; else context->keepalive_int = keepalive_int; // client might set this to 0 + context->keepalivecounter = init_count; // used to multiply keepalive_int as timeout + context->timeout = (((u64)(keepalive_int)+(u64)rexmt_int) * (u64)init_count); + context->timeout += 500000; // For internet play we need higher timeout than what the game wanted + context->handler = handler; + context->peerPort = new std::map(); + + // Fill in Selfpeer + context->mac = localmac; + + // Create locks + context->socketlock = new std::recursive_mutex; + context->eventlock = new std::recursive_mutex; + context->inputlock = new std::recursive_mutex; + + // Multithreading Lock + peerlock.lock(); //contextlock.lock(); + + // Add Callback Handler + context->handler.entryPoint = callbackAddr; + context->matching_thid = static_cast(matchingThreads.size()); + matchingThreads.push_back(0); + + // Link Context + //context->connected = true; + context->next = contexts; + contexts = context; + + // Multithreading UnLock + peerlock.unlock(); //contextlock.unlock(); + + // Just to make sure Adhoc is already connected + //hleDelayResult(context->id, "give time to init/cleanup", adhocEventDelayMS * 1000); + + // Return Matching ID + return hleLogDebug(Log::sceNet, context->id, "success"); + } + + // Free Memory + free(context); + } + + // Out of Memory + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_NO_SPACE, "adhoc matching no space"); } int NetAdhocMatching_Start(int matchingId, int evthPri, int evthPartitionId, int evthStack, int inthPri, int inthPartitionId, int inthStack, int optLen, u32 optDataAddr) { // Multithreading Lock - peerlock.lock(); + std::lock_guard peer_guard(peerlock); SceNetAdhocMatchingContext* item = findMatchingContext(matchingId); - if (item != NULL) { - //sceNetAdhocMatchingSetHelloOpt(matchingId, optLen, optDataAddr); //SetHelloOpt only works when context is running - if ((optLen > 0) && Memory::IsValidAddress(optDataAddr)) { - // Allocate the memory and copy the content - free(item->hello); - item->hello = (uint8_t*)malloc(optLen); - if (item->hello != NULL) { - Memory::Memcpy(item->hello, optDataAddr, optLen); - item->hellolen = optLen; - item->helloAddr = optDataAddr; - } - //else return ERROR_NET_ADHOC_MATCHING_NO_SPACE; //Faking success to prevent GTA:VCS from stuck unable to choose host/join menu - } - //else return ERROR_NET_ADHOC_MATCHING_INVALID_ARG; // ERROR_NET_ADHOC_MATCHING_INVALID_OPTLEN; // Returning Not Success will cause GTA:VC stuck unable to choose host/join menu - - // Create PDP Socket - int sock = sceNetAdhocPdpCreate((const char*)&item->mac, static_cast(item->port), item->rxbuflen, 0); - item->socket = sock; - if (sock < 1) { - peerlock.unlock(); - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_PORT_IN_USE, "adhoc matching port in use"); - } - - // Create & Start the Fake PSP Thread ("matching_ev%d" and "matching_io%d") - netAdhocMatchingValidateLoopMemory(); - std::string thrname = std::string("MatchingThr") + std::to_string(matchingId); - matchingThreads[item->matching_thid] = sceKernelCreateThread(thrname.c_str(), matchingThreadHackAddr, evthPri, evthStack, 0, 0); - //item->matchingThread = new HLEHelperThread(thrname.c_str(), "sceNetAdhocMatching", "__NetMatchingCallbacks", inthPri, inthStack); - if (matchingThreads[item->matching_thid] > 0) { - sceKernelStartThread(matchingThreads[item->matching_thid], 0, 0); //sceKernelStartThread(context->event_thid, sizeof(context), &context); - //item->matchingThread->Start(matchingId, 0); - } - - //Create the threads - if (!item->eventRunning) { - item->eventRunning = true; - item->eventThread = std::thread(matchingEventThread, matchingId); - } - if (!item->inputRunning) { - item->inputRunning = true; - item->inputThread = std::thread(matchingInputThread, matchingId); - } - - item->running = 1; - netAdhocMatchingStarted++; + if (item == NULL) { + // return ERROR_NET_ADHOC_MATCHING_INVALID_ID; //Faking success to prevent GTA:VCS from stuck unable to choose host/join menu + return 0; } - //else return ERROR_NET_ADHOC_MATCHING_INVALID_ID; //Faking success to prevent GTA:VCS from stuck unable to choose host/join menu - // Multithreading Unlock - peerlock.unlock(); + //sceNetAdhocMatchingSetHelloOpt(matchingId, optLen, optDataAddr); //SetHelloOpt only works when context is running + if ((optLen > 0) && Memory::IsValidAddress(optDataAddr)) { + // Allocate the memory and copy the content + free(item->hello); + item->hello = (uint8_t*)malloc(optLen); + if (item->hello != NULL) { + Memory::Memcpy(item->hello, optDataAddr, optLen); + item->hellolen = optLen; + item->helloAddr = optDataAddr; + } + //else return ERROR_NET_ADHOC_MATCHING_NO_SPACE; //Faking success to prevent GTA:VCS from stuck unable to choose host/join menu + } + //else return ERROR_NET_ADHOC_MATCHING_INVALID_ARG; // ERROR_NET_ADHOC_MATCHING_INVALID_OPTLEN; // Returning Not Success will cause GTA:VC stuck unable to choose host/join menu + + // Create PDP Socket + int sock = sceNetAdhocPdpCreate((const char*)&item->mac, static_cast(item->port), item->rxbuflen, 0); + item->socket = sock; + if (sock < 1) { + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_PORT_IN_USE, "adhoc matching port in use"); + } + + // Create & Start the Fake PSP Thread ("matching_ev%d" and "matching_io%d") + netAdhocMatchingValidateLoopMemory(); + std::string thrname = std::string("MatchingThr") + std::to_string(matchingId); + matchingThreads[item->matching_thid] = sceKernelCreateThread(thrname.c_str(), matchingThreadHackAddr, evthPri, evthStack, 0, 0); + //item->matchingThread = new HLEHelperThread(thrname.c_str(), "sceNetAdhocMatching", "__NetMatchingCallbacks", inthPri, inthStack); + if (matchingThreads[item->matching_thid] > 0) { + sceKernelStartThread(matchingThreads[item->matching_thid], 0, 0); //sceKernelStartThread(context->event_thid, sizeof(context), &context); + //item->matchingThread->Start(matchingId, 0); + } + + //Create the threads + if (!item->eventRunning) { + item->eventRunning = true; + item->eventThread = std::thread(matchingEventThread, matchingId); + } + if (!item->inputRunning) { + item->inputRunning = true; + item->inputThread = std::thread(matchingInputThread, matchingId); + } + + item->running = 1; + netAdhocMatchingStarted++; return 0; } From 94f4ad200bd06cbba1144b6f9c191561914f7798 Mon Sep 17 00:00:00 2001 From: Nemoumbra Date: Sun, 5 Jan 2025 00:46:42 +0300 Subject: [PATCH 7/8] Hadooken fixes: sceNetAdhocMatchingSelectTarget, NetAdhocMatching_CancelTargetWithOpt --- Core/HLE/sceNetAdhocMatching.cpp | 376 +++++++++++++++---------------- 1 file changed, 185 insertions(+), 191 deletions(-) diff --git a/Core/HLE/sceNetAdhocMatching.cpp b/Core/HLE/sceNetAdhocMatching.cpp index 3f2e861ae8..251ca17eb9 100644 --- a/Core/HLE/sceNetAdhocMatching.cpp +++ b/Core/HLE/sceNetAdhocMatching.cpp @@ -1936,211 +1936,205 @@ static int sceNetAdhocMatchingSelectTarget(int matchingId, const char *macAddres if (!g_Config.bEnableWlan) return -1; - // Initialized Library - if (netAdhocMatchingInited) { - // Valid Arguments - if (macAddress != NULL) { - SceNetEtherAddr * target = (SceNetEtherAddr *)macAddress; - - // Find Matching Context for ID - SceNetAdhocMatchingContext * context = findMatchingContext(matchingId); - - // Found Matching Context - if (context != NULL) { - // Running Context - if (context->running) { - // Search Result - SceNetAdhocMatchingMemberInternal * peer = findPeer(context, (SceNetEtherAddr *)target); - - // Found Peer in List - if (peer != NULL) { - // Valid Optional Data Length - if ((optLen == 0) || (optLen > 0 && optDataPtr != 0)) { - void * opt = NULL; - if (Memory::IsValidAddress(optDataPtr)) opt = Memory::GetPointerWriteUnchecked(optDataPtr); - // Host Mode - if (context->mode == PSP_ADHOC_MATCHING_MODE_PARENT) { - // Already Connected - if (peer->state == PSP_ADHOC_MATCHING_PEER_CHILD) return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_ALREADY_ESTABLISHED, "adhocmatching already established"); - - // Not enough space - if (countChildren(context) == (context->maxpeers - 1)) return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_EXCEED_MAXNUM, "adhocmatching exceed maxnum"); - - // Requesting Peer - if (peer->state == PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST) { - // Accept Peer in Group - peer->state = PSP_ADHOC_MATCHING_PEER_CHILD; - - // Sending order may need to be reversed since Stack appends to the front, so the order will be switched around. - - // Tell Children about new Sibling - sendBirthMessage(context, peer); - - // Spawn Established Event - //spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_ESTABLISHED, target, 0, NULL); - - // Send Accept Confirmation to Peer - sendAcceptMessage(context, peer, optLen, opt); - - // Return Success - return 0; - } - } - - // Client Mode - else if (context->mode == PSP_ADHOC_MATCHING_MODE_CHILD) { - // Already connected - if (findParent(context) != NULL) return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_ALREADY_ESTABLISHED, "adhocmatching already established"); - - // Outgoing Request in Progress - if (findOutgoingRequest(context) != NULL) return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_REQUEST_IN_PROGRESS, "adhocmatching request in progress"); - - // Valid Offer - if (peer->state == PSP_ADHOC_MATCHING_PEER_OFFER) { - // Switch into Join Request Mode - peer->state = PSP_ADHOC_MATCHING_PEER_OUTGOING_REQUEST; - - // Send Join Request to Peer - sendJoinRequest(context, peer, optLen, opt); - - // Return Success - return 0; - } - } - - // P2P Mode - else { - // Already connected - if (findP2P(context) != NULL) return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_ALREADY_ESTABLISHED, "adhocmatching already established"); - - // Outgoing Request in Progress - if (findOutgoingRequest(context) != NULL) return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_REQUEST_IN_PROGRESS, "adhocmatching request in progress"); - - // Join Request Mode - if (peer->state == PSP_ADHOC_MATCHING_PEER_OFFER) { - // Switch into Join Request Mode - peer->state = PSP_ADHOC_MATCHING_PEER_OUTGOING_REQUEST; - - // Send Join Request to Peer - sendJoinRequest(context, peer, optLen, opt); - - // Return Success - return 0; - } - - // Requesting Peer - else if (peer->state == PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST) { - // Accept Peer in Group - peer->state = PSP_ADHOC_MATCHING_PEER_P2P; - - // Tell Children about new Sibling - //sendBirthMessage(context, peer); - // Send Accept Confirmation to Peer - sendAcceptMessage(context, peer, optLen, opt); - - // Return Success - return 0; - } - } - - // How did this happen?! It shouldn't! - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_TARGET_NOT_READY, "adhocmatching target not ready"); - } - - // Invalid Optional Data Length - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_INVALID_OPTLEN, "adhocmatching invalid optlen"); - } - - // Peer not found - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_UNKNOWN_TARGET, "adhocmatching unknown target"); - } - - // Idle Context - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_NOT_RUNNING, "adhocmatching not running"); - } - - // Invalid Matching ID - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_INVALID_ID, "adhocmatching invalid id"); - } + if (!netAdhocMatchingInited) { + // Uninitialized Library + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_NOT_INITIALIZED, "adhocmatching not initialized"); + } + if (macAddress == NULL) { // Invalid Arguments return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_INVALID_ARG, "adhocmatching invalid arg"); } - // Uninitialized Library - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_NOT_INITIALIZED, "adhocmatching not initialized"); + SceNetEtherAddr * target = (SceNetEtherAddr *)macAddress; + + // Find Matching Context for ID + SceNetAdhocMatchingContext * context = findMatchingContext(matchingId); + + if (context == NULL) { + // Invalid Matching ID + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_INVALID_ID, "adhocmatching invalid id"); + } + + if (!context->running) { + // Idle Context + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_NOT_RUNNING, "adhocmatching not running"); + } + + // Search Result + SceNetAdhocMatchingMemberInternal * peer = findPeer(context, (SceNetEtherAddr *)target); + + if (peer == NULL) { + // Peer not found + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_UNKNOWN_TARGET, "adhocmatching unknown target"); + } + + if ((optLen != 0) && (optLen <= 0 || optDataPtr == 0)) { + // Invalid Optional Data Length + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_INVALID_OPTLEN, "adhocmatching invalid optlen"); + } + + void * opt = NULL; + if (Memory::IsValidAddress(optDataPtr)) opt = Memory::GetPointerWriteUnchecked(optDataPtr); + // Host Mode + if (context->mode == PSP_ADHOC_MATCHING_MODE_PARENT) { + // Already Connected + if (peer->state == PSP_ADHOC_MATCHING_PEER_CHILD) return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_ALREADY_ESTABLISHED, "adhocmatching already established"); + + // Not enough space + if (countChildren(context) == (context->maxpeers - 1)) return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_EXCEED_MAXNUM, "adhocmatching exceed maxnum"); + + // Requesting Peer + if (peer->state == PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST) { + // Accept Peer in Group + peer->state = PSP_ADHOC_MATCHING_PEER_CHILD; + + // Sending order may need to be reversed since Stack appends to the front, so the order will be switched around. + + // Tell Children about new Sibling + sendBirthMessage(context, peer); + + // Spawn Established Event + //spawnLocalEvent(context, PSP_ADHOC_MATCHING_EVENT_ESTABLISHED, target, 0, NULL); + + // Send Accept Confirmation to Peer + sendAcceptMessage(context, peer, optLen, opt); + + // Return Success + return 0; + } + } + + // Client Mode + else if (context->mode == PSP_ADHOC_MATCHING_MODE_CHILD) { + // Already connected + if (findParent(context) != NULL) return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_ALREADY_ESTABLISHED, "adhocmatching already established"); + + // Outgoing Request in Progress + if (findOutgoingRequest(context) != NULL) return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_REQUEST_IN_PROGRESS, "adhocmatching request in progress"); + + // Valid Offer + if (peer->state == PSP_ADHOC_MATCHING_PEER_OFFER) { + // Switch into Join Request Mode + peer->state = PSP_ADHOC_MATCHING_PEER_OUTGOING_REQUEST; + + // Send Join Request to Peer + sendJoinRequest(context, peer, optLen, opt); + + // Return Success + return 0; + } + } + + // P2P Mode + else { + // Already connected + if (findP2P(context) != NULL) return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_ALREADY_ESTABLISHED, "adhocmatching already established"); + + // Outgoing Request in Progress + if (findOutgoingRequest(context) != NULL) return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_REQUEST_IN_PROGRESS, "adhocmatching request in progress"); + + // Join Request Mode + if (peer->state == PSP_ADHOC_MATCHING_PEER_OFFER) { + // Switch into Join Request Mode + peer->state = PSP_ADHOC_MATCHING_PEER_OUTGOING_REQUEST; + + // Send Join Request to Peer + sendJoinRequest(context, peer, optLen, opt); + + // Return Success + return 0; + } + + // Requesting Peer + else if (peer->state == PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST) { + // Accept Peer in Group + peer->state = PSP_ADHOC_MATCHING_PEER_P2P; + + // Tell Children about new Sibling + //sendBirthMessage(context, peer); + // Send Accept Confirmation to Peer + sendAcceptMessage(context, peer, optLen, opt); + + // Return Success + return 0; + } + } + + // How did this happen?! It shouldn't! + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_TARGET_NOT_READY, "adhocmatching target not ready"); } int NetAdhocMatching_CancelTargetWithOpt(int matchingId, const char* macAddress, int optLen, u32 optDataPtr) { - // Initialized Library - if (netAdhocMatchingInited) { - SceNetEtherAddr* target = (SceNetEtherAddr*)macAddress; - void* opt = NULL; - if (Memory::IsValidAddress(optDataPtr)) opt = Memory::GetPointerWriteUnchecked(optDataPtr); + if (!netAdhocMatchingInited) { + // Uninitialized Library + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_NOT_INITIALIZED, "adhocmatching not initialized"); + } - // Valid Arguments - if (target != NULL && ((optLen == 0) || (optLen > 0 && opt != NULL))) { - // Find Matching Context - SceNetAdhocMatchingContext* context = findMatchingContext(matchingId); - - // Found Matching Context - if (context != NULL) { - // Running Context - if (context->running) { - // Find Peer - SceNetAdhocMatchingMemberInternal* peer = findPeer(context, (SceNetEtherAddr*)target); - - // Found Peer - if (peer != NULL) { - // Valid Peer Mode - if ((context->mode == PSP_ADHOC_MATCHING_MODE_CHILD && (peer->state == PSP_ADHOC_MATCHING_PEER_PARENT || peer->state == PSP_ADHOC_MATCHING_PEER_OUTGOING_REQUEST)) || - (context->mode == PSP_ADHOC_MATCHING_MODE_PARENT && (peer->state == PSP_ADHOC_MATCHING_PEER_CHILD || peer->state == PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST)) || - (context->mode == PSP_ADHOC_MATCHING_MODE_P2P && (peer->state == PSP_ADHOC_MATCHING_PEER_P2P || peer->state == PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST))) - { - // Notify other Children of Death - if (context->mode == PSP_ADHOC_MATCHING_MODE_PARENT && peer->state == PSP_ADHOC_MATCHING_PEER_CHILD && countConnectedPeers(context) > 1) { - // Send Death Message - sendDeathMessage(context, peer); - } - - // Mark Peer as Canceled - peer->state = PSP_ADHOC_MATCHING_PEER_CANCEL_IN_PROGRESS; - - // Send Cancel Event to Peer - sendCancelMessage(context, peer, optLen, opt); - - // Delete Peer from List - // Can't delete here, Threads still need this data. - // deletePeer(context, peer); - // Marking peer to be timedout instead of deleting immediately - peer->lastping = 0; - - hleEatCycles(adhocDefaultDelay); - // Return Success - return 0; - } - } - - // Peer not found - //return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_UNKNOWN_TARGET, "adhocmatching unknown target"); - // Faking success to prevent the game (ie. Soul Calibur) to repeatedly calling this function when the other player is disconnected - return 0; - } - - // Context not running - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_NOT_RUNNING, "adhocmatching not running"); - } - - // Invalid Matching ID - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_INVALID_ID, "adhocmatching invalid id"); - } + SceNetEtherAddr* target = (SceNetEtherAddr*)macAddress; + void* opt = NULL; + if (Memory::IsValidAddress(optDataPtr)) opt = Memory::GetPointerWriteUnchecked(optDataPtr); + if (target == NULL || ((optLen != 0) && (optLen <= 0 || opt == NULL))) { // Invalid Arguments return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_INVALID_ARG, "adhocmatching invalid arg"); } - // Uninitialized Library - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_NOT_INITIALIZED, "adhocmatching not initialized"); + // Find Matching Context + SceNetAdhocMatchingContext* context = findMatchingContext(matchingId); + + if (context == NULL) { + // Invalid Matching ID + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_INVALID_ID, "adhocmatching invalid id"); + } + + if (!context->running) { + // Context not running + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_NOT_RUNNING, "adhocmatching not running"); + } + + // Find Peer + SceNetAdhocMatchingMemberInternal* peer = findPeer(context, (SceNetEtherAddr*)target); + + if (peer == NULL) { + // Peer not found + //return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_UNKNOWN_TARGET, "adhocmatching unknown target"); + // Faking success to prevent the game (ie. Soul Calibur) to repeatedly calling this function when the other player is disconnected + return 0; + } + + // Valid Peer Mode + if ((context->mode == PSP_ADHOC_MATCHING_MODE_CHILD && (peer->state == PSP_ADHOC_MATCHING_PEER_PARENT || peer->state == PSP_ADHOC_MATCHING_PEER_OUTGOING_REQUEST)) || + (context->mode == PSP_ADHOC_MATCHING_MODE_PARENT && (peer->state == PSP_ADHOC_MATCHING_PEER_CHILD || peer->state == PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST)) || + (context->mode == PSP_ADHOC_MATCHING_MODE_P2P && (peer->state == PSP_ADHOC_MATCHING_PEER_P2P || peer->state == PSP_ADHOC_MATCHING_PEER_INCOMING_REQUEST))) + { + // Notify other Children of Death + if (context->mode == PSP_ADHOC_MATCHING_MODE_PARENT && peer->state == PSP_ADHOC_MATCHING_PEER_CHILD && countConnectedPeers(context) > 1) { + // Send Death Message + sendDeathMessage(context, peer); + } + + // Mark Peer as Canceled + peer->state = PSP_ADHOC_MATCHING_PEER_CANCEL_IN_PROGRESS; + + // Send Cancel Event to Peer + sendCancelMessage(context, peer, optLen, opt); + + // Delete Peer from List + // Can't delete here, Threads still need this data. + // deletePeer(context, peer); + // Marking peer to be timedout instead of deleting immediately + peer->lastping = 0; + + hleEatCycles(adhocDefaultDelay); + // Return Success + return 0; + } + + // Peer not found + //return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_UNKNOWN_TARGET, "adhocmatching unknown target"); + // Faking success to prevent the game (ie. Soul Calibur) to repeatedly calling this function when the other player is disconnected + return 0; } int sceNetAdhocMatchingCancelTargetWithOpt(int matchingId, const char *macAddress, int optLen, u32 optDataPtr) { From aae7b187d5ca556f492624bbf141df6b10818271 Mon Sep 17 00:00:00 2001 From: Nemoumbra Date: Sun, 5 Jan 2025 01:07:10 +0300 Subject: [PATCH 8/8] Hadouken fixes: SendData funcs + sceNetAdhocMatchingGetPoolStat --- Core/HLE/sceNetAdhocMatching.cpp | 227 +++++++++++++++---------------- 1 file changed, 107 insertions(+), 120 deletions(-) diff --git a/Core/HLE/sceNetAdhocMatching.cpp b/Core/HLE/sceNetAdhocMatching.cpp index 251ca17eb9..c2be59ad9e 100644 --- a/Core/HLE/sceNetAdhocMatching.cpp +++ b/Core/HLE/sceNetAdhocMatching.cpp @@ -2452,71 +2452,66 @@ int sceNetAdhocMatchingSendData(int matchingId, const char *mac, int dataLen, u3 if (!g_Config.bEnableWlan) return -1; - // Initialized Library - if (netAdhocMatchingInited) { - // Valid Arguments - if (mac != NULL) { - // Find Matching Context - SceNetAdhocMatchingContext * context = findMatchingContext(matchingId); - - // Found Context - if (context != NULL) { - // Running Context - if (context->running) { - // Invalid Data Length - if (dataLen <= 0 || dataAddr == 0) - // Invalid Data Length - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_INVALID_DATALEN, "invalid datalen"); - - void* data = NULL; - if (Memory::IsValidAddress(dataAddr)) data = Memory::GetPointerWriteUnchecked(dataAddr); - - // Lock the peer - std::lock_guard peer_guard(peerlock); - - // Find Target Peer - SceNetAdhocMatchingMemberInternal* peer = findPeer(context, (SceNetEtherAddr*)mac); - - // Found Peer - if (peer != NULL) { - // Valid Peer Connection State - if (peer->state == PSP_ADHOC_MATCHING_PEER_PARENT || peer->state == PSP_ADHOC_MATCHING_PEER_CHILD || peer->state == PSP_ADHOC_MATCHING_PEER_P2P) { - // Send in Progress - if (peer->sending) - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_DATA_BUSY, "data busy"); - - // Mark Peer as Sending - peer->sending = 1; - - // Send Data to Peer - sendBulkDataPacket(context, &peer->mac, dataLen, data); - - // Return Success - return 0; - } - - // Not connected / accepted - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_NOT_ESTABLISHED, "not established"); - } - - // Peer not found - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_UNKNOWN_TARGET, "unknown target"); - } - - // Context not running - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_NOT_RUNNING, "not running"); - } - - // Invalid Matching ID - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_INVALID_ID, "invalid id"); - } + if (!netAdhocMatchingInited) { + // Uninitialized Library + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_NOT_INITIALIZED, "not initialized"); + } + if (mac == NULL) { // Invalid Arguments return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_INVALID_ARG, "invalid arg"); } - // Uninitialized Library - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_NOT_INITIALIZED, "not initialized"); + // Find Matching Context + SceNetAdhocMatchingContext * context = findMatchingContext(matchingId); + + if (context == NULL) { + // Invalid Matching ID + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_INVALID_ID, "invalid id"); + } + + if (!context->running) { + // Context not running + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_NOT_RUNNING, "not running"); + } + + // Invalid Data Length + if (dataLen <= 0 || dataAddr == 0) { + // Invalid Data Length + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_INVALID_DATALEN, "invalid datalen"); + } + + void* data = NULL; + if (Memory::IsValidAddress(dataAddr)) data = Memory::GetPointerWriteUnchecked(dataAddr); + + // Lock the peer + std::lock_guard peer_guard(peerlock); + + // Find Target Peer + SceNetAdhocMatchingMemberInternal* peer = findPeer(context, (SceNetEtherAddr*)mac); + + if (peer == NULL) { + // Peer not found + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_UNKNOWN_TARGET, "unknown target"); + } + + if (peer->state != PSP_ADHOC_MATCHING_PEER_PARENT && peer->state != PSP_ADHOC_MATCHING_PEER_CHILD && peer->state != PSP_ADHOC_MATCHING_PEER_P2P) { + // Not connected / accepted + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_NOT_ESTABLISHED, "not established"); + } + + // Send in Progress + if (peer->sending) + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_DATA_BUSY, "data busy"); + + // Mark Peer as Sending + peer->sending = 1; + + // Send Data to Peer + sendBulkDataPacket(context, &peer->mac, dataLen, data); + + // Return Success + return 0; } int sceNetAdhocMatchingAbortSendData(int matchingId, const char *mac) { @@ -2524,53 +2519,48 @@ int sceNetAdhocMatchingAbortSendData(int matchingId, const char *mac) { if (!g_Config.bEnableWlan) return -1; - // Initialized Library - if (netAdhocMatchingInited) { - // Valid Arguments - if (mac != NULL) { - // Find Matching Context - SceNetAdhocMatchingContext * context = findMatchingContext(matchingId); - - // Found Context - if (context != NULL) { - // Running Context - if (context->running) { - // Find Target Peer - SceNetAdhocMatchingMemberInternal * peer = findPeer(context, (SceNetEtherAddr *)mac); - - // Found Peer - if (peer != NULL) { - // Peer is sending - if (peer->sending) { - // Set Peer as Bulk Idle - peer->sending = 0; - - // Stop Bulk Data Sending (if in progress) - abortBulkTransfer(context, peer); - } - - // Return Success - return 0; - } - - // Peer not found - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_UNKNOWN_TARGET, "adhocmatching unknown target"); - } - - // Context not running - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_NOT_RUNNING, "adhocmatching not running"); - } - - // Invalid Matching ID - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_INVALID_ID, "adhocmatching invalid id"); - } + if (!netAdhocMatchingInited) { + // Uninitialized Library + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_NOT_INITIALIZED, "adhocmatching not initialized"); + } + if (mac == NULL) { // Invalid Arguments return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_INVALID_ARG, "adhocmatching invalid arg"); } - // Uninitialized Library - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_NOT_INITIALIZED, "adhocmatching not initialized"); + // Find Matching Context + SceNetAdhocMatchingContext * context = findMatchingContext(matchingId); + + if (context == NULL) { + // Invalid Matching ID + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_INVALID_ID, "adhocmatching invalid id"); + } + + if (!context->running) { + // Context not running + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_NOT_RUNNING, "adhocmatching not running"); + } + + // Find Target Peer + SceNetAdhocMatchingMemberInternal * peer = findPeer(context, (SceNetEtherAddr *)mac); + + if (peer == NULL) { + // Peer not found + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_UNKNOWN_TARGET, "adhocmatching unknown target"); + } + + // Peer is sending + if (peer->sending) { + // Set Peer as Bulk Idle + peer->sending = 0; + + // Stop Bulk Data Sending (if in progress) + abortBulkTransfer(context, peer); + } + + // Return Success + return 0; } // Get the maximum memory usage by the matching library @@ -2588,32 +2578,29 @@ int sceNetAdhocMatchingGetPoolStat(u32 poolstatPtr) { if (!g_Config.bEnableWlan) return -1; - // Initialized Library - if (netAdhocMatchingInited) { - SceNetMallocStat * poolstat = NULL; - if (Memory::IsValidAddress(poolstatPtr)) poolstat = (SceNetMallocStat *)Memory::GetPointer(poolstatPtr); + if (!netAdhocMatchingInited) { + // Uninitialized Library + return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_NOT_INITIALIZED, "adhocmatching not initialized"); + } - // Valid Argument - if (poolstat != NULL) { - // Fill Poolstat with Fake Data - poolstat->pool = fakePoolSize; - poolstat->maximum = fakePoolSize / 2; // Max usage faked to halt the pool - poolstat->free = fakePoolSize - poolstat->maximum; - - // Return Success - return 0; - } + SceNetMallocStat * poolstat = NULL; + if (Memory::IsValidAddress(poolstatPtr)) poolstat = (SceNetMallocStat *)Memory::GetPointer(poolstatPtr); + if (poolstat == NULL) { // Invalid Argument return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_INVALID_ARG, "adhocmatching invalid arg"); } - // Uninitialized Library - return hleLogError(Log::sceNet, ERROR_NET_ADHOC_MATCHING_NOT_INITIALIZED, "adhocmatching not initialized"); + // Fill Poolstat with Fake Data + poolstat->pool = fakePoolSize; + poolstat->maximum = fakePoolSize / 2; // Max usage faked to halt the pool + poolstat->free = fakePoolSize - poolstat->maximum; + + // Return Success + return 0; } -void __NetMatchingCallbacks() //(int matchingId) -{ +void __NetMatchingCallbacks() { //(int matchingId) std::lock_guard adhocGuard(adhocEvtMtx); hleSkipDeadbeef(); // Note: Super Pocket Tennis / Thrillville Off the Rails seems to have a very short timeout (ie. ~5ms) while waiting for the event to arrived on the callback handler, but Lord of Arcana may not work well with 5ms (~3m or ~10ms seems to be good)