diff --git a/src/SocketManager.zig b/src/SocketManager.zig index cfccf46..da0cd52 100644 --- a/src/SocketManager.zig +++ b/src/SocketManager.zig @@ -6,6 +6,8 @@ const write = @import("./qtshit/write.zig"); const range = @import("./qtshit/utils/RangeIter.zig").range; const tls = @import("./deps/iguanaTLS/src/main.zig"); +const log = std.log.scoped(.SocketManager); + pub const SocketManager = struct { allocator: *std.mem.Allocator, baseStream: *std.net.Stream, @@ -47,8 +49,14 @@ pub const SocketManager = struct { } fn _writeFrame(s: *SocketManager, writer: anytype, data: std.ArrayList(u8)) !void { - try write.writeUInt(writer, @intCast(u32, data.items.len)); - try writer.writeAll(data.items); + var frameData = std.ArrayList(u8).init(s.allocator); + defer frameData.deinit(); + + try write.writeUInt(frameData.writer(), @intCast(u32, data.items.len)); + try frameData.writer().writeAll(data.items); + + log.debug("_writeFrame data={any}", .{data.items}); + try writer.writeAll(frameData.items); } pub fn writeFrame(s: *SocketManager, data: std.ArrayList(u8)) !void { @@ -70,6 +78,8 @@ pub const SocketManager = struct { const byte = try reader.readByte(); try data.append(byte); } + + log.debug("_readFrame data={any}", .{data.items}); return data; } diff --git a/src/client.zig b/src/client.zig index 7a7acba..1af43b6 100644 --- a/src/client.zig +++ b/src/client.zig @@ -63,9 +63,8 @@ pub const Client = struct { var featureList = std.ArrayList([]const u8).init(s.allocator); defer featureList.deinit(); - try featureList.append("LongTime"); - try featureList.append("LongMessageID"); + try featureList.append("LongMessageId"); try featureList.append("SenderPrefixes"); try featureList.append("RichMessages"); diff --git a/src/qtshit/read/readByte.zig b/src/qtshit/read/readByte.zig index 19094fb..0a9f47f 100644 --- a/src/qtshit/read/readByte.zig +++ b/src/qtshit/read/readByte.zig @@ -1,8 +1,10 @@ const std = @import("std"); +const log = std.log.scoped(.qtshit); pub fn readByte(reader: anytype) !u8 { - //std.debug.print("read: readByte\n", .{}); - return try reader.readByte(); + var byte = try reader.readByte(); + log.debug("readByte byte={d}", .{byte}); + return byte; } test "deserialize byte" { diff --git a/src/qtshit/read/readInt.zig b/src/qtshit/read/readInt.zig index a04685f..bd25705 100644 --- a/src/qtshit/read/readInt.zig +++ b/src/qtshit/read/readInt.zig @@ -1,8 +1,10 @@ const std = @import("std"); +const log = std.log.scoped(.qtshit); pub fn readInt(reader: anytype) !i32 { - //std.debug.print("read: readInt\n", .{}); - return try reader.readIntBig(i32); + var int = try reader.readIntBig(i32); + log.debug("readInt int={d}", .{int}); + return int; } test "deserialize int" { diff --git a/src/qtshit/read/readLong.zig b/src/qtshit/read/readLong.zig index 13b1d4e..27969e9 100644 --- a/src/qtshit/read/readLong.zig +++ b/src/qtshit/read/readLong.zig @@ -1,8 +1,10 @@ const std = @import("std"); +const log = std.log.scoped(.qtshit); pub fn readLong(reader: anytype) !i64 { - //std.debug.print("read: readLong\n", .{}); - return try reader.readIntBig(i64); + var long = try reader.readIntBig(i64); + log.debug("readLong long={d}", .{long}); + return long; } test "deserialize long" { diff --git a/src/qtshit/read/readQByteArray.zig b/src/qtshit/read/readQByteArray.zig index 254fab3..04b81cd 100644 --- a/src/qtshit/read/readQByteArray.zig +++ b/src/qtshit/read/readQByteArray.zig @@ -2,10 +2,11 @@ const std = @import("std"); const readUInt = @import("./readUInt.zig").readUInt; const range = @import("../utils/RangeIter.zig").range; const freeQByteArray = @import("../utils/free/freeQByteArray.zig").freeQByteArray; +const log = std.log.scoped(.qtshit); pub fn readQByteArray(reader: anytype, allocator: *std.mem.Allocator) ![]u8 { var length = try readUInt(reader); - if (length == std.math.maxInt(@TypeOf(length))) { + if (length == std.math.maxInt(@TypeOf(length)) or length == 589526130) { return ""; } var byteList = try allocator.alloc(u8, @intCast(usize, length)); @@ -15,7 +16,7 @@ pub fn readQByteArray(reader: anytype, allocator: *std.mem.Allocator) ![]u8 { const byte = try reader.readByte(); byteList[i] = byte; } - //std.debug.print("read: readQByteArray length={d} content={any} \n", .{length, byteList}); + log.debug("readQByteArray length={d} content={s}", .{ length, byteList }); return byteList; } diff --git a/src/qtshit/read/readQStringList.zig b/src/qtshit/read/readQStringList.zig index 2ae25b4..d6b7c24 100644 --- a/src/qtshit/read/readQStringList.zig +++ b/src/qtshit/read/readQStringList.zig @@ -1,5 +1,6 @@ const std = @import("std"); const range = @import("../utils/RangeIter.zig").range; +const log = std.log.scoped(.qtshit); const readUInt = @import("./readUInt.zig").readUInt; const readString = @import("./readString.zig").readString; @@ -7,13 +8,15 @@ const freeQStringList = @import("../utils/free/freeQStringList.zig").freeQString pub fn readQStringList(reader: anytype, allocator: *std.mem.Allocator) ![][]const u8 { var length = try readUInt(reader); - //std.debug.print("read: readQStringList length={d} \n", .{length}); + log.debug("readQStringList length={d}", .{length}); var stringList = try allocator.alloc([]const u8, @intCast(usize, length)); var iter = range(u32, 0, length); while (iter.next()) |i| { - stringList[i] = try readString(reader, allocator); + var string = try readString(reader, allocator); + log.debug("readQStringList length={d} index={d} string=\"{s}\"", .{ length, i, string }); + stringList[i] = string; } return stringList; diff --git a/src/qtshit/read/readQVariant.zig b/src/qtshit/read/readQVariant.zig index 1a9491f..27544dd 100644 --- a/src/qtshit/read/readQVariant.zig +++ b/src/qtshit/read/readQVariant.zig @@ -1,4 +1,6 @@ const std = @import("std"); +const log = std.log.scoped(.qtshit); + const readUInt = @import("./readUInt.zig").readUInt; const readByte = @import("./readByte.zig").readByte; const readQVariantT = @import("./readQVariantT.zig").readQVariantT; @@ -9,9 +11,11 @@ const QVariant = @import("../types/QVariant.zig").QVariant; pub fn readQVariant(reader: anytype, allocator: *std.mem.Allocator) !QVariant { var type_id = try readUInt(reader); var mysteryByte = try readByte(reader); - //std.debug.print("read: readQVariant type_id={d} mysteryByte={d} \n", .{type_id, mysteryByte}); + + log.debug("readQVariant type_id={d} mysteryByte={d}", .{ type_id, mysteryByte }); return try readQVariantT(reader, type_id, allocator); } + test "deserialize QVariant Byte" { var bytes = &[_]u8{ 0, 0, 0, 1, 0, 255 }; var fBS = std.io.fixedBufferStream(bytes); diff --git a/src/qtshit/read/readQVariantList.zig b/src/qtshit/read/readQVariantList.zig index d85449d..107a198 100644 --- a/src/qtshit/read/readQVariantList.zig +++ b/src/qtshit/read/readQVariantList.zig @@ -1,5 +1,6 @@ const std = @import("std"); const range = @import("../utils/RangeIter.zig").range; +const log = std.log.scoped(.qtshit); const readUInt = @import("./readUInt.zig").readUInt; const readQVariant = @import("./readQVariant.zig").readQVariant; @@ -9,7 +10,7 @@ const QVariant = @import("../types/QVariant.zig").QVariant; pub fn readQVariantList(reader: anytype, allocator: *std.mem.Allocator) ![]QVariant { var length = try readUInt(reader); - //std.debug.print("read: readQVariantList length={d}\n", .{length}); + log.debug("readQVariantList length={d}", .{length}); var variantList = try allocator.alloc(QVariant, @intCast(usize, length)); diff --git a/src/qtshit/read/readShort.zig b/src/qtshit/read/readShort.zig index 7f80c7f..93b45c4 100644 --- a/src/qtshit/read/readShort.zig +++ b/src/qtshit/read/readShort.zig @@ -1,8 +1,10 @@ const std = @import("std"); +const log = std.log.scoped(.qtshit); pub fn readShort(reader: anytype) !u16 { - //std.debug.print("read: readShort\n", .{}); - return try reader.readIntBig(u16); + var short = try reader.readIntBig(u16); + log.debug("readShort short={d}", .{short}); + return short; } test "deserialize short" { diff --git a/src/qtshit/read/readSignedByte.zig b/src/qtshit/read/readSignedByte.zig index b610ba5..78d66ec 100644 --- a/src/qtshit/read/readSignedByte.zig +++ b/src/qtshit/read/readSignedByte.zig @@ -1,8 +1,10 @@ const std = @import("std"); +const log = std.log.scoped(.qtshit); pub fn readSignedByte(reader: anytype) !i8 { - //std.debug.print("read: readSignedByte\n", .{}); - return try reader.readIntBig(i8); + var signedByte = try reader.readIntBig(i8); + log.debug("readSignedByte signedByte={d}", .{signedByte}); + return signedByte; } test "deserialize signed byte" { diff --git a/src/qtshit/read/readString.zig b/src/qtshit/read/readString.zig index e3cd331..3bc4b77 100644 --- a/src/qtshit/read/readString.zig +++ b/src/qtshit/read/readString.zig @@ -3,6 +3,7 @@ const readInt = @import("./readInt.zig").readInt; const range = @import("../utils/RangeIter.zig").range; const unicode = @import("../utils/unicode.zig"); const freeString = @import("../utils/free/freeString.zig").freeString; +const log = std.log.scoped(.qtshit); pub const ReadStringErrors = (error{ExpectedSecondSurrogateHalf} || error{UnexpectedSecondSurrogateHalf} || error{DanglingSurrogateHalf} || error{OutOfMemory}); @@ -11,17 +12,15 @@ pub fn readString(reader: anytype, allocator: *std.mem.Allocator) (@TypeOf(reade defer utf16Data.deinit(); var num_bytes = @divTrunc(try readInt(reader), 2); + log.debug("readString num_bytes={d}", .{num_bytes}); var iter = range(i32, 0, num_bytes); while (iter.next()) |i| { - if (i == num_bytes) break; try utf16Data.append(try reader.readIntLittle(u16)); } var utf8 = try unicode.utf16BEToUtf8(allocator, utf16Data.items); - - //std.debug.print("string: {s}\n", .{utf8}); - + log.debug("readString num_bytes={d} utf8=\"{s}\"", .{ num_bytes, utf8 }); return utf8; } diff --git a/src/qtshit/read/readUInt.zig b/src/qtshit/read/readUInt.zig index a76d3d8..26674a5 100644 --- a/src/qtshit/read/readUInt.zig +++ b/src/qtshit/read/readUInt.zig @@ -1,8 +1,10 @@ const std = @import("std"); +const log = std.log.scoped(.qtshit); pub fn readUInt(reader: anytype) !u32 { - //std.debug.print("read: readUInt\n", .{}); - return try reader.readIntBig(u32); + var uint = try reader.readIntBig(u32); + log.debug("readUInt uint={d}", .{uint}); + return uint; } test "deserialize UInt" { diff --git a/src/qtshit/read/usertypes/readBufferInfo.zig b/src/qtshit/read/usertypes/readBufferInfo.zig index 5a7d892..6cd0f6e 100644 --- a/src/qtshit/read/usertypes/readBufferInfo.zig +++ b/src/qtshit/read/usertypes/readBufferInfo.zig @@ -5,8 +5,10 @@ const readShort = @import("../readShort.zig").readShort; const readByte = @import("../readByte.zig").readByte; const readQByteArray = @import("../readQByteArray.zig").readQByteArray; const freeBufferInfo = @import("../../utils/free/freeBufferInfo.zig").freeBufferInfo; +const log = std.log.scoped(.qtshit); pub fn readBufferInfo(reader: anytype, allocator: *std.mem.Allocator) !BufferInfo { + log.debug("readBufferInfo", .{}); var id = try readInt(reader); var networkid = try readInt(reader); var btype = try readShort(reader); diff --git a/src/qtshit/read/usertypes/readMessage.zig b/src/qtshit/read/usertypes/readMessage.zig index 9caeb19..a7203ad 100644 --- a/src/qtshit/read/usertypes/readMessage.zig +++ b/src/qtshit/read/usertypes/readMessage.zig @@ -6,8 +6,11 @@ const readByte = @import("../readByte.zig").readByte; const readBufferInfo = @import("./readBufferInfo.zig").readBufferInfo; const readQByteArray = @import("../readQByteArray.zig").readQByteArray; const freeMessage = @import("../../utils/free/freeMessage.zig").freeMessage; +const log = std.log.scoped(.qtshit); + pub fn readMessage(reader: anytype, allocator: *std.mem.Allocator) !Message { + log.debug("readMessage", .{}); return Message{ .MessageID = try readLong(reader), .Timestamp = try readLong(reader),