aboutsummaryrefslogtreecommitdiff
path: root/packet_reader
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2024-04-14 00:35:19 +0100
committerubq323 <ubq323@ubq323.website>2024-04-14 00:35:19 +0100
commit6cff8d620cd92f9ddc63c6891b3e669b5294619e (patch)
treed3632c81992fdbf0f8435fa225920122bee29776 /packet_reader
parentc95e947d90a02091aef4e63693ce3205fdfba3b6 (diff)
multiple chunks on server and unload not near players. just need client to req chunks now
Diffstat (limited to 'packet_reader')
-rw-r--r--packet_reader/packet_reader.ha65
1 files changed, 50 insertions, 15 deletions
diff --git a/packet_reader/packet_reader.ha b/packet_reader/packet_reader.ha
index 30911df..df4d417 100644
--- a/packet_reader/packet_reader.ha
+++ b/packet_reader/packet_reader.ha
@@ -2,6 +2,7 @@ use io;
use fmt;
use endian;
use drawing;
+use drawing::{pos,CHUNKSIZE};
export type error = !str;
@@ -27,15 +28,23 @@ fn cast_u8s_to_u32s(in: []u8) []u32 =
export type packet_type = enum u8 {
DRAW_OP,
SEND_CHUNK,
+ POSITION,
+ REQ_CHUNK
};
export type packet_drawop = drawing::op;
export type packet_sendchunk = struct {
- world_pos: drawing::pos,
+ world_pos: pos,
chunk_data: []u32,
};
+export type packet_position = pos;
+export type packet_reqchunk = pos;
-export type packet = (packet_drawop | packet_sendchunk);
+export type packet = (
+ packet_drawop
+ | packet_sendchunk
+ | packet_position
+ | packet_reqchunk);
// call when input is ready. could block otherwise
export fn read(pr: *packet_reader, sock: io::handle) (void | io::error | io::EOF) = {
@@ -43,7 +52,7 @@ export fn read(pr: *packet_reader, sock: io::handle) (void | io::error | io::EOF
const read_pos = if (remaining_amt > 0) {
// still some unconsumed content in the buffer
// move unconsumed stuff to start of buffer
- fmt::printfln("moving {} remaining bytes",remaining_amt)!;
+ // fmt::printfln("moving {} remaining bytes",remaining_amt)!;
pr.buf[0..remaining_amt] = pr.good;
yield remaining_amt;
} else 0z;
@@ -51,10 +60,10 @@ export fn read(pr: *packet_reader, sock: io::handle) (void | io::error | io::EOF
case io::EOF => return io::EOF;
case let n: size => yield n;
};
- fmt::printfln("read {} bytes",nread)!;
+ // fmt::printfln("read {} bytes",nread)!;
const total_amt = read_pos + nread;
pr.good = pr.buf[0..total_amt];
- fmt::printfln("now {} bytes in buffer",total_amt)!;
+ // fmt::printfln("now {} bytes in buffer",total_amt)!;
};
// packet format:
@@ -69,34 +78,50 @@ export fn next(pr: *packet_reader) (packet | done | error) = {
// and return the packet,
// or, ascertain there is no full packet, and return done
if (len(pr.good) < size(u32)) return done;
- fmt::println("a")!;
const packet_len = endian::legetu32(pr.good[0..4]);
if (packet_len < 8) return "packet size field too small": error;
- fmt::println("b")!;
if (len(pr.good) < packet_len) return done;
- fmt::println("c")!;
const packet_bytes = pr.good[..packet_len];
pr.good = pr.good[packet_len..];
const ty = endian::legetu32(packet_bytes[4..8]): packet_type;
+ const payload = packet_bytes[8..];
switch (ty) {
case packet_type::DRAW_OP =>
- const op = drawing::deser_op(packet_bytes[8..]);
- fmt::println("d")!;
+ const op = drawing::deser_op(payload);
return op: packet_drawop;
case packet_type::SEND_CHUNK =>
// return value is BORROWED from the BUFFER
- const pos_bytes = packet_bytes[8..16];
- const chunk_data_bytes = packet_bytes[16..];
+ const pos_bytes = payload[0..8];
+ const chunk_data_bytes = payload[8..];
const pos = (
endian::legetu32(pos_bytes[0..4]): i32,
endian::legetu32(pos_bytes[4..8]): i32
- ): drawing::pos;
+ ): pos;
const d = cast_u8s_to_u32s(chunk_data_bytes);
- assert(len(d) == 512*512,"wrong chunk size??");
- fmt::println("e")!;
+ if (len(d) != CHUNKSIZE*CHUNKSIZE)
+ return "wrong chunk size??": error;
return packet_sendchunk { world_pos = pos, chunk_data = d };
+ case packet_type::POSITION =>
+ if (len(payload) != 8)
+ return "position packet wrong size": error;
+ const pos = (
+ endian::legetu32(payload[0..4]): i32,
+ endian::legetu32(payload[4..8]): i32
+ ): pos;
+ return pos: packet_position;
+ case packet_type::REQ_CHUNK =>
+ if (len(payload) != 8)
+ return "reqchunk packet wrong size": error;
+ const world_pos = (
+ endian::legetu32(payload[0..4]): i32,
+ endian::legetu32(payload[4..8]): i32
+ ): pos;
+ if (world_pos.0 % CHUNKSIZE != 0 || world_pos.1 % CHUNKSIZE != 0) {
+ return "invalid world_pos": error;
+ };
+ return world_pos: packet_reqchunk;
};
};
@@ -124,5 +149,15 @@ export fn send(sock: io::file, packet: packet) (void | io::error) = {
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)?;
+ case let pos: packet_position =>
+ const pos_buf: [8]u8 = [0...];
+ endian::leputu32(pos_buf[0..4], pos.0: u32);
+ endian::leputu32(pos_buf[4..8], pos.1: u32);
+ send_raw(sock, packet_type::POSITION, pos_buf)?;
+ case let world_pos: packet_reqchunk =>
+ const pos_buf: [8]u8 = [0...];
+ endian::leputu32(pos_buf[0..4], world_pos.0: u32);
+ endian::leputu32(pos_buf[4..8], world_pos.1: u32);
+ send_raw(sock, packet_type::REQ_CHUNK, pos_buf)?;
};
};