mirror of
https://git.robbyzambito.me/zaprus
synced 2025-12-21 00:34:50 +00:00
Initial testing of connection message
This commit is contained in:
@@ -6,14 +6,7 @@ pub fn deinit() void {
|
|||||||
network.deinit();
|
network.deinit();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sendRelay(payload: []const u8, allocator: std.mem.Allocator) !void {
|
inline fn broadcastSaprusMessage(msg: SaprusMessage, allocator: Allocator) !void {
|
||||||
const msg = SaprusMessage{
|
|
||||||
.relay = .{
|
|
||||||
.header = .{ .dest = .{ 255, 255, 255, 255 } },
|
|
||||||
.payload = payload,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const msg_bytes = try msg.toBytes(allocator);
|
const msg_bytes = try msg.toBytes(allocator);
|
||||||
defer allocator.free(msg_bytes);
|
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);
|
_ = 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 SaprusMessage = @import("./saprus_message.zig").SaprusMessage;
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const network = @import("network");
|
const network = @import("network");
|
||||||
|
|
||||||
|
const Allocator = std.mem.Allocator;
|
||||||
|
|||||||
@@ -79,8 +79,14 @@ pub const SaprusMessage = union(SaprusPacketType) {
|
|||||||
try base64Enc.encodeWriter(buf_w, payload);
|
try base64Enc.encodeWriter(buf_w, payload);
|
||||||
|
|
||||||
// Write the packet body to the output writer.
|
// 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.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);
|
try w.writeAll(payload_list.items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,7 +30,9 @@ pub fn main() !void {
|
|||||||
try Saprus.init();
|
try Saprus.init();
|
||||||
defer Saprus.deinit();
|
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");
|
const builtin = @import("builtin");
|
||||||
|
|||||||
Reference in New Issue
Block a user