mirror of
https://git.robbyzambito.me/zaprus
synced 2026-02-04 16:34:51 +00:00
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.
This commit is contained in:
@@ -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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user