aboutsummaryrefslogtreecommitdiff
path: root/packet_reader
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2024-04-22 01:21:38 +0100
committerubq323 <ubq323@ubq323.website>2024-04-22 01:21:38 +0100
commit4c7156d0b20447e29f354679acdcd28480df91bc (patch)
treefdcaca21c06781a8732e50c6081cbc8ea30c3fc8 /packet_reader
parent11c8b134d7de3d7b2249d74b26a87bc9d6acc27d (diff)
rlerle
Diffstat (limited to 'packet_reader')
-rw-r--r--packet_reader/packet_reader.ha19
-rw-r--r--packet_reader/rle.ha19
2 files changed, 28 insertions, 10 deletions
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);
diff --git a/packet_reader/rle.ha b/packet_reader/rle.ha
index af9f080..36e0b29 100644
--- a/packet_reader/rle.ha
+++ b/packet_reader/rle.ha
@@ -16,11 +16,11 @@ export fn rle_encode(d: []u32) []u32 = {
for (ix < len(d) && d[ix] == this) ix += 1;
const val = this;
let count = (ix - start): int;
- fmt::printfln("#{:_06x} x{}",val, count)!;
- for (count > 0) {
- append(out, item_encode(count%256, val));
+ for (count > 256) {
+ append(out, item_encode(256, val));
count -= 256;
};
+ append(out, item_encode(count, val));
start = ix;
};
return out;
@@ -63,3 +63,16 @@ fn dotest(eu: []u32) void = {
@test fn oeuoeu2() void = dotest([1,1,1]);
@test fn oeuoeu2() void = dotest([1,6]);
@test fn oeuoeu2() void = dotest([1]);
+
+fn testm(n: size) void = {
+ let e: []u32 = [];
+ append(e, [17...], n);
+ defer free(e);
+ dotest(e);
+};
+
+@test fn t254() void = testm(254);
+@test fn t255() void = testm(255);
+@test fn t256() void = testm(256);
+@test fn t257() void = testm(257);
+@test fn t258() void = testm(258);