summaryrefslogtreecommitdiff
path: root/src/Server/parse/Payload.zig
blob: b512a81f65b8b29176243b044f506f9db48299fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const std = @import("std");
const Reader = std.Io.Reader;
const Writer = std.Io.Writer;
const Allocator = std.mem.Allocator;

const Payload = @This();

len: u32,
short: [128]u8,
long: ?[]u8,

pub fn read(alloc: Allocator, in: *Reader, bytes: usize) !Payload {
    var res: Payload = .{
        .len = @intCast(bytes),
        .short = undefined,
        .long = null,
    };

    try in.readSliceAll(res.short[0..@min(bytes, res.short.len)]);
    if (bytes > res.short.len) {
        const long = try alloc.alloc(u8, bytes - res.short.len);
        errdefer alloc.free(long);
        try in.readSliceAll(long);
        res.long = long;
    }
    return res;
}

pub fn write(self: Payload, out: *Writer) !void {
    std.debug.assert(out.buffer.len >= self.short.len);
    std.debug.assert(self.len <= self.short.len or self.long != null);
    try out.writeAll(self.short[0..@min(self.len, self.short.len)]);
    if (self.long) |l| {
        try out.writeAll(l);
    }
}

pub fn deinit(self: Payload, alloc: Allocator) void {
    if (self.long) |l| {
        alloc.free(l);
    }
}

pub fn dupe(self: Payload, alloc: Allocator) !Payload {
    var res = self;
    if (self.long) |l| {
        res.long = try alloc.dupe(u8, l);
    }
    errdefer if (res.long) |l| alloc.free(l);
    return res;
}