const std = @import("std"); const json = std.json; const mem = std.mem; const net = std.Io.net; const Info = @import("Info.zig"); const Connect = @import("Connect.zig"); const crlf = "\r\n"; pub const Conn = struct { arena: std.heap.ArenaAllocator, io: std.Io, stream: net.Stream, stream_reader: net.Stream.Reader, stream_writer: net.Stream.Writer, info_buffer: []u8, server_info: Info, read_buffer: []u8, write_buffer: []u8, pub fn connect(gpa: mem.Allocator, io: std.Io, address: []const u8, port: u16, options: Connect) !Conn { var arena: std.heap.ArenaAllocator = .init(gpa); errdefer arena.deinit(); var allocator = arena.allocator(); const addr: net.IpAddress = try .parse(address, port); const stream = try addr.connect(io, .{ .mode = .stream }); const info_buffer: []u8 = try allocator.alloc(u8, 1 << 10); var info_reader = stream.reader(io, info_buffer); var i_reader = &info_reader.interface; const info = try i_reader.takeDelimiter('\r') orelse return error.ReadServerInfo; const server_info = try readInfo(allocator, info); // Take another byte, make sure it's a newline. // This sets us up to keep reading things as they come off the wire. const next_byte = try i_reader.takeByte(); if (next_byte != '\n') return error.MissingNewline; const read_buffer: []u8 = try allocator.alloc(u8, @intCast(server_info.max_payload)); const stream_reader = stream.reader(io, read_buffer); const write_buffer: []u8 = try allocator.alloc(u8, @intCast(server_info.max_payload)); var stream_writer = stream.writer(io, write_buffer); try sendConnect(options, &stream_writer.interface); return Conn{ .arena = arena, .io = io, .stream = stream, .stream_reader = stream_reader, .stream_writer = stream_writer, .info_buffer = info_buffer, .server_info = server_info, .read_buffer = read_buffer, .write_buffer = write_buffer, }; } fn readInfo(arena: std.mem.Allocator, buffer: []const u8) !Info { const info_prefix = "INFO "; if (!mem.startsWith(u8, buffer, info_prefix)) return error.ExpectedInfoPrefix; const info_json = mem.trimStart(u8, buffer, info_prefix); const server_info = try json.parseFromSliceLeaky(Info, arena, info_json, .{ .ignore_unknown_fields = true, }); return server_info; } fn sendConnect(c: Connect, w: *std.Io.Writer) !void { _ = try w.write("CONNECT "); const fmt = json.fmt(c, .{ .emit_null_optional_fields = false, }); try fmt.format(w); _ = try w.write(crlf); try w.flush(); } /// Releases all allocated memory before closing the `stream`. pub fn close(c: *Conn) void { c.arena.deinit(); c.stream.close(c.io); } inline fn reader(c: *Conn) *std.Io.Reader { return &c.stream_reader.interface; } inline fn writer(c: *Conn) *std.Io.Writer { return &c.stream_writer.interface; } pub fn publish(c: *Conn, message: Message) !void { if (message.headers) |headers| return c.publishWithHeaders(message, headers); var w = c.writer(); try w.print("PUB {s} ", .{message.subject}); if (message.reply_to) |reply_to| _ = try w.write(reply_to); if (message.payload) |payload| { try w.print("{d}" ++ crlf, .{payload.len}); try w.writeAll(payload); } else { _ = try w.write(" 0" ++ crlf); } _ = try w.write(crlf); try w.flush(); } fn publishWithHeaders(c: *Conn, message: Message, headers: Message.Headers) !void { _ = c; _ = message; _ = headers; return error.NotImplemented; } pub const Message = struct { subject: []const u8, reply_to: ?[]const u8 = null, payload: ?[]const u8 = null, headers: ?Headers = null, pub const Headers = std.StringHashMap([]const u8); }; };