Move sendRelay logic to Saprus struct

This commit is contained in:
2025-04-04 22:09:06 -04:00
parent 2965f9aaf7
commit 9d38320897
3 changed files with 50 additions and 37 deletions

43
src/saprus.zig Normal file
View File

@@ -0,0 +1,43 @@
pub fn init() !void {
try network.init();
}
pub fn deinit() void {
network.deinit();
}
pub fn sendRelay(payload: []const u8, allocator: std.mem.Allocator) !void {
const msg = SaprusMessage{
.relay = .{
.header = .{ .dest = .{ 255, 255, 255, 255 } },
.payload = payload,
},
};
const msg_bytes = try msg.toBytes(allocator);
defer allocator.free(msg_bytes);
var sock = try network.Socket.create(.ipv4, .udp);
defer sock.close();
try sock.setBroadcast(true);
// Bind to 0.0.0.0:0
const bind_addr = network.EndPoint{
.address = network.Address{ .ipv4 = network.Address.IPv4.any },
.port = 0,
};
const dest_addr = network.EndPoint{
.address = network.Address{ .ipv4 = network.Address.IPv4.broadcast },
.port = 8888,
};
try sock.bind(bind_addr);
_ = try sock.sendTo(dest_addr, msg_bytes);
}
const SaprusMessage = @import("./saprus_message.zig").SaprusMessage;
const std = @import("std");
const network = @import("network");