ableos/sysdata/libraries/sunset_proto/src/client.hb

79 lines
1.9 KiB
Plaintext
Raw Normal View History

2024-11-10 12:57:48 -06:00
.{math: .{Vec2}, buffer, log, memory} := @use("../../stn/src/lib.hb");
.{Surface, new_surface} := @use("../../render/src/lib.hb");
2024-11-10 19:03:01 -06:00
.{Window, WindowData, WindowProps, MessageHeader, message, BUFFER} := @use("./lib.hb")
2024-11-10 12:57:48 -06:00
2024-11-10 19:03:01 -06:00
server_id := @as(?uint, null)
2024-11-10 12:57:48 -06:00
2024-11-10 19:03:01 -06:00
init := fn(): void {
log.info("client: waiting for server\0")
id := 0
loop if id != 0 {
server_id = id
log.info("client: done waiting\0")
return
} else {
id = buffer.search(BUFFER)
2024-11-10 12:57:48 -06:00
}
}
2024-11-10 19:03:01 -06:00
// wait_for_message := fn($Expr: type, ptr: ^?Expr, buffer_id: uint): bool {
// timer := 0
// loop if timer < 1000 {
// buffer.recv(?Expr, buffer_id, ptr)
// if *ptr != null {
// return true
// }
// timer -= 1
// }
// return false
// }
new := fn(props: WindowProps): ?Window {
if server_id == null {
init()
2024-11-10 12:57:48 -06:00
}
2024-11-10 19:03:01 -06:00
buffer.write(?MessageHeader, &@as(?MessageHeader, .(message.syn, @sizeof(WindowProps))), @unwrap(server_id))
log.info("client: sent syn\0")
2024-11-10 12:57:48 -06:00
2024-11-10 19:03:01 -06:00
timer := 0
response := memory.uninit(MessageHeader)
// if wait_for_message(MessageHeader, &response, @unwrap(server_id)) == false {
// return null
// }
loop if timer < 1000 {
buffer.recv(?MessageHeader, @unwrap(server_id), &response)
if response != null {
break
}
timer += 1
} else {
log.error("client: no response in timeout\0")
2024-11-10 12:57:48 -06:00
return null
}
2024-11-10 19:03:01 -06:00
if response.kind != message.ack {
log.error("client: refused\0")
return null
2024-11-10 12:57:48 -06:00
}
2024-11-10 19:03:01 -06:00
log.info("client: received ack\0")
2024-11-10 12:57:48 -06:00
2024-11-10 19:03:01 -06:00
buffer_id := response.data
buffer.write(?WindowProps, &@as(?WindowProps, props), buffer_id)
log.info("client: sent props\0")
timer = 0
response2 := memory.uninit(WindowData)
loop if timer < 1000 {
buffer.recv(?WindowData, buffer_id, &response2)
if response2 != null {
log.info("client: received window data\0")
surface := new_surface(
response2.props.dimensions.x,
response2.props.dimensions.y,
)
return .(@as(WindowData, response2), surface)
}
timer += 1
2024-11-10 12:57:48 -06:00
}
2024-11-10 19:03:01 -06:00
return null
2024-11-10 12:57:48 -06:00
}