Initial testing of connection message

This commit is contained in:
2025-04-04 23:12:08 -04:00
parent 7b07520adb
commit efcd866d6c
3 changed files with 45 additions and 10 deletions

View File

@@ -6,14 +6,7 @@ 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,
},
};
inline fn broadcastSaprusMessage(msg: SaprusMessage, allocator: Allocator) !void {
const msg_bytes = try msg.toBytes(allocator);
defer allocator.free(msg_bytes);
@@ -38,6 +31,40 @@ pub fn sendRelay(payload: []const u8, allocator: std.mem.Allocator) !void {
_ = try sock.sendTo(dest_addr, msg_bytes);
}
pub fn sendRelay(payload: []const u8, allocator: Allocator) !void {
const msg = SaprusMessage{
.relay = .{
.header = .{ .dest = .{ 255, 255, 255, 255 } },
.payload = payload,
},
};
try broadcastSaprusMessage(msg, allocator);
}
pub fn sendInitialConnection(payload: []const u8, initial_port: u16, allocator: Allocator) !void {
const msg = SaprusMessage{
.connection = .{
.header = .{
.src_port = initial_port,
.dest_port = 6868,
.seq_num = 1,
.msg_id = 2,
.reserved = 5,
.options = .{
.opt2 = true,
.opt8 = true,
},
},
.payload = payload,
},
};
try broadcastSaprusMessage(msg, allocator);
}
const SaprusMessage = @import("./saprus_message.zig").SaprusMessage;
const std = @import("std");
const network = @import("network");
const Allocator = std.mem.Allocator;

View File

@@ -79,8 +79,14 @@ pub const SaprusMessage = union(SaprusPacketType) {
try base64Enc.encodeWriter(buf_w, payload);
// Write the packet body to the output writer.
try w.writeStructEndian(header, .big);
try w.writeInt(u16, @intCast(payload_list.items.len), .big);
// try w.writeStructEndian(header, .big);
const header_bytes = std.mem.asBytes(&header);
try w.writeAll(header_bytes[0 .. header_bytes.len - 2]);
try w.writeAll(payload_list.items);
}

View File

@@ -30,7 +30,9 @@ pub fn main() !void {
try Saprus.init();
defer Saprus.deinit();
try Saprus.sendRelay(if (message.items.len > 0) message.items else "Hello darkness my old friend", gpa);
try Saprus.sendInitialConnection(if (message.items.len > 0) message.items else "Hello darkness my old friend", 6868, gpa);
// try Saprus.sendRelay(if (message.items.len > 0) message.items else "Hello darkness my old friend", gpa);
}
const builtin = @import("builtin");