mirror of
https://git.robbyzambito.me/zaprus
synced 2025-12-20 16:24:50 +00:00
Remove allocation for messages
This commit is contained in:
@@ -3,6 +3,8 @@ const base64Dec = std.base64.Base64Decoder.init(std.base64.standard_alphabet_cha
|
||||
|
||||
var rand: ?Random = null;
|
||||
|
||||
const max_message_size = 2048;
|
||||
|
||||
pub fn init() !void {
|
||||
var prng = Random.DefaultPrng.init(blk: {
|
||||
var seed: u64 = undefined;
|
||||
@@ -47,16 +49,12 @@ fn broadcastSaprusMessage(msg: *SaprusMessage, udp_port: u16) !void {
|
||||
_ = try sock.sendTo(dest_addr, msg_bytes);
|
||||
}
|
||||
|
||||
pub fn sendRelay(payload: []const u8, dest: [4]u8, allocator: Allocator) !void {
|
||||
const msg_bytes = try allocator.alignedAlloc(
|
||||
u8,
|
||||
@alignOf(SaprusMessage),
|
||||
try SaprusMessage.lengthForPayloadLength(
|
||||
pub fn sendRelay(payload: []const u8, dest: [4]u8) !void {
|
||||
var buf: [max_message_size]u8 align(@alignOf(SaprusMessage)) = undefined;
|
||||
const msg_bytes = buf[0..try SaprusMessage.calcSize(
|
||||
.relay,
|
||||
base64Enc.calcSize(payload.len),
|
||||
),
|
||||
);
|
||||
defer allocator.free(msg_bytes);
|
||||
)];
|
||||
const msg: *SaprusMessage = .init(.relay, msg_bytes);
|
||||
|
||||
const relay = (try msg.getSaprusTypePayload()).relay;
|
||||
@@ -75,18 +73,14 @@ fn randomPort() u16 {
|
||||
return p;
|
||||
}
|
||||
|
||||
pub fn sendInitialConnection(payload: []const u8, initial_port: u16, allocator: Allocator) !*SaprusMessage {
|
||||
pub fn sendInitialConnection(payload: []const u8, initial_port: u16) !*SaprusMessage {
|
||||
const dest_port = randomPort();
|
||||
const msg_bytes = try allocator.alignedAlloc(
|
||||
u8,
|
||||
@alignOf(SaprusMessage),
|
||||
try SaprusMessage.lengthForPayloadLength(
|
||||
var buf: [max_message_size]u8 align(@alignOf(SaprusMessage)) = undefined;
|
||||
const msg_bytes = buf[0..try SaprusMessage.calcSize(
|
||||
.connection,
|
||||
base64Enc.calcSize(payload.len),
|
||||
),
|
||||
);
|
||||
|
||||
const msg: *SaprusMessage = .init(.connection, msg_bytes);
|
||||
)];
|
||||
const msg: *SaprusMessage = .init(.relay, msg_bytes);
|
||||
|
||||
const connection = (try msg.getSaprusTypePayload()).connection;
|
||||
connection.src_port = initial_port;
|
||||
@@ -98,11 +92,8 @@ pub fn sendInitialConnection(payload: []const u8, initial_port: u16, allocator:
|
||||
return msg;
|
||||
}
|
||||
|
||||
pub fn connect(payload: []const u8, allocator: Allocator) !?SaprusConnection {
|
||||
var initial_port: u16 = 0;
|
||||
if (rand) |r| {
|
||||
initial_port = r.intRangeAtMost(u16, 1024, 65000);
|
||||
} else unreachable;
|
||||
pub fn connect(payload: []const u8) !?SaprusConnection {
|
||||
const initial_port = randomPort();
|
||||
|
||||
var initial_conn_res: ?*SaprusMessage = null;
|
||||
|
||||
@@ -119,10 +110,9 @@ pub fn connect(payload: []const u8, allocator: Allocator) !?SaprusConnection {
|
||||
try sock.setReadTimeout(1 * std.time.us_per_s);
|
||||
try sock.bind(bind_addr);
|
||||
|
||||
const msg = try sendInitialConnection(payload, initial_port, allocator);
|
||||
defer allocator.free(msg.asBytes());
|
||||
const msg = try sendInitialConnection(payload, initial_port);
|
||||
|
||||
var response_buf: [4096]u8 align(@alignOf(SaprusMessage)) = undefined;
|
||||
var response_buf: [max_message_size]u8 align(@alignOf(SaprusMessage)) = undefined;
|
||||
_ = try sock.receive(&response_buf); // Ignore message that I sent.
|
||||
const len = try sock.receive(&response_buf);
|
||||
|
||||
@@ -148,5 +138,3 @@ const mem = std.mem;
|
||||
|
||||
const network = @import("network");
|
||||
const gcat = @import("gatorcat");
|
||||
|
||||
const Allocator = mem.Allocator;
|
||||
|
||||
@@ -54,12 +54,11 @@ pub fn main() !void {
|
||||
try SaprusClient.sendRelay(
|
||||
if (r.len > 0) r else "Hello darkness my old friend",
|
||||
dest,
|
||||
gpa,
|
||||
);
|
||||
// std.debug.print("Sent: {s}\n", .{r});
|
||||
return;
|
||||
} else if (res.args.connect) |c| {
|
||||
_ = SaprusClient.connect(if (c.len > 0) c else "Hello darkness my old friend", gpa) catch |err| switch (err) {
|
||||
_ = SaprusClient.connect(if (c.len > 0) c else "Hello darkness my old friend") catch |err| switch (err) {
|
||||
error.WouldBlock => null,
|
||||
else => return err,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user