.{math, log, string, random, buffer, memory} := @use("../../stn/src/lib.hb"); .{Surface, new_surface} := @use("../../render/src/lib.hb"); .{Window, WindowProps, WindowData, MessageHeader, BUFFER, message} := @use("./lib.hb") WindowServer := struct { window_count: uint, id: uint, // replace this with an actual collection when we get an allocator windows: ^WindowData, } server := @as(?WindowServer, null) init := fn(): void { if server != null { return } log.info("server: starting server\0") windows := memory.alloc(WindowData, 10) server = .(0, buffer.create(BUFFER), windows) } handle_connections := fn(): bool { if server == null { return false } recv := memory.uninit(MessageHeader) buffer.recv(?MessageHeader, @unwrap(server).id, &recv) if recv == null { return true } else if recv.kind == message.syn { buffer_id := buffer.create("asdf\0") buffer.write(?MessageHeader, &@as(?MessageHeader, .(message.ack, buffer_id)), @unwrap(server).id) log.info("server: sent ack\0") timer := 0 resp := memory.uninit(WindowProps) loop if timer < 1000 { buffer.recv(?WindowProps, buffer_id, &resp) if resp != null { break } timer += 1 } else { log.error("server: no props in timeout\0") return true } log.info("server: received props\0") surface := new_surface(resp.dimensions.x, resp.dimensions.y) data := WindowData.(@as(WindowProps, resp), buffer_id) buffer.write(?WindowData, &@as(?WindowData, data), buffer_id) log.info("server: sent window data\0"); *(@unwrap(server).windows + @unwrap(server).window_count) = data @unwrap(server).window_count += 1 } else if recv.kind == message.quit { // todo: quit } else if recv.kind == message.shutdown { return false } return true } should_shutdown := fn(): bool { return false }