summaryrefslogtreecommitdiff
path: root/src/Server/message.zig
blob: 80054533f90fdff48c2464be8b7aa2c22c10f789 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const std = @import("std");
const ArrayList = std.ArrayList;
const Allocator = std.mem.Allocator;
const Reader = std.Io.Reader;

const Payload = @import("Payload.zig");

pub const parse = @import("parse.zig");

pub const Control = @typeInfo(Message).@"union".tag_type.?;

pub const Message = union(enum) {
    INFO: ServerInfo,
    CONNECT: Connect,
    PUB: Pub,
    HPUB: HPub,
    SUB: Sub,
    UNSUB: Unsub,
    MSG: Msg,
    HMSG: HMsg,
    PING,
    PONG,
    @"+OK": void,
    @"-ERR": []const u8,
    pub const ServerInfo = struct {
        /// The unique identifier of the NATS server.
        server_id: []const u8,
        /// The name of the NATS server.
        server_name: []const u8,
        /// The version of NATS.
        version: []const u8,
        /// The version of golang the NATS server was built with.
        go: []const u8 = "0.0.0",
        /// The IP address used to start the NATS server,
        /// by default this will be 0.0.0.0 and can be
        /// configured with -client_advertise host:port.
        host: []const u8 = "0.0.0.0",
        /// The port number the NATS server is configured
        /// to listen on.
        port: u16 = 4222,
        /// Whether the server supports headers.
        headers: bool = false,
        /// Maximum payload size, in bytes, that the server
        /// will accept from the client.
        max_payload: u64,
        /// An integer indicating the protocol version of
        /// the server. The server version 1.2.0 sets this
        /// to 1 to indicate that it supports the "Echo"
        /// feature.
        proto: u32 = 1,
    };
    pub const Connect = struct {
        verbose: bool = false,
        pedantic: bool = false,
        tls_required: bool = false,
        auth_token: ?[]const u8 = null,
        user: ?[]const u8 = null,
        pass: ?[]const u8 = null,
        name: ?[]const u8 = null,
        lang: []const u8,
        version: []const u8,
        protocol: u32,
        echo: ?bool = null,
        sig: ?[]const u8 = null,
        jwt: ?[]const u8 = null,
        no_responders: ?bool = null,
        headers: ?bool = null,
        nkey: ?[]const u8 = null,

        pub fn deinit(self: Connect, alloc: Allocator) void {
            if (self.auth_token) |a| alloc.free(a);
            if (self.user) |u| alloc.free(u);
            if (self.pass) |p| alloc.free(p);
            if (self.name) |n| alloc.free(n);
            alloc.free(self.lang);
            alloc.free(self.version);
            if (self.sig) |s| alloc.free(s);
            if (self.jwt) |j| alloc.free(j);
            if (self.nkey) |n| alloc.free(n);
        }

        pub fn dupe(self: Connect, alloc: Allocator) !Connect {
            var res = self;
            res.auth_token = if (self.auth_token) |a| try alloc.dupe(u8, a) else null;
            errdefer if (res.auth_token) |a| alloc.free(a);
            res.user = if (self.user) |u| try alloc.dupe(u8, u) else null;
            errdefer if (res.user) |u| alloc.free(u);
            res.pass = if (self.pass) |p| try alloc.dupe(u8, p) else null;
            errdefer if (res.pass) |p| alloc.free(p);
            res.name = if (self.name) |n| try alloc.dupe(u8, n) else null;
            errdefer if (res.name) |n| alloc.free(n);
            res.lang = try alloc.dupe(u8, self.lang);
            errdefer alloc.free(res.lang);
            res.version = try alloc.dupe(u8, self.version);
            errdefer alloc.free(res.version);
            res.sig = if (self.sig) |s| try alloc.dupe(u8, s) else null;
            errdefer if (res.sig) |s| alloc.free(s);
            res.jwt = if (self.jwt) |j| try alloc.dupe(u8, j) else null;
            errdefer if (res.jwt) |j| alloc.free(j);
            res.nkey = if (self.nkey) |n| try alloc.dupe(u8, n) else null;
            errdefer if (res.nkey) |n| alloc.free(n);
            return res;
        }
    };
    pub const Pub = struct {
        /// The destination subject to publish to.
        subject: []const u8,
        /// The reply subject that subscribers can use to send a response back to the publisher/requestor.
        reply_to: ?[]const u8 = null,
        /// The message payload data.
        payload: []const u8,

        pub fn toMsg(self: Pub, sid: []const u8) Msg {
            return .{
                .subject = self.subject,
                .sid = sid,
                .reply_to = self.reply_to,
                .payload = self.payload,
            };
        }
    };
    pub const HPub = struct {
        header_bytes: usize,
        @"pub": Pub,

        pub fn deinit(self: HPub, alloc: Allocator) void {
            self.@"pub".deinit(alloc);
        }

        pub fn toHMsg(self: HPub, alloc: Allocator, sid: []const u8) !HMsg {
            return .{
                .header_bytes = self.header_bytes,
                .msg = try self.@"pub".toMsg(alloc, sid),
            };
        }
    };

    pub const HMsg = struct {
        header_bytes: usize,
        msg: Msg,

        pub fn deinit(self: HMsg, alloc: Allocator) void {
            self.msg.deinit(alloc);
        }

        pub fn dupe(self: HMsg, alloc: Allocator) !HMsg {
            var res = self;
            res.msg = try self.msg.dupe(alloc);
            errdefer alloc.free(res.msg);
            return res;
        }
    };
    pub const Sub = struct {
        /// The subject name to subscribe to.
        subject: []const u8,
        /// If specified, the subscriber will join this queue group.
        queue_group: ?[]const u8,
        /// A unique alphanumeric subscription ID, generated by the client.
        sid: []const u8,

        pub fn deinit(self: Sub, alloc: Allocator) void {
            alloc.free(self.subject);
            alloc.free(self.sid);
            if (self.queue_group) |q| alloc.free(q);
        }
    };
    pub const Unsub = struct {
        /// The unique alphanumeric subscription ID of the subject to unsubscribe from.
        sid: []const u8,
        /// A number of messages to wait for before automatically unsubscribing.
        max_msgs: ?usize = null,

        pub fn deinit(self: Unsub, alloc: Allocator) void {
            alloc.free(self.sid);
        }
    };
    pub const Msg = struct {
        subject: []const u8,
        sid: []const u8,
        reply_to: ?[]const u8,
        payload: []const u8,
    };
};