IpcReader
Socket reader that is expected to be reading socket messages
that are json messages. Growth is linearly based on the provided growth_rate.
Will only allocate more memory if required.
Calling deinit will close the socket and clear the buffer.
Properties
struct {
/// The underlaying allocator used to manage the buffer.
allocator: Allocator
/// Buffer that contains all messages. Grows based on `growth_rate`.
buffer: []u8
/// The growth rate of the message buffer.
growth_rate: usize
/// The end of a json message.
message_end: usize
/// The start of the json message.
message_start: usize
/// The current position in the buffer.
position: usize
/// The stream used to read or write.
stream: Stream
/// If the stream is closed for reading.
closed: bool
}ReadError
Set of possible errors when reading from the socket.
Stream.ReadError || Allocator.Error || error{Closed}WriteError
Set of possible error when writting to the stream.
Stream.WriteErrorInit
Sets the initial reader state in order to perform any necessary actions.
Exampleconst stream = std.net.connectUnixSocket("tmp/tmp.socket");
const ipc_reader = try IpcReader.init(stream, null);
defer ipc_reader.deinit();Signature
pub fn init(allocator: Allocator, stream: Stream, growth_rate: ?usize) Allocator.Error!SelfDeinit
Frees the buffer and closes the stream.
Signature
pub fn deinit(self: *Self) voidRead
Reads the bytes directly from the socket. Will allocate more memory as needed.
Signature
pub fn read(self: *Self) Self.ReadError!voidGrow
Grows the reader buffer based on the growth rate. Will use the allocator resize
method if available.
Signature
pub fn grow(self: *Self, size: usize) Allocator.Error!voidJsonMessage
"Reads" a json message and moves the necessary position members in order to have the necessary message.
Signature
pub fn jsonMessage(self: *@This()) usizeReadMessage
Reads one message from the socket stream. Will only make the socket read request if the buffer is at max capacity. Will grow the buffer as needed.
Signature
pub fn readMessage(self: *Self) Self.ReadError![]u8PrepareForRead
Prepares the reader for the next message.
Signature
pub fn prepareForRead(self: *Self) voidWriteMessage
Writes a message to the socket stream.
Signature
pub fn writeMessage(self: *Self, message: []u8) Stream.WriteError!voidReadError
Set of possible errors when reading from the socket.
Stream.ReadError || Allocator.Error || error{Closed}WriteError
Set of possible error when writting to the stream.
Stream.WriteError