commit cdd869a
shar
·
2026-06-24 03:27:52 +0000 UTC
parent 98a38c5
wire protocol: create object registry
2 files changed,
+53,
-1
+1,
-1
1@@ -11,7 +11,7 @@ pub fn build(b: *std.Build) void {
2 .optimize = optimize,
3 });
4
5- // --- Protocol scanner (host-native build tool) ---
6+ // --- Protocol scanner ---
7 const scanner_mod = b.createModule(.{
8 .root_source_file = b.path("tools/scanner.zig"),
9 .target = target,
+52,
-0
1@@ -0,0 +1,52 @@
2+const proto = @import("protocol.zig");
3+
4+extern "env" fn log_error(ptr: [*]const u8, len: u32) void;
5+
6+pub const Interface = proto.Interface;
7+
8+pub const Object = struct {
9+ id: u32,
10+ client_id: u32,
11+ iface: Interface,
12+};
13+
14+const MAX = 512;
15+var pool: [MAX]?Object = [_]?Object{null} ** MAX;
16+
17+pub fn register(client_id: u32, id: u32, iface: Interface) ?*Object {
18+ if (id == 0 or id >= MAX) {
19+ const msg = "objects: object ID out of range\n";
20+ log_error(msg, msg.len);
21+ return null;
22+ }
23+ if (pool[id] != null) {
24+ const msg = "objects: duplicate object ID\n";
25+ log_error(msg, msg.len);
26+ return null;
27+ }
28+ pool[id] = Object{ .id = id, .client_id = client_id, .iface = iface };
29+ return &(pool[id] orelse unreachable);
30+}
31+
32+pub fn lookup(client_id: u32, id: u32) ?*Object {
33+ if (id == 0 or id >= MAX) return null;
34+ const obj = pool[id] orelse return null;
35+ if (obj.client_id != client_id) return null;
36+ return &(pool[id] orelse unreachable);
37+}
38+
39+pub fn remove(client_id: u32, id: u32) void {
40+ if (id == 0 or id >= MAX) return;
41+ const obj = pool[id] orelse return;
42+ if (obj.client_id != client_id) return;
43+ pool[id] = null;
44+}
45+
46+pub fn removeAll(client_id: u32) void {
47+ var i: usize = 0;
48+ while (i < MAX) : (i += 1) {
49+ if (pool[i]) |obj| {
50+ if (obj.client_id == client_id) pool[i] = null;
51+ }
52+ }
53+}