aboutsummaryrefslogtreecommitdiff
path: root/src/Connection.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/Connection.zig')
-rw-r--r--src/Connection.zig49
1 files changed, 37 insertions, 12 deletions
diff --git a/src/Connection.zig b/src/Connection.zig
index 90109af..bb81c38 100644
--- a/src/Connection.zig
+++ b/src/Connection.zig
@@ -28,25 +28,50 @@ pub fn init(socket: RawSocket, headers: EthIpUdp, connection: SaprusMessage) Con
};
}
-pub fn next(self: Connection, io: Io, buf: []u8) ![]const u8 {
- _ = io;
- log.debug("Awaiting connection message", .{});
- const res = try self.socket.receive(buf);
- log.debug("Received {} byte connection message", .{res.len});
- const msg: SaprusMessage = try .parse(res[42..]);
- const connection_res = msg.connection;
-
- log.debug("Payload was {s}", .{connection_res.payload});
-
- return connection_res.payload;
+// 'p' as base64
+const pong = "cA==";
+
+pub fn next(self: *Connection, io: Io, buf: []u8) ![]const u8 {
+ while (true) {
+ log.debug("Awaiting connection message", .{});
+ const res = try self.socket.receive(buf);
+ log.debug("Received {} byte connection message", .{res.len});
+ const msg = SaprusMessage.parse(res[42..]) catch |err| {
+ log.err("Failed to parse next message: {t}\n{x}\n{x}", .{ err, res[0..], res[42..] });
+ return err;
+ };
+
+ switch (msg) {
+ .connection => |con_res| {
+ if (try con_res.management()) |mgt| {
+ log.debug("Received management message {t}", .{mgt});
+ switch (mgt) {
+ .ping => {
+ log.debug("Sending pong", .{});
+ try self.send(io, .{ .management = true }, pong);
+ log.debug("Sent pong message", .{});
+ },
+ else => |m| log.debug("Received management message that I don't know how to handle: {t}", .{m}),
+ }
+ } else {
+ log.debug("Payload was {s}", .{con_res.payload});
+ return con_res.payload;
+ }
+ },
+ else => |m| {
+ std.debug.panic("Expected connection message, instead got {x}. This means there is an error with the BPF.", .{@intFromEnum(m)});
+ },
+ }
+ }
}
-pub fn send(self: *Connection, io: Io, buf: []const u8) !void {
+pub fn send(self: *Connection, io: Io, options: SaprusMessage.Connection.Options, buf: []const u8) !void {
const io_source: std.Random.IoSource = .{ .io = io };
const rand = io_source.interface();
log.debug("Sending connection message", .{});
+ self.connection.connection.options = options;
self.connection.connection.payload = buf;
var connection_bytes_buf: [2048]u8 = undefined;
const connection_bytes = self.connection.toBytes(&connection_bytes_buf);