Networking improvements

Reliable packets now save the actual remote address
Clients now only accepts server packets from the desired server
This commit is contained in:
MysterD 2022-01-23 01:37:14 -08:00
parent 767809f56a
commit cdbf9eaabf
46 changed files with 131 additions and 56 deletions

View file

@ -39,7 +39,7 @@ static void on_activity_join_callback(UNUSED void* data, enum EDiscordResult res
network_player_connected(NPT_SERVER, 0, 0, 0, "Player");
}
ns_discord_save_id(gNetworkPlayerServer->localIndex, lobby->owner_id);
network_send_join_request();
network_send_mod_list_request();
}
gNetworkUserIds[0] = lobby->owner_id;

View file

@ -129,6 +129,16 @@ static void register_launch_command(void) {
LOGFILE_INFO(LFT_DISCORD, "cmd: %s", cmd);
}
static void* ns_discord_dup_addr(u8 localIndex) {
void* address = malloc(sizeof(DiscordUserId));
memcpy(address, &gNetworkUserIds[localIndex], sizeof(DiscordUserId));
return address;
}
static bool ns_discord_match_addr(void* addr1, void* addr2) {
return !memcmp(addr1, addr2, sizeof(u64));
}
static void ns_discord_update(void) {
if (!gDiscordInitialized) { return; }
discord_lobby_update();
@ -212,6 +222,8 @@ struct NetworkSystem gNetworkSystemDiscord = {
.get_id = ns_discord_get_id,
.save_id = ns_discord_save_id,
.clear_id = ns_discord_clear_id,
.dup_addr = ns_discord_dup_addr,
.match_addr = ns_discord_match_addr,
.update = ns_discord_update,
.send = ns_discord_network_send,
.shutdown = ns_discord_shutdown,

View file

@ -13,10 +13,12 @@ u8 discord_user_id_to_local_index(int64_t userId) {
return UNKNOWN_LOCAL_INDEX;
}
int ns_discord_network_send(u8 localIndex, u8* data, u16 dataLength) {
int ns_discord_network_send(u8 localIndex, void* address, u8* data, u16 dataLength) {
if (!gDiscordInitialized) { return 1; }
if (gCurLobbyId == 0) { return 2; }
DISCORD_REQUIRE(app.lobbies->send_network_message(app.lobbies, gCurLobbyId, gNetworkUserIds[localIndex], 0, data, dataLength));
DiscordUserId userId = gNetworkUserIds[localIndex];
if (localIndex == 0 && address != NULL) { userId = *(DiscordUserId*)address; }
DISCORD_REQUIRE(app.lobbies->send_network_message(app.lobbies, gCurLobbyId, userId, 0, data, dataLength));
return 0;
}
@ -31,7 +33,7 @@ void discord_network_on_message(UNUSED void* eventData, UNUSED int64_t lobbyId,
}
}
network_receive(localIndex, (u8*)data, (u16)dataLength);
network_receive(localIndex, &userId, (u8*)data, (u16)dataLength);
}
void discord_network_flush(void) {

View file

@ -5,7 +5,7 @@
extern int64_t gNetworkUserIds[MAX_PLAYERS];
u8 discord_user_id_to_local_index(int64_t userId);
int ns_discord_network_send(u8 localIndex, u8* data, u16 dataLength);
int ns_discord_network_send(u8 localIndex, void* addr, u8* data, u16 dataLength);
void discord_network_on_message(UNUSED void* eventData, int64_t lobbyId, int64_t userId, uint8_t channelId, uint8_t* data, uint32_t dataLength);
void discord_network_flush(void);
s64 ns_discord_get_id(u8 localId);

View file

@ -35,6 +35,7 @@ bool gNetworkAreaLoaded = false;
bool gNetworkAreaSyncing = true;
u32 gNetworkAreaTimerClock = 0;
u32 gNetworkAreaTimer = 0;
void* gNetworkServerAddr = NULL;
struct StringLinkedList gRegisteredMods = { 0 };
@ -49,6 +50,7 @@ struct ServerSettings gServerSettings = {
};
void network_set_system(enum NetworkSystemType nsType) {
network_forget_all_reliable();
switch (nsType) {
case NS_SOCKET: gNetworkSystem = &gNetworkSystemSocket; break;
#ifdef DISCORD_SDK
@ -86,6 +88,10 @@ bool network_init(enum NetworkType inNetworkType) {
LOG_ERROR("failed to initialize network system");
return false;
}
if (gNetworkServerAddr != NULL) {
free(gNetworkServerAddr);
gNetworkServerAddr = NULL;
}
// set network type
gNetworkType = inNetworkType;
@ -216,7 +222,7 @@ void network_send_to(u8 localIndex, struct Packet* p) {
// send
if (!tooManyPackets) {
int rc = gNetworkSystem->send(localIndex, p->buffer, p->cursor + sizeof(u32));
int rc = gNetworkSystem->send(localIndex, p->addr, p->buffer, p->cursor + sizeof(u32));
if (rc == SOCKET_ERROR) { LOG_ERROR("send error %d", rc); return; }
}
p->sent = true;
@ -259,11 +265,12 @@ void network_send(struct Packet* p) {
}
}
void network_receive(u8 localIndex, u8* data, u16 dataLength) {
void network_receive(u8 localIndex, void* addr, u8* data, u16 dataLength) {
// receive packet
struct Packet p = {
.localIndex = localIndex,
.cursor = 3,
.addr = addr,
.buffer = { 0 },
.dataLength = dataLength,
};
@ -284,6 +291,11 @@ void network_receive(u8 localIndex, u8* data, u16 dataLength) {
packet_receive(&p);
}
void* network_duplicate_address(u8 localIndex) {
assert(localIndex < MAX_PLAYERS);
return gNetworkSystem->dup_addr(localIndex);
}
static void network_update_area_timer(void) {
bool brokenClock = false;
#ifdef DEVELOPMENT
@ -369,5 +381,10 @@ void network_shutdown(bool sendLeaving) {
network_player_shutdown();
gNetworkSystem->shutdown();
if (gNetworkServerAddr != NULL) {
free(gNetworkServerAddr);
gNetworkServerAddr = NULL;
}
gNetworkType = NT_NONE;
}

View file

@ -44,8 +44,10 @@ struct NetworkSystem {
s64 (*get_id)(u8 localIndex);
void (*save_id)(u8 localIndex, s64 networkId);
void (*clear_id)(u8 localIndex);
void* (*dup_addr)(u8 localIndex);
bool (*match_addr)(void* addr1, void* addr2);
void (*update)(void);
int (*send)(u8 localIndex, u8* data, u16 dataLength);
int (*send)(u8 localIndex, void* addr, u8* data, u16 dataLength);
void (*shutdown)(void);
bool requireServerBroadcast;
};
@ -99,6 +101,7 @@ extern bool gNetworkAreaLoaded;
extern bool gNetworkAreaSyncing;
extern u32 gNetworkAreaTimer;
extern u32 gNetworkAreaTimerClock;
extern void* gNetworkServerAddr;
extern struct SyncObject gSyncObjects[];
extern struct ServerSettings gServerSettings;
extern struct StringLinkedList gRegisteredMods;
@ -111,7 +114,8 @@ void network_on_loaded_area(void);
bool network_allow_unknown_local_index(enum PacketType packetType);
void network_send_to(u8 localIndex, struct Packet* p);
void network_send(struct Packet* p);
void network_receive(u8 localIndex, u8* data, u16 dataLength);
void network_receive(u8 localIndex, void* addr, u8* data, u16 dataLength);
void* network_duplicate_address(u8 localIndex);
void network_update(void);
void network_register_mod(char* modName);
void network_shutdown(bool sendLeaving);

View file

@ -95,6 +95,17 @@ void packet_receive(struct Packet* p) {
// send an ACK if requested
network_send_ack(p);
// refuse packets from unknown servers
if (gNetworkServerAddr != NULL && gNetworkType == NT_CLIENT) {
bool fromServer = (p->localIndex == UNKNOWN_LOCAL_INDEX);
if (gNetworkPlayerServer != NULL) { fromServer = fromServer || p->localIndex == gNetworkPlayerServer->localIndex; }
LOG_INFO("matching? %d, %d", fromServer, gNetworkSystem->match_addr(gNetworkServerAddr, p->addr));
if (fromServer && !gNetworkSystem->match_addr(gNetworkServerAddr, p->addr)) {
LOG_INFO("refusing packet from unknown server");
return;
}
}
// refuse packets from unknown players other than join request
if (gNetworkType == NT_SERVER && p->localIndex == UNKNOWN_LOCAL_INDEX && !network_allow_unknown_local_index(packetType)) {
if (packetType != PACKET_PLAYER) {

View file

@ -74,6 +74,7 @@ struct Packet {
u8 localIndex;
u16 dataLength;
u16 cursor;
void* addr;
bool error;
bool reliable;
bool levelAreaMustMatch;

View file

@ -38,7 +38,7 @@ void network_send_area(struct NetworkPlayer* toNp) {
packet_ordered_begin();
{
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_AREA, true, PLMT_NONE);
// level location

View file

@ -10,7 +10,7 @@ void network_send_area_request(struct NetworkPlayer* fromNp, struct NetworkPlaye
return;
}
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_AREA_REQUEST, true, PLMT_NONE);
packet_write(&p, &fromNp->globalIndex, sizeof(u8));
packet_write(&p, &fromNp->currCourseNum, sizeof(s16));

View file

@ -44,7 +44,7 @@ void network_send_change_area(void) {
return;
}
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_CHANGE_AREA, true, PLMT_NONE);
packet_write(&p, &gCurrCourseNum, sizeof(s16));
packet_write(&p, &gCurrActStarNum, sizeof(s16));

View file

@ -50,7 +50,7 @@ void network_send_change_level(void) {
return;
}
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_CHANGE_LEVEL, true, PLMT_NONE);
packet_write(&p, &gCurrCourseNum, sizeof(s16));
packet_write(&p, &gCurrActStarNum, sizeof(s16));

View file

@ -31,7 +31,7 @@ static void print_network_player_table(void) {
void network_send_chat(char* message, u8 globalIndex) {
u16 messageLength = strlen(message);
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_CHAT, true, PLMT_NONE);
packet_write(&p, &globalIndex, sizeof(u8));
packet_write(&p, &messageLength, sizeof(u16));

View file

@ -50,7 +50,7 @@ void network_send_collect_coin(struct Object* o) {
if (gNetworkPlayerLocal == NULL || !gNetworkPlayerLocal->currAreaSyncValid) { return; }
u16 behaviorId = get_id_from_behavior(o->behavior);
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_COLLECT_COIN, true, PLMT_LEVEL);
packet_write(&p, &behaviorId, sizeof(u16));
packet_write(&p, &o->oPosX, sizeof(f32) * 3);

View file

@ -44,7 +44,7 @@ void network_send_collect_item(struct Object* o) {
if (gNetworkPlayerLocal == NULL || !gNetworkPlayerLocal->currAreaSyncValid) { return; }
u16 behaviorId = get_id_from_behavior(o->behavior);
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_COLLECT_ITEM, true, PLMT_AREA);
packet_write(&p, &behaviorId, sizeof(u16));
packet_write(&p, &o->oPosX, sizeof(f32) * 3);

View file

@ -47,7 +47,7 @@ static struct Object* find_nearest_star(const BehaviorScript* behavior, f32* pos
void network_send_collect_star(struct Object* o, s16 coinScore, s16 starIndex) {
u16 behaviorId = get_id_from_behavior(o->behavior);
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_COLLECT_STAR, true, PLMT_NONE);
packet_write(&p, &gCurrSaveFileNum, sizeof(s16));
packet_write(&p, &gCurrCourseNum, sizeof(s16));

View file

@ -25,7 +25,7 @@ u8 network_register_custom_packet(void (*send_callback)(struct Packet* p, void*
void network_send_custom(u8 customId, bool reliable, enum PacketLevelMatchType levelAreaMustMatch, void* params) {
if (customPackets[customId].send_callback == NULL) { return; }
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_CUSTOM, reliable, levelAreaMustMatch);
packet_write(&p, &customId, sizeof(u8));
customPackets[customId].send_callback(&p, params);

View file

@ -19,7 +19,7 @@ void network_send_debug_sync(void) {
objectCount++;
}
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_DEBUG_SYNC, true, PLMT_AREA);
packet_write(&p, &objectCount, sizeof(u8));
for (int i = 0; i < MAX_SYNC_OBJECTS; i++) {

View file

@ -17,7 +17,7 @@ static void network_send_next_download_request(void) {
void network_send_download_request(u16 index, u64 offset) {
SOFT_ASSERT(gNetworkType == NT_CLIENT);
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_DOWNLOAD_REQUEST, true, PLMT_NONE);
packet_write(&p, &index, sizeof(u16));
@ -68,7 +68,7 @@ void network_send_download(u16 index, u64 offset) {
fseek(entry->fp, offset, SEEK_SET);
fread(chunk, chunkSize, 1, entry->fp);
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_DOWNLOAD, true, PLMT_NONE);
packet_write(&p, &index, sizeof(u16));
@ -82,6 +82,10 @@ void network_send_download(u16 index, u64 offset) {
void network_receive_download(struct Packet* p) {
SOFT_ASSERT(gNetworkType == NT_CLIENT);
if (p->localIndex != UNKNOWN_LOCAL_INDEX) {
LOG_ERROR("Received download from known local index");
return;
}
u16 index;
u64 offset;

View file

@ -31,7 +31,7 @@ void network_send_join_request(void) {
gOverrideEeprom = eeprom;
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_JOIN_REQUEST, true, PLMT_NONE);
packet_write(&p, &configPlayerModel, sizeof(u8));
@ -79,7 +79,7 @@ void network_send_join(struct Packet* joinRequestPacket) {
snprintf(version, MAX_VERSION_LENGTH, "%s", get_version());
LOG_INFO("sending version: %s", version);
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_JOIN, true, PLMT_NONE);
packet_write(&p, &version, sizeof(u8) * MAX_VERSION_LENGTH);
packet_write(&p, &joinRequestPacket->localIndex, sizeof(u8));

View file

@ -4,7 +4,7 @@
#include "pc/debuglog.h"
void network_send_keep_alive(u8 localIndex) {
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_KEEP_ALIVE, false, PLMT_NONE);
network_send_to(localIndex, &p);
LOG_INFO("sending keep alive");

View file

@ -5,7 +5,7 @@
void network_send_kick(enum KickReasonType kickReason) {
u8 kickReasonType = kickReason;
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_KICK, false, PLMT_NONE);
packet_write(&p, &kickReasonType, sizeof(u8));
network_send_to(0, &p);

View file

@ -17,7 +17,7 @@ void network_send_leaving(u8 globalIndex) {
globalIndex = gNetworkPlayerLocal->globalIndex;
}
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_LEAVING, true, PLMT_NONE);
packet_write(&p, &globalIndex, sizeof(u8));
if (gNetworkType == NT_SERVER) {

View file

@ -15,7 +15,7 @@ void network_send_level(struct NetworkPlayer* toNp, bool sendArea) {
packet_ordered_begin();
{
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_LEVEL, true, PLMT_NONE);
// level location

View file

@ -20,7 +20,7 @@ void network_send_level_area_inform(struct NetworkPlayer* np) {
u16 seq = ++sLevelAreaInformSeq[np->globalIndex][i];
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_LEVEL_AREA_INFORM, true, PLMT_NONE);
packet_write(&p, &seq, sizeof(u16));
packet_write(&p, &np->globalIndex, sizeof(u8));

View file

@ -10,7 +10,7 @@ void network_send_level_area_request(struct NetworkPlayer* fromNp, struct Networ
return;
}
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_LEVEL_AREA_REQUEST, true, PLMT_NONE);
packet_write(&p, &fromNp->globalIndex, sizeof(u8));
packet_write(&p, &fromNp->currCourseNum, sizeof(s16));

View file

@ -36,7 +36,7 @@ static void network_send_level_macro_area(struct NetworkPlayer* destNp, u8 areaI
}
// write header
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_LEVEL_MACRO, true, PLMT_NONE);
packet_write(&p, &gCurrCourseNum, sizeof(s16));
packet_write(&p, &gCurrActStarNum, sizeof(s16));

View file

@ -10,7 +10,7 @@ void network_send_level_request(struct NetworkPlayer* fromNp, struct NetworkPlay
return;
}
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_LEVEL_REQUEST, true, PLMT_NONE);
packet_write(&p, &fromNp->globalIndex, sizeof(u8));
packet_write(&p, &fromNp->currCourseNum, sizeof(s16));

View file

@ -111,7 +111,7 @@ void network_send_level_respawn_info(struct Object* o, u8 respawnInfoBits) {
bool isMacroObject = (macroOffset != ERR_COULD_NOT_FIND_OBJECT);
// write header
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_LEVEL_RESPAWN_INFO, true, PLMT_NONE);
packet_write(&p, &gCurrCourseNum, sizeof(s16));
packet_write(&p, &gCurrActStarNum, sizeof(s16));
@ -136,7 +136,7 @@ void network_send_level_respawn_info(struct Object* o, u8 respawnInfoBits) {
if (np->currActNum != gCurrActStarNum) { continue; }
if (np->currLevelNum != gCurrLevelNum) { continue; }
if (np == gNetworkPlayerLocal) { continue; }
struct Packet p2;
struct Packet p2 = { 0 };
packet_duplicate(&p, &p2);
network_send_to(np->localIndex, &p2);
LOG_INFO("tx level respawn info to %d", np->globalIndex);
@ -184,7 +184,7 @@ void network_receive_level_respawn_info(struct Packet* p) {
if (np->currActNum != actNum) { continue; }
if (np->currLevelNum != levelNum) { continue; }
if (np == npFrom) { continue; }
struct Packet p2;
struct Packet p2 = { 0 };
packet_duplicate(p, &p2);
network_send_to(np->localIndex, &p2);
}

View file

@ -35,7 +35,7 @@ static void network_send_level_spawn_info_area(struct NetworkPlayer* destNp, u8
}
// write header
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_LEVEL_SPAWN_INFO, true, PLMT_NONE);
packet_write(&p, &gCurrCourseNum, sizeof(s16));
packet_write(&p, &gCurrActStarNum, sizeof(s16));

View file

@ -6,7 +6,7 @@
void network_send_mod_list_request(void) {
SOFT_ASSERT(gNetworkType == NT_CLIENT);
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_MOD_LIST_REQUEST, true, PLMT_NONE);
char version[MAX_VERSION_LENGTH] = { 0 };
@ -40,7 +40,7 @@ void network_receive_mod_list_request(struct Packet* p) {
void network_send_mod_list(void) {
SOFT_ASSERT(gNetworkType == NT_SERVER);
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_MOD_LIST, true, PLMT_NONE);
packet_write(&p, &sModEntryCount, sizeof(u16));
@ -58,6 +58,11 @@ void network_send_mod_list(void) {
void network_receive_mod_list(struct Packet* p) {
SOFT_ASSERT(gNetworkType == NT_CLIENT);
SOFT_ASSERT(p->localIndex == UNKNOWN_LOCAL_INDEX);
if (gNetworkServerAddr == NULL) {
gNetworkServerAddr = network_duplicate_address(0);
}
u16 modEntryCount = 0;
packet_read(p, &modEntryCount, sizeof(u16));

View file

@ -12,7 +12,7 @@ static void network_send_to_network_players(u8 sendToLocalIndex) {
u8 connectedCount = network_player_connected_count();
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_NETWORK_PLAYERS, true, PLMT_NONE);
packet_write(&p, &connectedCount, sizeof(u8));
for (int i = 0; i < MAX_PLAYERS; i++) {

View file

@ -38,7 +38,7 @@ struct DelayedPacketObject* delayedPacketObjectHead = NULL;
struct DelayedPacketObject* delayedPacketObjectTail = NULL;
void network_delayed_packet_object_remember(struct Packet* p) {
struct DelayedPacketObject* node = malloc(sizeof(struct DelayedPacketObject));
struct DelayedPacketObject* node = calloc(1, sizeof(struct DelayedPacketObject));
packet_duplicate(p, &node->p);
node->next = NULL;
LOG_INFO("saving delayed object");
@ -504,7 +504,7 @@ void network_send_object_reliability(struct Object* o, bool reliable) {
so->clockSinceUpdate = clock_elapsed();
// write the packet data
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_OBJECT, reliable, PLMT_AREA);
packet_write_object_header(&p, o);
packet_write_object_full_sync(&p, o);

View file

@ -102,7 +102,7 @@ static void packet_ordered_add_to_table(struct OrderedPacketTable* opt, struct P
}
// allocate the packet list
opl = malloc(sizeof(struct OrderedPacketList));
opl = calloc(1, sizeof(struct OrderedPacketList));
if (oplLast == NULL) {
opt->packets = opl;
} else {

View file

@ -192,7 +192,7 @@ void network_send_player(u8 localIndex) {
struct PacketPlayerData data = { 0 };
read_packet_data(&data, &gMarioStates[localIndex]);
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_PLAYER, false, PLMT_AREA);
packet_write(&p, &gNetworkPlayers[localIndex].globalIndex, sizeof(u8));
packet_write(&p, &data, sizeof(struct PacketPlayerData));

View file

@ -6,7 +6,7 @@ void network_send_player_settings(void) {
char playerName[MAX_PLAYER_STRING+1] = { 0 };
snprintf(playerName, MAX_PLAYER_STRING, "%s", configPlayerName);
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_PLAYER_SETTINGS, true, PLMT_NONE);
packet_write(&p, &gNetworkPlayers[0].globalIndex, sizeof(u8));
packet_write(&p, playerName, MAX_PLAYER_STRING * sizeof(u8));

View file

@ -31,6 +31,8 @@ static void remove_node_from_list(struct PacketLinkedList* node) {
if (node->prev != NULL) { node->prev->next = node->next; }
if (node->next != NULL) { node->next->prev = node->prev; }
assert(node->p.addr != NULL);
free(node->p.addr);
free(node);
}
@ -85,8 +87,9 @@ void network_remember_reliable(struct Packet* p) {
if (!p->reliable) { return; }
if (p->sent) { return; }
struct PacketLinkedList* node = malloc(sizeof(struct PacketLinkedList));
struct PacketLinkedList* node = calloc(1, sizeof(struct PacketLinkedList));
node->p = *p;
node->p.addr = network_duplicate_address(p->localIndex);
node->p.sent = true;
node->lastSend = clock_elapsed();
node->sendAttempts = 1;

View file

@ -13,7 +13,7 @@
void network_send_reservation_list(struct NetworkPlayer* np, u8 syncIds[]) {
SOFT_ASSERT(gNetworkType == NT_SERVER);
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_RESERVATION_LIST, true, PLMT_NONE);
packet_write(&p, &np->currCourseNum, sizeof(u8));

View file

@ -16,7 +16,7 @@ void network_send_reservation_release(u8 syncId) {
// make sure this is a reserved id
if (syncId < RESERVED_IDS_SYNC_OBJECT_OFFSET) { return; }
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_RESERVATION_RELEASE, true, PLMT_NONE);
extern s16 gCurrCourseNum, gCurrActStarNum, gCurrLevelNum, gCurrAreaIndex;

View file

@ -16,7 +16,7 @@ void network_send_reservation_use(u8 syncId) {
// make sure this is a reserved id
if (syncId < RESERVED_IDS_SYNC_OBJECT_OFFSET) { return; }
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_RESERVATION_USE, true, PLMT_NONE);
extern s16 gCurrCourseNum, gCurrActStarNum, gCurrLevelNum, gCurrAreaIndex;

View file

@ -6,7 +6,7 @@
void network_send_save_file(s32 fileIndex) {
if (gNetworkPlayerServer == NULL) { return; }
SOFT_ASSERT(gNetworkType == NT_CLIENT);
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_SAVE_FILE, true, PLMT_NONE);
packet_write(&p, &fileIndex, sizeof(s32));
network_send_to(gNetworkPlayerServer->localIndex, &p);

View file

@ -4,7 +4,7 @@
#include "buffers/buffers.h"
void network_send_save_set_flag(s32 fileIndex, s32 courseIndex, u8 courseStars, u32 flags) {
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_SAVE_SET_FLAG, true, PLMT_NONE);
packet_write(&p, &fileIndex, sizeof(s32));
packet_write(&p, &courseIndex, sizeof(s32));

View file

@ -53,7 +53,7 @@ void network_send_spawn_objects_to(u8 sendToLocalIndex, struct Object* objects[]
// prevent sending spawn objects during credits
if (gCurrActStarNum == 99) { return; }
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_SPAWN_OBJECTS, true, PLMT_AREA);
// objects

View file

@ -8,7 +8,7 @@
extern struct Object* gCurrentObject;
void network_send_spawn_star(struct Object* o, u8 starType, f32 x, f32 y, f32 z, u32 behParams) {
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_SPAWN_STAR, true, PLMT_AREA);
packet_write(&p, &starType, sizeof(u8));
packet_write(&p, &x, sizeof(f32));
@ -57,7 +57,7 @@ void network_send_spawn_star_nle(struct Object* o, u32 params) {
globalIndex = gNetworkPlayers[localIndex].globalIndex;
}
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_SPAWN_STAR_NLE, true, PLMT_AREA);
packet_write(&p, &globalIndex, sizeof(u8));
packet_write(&p, &o->oSyncID, sizeof(u32));

View file

@ -18,7 +18,7 @@ void network_send_sync_valid(struct NetworkPlayer* toNp, s16 courseNum, s16 actN
}
u8 myGlobalIndex = gNetworkPlayerLocal->globalIndex;
struct Packet p;
struct Packet p = { 0 };
packet_init(&p, PACKET_SYNC_VALID, true, PLMT_NONE);
packet_write(&p, &courseNum, sizeof(s16));
packet_write(&p, &actNum, sizeof(s16));

View file

@ -119,6 +119,16 @@ static void ns_socket_clear_id(u8 localId) {
LOG_INFO("cleared addr for id %d", localId);
}
static void* ns_socket_dup_addr(u8 localIndex) {
void* address = malloc(sizeof(struct sockaddr_in));
memcpy(address, &addr[localIndex], sizeof(struct sockaddr_in));
return address;
}
static bool ns_socket_match_addr(void* addr1, void* addr2) {
return !memcmp(addr1, addr2, sizeof(struct sockaddr_in));
}
static void ns_socket_update(void) {
if (gNetworkType == NT_NONE) { return; }
do {
@ -129,16 +139,20 @@ static void ns_socket_update(void) {
int rc = socket_receive(curSocket, &addr[0], data, PACKET_LENGTH + 1, &dataLength, &localIndex);
assert(dataLength < PACKET_LENGTH);
if (rc != NO_ERROR) { break; }
network_receive(localIndex, data, dataLength);
network_receive(localIndex, &addr[0], data, dataLength);
} while (true);
}
static int ns_socket_send(u8 localIndex, u8* data, u16 dataLength) {
static int ns_socket_send(u8 localIndex, void* address, u8* data, u16 dataLength) {
if (localIndex != 0) {
if (gNetworkType == NT_SERVER && gNetworkPlayers[localIndex].type != NPT_CLIENT) { return SOCKET_ERROR; }
if (gNetworkType == NT_CLIENT && gNetworkPlayers[localIndex].type != NPT_SERVER) { return SOCKET_ERROR; }
}
int rc = socket_send(curSocket, &addr[localIndex], data, dataLength);
struct sockaddr_in* userAddr = &addr[localIndex];
if (localIndex == 0 && address != NULL) { userAddr = (struct sockaddr_in*)address; }
int rc = socket_send(curSocket, userAddr, data, dataLength);
if (rc) {
LOG_ERROR(" localIndex: %d, packetType: %d, dataLength: %d", localIndex, data[0], dataLength);
}
@ -156,6 +170,8 @@ struct NetworkSystem gNetworkSystemSocket = {
.get_id = ns_socket_get_id,
.save_id = ns_socket_save_id,
.clear_id = ns_socket_clear_id,
.dup_addr = ns_socket_dup_addr,
.match_addr = ns_socket_match_addr,
.update = ns_socket_update,
.send = ns_socket_send,
.shutdown = ns_socket_shutdown,