From 4c7156d0b20447e29f354679acdcd28480df91bc Mon Sep 17 00:00:00 2001 From: ubq323 Date: Mon, 22 Apr 2024 01:21:38 +0100 Subject: rlerle --- packet_reader/packet_reader.ha | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'packet_reader/packet_reader.ha') diff --git a/packet_reader/packet_reader.ha b/packet_reader/packet_reader.ha index a1b5b8f..d541a11 100644 --- a/packet_reader/packet_reader.ha +++ b/packet_reader/packet_reader.ha @@ -100,17 +100,20 @@ export fn next(pr: *packet_reader) (packet | done | error) = { case drawing::deser_fail => return "deser fail": error; }; case packet_type::SEND_CHUNK => - // return value is BORROWED from the BUFFER + // return value muste be FREED by CALLER + // (we don't really need a copy here. todo) + // ((the copy isn't here, it's wherever uses here)) const pos_bytes = payload[0..8]; - const chunk_data_bytes = payload[8..]; + const compressed_data_bytes = payload[8..]; const pos = ( endian::legetu32(pos_bytes[0..4]): i32, endian::legetu32(pos_bytes[4..8]): i32 ): pos; - const d = cast_u8s_to_u32s(chunk_data_bytes); - if (len(d) != CHUNKSIZE*CHUNKSIZE) + const chunk_data_compressed = cast_u8s_to_u32s(compressed_data_bytes); + const chunk_data = rle_decode(chunk_data_compressed); + if (len(chunk_data) != CHUNKSIZE*CHUNKSIZE) return "wrong chunk size??": error; - return packet_sendchunk { world_pos = pos, chunk_data = d }; + return packet_sendchunk { world_pos = pos, chunk_data = chunk_data }; case packet_type::POSITION => if (len(payload) != 8) return "position packet wrong size": error; @@ -155,8 +158,10 @@ export fn send(sock: io::file, packet: packet) (void | io::error) = { const pos_buf: [8]u8 = [0...]; endian::leputu32(pos_buf[0..4],packet.world_pos.0: u32); endian::leputu32(pos_buf[4..8],packet.world_pos.1: u32); - const chunk_data_bytes = cast_u32s_to_u8s(packet.chunk_data); - send_raw(sock, packet_type::SEND_CHUNK, pos_buf, chunk_data_bytes)?; + const chunk_data_compressed = rle_encode(packet.chunk_data); + defer free(chunk_data_compressed); + const compressed_data_bytes = cast_u32s_to_u8s(chunk_data_compressed); + send_raw(sock, packet_type::SEND_CHUNK, pos_buf, compressed_data_bytes)?; case let pos: packet_position => const pos_buf: [8]u8 = [0...]; endian::leputu32(pos_buf[0..4], pos.0: u32); -- cgit v1.2.3