aboutsummaryrefslogtreecommitdiff
path: root/src/message.zig
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2026-02-01 14:29:53 -0500
committerRobby Zambito <contact@robbyzambito.me>2026-02-01 19:16:22 -0500
commit558f40213b895810a78b2bbcbdbb95e88a301fde (patch)
tree6f164c0d109e7cfeff7db6a0a89f2baa2c8091f3 /src/message.zig
parentdaf18d35526870e39f3009b6bf9a64d0b4859b9f (diff)
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.
Diffstat (limited to 'src/message.zig')
-rw-r--r--src/message.zig32
1 files changed, 27 insertions, 5 deletions
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());
}