aboutsummaryrefslogtreecommitdiff
path: root/src/startup_message.zig
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2023-09-26 06:51:06 +0100
committerMartin Ashby <martin@ashbysoft.com>2023-09-26 06:51:06 +0100
commit183d60a6e87230cc767c56900b94c9c694596de1 (patch)
treec08b473a293dc465989a09c5d681191898cf2c2f /src/startup_message.zig
parent02f9e99bfccad8837d327880f756ec7bab711783 (diff)
downloadpgz-183d60a6e87230cc767c56900b94c9c694596de1.tar.gz
pgz-183d60a6e87230cc767c56900b94c9c694596de1.tar.bz2
pgz-183d60a6e87230cc767c56900b94c9c694596de1.tar.xz
pgz-183d60a6e87230cc767c56900b94c9c694596de1.zip
Move protocol definitions into a subfolder
Diffstat (limited to 'src/startup_message.zig')
-rw-r--r--src/startup_message.zig85
1 files changed, 0 insertions, 85 deletions
diff --git a/src/startup_message.zig b/src/startup_message.zig
deleted file mode 100644
index b5031c1..0000000
--- a/src/startup_message.zig
+++ /dev/null
@@ -1,85 +0,0 @@
-const std = @import("std");
-const ProtocolError = @import("main.zig").ProtocolError;
-const SSHashMap = std.StringHashMap([]const u8);
-const ByteArrayList = std.ArrayList(u8);
-
-const StartupMessage = @This();
-
-const ProtocolVersionNumber: u32 = 196608; // 3.0
-
-bytes: ?[]const u8 = null, // Owned
-parameters: SSHashMap,
-
-// message length should already have been read, b should contain the payload
-pub fn read(allocator: std.mem.Allocator, b: []const u8) !StartupMessage {
- if (b.len < 4) return ProtocolError.InvalidMessageLength;
-
- var bytes = try allocator.dupe(u8, b);
- errdefer allocator.free(bytes);
- const protocol_version = std.mem.readIntSliceBig(u32, bytes[0..4]);
- if (protocol_version != ProtocolVersionNumber) return ProtocolError.InvalidProtocolVersion;
-
- var parameters = SSHashMap.init(allocator);
- var it = std.mem.splitScalar(u8, bytes[4..], 0);
- while (it.next()) |next| {
- const key = next;
- const value = it.next() orelse return ProtocolError.InvalidKeyValuePair;
- try parameters.put(key, value);
- }
-
- return .{
- .bytes = bytes,
- .parameters = parameters,
- };
-}
-
-pub fn write(self: StartupMessage, allocator: std.mem.Allocator, stream_writer: anytype) !void {
- var al = ByteArrayList.init(allocator);
- defer al.deinit();
- var cw = std.io.countingWriter(al.writer());
- var writer = cw.writer();
- try writer.writeIntBig(u32, 0); // length placeholder
- try writer.writeIntBig(u32, ProtocolVersionNumber);
- var it = self.parameters.iterator();
- while (it.next()) |entry| {
- try writer.writeAll(entry.key_ptr.*);
- try writer.writeByte(0);
- try writer.writeAll(entry.value_ptr.*);
- try writer.writeByte(0);
- }
- try writer.writeByte(0);
- std.mem.writeIntBig(u32, al.items[0..4], @as(u32, @intCast(cw.bytes_written)));
- try stream_writer.writeAll(al.items);
-}
-
-pub fn deinit(self: *StartupMessage, allocator: std.mem.Allocator) void {
- self.parameters.deinit();
- if (self.bytes != null) {
- allocator.free(self.bytes.?);
- }
-}
-
-test "round trip" {
- const allocator = std.testing.allocator;
- var params = SSHashMap.init(allocator);
- try params.put("hello", "postgres");
- var sm = StartupMessage{
- .parameters = params,
- };
- defer sm.deinit(allocator);
-
- var bal = ByteArrayList.init(allocator);
- defer bal.deinit();
- try sm.write(allocator, bal.writer());
-
- var fbs = std.io.fixedBufferStream(bal.items);
- var reader = fbs.reader();
- const len = try reader.readIntBig(u32);
- const buf = try allocator.alloc(u8, len - 4);
- defer allocator.free(buf);
- try reader.readNoEof(buf);
- var sm2 = try StartupMessage.read(allocator, buf);
- defer sm2.deinit(allocator);
-
- try std.testing.expectEqualStrings("postgres", sm2.parameters.get("hello").?);
-}