aboutsummaryrefslogtreecommitdiff
path: root/src/message.zig
diff options
context:
space:
mode:
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());
}