diff --git a/protocol/src/test/swift/main.swift b/protocol/src/test/swift/main.swift new file mode 100644 index 00000000..7bc67421 --- /dev/null +++ b/protocol/src/test/swift/main.swift @@ -0,0 +1,60 @@ +import Foundation + +ProtocolManager.initProtocol() + +//let filePath = "/Users/wjd/Desktop/zfoo-swift/zfoo-swift-support/zfoo-swift-support/compatible/normal-no-compatible.bytes" +//let filePath = "/Users/wjd/Desktop/zfoo-swift/zfoo-swift-support/zfoo-swift-support/compatible/normal-inner-compatible.bytes" +//let filePath = "/Users/wjd/Desktop/zfoo-swift/zfoo-swift-support/zfoo-swift-support/compatible/normal-out-compatible.bytes" +//let filePath = "/Users/wjd/Desktop/zfoo-swift/zfoo-swift-support/zfoo-swift-support/compatible/normal-out-inner-compatible.bytes" +//let filePath = "/Users/wjd/Desktop/zfoo-swift/zfoo-swift-support/zfoo-swift-support/compatible/normal-out-inner-inner-compatible-big.bytes" +let filePath = "/Users/wjd/Desktop/zfoo-swift/zfoo-swift-support/zfoo-swift-support/compatible/normal-out-inner-inner-compatible.bytes" +let fileUrl = URL(fileURLWithPath: filePath) +let fileData = try Data(contentsOf: fileUrl) +let bytes = [UInt8](fileData) +let buffer = ByteBuffer() +buffer.writeUBytes(bytes) +let packet = ProtocolManager.read(buffer) +let newBuffer = ByteBuffer() +ProtocolManager.write(newBuffer, packet) +let newPacket = ProtocolManager.read(newBuffer) + +byteBufferTest() + +func byteBufferTest() { + let buffer = ByteBuffer() + assert(buffer.getCapacity() == 128) + buffer.writeBool(true) + assert(buffer.readBool() == true) + buffer.writeByte(99) + assert(buffer.readByte() == 99) + buffer.writeByte(-99) + assert(buffer.readByte() == -99) + buffer.writeShort(9999) + assert(buffer.readShort() == 9999) + buffer.writeInt(2147483647) + assert(buffer.readInt() == 2147483647) + buffer.writeInt(-2147483648) + assert(buffer.readInt() == -2147483648) + buffer.writeRawInt(2147483647) + assert(buffer.readRawInt() == 2147483647) + buffer.writeRawInt(-2147483648) + assert(buffer.readRawInt() == -2147483648) + buffer.writeLong(9223372036854775807) + assert(buffer.readLong() == 9223372036854775807) + buffer.writeLong(-9999999999999) + assert(buffer.readLong() == -9999999999999) + buffer.writeRawLong(9223372036854775807) + assert(buffer.readRawLong() == 9223372036854775807) + buffer.writeRawLong(-9999999999999) + assert(buffer.readRawLong() == -9999999999999) + buffer.writeFloat(99.0) + assert(buffer.readFloat() == 99.0) + buffer.writeFloat(-99.0) + assert(buffer.readFloat() == -99.0) + buffer.writeDouble(99.0) + assert(buffer.readDouble() == 99.0) + buffer.writeDouble(-99.0) + assert(buffer.readDouble() == -99.0) + buffer.writeString("Hello World! 你好,世界!") + assert(buffer.readString() == "Hello World! 你好,世界!") +} diff --git a/protocol/src/test/swift/zfooswift/ByteBuffer.swift b/protocol/src/test/swift/zfooswift/ByteBuffer.swift new file mode 100644 index 00000000..5ee9665f --- /dev/null +++ b/protocol/src/test/swift/zfooswift/ByteBuffer.swift @@ -0,0 +1,998 @@ +import Foundation + +class ByteBuffer { + var buffer: [Int8] = Array(repeating: 0, count: 128) + var writeOffset: Int = 0 + var readOffset: Int = 0 + + func adjustPadding(_ predictionLength: Int, _ beforewriteIndex: Int) { + // 因为写入的是可变长的int,如果预留的位置过多,则清除多余的位置 + let currentwriteIndex = writeOffset + let predictionCount = writeIntCount(predictionLength) + let length = currentwriteIndex - beforewriteIndex - predictionCount + let lengthCount = writeIntCount(length) + let padding = lengthCount - predictionCount + if (padding == 0) { + writeOffset = beforewriteIndex + writeInt(length) + writeOffset = currentwriteIndex + } else { + let bytes = Array(buffer[(currentwriteIndex - length).. Bool { + return length != -1 && readOffset < length + beforeReadIndex + } + + func getBuffer() -> [Int8] { + return buffer + } + + func getWriteOffset() -> Int { + return writeOffset + } + + func setWriteOffset(_ writeIndex: Int) { + writeOffset = writeIndex + } + + func getReadOffset() -> Int { + return readOffset + } + + func setReadOffset(_ readIndex: Int) { + readOffset = readIndex + } + + func isReadable() -> Bool { + return writeOffset > readOffset + } + + func writeBytes(_ bytes: [Int8]) { + let length = bytes.count + ensureCapacity(length) + buffer[writeOffset..<(writeOffset + length)] = bytes[0.. [Int8] { + let bytes = buffer[readOffset..<(readOffset + length)] + readOffset += length + return Array(bytes) + } + + func writeUBytes(_ bytes: [UInt8]) { + let length = bytes.count + ensureCapacity(length) + let newBytes = bytes.map { Int8(bitPattern: $0) } + writeBytes(newBytes) + } + + func toBytes() -> [Int8] { + return Array(buffer[0.. Int { + return buffer.count - writeOffset; + } + + func ensureCapacity(_ capacity: Int) { + while (capacity - getCapacity() > 0) { + let newSize = buffer.count + let newBytes = Array(repeating: 0, count: newSize) + buffer.append(contentsOf: newBytes) + } + } + + func writeBool(_ value: Bool) { + ensureCapacity(1) + buffer[writeOffset] = value ? 1 : 0 + writeOffset += 1 + } + + func readBool() -> Bool { + let value = buffer[readOffset] == 1 ? true : false + readOffset += 1 + return value + } + + func writeByte(_ value: Int8) { + ensureCapacity(1) + buffer[writeOffset] = value + writeOffset += 1 + } + + func readByte() -> Int8 { + let value = buffer[readOffset] + readOffset += 1 + return value + } + + func writeUByte(_ value: UInt8) { + ensureCapacity(1) + buffer[writeOffset] = Int8(bitPattern: value) + writeOffset += 1 + } + + func readUByte() -> UInt8 { + let value = buffer[readOffset] + readOffset += 1 + return UInt8(bitPattern: value) + } + + func writeShort(_ value: Int16) { + ensureCapacity(2) + buffer[writeOffset] = Int8(bitPattern: UInt8(value >> 8 & 0xFF)) + buffer[writeOffset + 1] = Int8(bitPattern: UInt8(value & 0xFF)) + writeOffset += 2 + } + + func readShort() -> Int16 { + let value = Int16(UInt8(bitPattern: buffer[readOffset])) << 8 | Int16(UInt8(bitPattern: buffer[readOffset + 1])) + readOffset += 2 + return value + } + + func writeIntCount(_ intValue: Int) -> Int { + let longValue = Int64(intValue) + let value = UInt64(bitPattern: ((longValue << 1) ^ (longValue >> 63))) + if (value >> 7 == 0) { + return 1 + } + if (value >> 14 == 0) { + return 2 + } + if (value >> 21 == 0) { + return 3 + } + + if (value >> 28 == 0) { + return 4 + } + return 5 + } + + func writeRawInt(_ value: Int32) { + writeUByte(UInt8(value >> 24 & 0xFF)) + writeUByte(UInt8(value >> 16 & 0xFF)) + writeUByte(UInt8(value >> 8 & 0xFF)) + writeUByte(UInt8(value & 0xFF)) + } + + func readRawInt() -> Int32 { + let value = Int32(readUByte()) << 24 | Int32(readUByte()) << 16 | Int32(readUByte()) << 8 | Int32(readUByte()) + return value + } + + func writeRawLong(_ value: Int64) { + writeUByte(UInt8(value >> 56 & 0xFF)) + writeUByte(UInt8(value >> 48 & 0xFF)) + writeUByte(UInt8(value >> 40 & 0xFF)) + writeUByte(UInt8(value >> 32 & 0xFF)) + writeUByte(UInt8(value >> 24 & 0xFF)) + writeUByte(UInt8(value >> 16 & 0xFF)) + writeUByte(UInt8(value >> 8 & 0xFF)) + writeUByte(UInt8(value & 0xFF)) + } + + func readRawLong() -> Int64 { + let value = Int64(readUByte()) << 56 | Int64(readUByte()) << 48 | Int64(readUByte()) << 40 | Int64(readUByte()) << 32 | Int64(readUByte()) << 24 | Int64(readUByte()) << 16 | Int64(readUByte()) << 8 | Int64(readUByte()) + return value + } + + func writeInt(_ value: Int) { + var v = value + if (v > 2147483647) { + v = 2147483647 + } else if (v < -2147483648) { + v = -2147483648 + } + writeLong(Int64(v)) + } + + func readInt() -> Int { + return Int(readLong()) + } + + func writeLong(_ longValue: Int64) { + let value = UInt64(bitPattern: ((longValue << 1) ^ (longValue >> 63))) + + if (value >> 7 == 0) { + writeUByte(UInt8(value)) + return; + } + + if (value >> 14 == 0) { + writeUByte(UInt8((value & 0x7F) | 0x80)) + writeUByte(UInt8(value >> 7)) + return; + } + + if (value >> 21 == 0) { + writeUByte(UInt8(value & 0x7F | 0x80)) + writeUByte(UInt8((value >> 7 & 0x7F) | 0x80)) + writeUByte(UInt8(value >> 14)) + return; + } + + if ((value >> 28) == 0) { + writeUByte(UInt8(value & 0x7F | 0x80)) + writeUByte(UInt8((value >> 7 & 0x7F) | 0x80)) + writeUByte(UInt8((value >> 14 & 0x7F) | 0x80)) + writeUByte(UInt8(value >> 21)) + return; + } + + if (value >> 35 == 0) { + writeUByte(UInt8(value & 0x7F | 0x80)) + writeUByte(UInt8((value >> 7 & 0x7F) | 0x80)) + writeUByte(UInt8((value >> 14 & 0x7F) | 0x80)) + writeUByte(UInt8((value >> 21 & 0x7F) | 0x80)) + writeUByte(UInt8(value >> 28)) + return; + } + + if (value >> 42 == 0) { + writeUByte(UInt8(value & 0x7F | 0x80)) + writeUByte(UInt8((value >> 7 & 0x7F) | 0x80)) + writeUByte(UInt8((value >> 14 & 0x7F) | 0x80)) + writeUByte(UInt8((value >> 21 & 0x7F) | 0x80)) + writeUByte(UInt8((value >> 28 & 0x7F) | 0x80)) + writeUByte(UInt8(value >> 35)) + return; + } + + if (value >> 49 == 0) { + writeUByte(UInt8(value & 0x7F | 0x80)) + writeUByte(UInt8((value >> 7 & 0x7F) | 0x80)) + writeUByte(UInt8((value >> 14 & 0x7F) | 0x80)) + writeUByte(UInt8((value >> 21 & 0x7F) | 0x80)) + writeUByte(UInt8((value >> 28 & 0x7F) | 0x80)) + writeUByte(UInt8((value >> 35 & 0x7F) | 0x80)) + writeUByte(UInt8(value >> 42)) + return; + } + + if ((value >> 56) == 0) { + writeUByte(UInt8(value & 0x7F | 0x80)) + writeUByte(UInt8((value >> 7 & 0x7F) | 0x80)) + writeUByte(UInt8((value >> 14 & 0x7F) | 0x80)) + writeUByte(UInt8((value >> 21 & 0x7F) | 0x80)) + writeUByte(UInt8((value >> 28 & 0x7F) | 0x80)) + writeUByte(UInt8((value >> 35 & 0x7F) | 0x80)) + writeUByte(UInt8((value >> 42 & 0x7F) | 0x80)) + writeUByte(UInt8(value >> 49)) + return; + } + + writeUByte(UInt8(value & 0x7F | 0x80)) + writeUByte(UInt8((value >> 7 & 0x7F) | 0x80)) + writeUByte(UInt8((value >> 14 & 0x7F) | 0x80)) + writeUByte(UInt8((value >> 21 & 0x7F) | 0x80)) + writeUByte(UInt8((value >> 28 & 0x7F) | 0x80)) + writeUByte(UInt8((value >> 35 & 0x7F) | 0x80)) + writeUByte(UInt8((value >> 42 & 0x7F) | 0x80)) + writeUByte(UInt8((value >> 49 & 0x7F) | 0x80)) + writeUByte(UInt8(value >> 56)) + } + + func readLong() -> Int64 { + var b = UInt64(readUByte()) + var value = b & 0x7F + if ((b & 0x80) != 0) { + b = UInt64(readUByte()) + value |= (b & 0x7F) << 7 + if ((b & 0x80) != 0) { + b = UInt64(readUByte()) + value |= (b & 0x7F) << 14 + if ((b & 0x80) != 0) { + b = UInt64(readUByte()) + value |= (b & 0x7F) << 21 + if ((b & 0x80) != 0) { + b = UInt64(readUByte()) + value |= (b & 0x7F) << 28 + if ((b & 0x80) != 0) { + b = UInt64(readUByte()) + value |= (b & 0x7F) << 35 + if ((b & 0x80) != 0) { + b = UInt64(readUByte()) + value |= (b & 0x7F) << 42 + if ((b & 0x80) != 0) { + b = UInt64(readUByte()) + value |= (b & 0x7F) << 49 + if ((b & 0x80) != 0) { + b = UInt64(readUByte()) + value |= b << 56 + } + } + } + } + } + } + } + } + return Int64(bitPattern: value >> 1) ^ -(Int64(bitPattern: value) & 1) + } + + func writeFloat(_ value: Float32) { + let v = value.bitPattern + writeRawInt(Int32(bitPattern: v)) + } + + func readFloat() -> Float32 { + let value = UInt32(bitPattern: readRawInt()) + return Float32(bitPattern: value) + } + + func writeDouble(_ value: Float64) { + let v = value.bitPattern + writeRawLong(Int64(bitPattern: v)) + } + + func readDouble() -> Float64 { + let value = UInt64(bitPattern: readRawLong()) + return Float64(bitPattern: value) + } + + func writeString(_ value: String) { + if (value.isEmpty) { + writeInt(0) + return + } + if let data = value.data(using: .utf8) { + let byteArray = [UInt8](data) + let bytes = byteArray.map { Int8(bitPattern: $0) } + writeInt(bytes.count) + writeBytes(bytes) + } + } + + func readString() -> String { + let length = readInt() + if (length <= 0) { + return "" + } + let int8Array = readBytes(length) + let bytes = int8Array.map { UInt8(bitPattern: $0) } + let value = String(bytes: bytes, encoding: .utf8)! + return value + } + + func writePacket(_ packet: Any?, _ protocolId: Int) { + let pro = ProtocolManager.getProtocol(protocolId) + pro.write(self, packet) + } + + func readPacket(_ protocolId: Int) -> Any { + let pro = ProtocolManager.getProtocol(protocolId) + return pro.read(self) + } + + func writeBoolArray(_ array: Array) { + if (array.isEmpty) { + writeInt(0) + } else { + writeInt(array.count) + for ele in array { + writeBool(ele) + } + } + } + + func readBoolArray() -> Array { + let size = readInt() + var array = Array() + if (size > 0) { + for _ in 0..) { + if (array.isEmpty) { + writeInt(0) + } else { + writeInt(array.count) + for ele in array { + writeByte(ele) + } + } + } + + func readByteArray() -> Array { + let size = readInt() + var array = Array() + if (size > 0) { + for _ in 0..) { + if (array.isEmpty) { + writeInt(0) + } else { + writeInt(array.count) + for ele in array { + writeShort(ele) + } + } + } + + func readShortArray() -> Array { + let size = readInt() + var array = Array() + if (size > 0) { + for _ in 0..) { + if (array.isEmpty) { + writeInt(0) + } else { + writeInt(array.count) + for ele in array { + writeInt(ele) + } + } + } + + func readIntArray() -> Array { + let size = readInt() + var array = Array() + if (size > 0) { + for _ in 0..) { + if (array.isEmpty) { + writeInt(0) + } else { + writeInt(array.count) + for ele in array { + writeLong(ele) + } + } + } + + func readLongArray() -> Array { + let size = readInt() + var array = Array() + if (size > 0) { + for _ in 0..) { + if (array.isEmpty) { + writeInt(0) + } else { + writeInt(array.count) + for ele in array { + writeFloat(ele) + } + } + } + + func readFloatArray() -> Array { + let size = readInt() + var array = Array() + if (size > 0) { + for _ in 0..) { + if (array.isEmpty) { + writeInt(0) + } else { + writeInt(array.count) + for ele in array { + writeDouble(ele) + } + } + } + + func readDoubleArray() -> Array { + let size = readInt() + var array = Array() + if (size > 0) { + for _ in 0..) { + if (array.isEmpty) { + writeInt(0) + } else { + writeInt(array.count) + for ele in array { + writeString(ele) + } + } + } + + func readStringArray() -> Array { + let size = readInt() + var array = Array() + if (size > 0) { + for _ in 0.., _ protocolId: Int) { + if (array.isEmpty) { + writeInt(0) + } else { + writeInt(array.count) + for ele in array { + writePacket(ele, protocolId) + } + } + } + + func readPacketArray(_ protocolId: Int) -> Array { + let size = readInt() + var array = Array() + if (size > 0) { + for _ in 0..) { + if (set.isEmpty) { + writeInt(0) + } else { + writeInt(set.count) + for ele in set { + writeBool(ele) + } + } + } + + func readBoolSet() -> Set { + let size = readInt() + var set = Set() + if (size > 0) { + for _ in 0..) { + if (set.isEmpty) { + writeInt(0) + } else { + writeInt(set.count) + for ele in set { + writeByte(ele) + } + } + } + + func readByteSet() -> Set { + let size = readInt() + var set = Set() + if (size > 0) { + for _ in 0..) { + if (set.isEmpty) { + writeInt(0) + } else { + writeInt(set.count) + for ele in set { + writeShort(ele) + } + } + } + + func readShortSet() -> Set { + let size = readInt() + var set = Set() + if (size > 0) { + for _ in 0..) { + if (set.isEmpty) { + writeInt(0) + } else { + writeInt(set.count) + for ele in set { + writeInt(ele) + } + } + } + + func readIntSet() -> Set { + let size = readInt() + var set = Set() + if (size > 0) { + for _ in 0..) { + if (set.isEmpty) { + writeInt(0) + } else { + writeInt(set.count) + for ele in set { + writeLong(ele) + } + } + } + + func readLongSet() -> Set { + let size = readInt() + var set = Set() + if (size > 0) { + for _ in 0..) { + if (set.isEmpty) { + writeInt(0) + } else { + writeInt(set.count) + for ele in set { + writeFloat(ele) + } + } + } + + func readFloatSet() -> Set { + let size = readInt() + var set = Set() + if (size > 0) { + for _ in 0..) { + if (set.isEmpty) { + writeInt(0) + } else { + writeInt(set.count) + for ele in set { + writeDouble(ele) + } + } + } + + func readDoubleSet() -> Set { + let size = readInt() + var set = Set() + if (size > 0) { + for _ in 0..) { + if (set.isEmpty) { + writeInt(0) + } else { + writeInt(set.count) + for ele in set { + writeString(ele) + } + } + } + + func readStringSet() -> Set { + let size = readInt() + var set = Set() + if (size > 0) { + for _ in 0..) { + if (map.isEmpty) { + writeInt(0) + } else { + writeInt(map.count) + for (key, value) in map { + writeInt(key) + writeInt(value) + } + } + } + + func readIntIntMap() -> Dictionary { + let size = readInt() + var map = Dictionary() + if (size > 0) { + for _ in 0..) { + if (map.isEmpty) { + writeInt(0) + } else { + writeInt(map.count) + for (key, value) in map { + writeInt(key) + writeLong(value) + } + } + } + + func readIntLongMap() -> Dictionary { + let size = readInt() + var map = Dictionary() + if (size > 0) { + for _ in 0..) { + if (map.isEmpty) { + writeInt(0) + } else { + writeInt(map.count) + for (key, value) in map { + writeInt(key) + writeString(value) + } + } + } + + func readIntStringMap() -> Dictionary { + let size = readInt() + var map = Dictionary() + if (size > 0) { + for _ in 0.., _ protocolId: Int) { + if (map.isEmpty) { + writeInt(0) + } else { + writeInt(map.count) + for (key, value) in map { + writeInt(key) + writePacket(value, protocolId) + } + } + } + + func readIntPacketMap(_ protocolId: Int) -> Dictionary { + let size = readInt() + var map = Dictionary() + if (size > 0) { + for _ in 0..) { + if (map.isEmpty) { + writeInt(0) + } else { + writeInt(map.count) + for (key, value) in map { + writeLong(key) + writeInt(value) + } + } + } + + func readLongIntMap() -> Dictionary { + let size = readInt() + var map = Dictionary() + if (size > 0) { + for _ in 0..) { + if (map.isEmpty) { + writeInt(0) + } else { + writeInt(map.count) + for (key, value) in map { + writeLong(key) + writeLong(value) + } + } + } + + func readLongLongMap() -> Dictionary { + let size = readInt() + var map = Dictionary() + if (size > 0) { + for _ in 0.., _ protocolId: Int) { + if (map.isEmpty) { + writeInt(0) + } else { + writeInt(map.count) + for (key, value) in map { + writeLong(key) + writePacket(value, protocolId) + } + } + } + + func readLongPacketMap(_ protocolId: Int) -> Dictionary { + let size = readInt() + var map = Dictionary() + if (size > 0) { + for _ in 0..) { + if (map.isEmpty) { + writeInt(0) + } else { + writeInt(map.count) + for (key, value) in map { + writeString(key) + writeInt(value) + } + } + } + + func readStringIntMap() -> Dictionary { + let size = readInt() + var map = Dictionary() + if (size > 0) { + for _ in 0..) { + if (map.isEmpty) { + writeInt(0) + } else { + writeInt(map.count) + for (key, value) in map { + writeString(key) + writeLong(value) + } + } + } + + func readStringLongMap() -> Dictionary { + let size = readInt() + var map = Dictionary() + if (size > 0) { + for _ in 0.., _ protocolId: Int) { + if (map.isEmpty) { + writeInt(0) + } else { + writeInt(map.count) + for (key, value) in map { + writeString(key) + writePacket(value, protocolId) + } + } + } + + func readStringPacketMap(_ protocolId: Int) -> Dictionary { + let size = readInt() + var map = Dictionary() + if (size > 0) { + for _ in 0.. Int +} + + +protocol IProtocolRegistration { + func write(_ buffer: ByteBuffer, _ packet: Any?) + + func read(_ buffer: ByteBuffer) -> Any +} diff --git a/protocol/src/test/swift/zfooswift/ProtocolManager.swift b/protocol/src/test/swift/zfooswift/ProtocolManager.swift new file mode 100644 index 00000000..9e04b91b --- /dev/null +++ b/protocol/src/test/swift/zfooswift/ProtocolManager.swift @@ -0,0 +1,32 @@ +import Foundation + +class ProtocolManager { + static var protocols = Dictionary() + + static func initProtocol() { + // initProtocol + protocols[0] = EmptyObjectRegistration() + protocols[101] = NormalObjectRegistration() + protocols[102] = ObjectARegistration() + protocols[103] = ObjectBRegistration() + protocols[104] = SimpleObjectRegistration() + } + + static func getProtocol(_ protocolId: Int) -> IProtocolRegistration { + return protocols[protocolId]! + } + + static func write(_ buffer: ByteBuffer, _ packet: Any) { + let p = packet as! IProtocol + let protocolId = p.protocolId() + let pro = getProtocol(protocolId) + buffer.writeShort(Int16(protocolId)) + pro.write(buffer, p) + } + + static func read(_ buffer: ByteBuffer) -> Any { + let protocolId = buffer.readShort() + let pro = getProtocol(Int(protocolId)) + return pro.read(buffer) + } +} \ No newline at end of file diff --git a/protocol/src/test/swift/zfooswift/packet/EmptyObject.swift b/protocol/src/test/swift/zfooswift/packet/EmptyObject.swift new file mode 100644 index 00000000..35673265 --- /dev/null +++ b/protocol/src/test/swift/zfooswift/packet/EmptyObject.swift @@ -0,0 +1,35 @@ +import Foundation + + +class EmptyObject : IProtocol { + + + func protocolId() -> Int { + return 0 + } +} + +class EmptyObjectRegistration : IProtocolRegistration { + func write(_ buffer: ByteBuffer, _ packet: Any?) { + if (packet == nil) { + buffer.writeInt(0) + return + } + let message = packet as! EmptyObject + buffer.writeInt(-1) + } + + func read(_ buffer: ByteBuffer) -> Any { + let length = buffer.readInt() + let packet = EmptyObject() + if (length == 0) { + return packet + } + let beforeReadIndex = buffer.getReadOffset() + + if (length > 0) { + buffer.setReadOffset(beforeReadIndex + length) + } + return packet + } +} \ No newline at end of file diff --git a/protocol/src/test/swift/zfooswift/packet/NormalObject.swift b/protocol/src/test/swift/zfooswift/packet/NormalObject.swift new file mode 100644 index 00000000..a8c38dfc --- /dev/null +++ b/protocol/src/test/swift/zfooswift/packet/NormalObject.swift @@ -0,0 +1,120 @@ +import Foundation + +// 常规的对象,取所有语言语法的交集,基本上所有语言都支持下面的语法 +class NormalObject : IProtocol { + var a: Int8 = 0 + var aaa: Array = [] + var b: Int16 = 0 + // 整数类型 + var c: Int = 0 + var d: Int64 = 0 + var e: Float32 = 0.0 + var f: Float64 = 0.0 + var g: Bool = false + var jj: String = "" + var kk: ObjectA? = nil + var l: Array = [] + var ll: Array = [] + var lll: Array = [] + var llll: Array = [] + var m: Dictionary = [:] + var mm: Dictionary = [:] + var s: Set = [] + var ssss: Set = [] + var outCompatibleValue: Int = 0 + var outCompatibleValue2: Int = 0 + + func protocolId() -> Int { + return 101 + } +} + +class NormalObjectRegistration : IProtocolRegistration { + func write(_ buffer: ByteBuffer, _ packet: Any?) { + if (packet == nil) { + buffer.writeInt(0) + return + } + let message = packet as! NormalObject + let beforeWriteIndex = buffer.getWriteOffset() + buffer.writeInt(857) + buffer.writeByte(message.a) + buffer.writeByteArray(message.aaa) + buffer.writeShort(message.b) + buffer.writeInt(message.c) + buffer.writeLong(message.d) + buffer.writeFloat(message.e) + buffer.writeDouble(message.f) + buffer.writeBool(message.g) + buffer.writeString(message.jj) + buffer.writePacket(message.kk, 102) + buffer.writeIntArray(message.l) + buffer.writeLongArray(message.ll) + buffer.writePacketArray(message.lll, 102) + buffer.writeStringArray(message.llll) + buffer.writeIntStringMap(message.m) + buffer.writeIntPacketMap(message.mm, 102) + buffer.writeIntSet(message.s) + buffer.writeStringSet(message.ssss) + buffer.writeInt(message.outCompatibleValue) + buffer.writeInt(message.outCompatibleValue2) + buffer.adjustPadding(857, beforeWriteIndex) + } + + func read(_ buffer: ByteBuffer) -> Any { + let length = buffer.readInt() + let packet = NormalObject() + if (length == 0) { + return packet + } + let beforeReadIndex = buffer.getReadOffset() + let result0 = buffer.readByte() + packet.a = result0 + let array1 = buffer.readByteArray() + packet.aaa = array1 + let result2 = buffer.readShort() + packet.b = result2 + let result3 = buffer.readInt() + packet.c = result3 + let result4 = buffer.readLong() + packet.d = result4 + let result5 = buffer.readFloat() + packet.e = result5 + let result6 = buffer.readDouble() + packet.f = result6 + let result7 = buffer.readBool() + packet.g = result7 + let result8 = buffer.readString() + packet.jj = result8 + let result9 = buffer.readPacket(102) as! ObjectA + packet.kk = result9 + let list10 = buffer.readIntArray() + packet.l = list10 + let list11 = buffer.readLongArray() + packet.ll = list11 + let list12 = buffer.readPacketArray(102) as! Array + packet.lll = list12 + let list13 = buffer.readStringArray() + packet.llll = list13 + let map14 = buffer.readIntStringMap() + packet.m = map14 + let map15 = buffer.readIntPacketMap(102) as! Dictionary + packet.mm = map15 + let set16 = buffer.readIntSet() + packet.s = set16 + let set17 = buffer.readStringSet() + packet.ssss = set17 + if (buffer.compatibleRead(beforeReadIndex, length)) { + let result18 = buffer.readInt() + packet.outCompatibleValue = result18 + } + if (buffer.compatibleRead(beforeReadIndex, length)) { + let result19 = buffer.readInt() + packet.outCompatibleValue2 = result19 + } + if (length > 0) { + buffer.setReadOffset(beforeReadIndex + length) + } + return packet + } +} \ No newline at end of file diff --git a/protocol/src/test/swift/zfooswift/packet/ObjectA.swift b/protocol/src/test/swift/zfooswift/packet/ObjectA.swift new file mode 100644 index 00000000..875dc0da --- /dev/null +++ b/protocol/src/test/swift/zfooswift/packet/ObjectA.swift @@ -0,0 +1,53 @@ +import Foundation + + +class ObjectA : IProtocol { + var a: Int = 0 + var m: Dictionary = [:] + var objectB: ObjectB? = nil + var innerCompatibleValue: Int = 0 + + func protocolId() -> Int { + return 102 + } +} + +class ObjectARegistration : IProtocolRegistration { + func write(_ buffer: ByteBuffer, _ packet: Any?) { + if (packet == nil) { + buffer.writeInt(0) + return + } + let message = packet as! ObjectA + let beforeWriteIndex = buffer.getWriteOffset() + buffer.writeInt(201) + buffer.writeInt(message.a) + buffer.writeIntStringMap(message.m) + buffer.writePacket(message.objectB, 103) + buffer.writeInt(message.innerCompatibleValue) + buffer.adjustPadding(201, beforeWriteIndex) + } + + func read(_ buffer: ByteBuffer) -> Any { + let length = buffer.readInt() + let packet = ObjectA() + if (length == 0) { + return packet + } + let beforeReadIndex = buffer.getReadOffset() + let result0 = buffer.readInt() + packet.a = result0 + let map1 = buffer.readIntStringMap() + packet.m = map1 + let result2 = buffer.readPacket(103) as! ObjectB + packet.objectB = result2 + if (buffer.compatibleRead(beforeReadIndex, length)) { + let result3 = buffer.readInt() + packet.innerCompatibleValue = result3 + } + if (length > 0) { + buffer.setReadOffset(beforeReadIndex + length) + } + return packet + } +} \ No newline at end of file diff --git a/protocol/src/test/swift/zfooswift/packet/ObjectB.swift b/protocol/src/test/swift/zfooswift/packet/ObjectB.swift new file mode 100644 index 00000000..138d10ae --- /dev/null +++ b/protocol/src/test/swift/zfooswift/packet/ObjectB.swift @@ -0,0 +1,45 @@ +import Foundation + + +class ObjectB : IProtocol { + var flag: Bool = false + var innerCompatibleValue: Int = 0 + + func protocolId() -> Int { + return 103 + } +} + +class ObjectBRegistration : IProtocolRegistration { + func write(_ buffer: ByteBuffer, _ packet: Any?) { + if (packet == nil) { + buffer.writeInt(0) + return + } + let message = packet as! ObjectB + let beforeWriteIndex = buffer.getWriteOffset() + buffer.writeInt(4) + buffer.writeBool(message.flag) + buffer.writeInt(message.innerCompatibleValue) + buffer.adjustPadding(4, beforeWriteIndex) + } + + func read(_ buffer: ByteBuffer) -> Any { + let length = buffer.readInt() + let packet = ObjectB() + if (length == 0) { + return packet + } + let beforeReadIndex = buffer.getReadOffset() + let result0 = buffer.readBool() + packet.flag = result0 + if (buffer.compatibleRead(beforeReadIndex, length)) { + let result1 = buffer.readInt() + packet.innerCompatibleValue = result1 + } + if (length > 0) { + buffer.setReadOffset(beforeReadIndex + length) + } + return packet + } +} \ No newline at end of file diff --git a/protocol/src/test/swift/zfooswift/packet/SimpleObject.swift b/protocol/src/test/swift/zfooswift/packet/SimpleObject.swift new file mode 100644 index 00000000..81c33240 --- /dev/null +++ b/protocol/src/test/swift/zfooswift/packet/SimpleObject.swift @@ -0,0 +1,41 @@ +import Foundation + + +class SimpleObject : IProtocol { + var c: Int = 0 + var g: Bool = false + + func protocolId() -> Int { + return 104 + } +} + +class SimpleObjectRegistration : IProtocolRegistration { + func write(_ buffer: ByteBuffer, _ packet: Any?) { + if (packet == nil) { + buffer.writeInt(0) + return + } + let message = packet as! SimpleObject + buffer.writeInt(-1) + buffer.writeInt(message.c) + buffer.writeBool(message.g) + } + + func read(_ buffer: ByteBuffer) -> Any { + let length = buffer.readInt() + let packet = SimpleObject() + if (length == 0) { + return packet + } + let beforeReadIndex = buffer.getReadOffset() + let result0 = buffer.readInt() + packet.c = result0 + let result1 = buffer.readBool() + packet.g = result1 + if (length > 0) { + buffer.setReadOffset(beforeReadIndex + length) + } + return packet + } +} \ No newline at end of file