From 558f40213b895810a78b2bbcbdbb95e88a301fde Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Sun, 1 Feb 2026 14:29:53 -0500 Subject: Update to Saprus 0.2.1 Handle management messages instead of letting them bubble up through the connection to the consumer. Right now, this just means handling ping messages by sending a pong. Also updated to follow the new handshake flow. The sentinel will mirror the ports instead of matching them. Now filters on the full source and dest ports, which are less likely to have erroneous matches. --- src/message.zig | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'src/message.zig') diff --git a/src/message.zig b/src/message.zig index e8ef268..0c1410d 100644 --- a/src/message.zig +++ b/src/message.zig @@ -169,11 +169,11 @@ const Connection = struct { seq: u32, id: u32, reserved: u8 = undefined, - options: Options = undefined, + options: Options = .{}, payload: []const u8, - /// Reserved option values. - /// Currently unused. + /// Option values. + /// Currently used! pub const Options = packed struct(u8) { opt1: bool = false, opt2: bool = false, @@ -182,7 +182,7 @@ const Connection = struct { opt5: bool = false, opt6: bool = false, opt7: bool = false, - opt8: bool = false, + management: bool = false, }; /// Asserts that buf is large enough to fit the connection message. @@ -199,6 +199,28 @@ const Connection = struct { out.writeAll(self.payload) catch unreachable; return out.buffered(); } + + /// If the current message is a management message, return what kind. + /// Else return null. + pub fn management(self: Connection) MessageParseError!?Management { + const b64_dec = std.base64.standard.Decoder; + if (self.options.management) { + var buf: [1]u8 = undefined; + _ = b64_dec.decode(&buf, self.payload) catch return error.InvalidMessage; + + return switch (buf[0]) { + 'P' => .ping, + 'p' => .pong, + else => error.UnknownSaprusType, + }; + } + return null; + } + + pub const Management = enum { + ping, + pong, + }; }; test "Round trip" { @@ -223,5 +245,5 @@ const Writer = std.Io.Writer; const Reader = std.Io.Reader; test { - std.testing.refAllDeclsRecursive(@This()); + std.testing.refAllDecls(@This()); } -- cgit