mirror of
https://git.robbyzambito.me/zaprus
synced 2025-12-20 16:24:50 +00:00
move aux funcs back into the union
also move the body of the base64 handling back to the only place it is used now
This commit is contained in:
88
src/main.zig
88
src/main.zig
@@ -57,6 +57,23 @@ const SaprusMessage = union(SaprusPacketType) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline fn toBytesAux(
|
||||||
|
Header: type,
|
||||||
|
header: Header,
|
||||||
|
payload: []const u8,
|
||||||
|
w: std.ArrayList(u8).Writer,
|
||||||
|
allocator: Allocator,
|
||||||
|
) !void {
|
||||||
|
var payload_list = std.ArrayList(u8).init(allocator);
|
||||||
|
defer payload_list.deinit();
|
||||||
|
const buf_w = payload_list.writer();
|
||||||
|
try base64Enc.encodeWriter(buf_w, payload);
|
||||||
|
|
||||||
|
try w.writeStructEndian(header, .big);
|
||||||
|
try w.writeInt(u16, @intCast(payload_list.items.len), .big);
|
||||||
|
try w.writeAll(payload_list.items);
|
||||||
|
}
|
||||||
|
|
||||||
fn toBytes(self: SaprusMessage, allocator: Allocator) ![]u8 {
|
fn toBytes(self: SaprusMessage, allocator: Allocator) ![]u8 {
|
||||||
var buf = std.ArrayList(u8).init(allocator);
|
var buf = std.ArrayList(u8).init(allocator);
|
||||||
const w = buf.writer();
|
const w = buf.writer();
|
||||||
@@ -71,6 +88,28 @@ const SaprusMessage = union(SaprusPacketType) {
|
|||||||
return buf.toOwnedSlice();
|
return buf.toOwnedSlice();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline fn fromBytesAux(
|
||||||
|
packet: SaprusPacketType,
|
||||||
|
Header: type,
|
||||||
|
r: std.io.FixedBufferStream([]const u8).Reader,
|
||||||
|
allocator: Allocator,
|
||||||
|
) !SaprusMessage {
|
||||||
|
const header = try r.readStructEndian(Header, .big);
|
||||||
|
const len = try r.readInt(u16, .big);
|
||||||
|
|
||||||
|
var payload_buf = std.ArrayList(u8).init(allocator);
|
||||||
|
defer payload_buf.deinit();
|
||||||
|
try r.readAllArrayList(&payload_buf, len);
|
||||||
|
|
||||||
|
const payload = try allocator.alloc(u8, try base64Dec.calcSizeForSlice(payload_buf.items));
|
||||||
|
try base64Dec.decode(payload, payload_buf.items);
|
||||||
|
|
||||||
|
return @unionInit(SaprusMessage, @tagName(packet), .{
|
||||||
|
.header = header,
|
||||||
|
.payload = payload,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
fn fromBytes(bytes: []const u8, allocator: Allocator) !SaprusMessage {
|
fn fromBytes(bytes: []const u8, allocator: Allocator) !SaprusMessage {
|
||||||
var s = std.io.fixedBufferStream(bytes);
|
var s = std.io.fixedBufferStream(bytes);
|
||||||
const r = s.reader();
|
const r = s.reader();
|
||||||
@@ -84,37 +123,6 @@ const SaprusMessage = union(SaprusPacketType) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
inline fn toBytesAux(
|
|
||||||
Header: type,
|
|
||||||
header: Header,
|
|
||||||
payload: []const u8,
|
|
||||||
w: std.ArrayList(u8).Writer,
|
|
||||||
allocator: Allocator,
|
|
||||||
) !void {
|
|
||||||
const payload_list = try encodeToList(payload, allocator);
|
|
||||||
defer payload_list.deinit();
|
|
||||||
|
|
||||||
try w.writeStructEndian(header, .big);
|
|
||||||
try w.writeInt(u16, @intCast(payload_list.items.len), .big);
|
|
||||||
try w.writeAll(payload_list.items);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline fn fromBytesAux(
|
|
||||||
packet: SaprusPacketType,
|
|
||||||
Header: type,
|
|
||||||
r: StringReader,
|
|
||||||
allocator: Allocator,
|
|
||||||
) !SaprusMessage {
|
|
||||||
const header = try r.readStructEndian(Header, .big);
|
|
||||||
const len = try r.readInt(u16, .big);
|
|
||||||
const payload = try decodeFromReader(r, len, allocator);
|
|
||||||
|
|
||||||
return @unionInit(SaprusMessage, @tagName(packet), .{
|
|
||||||
.header = header,
|
|
||||||
.payload = payload,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
var dba: ?DebugAllocator = if (comptime is_debug) DebugAllocator.init else null;
|
var dba: ?DebugAllocator = if (comptime is_debug) DebugAllocator.init else null;
|
||||||
defer if (dba) |*d| {
|
defer if (dba) |*d| {
|
||||||
@@ -157,28 +165,10 @@ pub fn main() !void {
|
|||||||
_ = try sock.sendTo(dest_addr, msg_bytes);
|
_ = try sock.sendTo(dest_addr, msg_bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn encodeToList(buf: []const u8, allocator: Allocator) !std.ArrayList(u8) {
|
|
||||||
var b64_buf = std.ArrayList(u8).init(allocator);
|
|
||||||
const buf_w = b64_buf.writer();
|
|
||||||
try base64Enc.encodeWriter(buf_w, buf);
|
|
||||||
return b64_buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn decodeFromReader(reader: StringReader, len: u16, allocator: Allocator) ![]const u8 {
|
|
||||||
var b64_buf = std.ArrayList(u8).init(allocator);
|
|
||||||
defer b64_buf.deinit();
|
|
||||||
try reader.readAllArrayList(&b64_buf, len);
|
|
||||||
|
|
||||||
const payload = try allocator.alloc(u8, try base64Dec.calcSizeForSlice(b64_buf.items));
|
|
||||||
try base64Dec.decode(payload, b64_buf.items);
|
|
||||||
return payload;
|
|
||||||
}
|
|
||||||
|
|
||||||
const builtin = @import("builtin");
|
const builtin = @import("builtin");
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const Allocator = std.mem.Allocator;
|
const Allocator = std.mem.Allocator;
|
||||||
const DebugAllocator = std.heap.DebugAllocator(.{});
|
const DebugAllocator = std.heap.DebugAllocator(.{});
|
||||||
const StringReader = std.io.FixedBufferStream([]const u8).Reader;
|
|
||||||
|
|
||||||
const network = @import("network");
|
const network = @import("network");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user