diff options
author | ubq323 <ubq323@ubq323.website> | 2024-04-10 23:03:50 +0100 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2024-04-11 13:35:06 +0100 |
commit | 79a9af88e53a6b0f7070a5fe97bbd399d139d00d (patch) | |
tree | 8df45e69d9d3d9ccab483b650f1469eb2118ce6f /packet_reader | |
parent | 2677d3ae0f4f5323ca67c6436cd3a55fd4fe8021 (diff) |
the PAINT is now MULTIPLAYER !!!!!
Diffstat (limited to 'packet_reader')
-rw-r--r-- | packet_reader/packet_reader.ha | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/packet_reader/packet_reader.ha b/packet_reader/packet_reader.ha new file mode 100644 index 0000000..43b85d1 --- /dev/null +++ b/packet_reader/packet_reader.ha @@ -0,0 +1,52 @@ +use io; +use fmt; + +export type packet = []u8; +def PCKSZ: size = 8; + +export type packet_reader = struct { + buf: [256]u8, + good: []u8, +}; + +export fn new() packet_reader = { + let pr = packet_reader { + buf = [0...], + ... + }; + pr.good = pr.buf[0..0]; + return pr; + +}; + +// call when input is ready. could block otherwise +export fn read(pr: *packet_reader, sock: io::handle) (void | io::error | io::EOF) = { + const remaining_amt = len(pr.good); + 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)!; + pr.buf[0..remaining_amt] = pr.good; + yield remaining_amt; + } else 0z; + const nread = match(io::read(sock, pr.buf[read_pos..])?) { + case io::EOF => return io::EOF; + case let n: size => yield n; + }; + 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)!; +}; + + +export fn next(pr: *packet_reader) (packet | done) = { + // either parse a full packet out of the front of good, + // move good along that many bytes, + // and return the packet, + // or, ascertain there is no full packet, and return done + if (len(pr.good) < PCKSZ) return done; + const packet_bytes = pr.good[..PCKSZ]; + pr.good = pr.good[PCKSZ..]; + return packet_bytes; +}; |