Refactored packet code out of network.c/h

This commit is contained in:
MysterD 2020-09-15 22:58:16 -07:00
parent eaaab8850f
commit 2a10cca29a
4 changed files with 153 additions and 130 deletions

View file

@ -111,28 +111,7 @@ void network_receive(u8* data, u16 dataLength) {
}
// execute packet
switch ((u8)p.buffer[0]) {
case PACKET_ACK: network_receive_ack(&p); break;
case PACKET_PLAYER: network_receive_player(&p); break;
case PACKET_OBJECT: network_receive_object(&p); break;
case PACKET_SPAWN_OBJECTS: network_receive_spawn_objects(&p); break;
case PACKET_SPAWN_STAR: network_receive_spawn_star(&p); break;
case PACKET_LEVEL_WARP: network_receive_level_warp(&p); break;
case PACKET_INSIDE_PAINTING: network_receive_inside_painting(&p); break;
case PACKET_COLLECT_STAR: network_receive_collect_star(&p); break;
case PACKET_COLLECT_COIN: network_receive_collect_coin(&p); break;
case PACKET_COLLECT_ITEM: network_receive_collect_item(&p); break;
case PACKET_RESERVATION_REQUEST: network_receive_reservation_request(&p); break;
case PACKET_RESERVATION: network_receive_reservation(&p); break;
case PACKET_SAVE_FILE_REQUEST: network_receive_save_file_request(&p); break;
case PACKET_SAVE_FILE: network_receive_save_file(&p); break;
case PACKET_CHAT: network_receive_chat(&p); break;
case PACKET_CUSTOM: network_receive_custom(&p); break;
default: LOG_ERROR("received unknown packet: %d", p.buffer[0]);
}
// send an ACK if requested
network_send_ack(&p);
packet_receive(&p);
}
void network_update(void) {

View file

@ -5,6 +5,7 @@
#include <time.h>
#include <types.h>
#include <assert.h>
#include "packets/packet.h"
#include "../cliopts.h"
// Mario 64 specific externs
@ -32,35 +33,6 @@ struct NetworkSystem {
void (*shutdown)(void);
};
enum PacketType {
PACKET_ACK,
PACKET_PLAYER,
PACKET_OBJECT,
PACKET_SPAWN_OBJECTS,
PACKET_SPAWN_STAR,
PACKET_LEVEL_WARP,
PACKET_INSIDE_PAINTING,
PACKET_COLLECT_STAR,
PACKET_COLLECT_COIN,
PACKET_COLLECT_ITEM,
PACKET_RESERVATION_REQUEST,
PACKET_RESERVATION,
PACKET_SAVE_FILE_REQUEST,
PACKET_SAVE_FILE,
PACKET_CHAT,
PACKET_CUSTOM = 255,
};
struct Packet {
u16 dataLength;
u16 cursor;
bool error;
bool reliable;
u16 seqId;
bool sent;
u8 buffer[PACKET_LENGTH];
};
struct SyncObject {
struct Object* o;
u16 reserved;
@ -110,83 +82,4 @@ void network_on_joined(void);
void network_update(void);
void network_shutdown(void);
// packet_read_write.c
void packet_init(struct Packet* packet, enum PacketType packetType, bool reliable);
void packet_write(struct Packet* packet, void* data, u16 length);
void packet_read(struct Packet* packet, void* data, u16 length);
u32 packet_hash(struct Packet* packet);
bool packet_check_hash(struct Packet* packet);
// packet_reliable.c
void network_send_ack(struct Packet* p);
void network_receive_ack(struct Packet* p);
void network_remember_reliable(struct Packet* p);
void network_update_reliable(void);
// packet_player.c
void network_update_player(void);
void network_receive_player(struct Packet* p);
// packet_object.c
struct SyncObject* network_init_object(struct Object* object, float maxSyncDistance);
void network_init_object_field(struct Object* o, void* field);
bool network_owns_object(struct Object* o);
bool network_sync_object_initialized(struct Object* o);
void network_clear_sync_objects(void);
void network_set_sync_id(struct Object* o);
void network_send_object(struct Object* o);
void network_send_object_reliability(struct Object* o, bool reliable);
void network_receive_object(struct Packet* p);
void network_forget_sync_object(struct SyncObject* so);
void network_update_objects(void);
// packet_spawn_object.c
void network_send_spawn_objects(struct Object* objects[], u32 models[], u8 objectCount);
void network_receive_spawn_objects(struct Packet* p);
// packet_spawn_star.c
void network_send_spawn_star(struct Object* o, u8 starType, f32 x, f32 y, f32 z, u32 behParams);
void network_receive_spawn_star(struct Packet* p);
// packet_level_warp.c
void network_send_level_warp_begin(void);
void network_receive_level_warp(struct Packet* p);
// packet_inside_painting.c
void network_send_inside_painting(void);
void network_receive_inside_painting(struct Packet* p);
// packet_collect_star.c
void network_send_collect_star(struct Object* o, s16 coinScore, s16 starIndex);
void network_receive_collect_star(struct Packet* p);
// packet_collect_coin.c
void network_send_collect_coin(struct Object* o);
void network_receive_collect_coin(struct Packet* p);
// packet_collect_item.c
void network_send_collect_item(struct Object* o);
void network_receive_collect_item(struct Packet* p);
// packet_reservation.c
void network_send_reservation_request(void);
void network_receive_reservation_request(UNUSED struct Packet* p);
void network_send_reservation(void);
void network_receive_reservation(struct Packet* p);
// packet_save_file.c
void network_send_save_file_request(void);
void network_receive_save_file_request(UNUSED struct Packet* p);
void network_send_save_file(void);
void network_receive_save_file(struct Packet* p);
// packet_custom.c
u8 network_register_custom_packet(void (*send_callback)(struct Packet* p, void* params), void (*receive_callback)(struct Packet* p));
void network_send_custom(u8 customId, bool reliable, void* params);
void network_receive_custom(struct Packet* p);
// packet_chat.c
void network_send_chat(char* message);
void network_receive_chat(struct Packet* p);
#endif

View file

@ -0,0 +1,28 @@
#include <stdio.h>
#include "../network.h"
#include "pc/debuglog.h"
void packet_receive(struct Packet* p) {
switch ((u8)p->buffer[0]) {
case PACKET_ACK: network_receive_ack(p); break;
case PACKET_PLAYER: network_receive_player(p); break;
case PACKET_OBJECT: network_receive_object(p); break;
case PACKET_SPAWN_OBJECTS: network_receive_spawn_objects(p); break;
case PACKET_SPAWN_STAR: network_receive_spawn_star(p); break;
case PACKET_LEVEL_WARP: network_receive_level_warp(p); break;
case PACKET_INSIDE_PAINTING: network_receive_inside_painting(p); break;
case PACKET_COLLECT_STAR: network_receive_collect_star(p); break;
case PACKET_COLLECT_COIN: network_receive_collect_coin(p); break;
case PACKET_COLLECT_ITEM: network_receive_collect_item(p); break;
case PACKET_RESERVATION_REQUEST: network_receive_reservation_request(p); break;
case PACKET_RESERVATION: network_receive_reservation(p); break;
case PACKET_SAVE_FILE_REQUEST: network_receive_save_file_request(p); break;
case PACKET_SAVE_FILE: network_receive_save_file(p); break;
case PACKET_CHAT: network_receive_chat(p); break;
case PACKET_CUSTOM: network_receive_custom(p); break;
default: LOG_ERROR("received unknown packet: %d", p->buffer[0]);
}
// send an ACK if requested
network_send_ack(p);
}

View file

@ -0,0 +1,123 @@
#ifndef PACKET_H
#define PACKET_H
#include "PR/ultratypes.h"
#include <time.h>
#include <types.h>
#include <assert.h>
#include <stdbool.h>
#define PACKET_LENGTH 1024
enum PacketType {
PACKET_ACK,
PACKET_PLAYER,
PACKET_OBJECT,
PACKET_SPAWN_OBJECTS,
PACKET_SPAWN_STAR,
PACKET_LEVEL_WARP,
PACKET_INSIDE_PAINTING,
PACKET_COLLECT_STAR,
PACKET_COLLECT_COIN,
PACKET_COLLECT_ITEM,
PACKET_RESERVATION_REQUEST,
PACKET_RESERVATION,
PACKET_SAVE_FILE_REQUEST,
PACKET_SAVE_FILE,
PACKET_CHAT,
PACKET_CUSTOM = 255,
};
struct Packet {
u16 dataLength;
u16 cursor;
bool error;
bool reliable;
u16 seqId;
bool sent;
u8 buffer[PACKET_LENGTH];
};
// packet.c
void packet_receive(struct Packet* packet);
// packet_read_write.c
void packet_init(struct Packet* packet, enum PacketType packetType, bool reliable);
void packet_write(struct Packet* packet, void* data, u16 length);
void packet_read(struct Packet* packet, void* data, u16 length);
u32 packet_hash(struct Packet* packet);
bool packet_check_hash(struct Packet* packet);
// packet_reliable.c
void network_send_ack(struct Packet* p);
void network_receive_ack(struct Packet* p);
void network_remember_reliable(struct Packet* p);
void network_update_reliable(void);
// packet_player.c
void network_update_player(void);
void network_receive_player(struct Packet* p);
// packet_object.c
struct SyncObject* network_init_object(struct Object* object, float maxSyncDistance);
void network_init_object_field(struct Object* o, void* field);
bool network_owns_object(struct Object* o);
bool network_sync_object_initialized(struct Object* o);
void network_clear_sync_objects(void);
void network_set_sync_id(struct Object* o);
void network_send_object(struct Object* o);
void network_send_object_reliability(struct Object* o, bool reliable);
void network_receive_object(struct Packet* p);
void network_forget_sync_object(struct SyncObject* so);
void network_update_objects(void);
// packet_spawn_object.c
void network_send_spawn_objects(struct Object* objects[], u32 models[], u8 objectCount);
void network_receive_spawn_objects(struct Packet* p);
// packet_spawn_star.c
void network_send_spawn_star(struct Object* o, u8 starType, f32 x, f32 y, f32 z, u32 behParams);
void network_receive_spawn_star(struct Packet* p);
// packet_level_warp.c
void network_send_level_warp_begin(void);
void network_receive_level_warp(struct Packet* p);
// packet_inside_painting.c
void network_send_inside_painting(void);
void network_receive_inside_painting(struct Packet* p);
// packet_collect_star.c
void network_send_collect_star(struct Object* o, s16 coinScore, s16 starIndex);
void network_receive_collect_star(struct Packet* p);
// packet_collect_coin.c
void network_send_collect_coin(struct Object* o);
void network_receive_collect_coin(struct Packet* p);
// packet_collect_item.c
void network_send_collect_item(struct Object* o);
void network_receive_collect_item(struct Packet* p);
// packet_reservation.c
void network_send_reservation_request(void);
void network_receive_reservation_request(UNUSED struct Packet* p);
void network_send_reservation(void);
void network_receive_reservation(struct Packet* p);
// packet_save_file.c
void network_send_save_file_request(void);
void network_receive_save_file_request(UNUSED struct Packet* p);
void network_send_save_file(void);
void network_receive_save_file(struct Packet* p);
// packet_custom.c
u8 network_register_custom_packet(void (*send_callback)(struct Packet* p, void* params), void (*receive_callback)(struct Packet* p));
void network_send_custom(u8 customId, bool reliable, void* params);
void network_receive_custom(struct Packet* p);
// packet_chat.c
void network_send_chat(char* message);
void network_receive_chat(struct Packet* p);
#endif