2024-11-30 15:52:18 -06:00
|
|
|
.{math: .{Vec2}, buffer, memory} := @use("stn");
|
2024-11-10 12:57:48 -06:00
|
|
|
.{Surface} := @use("../../render/src/lib.hb")
|
|
|
|
|
2024-11-13 11:03:31 -06:00
|
|
|
$BUFFER_SERVER := "sunset_server\0"
|
|
|
|
$BUFFER_CLIENT := "sunset_client\0"
|
|
|
|
|
|
|
|
Channel := packed struct {
|
|
|
|
client: uint,
|
|
|
|
server: uint,
|
|
|
|
}
|
2024-11-10 12:57:48 -06:00
|
|
|
|
|
|
|
client := @use("./client.hb")
|
|
|
|
server := @use("./server.hb")
|
|
|
|
message := @use("./message.hb")
|
2024-11-13 11:03:31 -06:00
|
|
|
permissions := @use("./permissions.hb")
|
|
|
|
|
2024-11-14 15:28:21 -06:00
|
|
|
$send_message := fn($Expr: type, kind: MessageKind, msg: Expr, buffer_id: uint): void {
|
2024-11-13 11:03:31 -06:00
|
|
|
buffer.write(?Message(Expr), buffer_id, &@as(?Message(Expr), .(.(kind), msg)))
|
|
|
|
}
|
|
|
|
|
2024-11-14 15:28:21 -06:00
|
|
|
$send_header := fn(kind: MessageKind, buffer_id: uint): void {
|
2024-11-13 11:03:31 -06:00
|
|
|
buffer.write(?MessageHeader, buffer_id, &@as(?MessageHeader, .(kind)))
|
|
|
|
}
|
2024-11-10 12:57:48 -06:00
|
|
|
|
2024-11-13 11:03:31 -06:00
|
|
|
$recv_message := fn($Expr: type, buffer_id: uint): Message(Expr) {
|
|
|
|
response := @as(?Message(Expr), null)
|
|
|
|
buffer.recv(?Message(Expr), buffer_id, &response)
|
|
|
|
return response
|
2024-11-11 15:48:43 -06:00
|
|
|
}
|
|
|
|
|
2024-11-13 11:03:31 -06:00
|
|
|
$recv_header := fn(buffer_id: uint): ?MessageHeader {
|
|
|
|
response := @as(?MessageHeader, null)
|
|
|
|
buffer.recv(?MessageHeader, buffer_id, &response)
|
|
|
|
return response
|
2024-11-11 15:48:43 -06:00
|
|
|
}
|
|
|
|
|
2024-11-14 15:28:21 -06:00
|
|
|
await_channel := fn(): Channel {
|
|
|
|
channel := Channel.(0, 0)
|
2024-11-13 17:08:20 -06:00
|
|
|
loop if channel.server != 0 break else {
|
|
|
|
channel.server = buffer.search(BUFFER_SERVER)
|
2024-11-13 11:03:31 -06:00
|
|
|
}
|
2024-11-13 17:08:20 -06:00
|
|
|
loop if channel.client != 0 break else {
|
|
|
|
channel.client = buffer.search(BUFFER_CLIENT)
|
2024-11-13 11:03:31 -06:00
|
|
|
}
|
2024-11-13 17:08:20 -06:00
|
|
|
return channel
|
2024-11-11 15:48:43 -06:00
|
|
|
}
|
|
|
|
|
2024-11-13 11:03:31 -06:00
|
|
|
await_message := fn($Expr: type, buffer_id: uint): Message(Expr) {
|
|
|
|
response := @as(?Message(Expr), null)
|
2024-11-11 15:48:43 -06:00
|
|
|
loop {
|
2024-11-27 10:53:17 -06:00
|
|
|
// awaiting here causes flickering... idk why
|
|
|
|
buffer.await(buffer_id)
|
2024-11-13 11:03:31 -06:00
|
|
|
buffer.recv(?Message(Expr), buffer_id, &response)
|
2024-11-11 15:48:43 -06:00
|
|
|
if response != null {
|
2024-11-13 11:03:31 -06:00
|
|
|
return @as(Message(Expr), response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await_header := fn(buffer_id: uint): MessageHeader {
|
|
|
|
response := @as(?MessageHeader, null)
|
|
|
|
loop {
|
2024-11-27 10:53:17 -06:00
|
|
|
// awaiting here causes flickering... idk why
|
|
|
|
buffer.await(buffer_id)
|
2024-11-13 11:03:31 -06:00
|
|
|
buffer.recv(?MessageHeader, buffer_id, &response)
|
|
|
|
if response != null {
|
2024-11-27 10:53:17 -06:00
|
|
|
return @as(MessageHeader, response)
|
2024-11-11 15:48:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-14 15:28:21 -06:00
|
|
|
MessageKind := u8
|
|
|
|
|
2024-11-10 19:03:01 -06:00
|
|
|
MessageHeader := packed struct {
|
2024-11-14 15:28:21 -06:00
|
|
|
kind: MessageKind,
|
2024-11-13 11:03:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Message := fn($Expr: type): type {
|
|
|
|
return packed struct {
|
|
|
|
header: MessageHeader,
|
|
|
|
body: Expr,
|
|
|
|
}
|
2024-11-10 12:57:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
WindowProps := struct {
|
|
|
|
position: Vec2(uint),
|
|
|
|
dimensions: Vec2(uint),
|
2024-11-11 15:48:43 -06:00
|
|
|
// ! replace with owned string type later
|
2024-11-10 12:57:48 -06:00
|
|
|
title: ^u8,
|
|
|
|
}
|
|
|
|
|
2024-11-10 19:03:01 -06:00
|
|
|
WindowData := struct {
|
2024-11-10 12:57:48 -06:00
|
|
|
props: WindowProps,
|
2024-11-13 11:03:31 -06:00
|
|
|
channel: Channel,
|
|
|
|
permissions: uint,
|
2024-11-10 12:57:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Window := struct {
|
2024-11-10 19:03:01 -06:00
|
|
|
data: WindowData,
|
2024-11-10 12:57:48 -06:00
|
|
|
surface: Surface,
|
|
|
|
}
|