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-11 15:48:43 -06:00
|
|
|
.{Window, WindowProps, WindowData, MessageHeader, BUFFER, message, receive_message, send_message, await_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,
|
2024-11-11 15:48:43 -06:00
|
|
|
// ! replace this with an actual collection when we get an allocator
|
2024-11-10 19:03:01 -06:00
|
|
|
windows: ^WindowData,
|
|
|
|
}
|
2024-11-10 12:57:48 -06:00
|
|
|
|
2024-11-11 15:48:43 -06:00
|
|
|
// ! in the future this should be safely handled
|
|
|
|
server := @as(WindowServer, idk)
|
2024-11-10 12:57:48 -06:00
|
|
|
|
2024-11-11 15:48:43 -06:00
|
|
|
start := fn(): void {
|
2024-11-10 19:03:01 -06:00
|
|
|
windows := memory.alloc(WindowData, 10)
|
|
|
|
server = .(0, buffer.create(BUFFER), windows)
|
2024-11-11 15:48:43 -06:00
|
|
|
log.debug("server: started server\0")
|
2024-11-10 12:57:48 -06:00
|
|
|
}
|
|
|
|
|
2024-11-11 15:48:43 -06:00
|
|
|
// ! this function will be rewritten to several functions that allow the server mainloop to handle these itself
|
2024-11-10 19:03:01 -06:00
|
|
|
handle_connections := fn(): bool {
|
2024-11-11 15:48:43 -06:00
|
|
|
recv := receive_message(MessageHeader, server.id)
|
2024-11-10 19:03:01 -06:00
|
|
|
if recv == null {
|
|
|
|
return true
|
2024-11-11 15:48:43 -06:00
|
|
|
}
|
|
|
|
if recv.kind == message.syn {
|
|
|
|
buffer_id := buffer.create_without_name()
|
|
|
|
|
|
|
|
send_message(MessageHeader, .(message.ack, buffer_id), server.id)
|
|
|
|
log.debug("server: sent ack\0")
|
|
|
|
|
|
|
|
resp := await_message(WindowProps, buffer_id)
|
|
|
|
log.debug("server: received props\0")
|
2024-11-10 19:03:01 -06:00
|
|
|
|
|
|
|
data := WindowData.(@as(WindowProps, resp), buffer_id)
|
2024-11-11 15:48:43 -06:00
|
|
|
send_message(WindowData, data, buffer_id)
|
|
|
|
log.debug("server: sent window data\0");
|
|
|
|
*(server.windows + server.window_count) = data
|
|
|
|
server.window_count += 1
|
2024-11-10 19:03:01 -06:00
|
|
|
} else if recv.kind == message.shutdown {
|
2024-11-11 15:48:43 -06:00
|
|
|
log.warn("server: shutdown handled without validation\n\r this is temporary behaviour\0")
|
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
|
|
|
return true
|
2024-11-10 12:57:48 -06:00
|
|
|
}
|