Prevented race condition that caused all objects to disappear in rare circumstances

This commit is contained in:
MysterD 2022-02-17 18:13:43 -08:00
parent efcf78dee1
commit f5df2917b8
6 changed files with 68 additions and 1 deletions

View File

@ -14,7 +14,7 @@
#ifdef DEBUG
#include "pc/lua/smlua.h"
static u8 warpToLevel = LEVEL_SSL;
static u8 warpToLevel = LEVEL_BOB;
static u8 warpToArea = 27;
// warpToArea: 26 = basement
// warpToArea: 27 = upstairs

View File

@ -89,6 +89,8 @@ void packet_process(struct Packet* p) {
case PACKET_NETWORK_PLAYERS_REQUEST: network_receive_network_players_request(p); break;
case PACKET_REQUEST_FAILED: network_receive_request_failed(p); break;
// custom
case PACKET_CUSTOM: network_receive_custom(p); break;
default: LOG_ERROR("received unknown packet: %d", p->buffer[0]);

View File

@ -64,6 +64,8 @@ enum PacketType {
PACKET_NETWORK_PLAYERS_REQUEST,
PACKET_REQUEST_FAILED,
///
PACKET_CUSTOM = 255,
};
@ -337,4 +339,8 @@ void network_receive_lua_sync_table_request(struct Packet* p);
void network_send_lua_sync_table(u8 toLocalIndex, u64 seq, u16 remoteIndex, u16 lntKeyCount, struct LSTNetworkType* lntKey, struct LSTNetworkType* lntValue);
void network_receive_lua_sync_table(struct Packet* p);
// packet_request_failed.c
void network_send_request_failed(struct NetworkPlayer* toNp, u8 requestType);
void network_receive_request_failed(struct Packet* p);
#endif

View File

@ -35,12 +35,21 @@ void network_receive_level_area_request(struct Packet* p) {
struct NetworkPlayer* toNp = network_player_from_global_index(globalIndex);
if (toNp == NULL || toNp->localIndex == UNKNOWN_LOCAL_INDEX || !toNp->connected) {
LOG_ERROR("Receiving level area request from inactive player!");
if (toNp != NULL) { network_send_request_failed(toNp, 1); }
return;
}
extern s16 gCurrCourseNum, gCurrActStarNum, gCurrLevelNum, gCurrAreaIndex;
if (courseNum != gCurrCourseNum || actNum != gCurrActStarNum || levelNum != gCurrLevelNum || areaIndex != gCurrAreaIndex) {
LOG_ERROR("rx level area request: received an improper location");
if (toNp != NULL) { network_send_request_failed(toNp, 1); }
return;
}
struct NetworkPlayer* np = gNetworkPlayerLocal;
if (np == NULL || !np->currAreaSyncValid || !np->currLevelSyncValid) {
LOG_ERROR("rx level area request: received when we're not synchronized");
if (toNp != NULL) { network_send_request_failed(toNp, 1); }
return;
}

View File

@ -33,12 +33,21 @@ void network_receive_level_request(struct Packet* p) {
struct NetworkPlayer* toNp = network_player_from_global_index(globalIndex);
if (toNp == NULL || toNp->localIndex == UNKNOWN_LOCAL_INDEX || !toNp->connected) {
LOG_ERROR("Receiving level request from inactive player!");
if (toNp != NULL) { network_send_request_failed(toNp, 0); }
return;
}
extern s16 gCurrCourseNum, gCurrActStarNum, gCurrLevelNum;
if (courseNum != gCurrCourseNum || actNum != gCurrActStarNum || levelNum != gCurrLevelNum) {
LOG_ERROR("rx level request: received an improper location");
if (toNp != NULL) { network_send_request_failed(toNp, 0); }
return;
}
struct NetworkPlayer* np = gNetworkPlayerLocal;
if (np == NULL || !np->currLevelSyncValid) {
LOG_ERROR("rx level request: received when we're not synchronized");
if (toNp != NULL) { network_send_request_failed(toNp, 0); }
return;
}

View File

@ -0,0 +1,41 @@
#include <stdio.h>
#include "../network.h"
#define DISABLE_MODULE_LOG 1
#include "pc/debuglog.h"
void network_send_request_failed(struct NetworkPlayer* toNp, u8 requestType) {
if (gNetworkType == NT_SERVER && toNp == gNetworkPlayerLocal) {
struct NetworkPlayer* np = gNetworkPlayerLocal;
if (requestType == 0 && !np->currLevelSyncValid) {
LOG_INFO("re-requesting level");
network_send_change_level();
} else if (requestType == 1 && (!np->currAreaSyncValid || !np->currLevelSyncValid)) {
LOG_INFO("re-requesting area");
network_send_change_area();
}
return;
}
struct Packet p = { 0 };
packet_init(&p, PACKET_REQUEST_FAILED, true, PLMT_NONE);
packet_write(&p, &requestType, sizeof(u8));
network_send_to(toNp->localIndex, &p);
LOG_INFO("tx request failed");
}
void network_receive_request_failed(struct Packet* p) {
LOG_INFO("rx request failed");
u8 requestType;
packet_read(p, &requestType, sizeof(u8));
struct NetworkPlayer* np = gNetworkPlayerLocal;
if (requestType == 0 && !np->currLevelSyncValid) {
LOG_INFO("re-requesting level");
network_send_change_level();
} else if (requestType == 1 && (!np->currAreaSyncValid || !np->currLevelSyncValid)) {
LOG_INFO("re-requesting area");
network_send_change_area();
}
}