aboutsummaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2024-04-13 00:49:14 +0100
committerubq323 <ubq323@ubq323.website>2024-04-13 00:49:14 +0100
commitbf03e2a0d86a4b5202a72aa66da5dc0e2c0b412b (patch)
treeda47f07c9f3c7946044153f21b7d4bcf991ea1bf /server
parent0eeff114b44ccdd6bc430bc5f9583e2a3eea9823 (diff)
multiple packet types. code quality is declining steadily
Diffstat (limited to 'server')
-rw-r--r--server/main.ha68
1 files changed, 42 insertions, 26 deletions
diff --git a/server/main.ha b/server/main.ha
index abcb0fe..9f21932 100644
--- a/server/main.ha
+++ b/server/main.ha
@@ -45,7 +45,7 @@ export fn main() void = {
loop(listener, pictures);
};
-def save_interval = 5 * time::SECOND;
+def save_interval = 20 * time::SECOND;
fn save_world(pictures: []drawing::picture) void = {
fmt::printfln("saving world!")!;
@@ -59,13 +59,20 @@ fn save_world(pictures: []drawing::picture) void = {
fmt::printfln("\t. {}",filename)!;
io::close(file)!;
};
+
const header = fmt::asprintf("P6\n{} {}\n{}\n", pic.w, pic.h, 255);
defer free(header);
io::writeall(file, strings::toutf8(header))!;
+
+ let buf: []u8 = alloc([0...],3*pic.w*pic.h);
+ defer free(buf);
for (let i = 0z; i < pic.w * pic.h; i += 1) {
const px = pic.d[i];
- io::writeall(file, [(px&0xff): u8, ((px>>8)&0xff): u8, ((px>>16)&0xff): u8])!;
+ buf[3*i ] = ((px>>16)&0xff): u8;
+ buf[3*i+1] = ((px>>8) &0xff): u8;
+ buf[3*i+2] = ((px) &0xff): u8;
};
+ io::writeall(file, buf)!;
};
};
@@ -81,6 +88,7 @@ fn loop(listener: net::socket, pictures: []drawing::picture) void = {
let now = time::now(time::clock::MONOTONIC);
let next = time::add(now, save_interval);
+ save_world(pictures);
for (true) {
fmt::println("poll.")!;
@@ -104,36 +112,44 @@ fn loop(listener: net::socket, pictures: []drawing::picture) void = {
append(packet_readers, packet_reader::new());
fmt::printfln("there are now {},{} conns.",len(pollfds),len(packet_readers))!;
};
- for (let i = 1z; i < len(pollfds); i += 1) {
- if (0 != pollfds[i].revents & poll::event::POLLIN) {
- match (packet_reader::read(&packet_readers[i-1], pollfds[i].fd)) {
+ for (let connidx = 1z; connidx < len(pollfds); connidx += 1) {
+ if (0 != pollfds[connidx].revents & poll::event::POLLIN) {
+ match (packet_reader::read(&packet_readers[connidx-1], pollfds[connidx].fd)) {
case void =>
- let j = 1;
- for (const packet_bytes => packet_reader::next(&packet_readers[i-1])) {
- fmt::printf("#{}:{} got {:2}",i,j,len(packet_bytes))!;
- j += 1;
- for (let byte .. packet_bytes) fmt::printf(" {:x}",byte)!;
- const op = drawing::deser_op(packet_bytes) as drawing::op_circle;
- drawing::perform(pictures, op);
- fmt::printfln("\t({:+6},{:+6})",op.0,op.1)!;
- for (let k = 1z; k < len(pollfds); k += 1) {
- if (k == i) continue;
- fmt::printfln("\t -> #{}",k)!;
- io::writeall(pollfds[k].fd, packet_bytes)!;
- };
+ for (const packet => packet_reader::next(&packet_readers[connidx-1])!) {
+ handle_packet(pictures, pollfds, packet, connidx);
};
case let err: io::error =>
- fmt::printfln("#{} error: {}",i,io::strerror(err))!;
- delete(pollfds[i]);
- delete(packet_readers[i-1]);
- i -= 1;
+ fmt::printfln("#{} error: {}",connidx,io::strerror(err))!;
+ delete(pollfds[connidx]);
+ delete(packet_readers[connidx-1]);
+ connidx -= 1;
case io::EOF =>
- fmt::printfln("#{} disconnect",i)!;
- delete(pollfds[i]);
- delete(packet_readers[i-1]);
- i -= 1;
+ fmt::printfln("#{} disconnect",connidx)!;
+ delete(pollfds[connidx]);
+ delete(packet_readers[connidx-1]);
+ connidx -= 1;
};
};
};
};
};
+
+// ehh, really need a struct to put pollfds, pictures, connections... in
+fn handle_packet(pictures: []drawing::picture, pollfds: []poll::pollfd, packet: packet_reader::packet, connidx: size) void = {
+ match (packet) {
+ case let op: packet_reader::packet_drawop =>
+ const opc = op as drawing::op_circle;
+ fmt::printfln("#{}: drawop ({:+6},{:+6})",connidx,opc.0,opc.1)!;
+ drawing::perform(pictures, opc);
+ for (let otheridx = 1z; otheridx < len(pollfds); otheridx += 1) {
+ if (otheridx == connidx) continue;
+ fmt::printfln("\t -> #{}",otheridx)!;
+ // ehhh shouldn't reserialize for each other thing
+ packet_reader::send(pollfds[otheridx].fd, packet)!;
+ };
+ case =>
+ abort("other packets not supported yet");
+ };
+};
+