2024-11-10 12:57:48 -06:00
|
|
|
.{math, log, string, random, buffer, memory} := @use("../../stn/src/lib.hb");
|
|
|
|
.{Surface, new_surface} := @use("../../render/src/lib.hb");
|
2024-11-10 19:03:01 -06:00
|
|
|
.{Window, WindowProps, WindowData, MessageHeader, BUFFER, message} := @use("./lib.hb")
|
2024-11-10 12:57:48 -06:00
|
|
|
|
2024-11-10 19:03:01 -06:00
|
|
|
WindowServer := struct {
|
|
|
|
window_count: uint,
|
|
|
|
id: uint,
|
|
|
|
// replace this with an actual collection when we get an allocator
|
|
|
|
windows: ^WindowData,
|
|
|
|
}
|
2024-11-10 12:57:48 -06:00
|
|
|
|
|
|
|
server := @as(?WindowServer, null)
|
|
|
|
|
2024-11-10 19:03:01 -06:00
|
|
|
init := fn(): void {
|
|
|
|
if server != null {
|
|
|
|
return
|
|
|
|
}
|
2024-11-10 12:57:48 -06:00
|
|
|
log.info("server: starting server\0")
|
2024-11-10 19:03:01 -06:00
|
|
|
windows := memory.alloc(WindowData, 10)
|
|
|
|
server = .(0, buffer.create(BUFFER), windows)
|
2024-11-10 12:57:48 -06:00
|
|
|
}
|
|
|
|
|
2024-11-10 19:03:01 -06:00
|
|
|
handle_connections := fn(): bool {
|
2024-11-10 12:57:48 -06:00
|
|
|
if server == null {
|
2024-11-10 19:03:01 -06:00
|
|
|
return false
|
2024-11-10 12:57:48 -06:00
|
|
|
}
|
2024-11-10 19:03:01 -06:00
|
|
|
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
|
2024-11-10 12:57:48 -06:00
|
|
|
}
|
2024-11-10 19:03:01 -06:00
|
|
|
return true
|
2024-11-10 12:57:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
should_shutdown := fn(): bool {
|
|
|
|
return false
|
|
|
|
}
|