From 0eeff114b44ccdd6bc430bc5f9583e2a3eea9823 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Thu, 11 Apr 2024 20:09:52 +0100 Subject: save the world.... my final message..... --- server/main.ha | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 67 insertions(+), 4 deletions(-) (limited to 'server') diff --git a/server/main.ha b/server/main.ha index 59e1306..abcb0fe 100644 --- a/server/main.ha +++ b/server/main.ha @@ -1,25 +1,76 @@ +use bufio; use fmt; use net; use net::tcp; use net::ip; use io; use os; +use fs; +use strings; +use time; +use time::date; +use time::chrono; use unix::poll; -use bufio; use drawing; +use drawing::{pos}; use packet_reader; + def PORT: u16 = 41460; type conn = net::socket; +def CHUNKSIZE = 512; export fn main() void = { + // create 4 pictures. later we will have more pictures + // but for now there are just 4, and they are at fixed positions + const offs: [_]pos = [ + (0,0), (0,CHUNKSIZE), (CHUNKSIZE,0), (CHUNKSIZE,CHUNKSIZE), + ]; + + let pictures: []drawing::picture = alloc([],len(offs)); + for (let i = 0z; i < len(offs); i +=1){ + append(pictures, drawing::picture { + w = CHUNKSIZE, + h = CHUNKSIZE, + d = alloc([0...],CHUNKSIZE*CHUNKSIZE: size): *[*]u32, + world_pos = offs[i], + }); + }; + for (const p &.. pictures) drawing::clear_picture(p, 0xffffff); + + const listener = tcp::listen(ip::ANY_V4, PORT)!; - loop(listener); + loop(listener, pictures); }; -fn loop(listener: net::socket) void = { +def save_interval = 5 * time::SECOND; + +fn save_world(pictures: []drawing::picture) void = { + fmt::printfln("saving world!")!; + for (const pic &.. pictures) { + const filename = fmt::asprintf("./c.{}.{}.ppm",pic.world_pos.0, pic.world_pos.1); + fmt::printfln("\t-> {}",filename)!; + defer free(filename); + const mode = fs::mode::USER_RW | fs::mode::GROUP_RW; + const file = os::create(filename, mode)!; + defer { + 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))!; + 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])!; + }; + }; + +}; + +fn loop(listener: net::socket, pictures: []drawing::picture) void = { // pollfds[0] is the listener // pollfds[n>0] corresponds to packet_readers[n-1] let pollfds: []poll::pollfd = alloc([ poll::pollfd { @@ -28,9 +79,20 @@ fn loop(listener: net::socket) void = { let packet_readers: []packet_reader::packet_reader = []; + let now = time::now(time::clock::MONOTONIC); + let next = time::add(now, save_interval); + for (true) { fmt::println("poll.")!; - poll::poll(pollfds, poll::INDEF)!; + const timeout = time::diff(now, next); + poll::poll(pollfds, timeout)!; + + now = time::now(time::clock::MONOTONIC); + if (time::compare(now, next) >= 0) { + save_world(pictures); + next = time::add(now, save_interval); + }; + if (0 != pollfds[0].revents & poll::event::POLLIN) { fmt::println("new conn")!; const new_conn = tcp::accept(pollfds[0].fd)!; @@ -52,6 +114,7 @@ fn loop(listener: net::socket) void = { 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; -- cgit v1.2.3