aboutsummaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2024-04-05 02:01:34 +0100
committerubq323 <ubq323@ubq323.website>2024-04-05 02:01:34 +0100
commitd097561c1ee24f20d5dd48fbe35301e8d9cc7cac (patch)
tree2a54e6611fc07127909531eb0ccdca5979e60591 /server
parentdf26827a3f00d64f5af01271818c7c8bd7eaa387 (diff)
refactor slightly
Diffstat (limited to 'server')
-rw-r--r--server/main.ha49
-rw-r--r--server/packet.ha52
2 files changed, 52 insertions, 49 deletions
diff --git a/server/main.ha b/server/main.ha
index f1eec0a..4476221 100644
--- a/server/main.ha
+++ b/server/main.ha
@@ -11,55 +11,6 @@ def PORT: u16 = 41460;
type conn = net::socket;
-type packet = []u8;
-def PCKSZ: size = 4;
-
-type packet_reader = struct {
- buf: [256]u8,
- good: []u8,
-};
-
-fn packet_reader_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
-fn packet_reader_read(pr: *packet_reader, sock: net::socket) (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)!;
-};
-
-
-fn packet_reader_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;
-};
export fn main() void = {
const listener = tcp::listen(ip::ANY_V4, PORT)!;
diff --git a/server/packet.ha b/server/packet.ha
new file mode 100644
index 0000000..b12d121
--- /dev/null
+++ b/server/packet.ha
@@ -0,0 +1,52 @@
+use io;
+use fmt;
+
+type packet = []u8;
+def PCKSZ: size = 4;
+
+type packet_reader = struct {
+ buf: [256]u8,
+ good: []u8,
+};
+
+fn packet_reader_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
+fn packet_reader_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)!;
+};
+
+
+fn packet_reader_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;
+};