Rename a bunch o things.
This commit is contained in:
parent
fbb68deb7f
commit
2ac78e21e6
|
@ -22,11 +22,11 @@ pub const Client = struct {
|
||||||
pub fn handshake(s: *Client) !void {
|
pub fn handshake(s: *Client) !void {
|
||||||
const magic = 0x42b33f00;
|
const magic = 0x42b33f00;
|
||||||
|
|
||||||
try write.add_int(s.stream.writer(), magic);
|
try write.writeUInt(s.stream.writer(), magic);
|
||||||
try write.add_int(s.stream.writer(), 0x80000002);
|
try write.writeUInt(s.stream.writer(), 0x80000002);
|
||||||
var flags = try read.get_byte(s.stream.reader());
|
var flags = try read.readByte(s.stream.reader());
|
||||||
var extra = try read.get_short(s.stream.reader());
|
var extra = try read.readShort(s.stream.reader());
|
||||||
var version = try read.get_signed_byte(s.stream.reader());
|
var version = try read.readSignedByte(s.stream.reader());
|
||||||
|
|
||||||
std.debug.print("Handshake: flags={d} extra={d} version={d} \n", .{ flags, extra, version });
|
std.debug.print("Handshake: flags={d} extra={d} version={d} \n", .{ flags, extra, version });
|
||||||
}
|
}
|
||||||
|
@ -35,31 +35,26 @@ pub const Client = struct {
|
||||||
|
|
||||||
var map = std.StringHashMap(qvar.QVariant).init(s.allocator);
|
var map = std.StringHashMap(qvar.QVariant).init(s.allocator);
|
||||||
|
|
||||||
try map.put("MsgType", .{ .string = "ClientInit" });
|
try map.put("MsgType", .{ .String = "ClientInit" });
|
||||||
try map.put("ProtocolVersion", .{ .int = 10 });
|
try map.put("ProtocolVersion", .{ .UInt = 10 });
|
||||||
try map.put("ClientVersion", .{ .string = "0.1 (quasselclient)" });
|
try map.put("ClientVersion", .{ .String = "0.1 (quasselclient)" });
|
||||||
try map.put("ClientDate", .{ .string = "Wed, 02 Jun 2021 17:30:30 +0100" });
|
try map.put("ClientDate", .{ .String = "Wed, 02 Jun 2021 17:30:30 +0100" });
|
||||||
try map.put("Features", .{ .int = 0 });
|
try map.put("Features", .{ .Int = 0 });
|
||||||
var empty = std.ArrayList([]const u8).init(s.allocator);
|
var empty = std.ArrayList([]const u8).init(s.allocator);
|
||||||
try map.put("FeatureList", .{ .stringlist = empty });
|
try map.put("FeatureList", .{ .QStringList = empty });
|
||||||
|
|
||||||
try write.add_message(list.writer(), s.allocator, map);
|
try write.writeFrame(list.writer(), s.allocator, map);
|
||||||
try dumpDebug("ClientInit.bin", list);
|
try dumpDebug("ClientInit.bin", list);
|
||||||
try s.stream.writer().writeAll(list.items);
|
try s.stream.writer().writeAll(list.items);
|
||||||
|
|
||||||
std.debug.print("\n\nInitPacket: \n", .{});
|
std.debug.print("\n\nInitPacket: \n", .{});
|
||||||
|
|
||||||
qvar.pretty_print_variant(.{.qmap = map}, 0);
|
qvar.pretty_print_variant(.{.QVariantMap = map}, 0);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var size = read.get_int(s.stream.reader());
|
|
||||||
//std.debug.print("InitResp: size={d} \n", .{size});
|
|
||||||
|
|
||||||
|
var size = read.readUInt(s.stream.reader());
|
||||||
std.debug.print("\n\nInitResponse: \n", .{});
|
std.debug.print("\n\nInitResponse: \n", .{});
|
||||||
var varient = try read.get_variant(s.stream.reader(), s.allocator);
|
var varient = try read.readQVariant(s.stream.reader(), s.allocator);
|
||||||
qvar.pretty_print_variant(varient, 0);
|
qvar.pretty_print_variant(varient, 0);
|
||||||
//std.debug.print("InitResp: type={s} \n", .{varient.qmap.get("MsgType").?.string});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn quassel_login(s: *Client, username: []const u8, password: []const u8) !void {
|
pub fn quassel_login(s: *Client, username: []const u8, password: []const u8) !void {
|
||||||
|
@ -67,11 +62,11 @@ pub const Client = struct {
|
||||||
|
|
||||||
var map = std.StringHashMap(qvar.QVariant).init(s.allocator);
|
var map = std.StringHashMap(qvar.QVariant).init(s.allocator);
|
||||||
|
|
||||||
try map.put("MsgType", .{ .string = "ClientLogin" });
|
try map.put("MsgType", .{ .String = "ClientLogin" });
|
||||||
try map.put("User", .{ .string = username });
|
try map.put("User", .{ .String = username });
|
||||||
try map.put("Password", .{ .string = password });
|
try map.put("Password", .{ .String = password });
|
||||||
|
|
||||||
try write.add_message(list.writer(), s.allocator, map);
|
try write.writeFrame(list.writer(), s.allocator, map);
|
||||||
try dumpDebug("ClientLogin.bin", list);
|
try dumpDebug("ClientLogin.bin", list);
|
||||||
|
|
||||||
try s.stream.writer().writeAll(list.items);
|
try s.stream.writer().writeAll(list.items);
|
||||||
|
|
|
@ -2,58 +2,57 @@ const std = @import("std");
|
||||||
const range = @import("./rangeiter.zig").range;
|
const range = @import("./rangeiter.zig").range;
|
||||||
|
|
||||||
pub const QVariant = union(enum) {
|
pub const QVariant = union(enum) {
|
||||||
int: u32,
|
Int: i32,
|
||||||
short: u16,
|
UInt: u32,
|
||||||
byte: u8,
|
Short: u16,
|
||||||
bytearray: std.ArrayList(u8),
|
Byte: u8,
|
||||||
string: []const u8,
|
String: []const u8,
|
||||||
stringlist: std.ArrayList([]const u8),
|
QByteArray: std.ArrayList(u8),
|
||||||
qmap: std.StringHashMap(QVariant),
|
QStringList: std.ArrayList([]const u8),
|
||||||
qvarlist: std.ArrayList(QVariant),
|
QVariantMap: std.StringHashMap(QVariant),
|
||||||
empty: bool,
|
QVariantList: std.ArrayList(QVariant),
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const QVariantTypes = enum(u32) {
|
pub const QVariantTypes = enum(u32) {
|
||||||
byte = 1,
|
Byte = 1,
|
||||||
// IDK why there is 2???
|
Int = 2,
|
||||||
int_1 = 2,
|
UInt = 3,
|
||||||
int_2 = 3,
|
QVariantMap = 8,
|
||||||
qmap = 8,
|
QVariantList = 9,
|
||||||
qvarlist = 9,
|
String = 10,
|
||||||
string = 10,
|
QStringList = 11,
|
||||||
stringlist = 11,
|
QByteArray = 12,
|
||||||
bytearray = 12,
|
Short = 133,
|
||||||
short = 133,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn qvariant_type_id(variant: QVariant) !u32 {
|
pub fn qvariant_type_id(variant: QVariant) !u32 {
|
||||||
switch (variant) {
|
switch (variant) {
|
||||||
.byte => {
|
.Byte => {
|
||||||
return @enumToInt(QVariantTypes.byte);
|
return @enumToInt(QVariantTypes.Byte);
|
||||||
},
|
},
|
||||||
.int => {
|
.Int => {
|
||||||
return @enumToInt(QVariantTypes.int_2);
|
return @enumToInt(QVariantTypes.Int);
|
||||||
},
|
},
|
||||||
.qmap => {
|
.UInt => {
|
||||||
return @enumToInt(QVariantTypes.qmap);
|
return @enumToInt(QVariantTypes.UInt);
|
||||||
},
|
},
|
||||||
.qvarlist => {
|
.QVariantMap => {
|
||||||
return @enumToInt(QVariantTypes.qvarlist);
|
return @enumToInt(QVariantTypes.QVariantMap);
|
||||||
},
|
},
|
||||||
.string => {
|
.QVariantList => {
|
||||||
return @enumToInt(QVariantTypes.string);
|
return @enumToInt(QVariantTypes.QVariantList);
|
||||||
},
|
},
|
||||||
.stringlist => {
|
.String => {
|
||||||
return @enumToInt(QVariantTypes.stringlist);
|
return @enumToInt(QVariantTypes.String);
|
||||||
},
|
},
|
||||||
.bytearray => {
|
.QStringList => {
|
||||||
return @enumToInt(QVariantTypes.bytearray);
|
return @enumToInt(QVariantTypes.QStringList);
|
||||||
},
|
},
|
||||||
.short => {
|
.QByteArray => {
|
||||||
return @enumToInt(QVariantTypes.short);
|
return @enumToInt(QVariantTypes.QByteArray);
|
||||||
},
|
},
|
||||||
else => {
|
.Short => {
|
||||||
return 0;
|
return @enumToInt(QVariantTypes.Short);
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -69,14 +68,17 @@ pub fn pretty_print_variant(variant: QVariant, indentLevel: u64) void {
|
||||||
print_indent_level(indentLevel);
|
print_indent_level(indentLevel);
|
||||||
|
|
||||||
switch (variant) {
|
switch (variant) {
|
||||||
.byte => |b| {
|
.Byte => |b| {
|
||||||
std.debug.print("Byte: {d}\n", .{b});
|
std.debug.print("Byte: {d}\n", .{b});
|
||||||
},
|
},
|
||||||
.int => |i| {
|
.Int => |i| {
|
||||||
std.debug.print("Int: {d}\n", .{i});
|
std.debug.print("Int: {d}\n", .{i});
|
||||||
},
|
},
|
||||||
.qmap => |q| {
|
.UInt => |i| {
|
||||||
std.debug.print("QMap:\n", .{});
|
std.debug.print("UInt: {d}\n", .{i});
|
||||||
|
},
|
||||||
|
.QVariantMap => |q| {
|
||||||
|
std.debug.print("QVariantMap:\n", .{});
|
||||||
var qMapIter = q.iterator();
|
var qMapIter = q.iterator();
|
||||||
while (qMapIter.next()) |v| {
|
while (qMapIter.next()) |v| {
|
||||||
print_indent_level(indentLevel + 1);
|
print_indent_level(indentLevel + 1);
|
||||||
|
@ -86,8 +88,8 @@ pub fn pretty_print_variant(variant: QVariant, indentLevel: u64) void {
|
||||||
pretty_print_variant(v.value, indentLevel + 2);
|
pretty_print_variant(v.value, indentLevel + 2);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
.qvarlist => |l| {
|
.QVariantList => |l| {
|
||||||
std.debug.print("QVarList:\n", .{});
|
std.debug.print("QVariantList:\n", .{});
|
||||||
|
|
||||||
for (l.items) |varient, i| {
|
for (l.items) |varient, i| {
|
||||||
print_indent_level(indentLevel + 1);
|
print_indent_level(indentLevel + 1);
|
||||||
|
@ -95,14 +97,20 @@ pub fn pretty_print_variant(variant: QVariant, indentLevel: u64) void {
|
||||||
pretty_print_variant(varient, indentLevel + 2);
|
pretty_print_variant(varient, indentLevel + 2);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
.string => |s| {
|
.String => |s| {
|
||||||
std.debug.print("String: \"{s}\"\n", .{s});
|
std.debug.print("String: \"{s}\"\n", .{s});
|
||||||
},
|
},
|
||||||
.stringlist => {},
|
.QStringList => |l| {
|
||||||
.bytearray => {},
|
std.debug.print("QStringList:\n", .{});
|
||||||
.short => |s| {
|
|
||||||
|
for (l.items) |str, i| {
|
||||||
|
print_indent_level(indentLevel + 1);
|
||||||
|
std.debug.print("\"{s}\" \n", .{str});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.QByteArray => {},
|
||||||
|
.Short => |s| {
|
||||||
std.debug.print("Short: \"{d}\"\n", .{s});
|
std.debug.print("Short: \"{d}\"\n", .{s});
|
||||||
},
|
},
|
||||||
else => {},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,48 +2,52 @@ const std = @import("std");
|
||||||
const qvar = @import("./qvariant.zig");
|
const qvar = @import("./qvariant.zig");
|
||||||
const range = @import("./rangeiter.zig").range;
|
const range = @import("./rangeiter.zig").range;
|
||||||
|
|
||||||
pub fn get_int(reader: anytype) !u32 {
|
pub fn readInt(reader: anytype) !i32 {
|
||||||
|
return try reader.readIntBig(i32);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn readUInt(reader: anytype) !u32 {
|
||||||
return try reader.readIntBig(u32);
|
return try reader.readIntBig(u32);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_short(reader: anytype) !u16 {
|
pub fn readShort(reader: anytype) !u16 {
|
||||||
return try reader.readIntBig(u16);
|
return try reader.readIntBig(u16);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_byte(reader: anytype) !u8 {
|
pub fn readByte(reader: anytype) !u8 {
|
||||||
return try reader.readByte();
|
return try reader.readByte();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_signed_byte(reader: anytype) !i8 {
|
pub fn readSignedByte(reader: anytype) !i8 {
|
||||||
return try reader.readIntBig(i8);
|
return try reader.readIntBig(i8);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_bytearray(reader: anytype, allocator: *std.mem.Allocator) !std.ArrayList(u8) {
|
pub fn readQByteArray(reader: anytype, allocator: *std.mem.Allocator) !std.ArrayList(u8) {
|
||||||
var byteList = std.ArrayList(u8).init(allocator);
|
var byteList = std.ArrayList(u8).init(allocator);
|
||||||
var length = try get_int(reader);
|
var length = try readUInt(reader);
|
||||||
|
|
||||||
var index: usize = 0;
|
var index: usize = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
if (index == length) return byteList;
|
if (index == length) return byteList;
|
||||||
|
|
||||||
const byte = try reader.readByte();
|
const byte = try readByte(reader);
|
||||||
try byteList.append(byte);
|
try byteList.append(byte);
|
||||||
index += 1;
|
index += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_string(reader: anytype, allocator: *std.mem.Allocator) ![]u8 {
|
pub fn readString(reader: anytype, allocator: *std.mem.Allocator) ![]u8 {
|
||||||
var data = std.ArrayList(u8).init(allocator);
|
var data = std.ArrayList(u8).init(allocator);
|
||||||
defer data.deinit();
|
defer data.deinit();
|
||||||
|
|
||||||
var length = try reader.readIntBig(i32);
|
var length = try readInt(reader);
|
||||||
var chars = @divTrunc(length, 2);
|
var chars = @divTrunc(length, 2);
|
||||||
|
|
||||||
var index: usize = 0;
|
var index: usize = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
if (index == chars) break;
|
if (index == chars) break;
|
||||||
|
|
||||||
const byte = try reader.readIntBig(u16);
|
const byte = try readShort(reader);
|
||||||
try data.append(@truncate(u8, byte));
|
try data.append(@truncate(u8, byte));
|
||||||
index += 1;
|
index += 1;
|
||||||
}
|
}
|
||||||
|
@ -56,84 +60,87 @@ pub fn get_string(reader: anytype, allocator: *std.mem.Allocator) ![]u8 {
|
||||||
return ut8Str;
|
return ut8Str;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_qvarlist(reader: anytype, allocator: *std.mem.Allocator) !std.ArrayList(qvar.QVariant) {
|
pub fn readQVariantList(reader: anytype, allocator: *std.mem.Allocator) !std.ArrayList(qvar.QVariant) {
|
||||||
var varlist = std.ArrayList(qvar.QVariant).init(allocator);
|
var varlist = std.ArrayList(qvar.QVariant).init(allocator);
|
||||||
var size = try get_int(reader);
|
var size = try readUInt(reader);
|
||||||
var i: u32 = 0;
|
var i: u32 = 0;
|
||||||
while (i < size) {
|
while (i < size) {
|
||||||
try varlist.append(try get_variant(reader, allocator));
|
try varlist.append(try readQVariant(reader, allocator));
|
||||||
i = i + 1;
|
i = i + 1;
|
||||||
}
|
}
|
||||||
return varlist;
|
return varlist;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_stringlist(reader: anytype, allocator: *std.mem.Allocator) !std.ArrayList([]const u8) {
|
pub fn readQStringList(reader: anytype, allocator: *std.mem.Allocator) !std.ArrayList([]const u8) {
|
||||||
var strList = std.ArrayList([]const u8).init(allocator);
|
var strList = std.ArrayList([]const u8).init(allocator);
|
||||||
var size = try get_int(reader);
|
var size = try readUInt(reader);
|
||||||
var i: u32 = 0;
|
var i: u32 = 0;
|
||||||
while (i < size) {
|
while (i < size) {
|
||||||
try strList.append(try get_string(reader, allocator));
|
try strList.append(try readString(reader, allocator));
|
||||||
i = i + 1;
|
i = i + 1;
|
||||||
}
|
}
|
||||||
return strList;
|
return strList;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_variant_t(reader: anytype, type_id: u32, allocator: *std.mem.Allocator) (@TypeOf(reader).Error || error{EndOfStream} || error{OutOfMemory})!qvar.QVariant {
|
pub fn readQVariantT(reader: anytype, type_id: u32, allocator: *std.mem.Allocator) (@TypeOf(reader).Error || error{EndOfStream} || error{OutOfMemory})!qvar.QVariant {
|
||||||
switch (type_id) {
|
switch (type_id) {
|
||||||
@enumToInt(qvar.QVariantTypes.byte) => {
|
@enumToInt(qvar.QVariantTypes.Byte) => {
|
||||||
var byte = try get_byte(reader);
|
var byte = try readByte(reader);
|
||||||
return qvar.QVariant{ .byte = byte };
|
return qvar.QVariant{ .Byte = byte };
|
||||||
},
|
},
|
||||||
@enumToInt(qvar.QVariantTypes.int_1), @enumToInt(qvar.QVariantTypes.int_2) => {
|
@enumToInt(qvar.QVariantTypes.Int) => {
|
||||||
var int = try get_int(reader);
|
var int = try readInt(reader);
|
||||||
return qvar.QVariant{ .int = int };
|
return qvar.QVariant{ .Int = int };
|
||||||
},
|
},
|
||||||
@enumToInt(qvar.QVariantTypes.qmap) => {
|
@enumToInt(qvar.QVariantTypes.UInt) => {
|
||||||
var map = try get_qvariantmap(reader, allocator);
|
var uint = try readUInt(reader);
|
||||||
return qvar.QVariant{ .qmap = map };
|
return qvar.QVariant{ .UInt = uint };
|
||||||
},
|
},
|
||||||
@enumToInt(qvar.QVariantTypes.qvarlist) => {
|
@enumToInt(qvar.QVariantTypes.QVariantMap) => {
|
||||||
var list = try get_qvarlist(reader, allocator);
|
var map = try readQVariantMap(reader, allocator);
|
||||||
return qvar.QVariant{ .qvarlist = list };
|
return qvar.QVariant{ .QVariantMap = map };
|
||||||
},
|
},
|
||||||
@enumToInt(qvar.QVariantTypes.string) => {
|
@enumToInt(qvar.QVariantTypes.QVariantList) => {
|
||||||
var str = try get_string(reader, allocator);
|
var list = try readQVariantList(reader, allocator);
|
||||||
return qvar.QVariant{ .string = str };
|
return qvar.QVariant{ .QVariantList = list };
|
||||||
},
|
},
|
||||||
@enumToInt(qvar.QVariantTypes.stringlist) => {
|
@enumToInt(qvar.QVariantTypes.String) => {
|
||||||
var strlist = try get_stringlist(reader, allocator);
|
var str = try readString(reader, allocator);
|
||||||
return qvar.QVariant{ .stringlist = strlist };
|
return qvar.QVariant{ .String = str };
|
||||||
},
|
},
|
||||||
@enumToInt(qvar.QVariantTypes.bytearray) => {
|
@enumToInt(qvar.QVariantTypes.QStringList) => {
|
||||||
var bArray = try get_bytearray(reader, allocator);
|
var strlist = try readQStringList(reader, allocator);
|
||||||
return qvar.QVariant{ .bytearray = bArray };
|
return qvar.QVariant{ .QStringList = strlist };
|
||||||
},
|
},
|
||||||
@enumToInt(qvar.QVariantTypes.short) => {
|
@enumToInt(qvar.QVariantTypes.QByteArray) => {
|
||||||
var short = try get_short(reader);
|
var bArray = try readQByteArray(reader, allocator);
|
||||||
return qvar.QVariant{ .short = short };
|
return qvar.QVariant{ .QByteArray = bArray };
|
||||||
|
},
|
||||||
|
@enumToInt(qvar.QVariantTypes.Short) => {
|
||||||
|
var short = try readShort(reader);
|
||||||
|
return qvar.QVariant{ .Short = short };
|
||||||
},
|
},
|
||||||
else => {
|
else => {
|
||||||
@panic("Unsupported!");
|
@panic("Unknown Type");
|
||||||
|
}
|
||||||
},
|
|
||||||
}
|
}
|
||||||
return qvar.QVariant{ .empty = true };
|
return qvar.QVariant{ .empty = true };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_variant(reader: anytype, allocator: *std.mem.Allocator) !qvar.QVariant {
|
pub fn readQVariant(reader: anytype, allocator: *std.mem.Allocator) !qvar.QVariant {
|
||||||
var type_id = try get_int(reader);
|
var type_id = try readUInt(reader);
|
||||||
_ = try get_byte(reader);
|
_ = try readByte(reader);
|
||||||
return try get_variant_t(reader, type_id, allocator);
|
return try readQVariantT(reader, type_id, allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_qvariantmap(reader: anytype, allocator: *std.mem.Allocator) !std.StringHashMap(qvar.QVariant) {
|
pub fn readQVariantMap(reader: anytype, allocator: *std.mem.Allocator) !std.StringHashMap(qvar.QVariant) {
|
||||||
var map = std.StringHashMap(qvar.QVariant).init(allocator);
|
var map = std.StringHashMap(qvar.QVariant).init(allocator);
|
||||||
var numItems = try get_int(reader);
|
var numItems = try readUInt(reader);
|
||||||
|
|
||||||
var iter = range(u32, 0, numItems);
|
var iter = range(u32, 0, numItems);
|
||||||
while (iter.next()) |i| {
|
while (iter.next()) |i| {
|
||||||
var key = try get_string(reader, allocator);
|
var key = try readString(reader, allocator);
|
||||||
var value = try get_variant(reader, allocator);
|
var value = try readQVariant(reader, allocator);
|
||||||
try map.put(key, value);
|
try map.put(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,108 +1,119 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const qvar = @import("./qvariant.zig");
|
const qvar = @import("./qvariant.zig");
|
||||||
|
|
||||||
pub fn add_int(writer: anytype, number: u32) !void {
|
const AllKnownErrors = (std.os.WriteError || error{OutOfMemory});
|
||||||
|
|
||||||
|
pub fn writeInt(writer: anytype, number: i32) !void {
|
||||||
|
try writer.writeIntBig(i32, number);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn writeUInt(writer: anytype, number: u32) !void {
|
||||||
try writer.writeIntBig(u32, number);
|
try writer.writeIntBig(u32, number);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_short(writer: anytype, number: u16) !void {
|
pub fn writeShort(writer: anytype, number: u16) !void {
|
||||||
try writer.writeIntBig(u16, number);
|
try writer.writeIntBig(u16, number);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_byte(writer: anytype, byte: u8) !void {
|
pub fn writeByte(writer: anytype, byte: u8) !void {
|
||||||
try writer.writeByte(byte);
|
try writer.writeByte(byte);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_bytearray(writer: anytype, array: std.ArrayList(u8)) !void {
|
pub fn writeByteArray(writer: anytype, array: std.ArrayList(u8)) !void {
|
||||||
try add_int(writer, @intCast(u32, array.items.len));
|
try writeUInt(writer, @intCast(u32, array.items.len));
|
||||||
try writer.writeAll(array.items);
|
try writer.writeAll(array.items);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_string(writer: anytype, allocator: *std.mem.Allocator, str: []const u8) !void {
|
pub fn writeString(writer: anytype, allocator: *std.mem.Allocator, str: []const u8) !void {
|
||||||
var ut16Str = try allocator.alloc(u16, str.len);
|
var ut16Str = try allocator.alloc(u16, str.len);
|
||||||
defer allocator.free(ut16Str);
|
defer allocator.free(ut16Str);
|
||||||
for (str) |character, index| {
|
for (str) |character, index| {
|
||||||
ut16Str[index] = std.mem.nativeToBig(u16, @as(u16, character));
|
ut16Str[index] = std.mem.nativeToBig(u16, @as(u16, character));
|
||||||
}
|
}
|
||||||
// std.debug.print("add_lenchars: {d} {d} \n", .{@intCast(i32, ut16Str.len*8*2), str.len});
|
|
||||||
|
|
||||||
try writer.writeIntBig(i32, @intCast(i32, ut16Str.len*2));
|
try writeInt(writer, @intCast(i32, ut16Str.len * 2));
|
||||||
try writer.writeAll(std.mem.sliceAsBytes(ut16Str));
|
try writer.writeAll(std.mem.sliceAsBytes(ut16Str));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_stringlist(writer: anytype, allocator: *std.mem.Allocator, strList: std.ArrayList([]const u8)) !void {
|
pub fn writeQStringList(writer: anytype, allocator: *std.mem.Allocator, strList: std.ArrayList([]const u8)) !void {
|
||||||
try add_int(writer, @intCast(u32, strList.items.len));
|
try writeUInt(writer, @intCast(u32, strList.items.len));
|
||||||
for (strList.items) |string| {
|
for (strList.items) |string| {
|
||||||
try add_string(writer, allocator, string);
|
try writeString(writer, allocator, string);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_variantlist(writer: anytype, allocator: *std.mem.Allocator, varList: std.ArrayList([]qvar.QVariant)) !void {
|
pub fn writeQVariantList(writer: anytype, allocator: *std.mem.Allocator, varList: std.ArrayList(qvar.QVariant)) AllKnownErrors!void {
|
||||||
try add_int(writer, @intCast(u32, varList.items.len));
|
try writeUInt(writer, @intCast(u32, varList.items.len));
|
||||||
for (varList.items) |v| {
|
for (varList.items) |v| {
|
||||||
try add_variant(writer, allocator, v);
|
try writeQVariant(writer, allocator, v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call this and Then write your type
|
// Call this and Then write your type
|
||||||
pub fn add_qvariant_with_id(writer: anytype, type_id: u32) !void {
|
pub fn writeQVariantHeader(writer: anytype, type_id: u32) !void {
|
||||||
try add_int(writer, type_id);
|
try writeUInt(writer, type_id);
|
||||||
try add_byte(writer, 0);
|
try writeByte(writer, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_qvariant(writer: anytype, allocator: *std.mem.Allocator, variant: qvar.QVariant) !void {
|
pub fn writeQVariant(writer: anytype, allocator: *std.mem.Allocator, variant: qvar.QVariant) !void {
|
||||||
try add_qvariant_with_id(writer, try qvar.qvariant_type_id(variant));
|
try writeQVariantHeader(writer, try qvar.qvariant_type_id(variant));
|
||||||
switch (variant) {
|
switch (variant) {
|
||||||
.byte => |out| {
|
.Byte => |out| {
|
||||||
try add_byte(writer, out);
|
try writeByte(writer, out);
|
||||||
},
|
},
|
||||||
.int => |out| {
|
.Int => |out| {
|
||||||
try add_int(writer, out);
|
try writeInt(writer, out);
|
||||||
},
|
},
|
||||||
.qmap => |out| {
|
.UInt => |out| {
|
||||||
try add_qvariantmap(writer, allocator, out);
|
try writeUInt(writer, out);
|
||||||
},
|
},
|
||||||
.string => |out| {
|
.QVariantMap => |out| {
|
||||||
try add_string(writer, allocator, out);
|
try writeQVariantMap(writer, allocator, out);
|
||||||
},
|
},
|
||||||
.stringlist => |out| {
|
.QVariantList => |out| {
|
||||||
try add_stringlist(writer, allocator, out);
|
try writeQVariantList(writer, allocator, out);
|
||||||
},
|
},
|
||||||
.bytearray => |out| {
|
.String => |out| {
|
||||||
try add_bytearray(writer, out);
|
try writeString(writer, allocator, out);
|
||||||
},
|
},
|
||||||
.short => |out| {
|
.QStringList => |out| {
|
||||||
try add_short(writer, out);
|
try writeQStringList(writer, allocator, out);
|
||||||
},
|
},
|
||||||
else => {
|
.QByteArray => |out| {
|
||||||
@panic("Unsupported!");
|
try writeByteArray(writer, out);
|
||||||
},
|
},
|
||||||
|
.Short => |out| {
|
||||||
|
try writeShort(writer, out);
|
||||||
|
},
|
||||||
|
//else => {
|
||||||
|
// @panic("Unsupported!");
|
||||||
|
//},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_qvariantmap(writer: anytype, allocator: *std.mem.Allocator, map: std.StringHashMap(qvar.QVariant)) (@TypeOf(writer).Error || std.os.WriteError || error{OutOfMemory})!void {
|
pub fn writeQVariantMap(writer: anytype, allocator: *std.mem.Allocator, map: std.StringHashMap(qvar.QVariant)) (@TypeOf(writer).Error || std.os.WriteError || error{OutOfMemory})!void {
|
||||||
var data = std.ArrayList(u8).init(allocator);
|
var data = std.ArrayList(u8).init(allocator);
|
||||||
defer data.deinit();
|
defer data.deinit();
|
||||||
|
|
||||||
var writeIterator = map.iterator();
|
var writeIterator = map.iterator();
|
||||||
while (writeIterator.next()) |entry| {
|
while (writeIterator.next()) |entry| {
|
||||||
try add_string(data.writer(), allocator, entry.key);
|
try writeString(data.writer(), allocator, entry.key);
|
||||||
try add_qvariant(data.writer(), allocator, entry.value);
|
try writeQVariant(data.writer(), allocator, entry.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// qvariantmap type
|
try writeUInt(writer, map.count());
|
||||||
try add_int(writer, @enumToInt(qvar.QVariantTypes.qmap));
|
|
||||||
try add_byte(writer, 0);
|
|
||||||
try add_int(writer, map.count());
|
|
||||||
|
|
||||||
try writer.writeAll(data.items);
|
try writer.writeAll(data.items);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_message(writer: anytype, allocator: *std.mem.Allocator, map: std.StringHashMap(qvar.QVariant)) (@TypeOf(writer).Error || std.os.WriteError || error{OutOfMemory})!void {
|
pub fn writeFrame(writer: anytype, allocator: *std.mem.Allocator, map: std.StringHashMap(qvar.QVariant)) (@TypeOf(writer).Error || std.os.WriteError || error{OutOfMemory})!void {
|
||||||
var data = std.ArrayList(u8).init(allocator);
|
var data = std.ArrayList(u8).init(allocator);
|
||||||
try add_qvariantmap(data.writer(), allocator, map);
|
try writeQVariant(data.writer(), allocator, .{
|
||||||
|
.QVariantMap = map,
|
||||||
|
});
|
||||||
|
|
||||||
try add_int(writer, @intCast(u32, data.items.len));
|
try writeUInt(writer, @intCast(u32, data.items.len));
|
||||||
|
|
||||||
try writer.writeAll(data.items);
|
try writer.writeAll(data.items);
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Reference in a new issue