#std.Io.net.Stream.writer Help

1 messages · Page 1 of 1 (latest)

clever compass
#

I've been working on some socket stuff recently, and can't figure out how to send messages over the writer. I've connected to it and just haven't been able to figure this out, i've not been working with Zig for long, so hoping to get some help
This is my super sophisticated code.

const std = @import("std");
const unix = std.Io.net.UnixAddress;

pub fn main() !void {
    const alloc = std.heap.page_allocator;
    var arena = std.heap.ArenaAllocator.init(alloc);
    const allocator = arena.allocator();

    var threaded_io: std.Io.Threaded = .init(allocator, .{});
    const iio: std.Io = threaded_io.io();

    const addr_value: unix = .{ .path = "/tmp/temp.socket" };
    const addr: *const unix = &addr_value;
    const stream = try std.Io.net.UnixAddress.connect(addr, iio);
    var writeBuffer: [1024]u8 = undefined;
    const writer = stream.writer(iio, &writeBuffer);
    _ = writer;
}
hushed dune
#
try writer.interface.writeAll(my_data);
// when you are done call this
try writer.interface.flush();

writers and readers are buffered, meaning they only send data when the buffer is full.
flush will force any remaining data in the buffer to be sent even when it is not full.

clever compass
#

OOOH

#

I tried that, I just didn't flush it

#

I didn't know that

hushed dune
#

another solution is to use a 0 sized buffer, then you dont need to flush since it is always "full"
you can use &.{} as a shorthand for a 0 sized buffer, "" also works, but I dont like it

clever compass
#

Yeah, that feels a little weird tbh

#

Since I have you. can I ask what the difference in these 2 are. I'm so confused why I get this error so often

main.zig:19:29: error: expected type '*Io.Writer', found '*const Io.Writer'

what's the main way to solve that?

hushed dune
#

if you chain readers/writers it will be normal as you dont want all of them buffered, only key points (usually the ends onf the chain)

clever compass
#

Ooooh. I didn't realize I did that

#

Omg, thank you so much

#

It's working now

hushed dune
#

a very important thing to be aware of, the reader/writer interfaces must be used through pointers to the field of the implementation type, so whenever passint the interface to functions/types/variables it must be a pointer.
writer.interface.foo() will automagically pass a pointer if foo requires one

clever compass
#

I see. thanks so much