stream world data from pxl8d to pxl8

This commit is contained in:
asrael 2026-01-25 09:26:30 -06:00
parent 39ee0fefb7
commit a71a9840b2
55 changed files with 5290 additions and 2131 deletions

View file

@ -1,4 +1,6 @@
#include "pxl8_net.h"
#include "pxl8_chunk_cache.h"
#include "pxl8_log.h"
#include "pxl8_mem.h"
#include <stdlib.h>
@ -28,6 +30,9 @@
struct pxl8_net {
char address[256];
u32 chunk_id;
u8 chunk_type;
pxl8_chunk_cache* chunk_cache;
bool connected;
pxl8_entity_state entities[PXL8_MAX_SNAPSHOT_ENTITIES];
pxl8_event_msg events[PXL8_MAX_SNAPSHOT_EVENTS];
@ -36,7 +41,6 @@ struct pxl8_net {
u64 input_head;
u64 input_oldest_tick;
f32 interp_time;
pxl8_net_mode mode;
u16 port;
u8 predicted_state[PXL8_NET_USERDATA_SIZE];
u64 predicted_tick;
@ -98,7 +102,6 @@ pxl8_net* pxl8_net_create(const pxl8_net_config* config) {
pxl8_net* net = pxl8_calloc(1, sizeof(pxl8_net));
if (!net) return NULL;
net->mode = config->mode;
net->port = config->port ? config->port : PXL8_NET_DEFAULT_PORT;
net->sock = INVALID_SOCK;
net->connected = false;
@ -209,6 +212,32 @@ bool pxl8_net_poll(pxl8_net* net) {
pxl8_msg_header hdr;
usize offset = pxl8_protocol_deserialize_header(net->recv_buf, len, &hdr);
if (hdr.type == PXL8_MSG_CHUNK) {
if (!net->chunk_cache) return false;
pxl8_chunk_msg_header chunk_hdr;
offset += pxl8_protocol_deserialize_chunk_msg_header(net->recv_buf + offset, len - offset, &chunk_hdr);
const u8* payload = net->recv_buf + offset;
usize payload_len = chunk_hdr.payload_size;
if (payload_len > len - offset) {
payload_len = len - offset;
}
pxl8_chunk_cache_receive(net->chunk_cache, &chunk_hdr, payload, payload_len);
return true;
}
if (hdr.type == PXL8_MSG_CHUNK_ENTER) {
pxl8_chunk_enter_msg chunk_msg;
pxl8_protocol_deserialize_chunk_enter(net->recv_buf + offset, len - offset, &chunk_msg);
net->chunk_id = chunk_msg.chunk_id;
net->chunk_type = chunk_msg.chunk_type;
pxl8_debug("[CLIENT] Received CHUNK_ENTER type=%u id=%u", chunk_msg.chunk_type, chunk_msg.chunk_id);
return true;
}
if (hdr.type != PXL8_MSG_SNAPSHOT) return false;
pxl8_snapshot_header snap;
@ -311,3 +340,23 @@ void pxl8_net_update(pxl8_net* net, f32 dt) {
if (!net) return;
net->interp_time += dt;
}
void pxl8_net_set_chunk_cache(pxl8_net* net, pxl8_chunk_cache* cache) {
if (!net) return;
net->chunk_cache = cache;
}
pxl8_chunk_cache* pxl8_net_chunk_cache(pxl8_net* net) {
if (!net) return NULL;
return net->chunk_cache;
}
u32 pxl8_net_chunk_id(const pxl8_net* net) {
if (!net) return 0;
return net->chunk_id;
}
u8 pxl8_net_chunk_type(const pxl8_net* net) {
if (!net) return PXL8_CHUNK_TYPE_VXL;
return net->chunk_type;
}