diff --git a/gdProtocol/ProtocolManager.gd b/gdProtocol/ProtocolManager.gd index b2aab51..0e08f01 100644 --- a/gdProtocol/ProtocolManager.gd +++ b/gdProtocol/ProtocolManager.gd @@ -10,8 +10,8 @@ static func getProtocol(protocolId: int): return protocols[protocolId] static func newInstance(protocolId: int): - var protocol = protocols[protocolId] - return protocol.new() + var protocol = protocols[protocolId] + return protocol.new() static func write(buffer, packet): var protocolId: int = packet.PROTOCOL_ID @@ -20,10 +20,10 @@ static func write(buffer, packet): protocol.write(buffer, packet) static func read(buffer): - var protocolId = buffer.readShort(); - var protocol = protocols[protocolId] - var packet = protocol.read(buffer); - return packet; + var protocolId = buffer.readShort(); + var protocol = protocols[protocolId] + var packet = protocol.read(buffer); + return packet; static func initProtocol(): protocols[100] = ComplexObject diff --git a/gdProtocol/buffer/ByteBuffer.gd b/gdProtocol/buffer/ByteBuffer.gd index f66cbfc..fea48a9 100644 --- a/gdProtocol/buffer/ByteBuffer.gd +++ b/gdProtocol/buffer/ByteBuffer.gd @@ -31,6 +31,9 @@ func setReadOffset(readIndex: int) -> void: func getReadOffset() -> int: return readOffset +func isReadable() -> bool: + return writeOffset > readOffset + # -------------------------------------------------write/read------------------------------------------------- func writePoolByteArray(value: PoolByteArray): var length = value.size() diff --git a/gdProtocol/packet/ComplexObject.gd b/gdProtocol/packet/ComplexObject.gd index ca35096..9ec6712 100644 --- a/gdProtocol/packet/ComplexObject.gd +++ b/gdProtocol/packet/ComplexObject.gd @@ -59,6 +59,9 @@ var ss # java.util.Set>> var sss # java.util.Set> var ssss # java.util.Set var sssss # java.util.Set> +# 如果要修改协议并且兼容老协议,需要加上Compatible注解,按照增加的顺序添加order +var myCompatible # int +var myObject # com.zfoo.protocol.packet.ObjectA const PROTOCOL_ID = 100 @@ -202,6 +205,8 @@ static func write(buffer, packet): buffer.writeInt(packet.sssss.size()) for element18 in packet.sssss: buffer.writeIntStringMap(element18) + buffer.writeInt(packet.myCompatible) + buffer.writePacket(packet.myObject, 102) static func read(buffer): @@ -393,4 +398,12 @@ static func read(buffer): var map120 = buffer.readIntStringMap() result117.append(map120) packet.sssss = result117 + if (!buffer.isReadable()): + return packet + var result121 = buffer.readInt() + packet.myCompatible = result121 + if (!buffer.isReadable()): + return packet + var result122 = buffer.readPacket(102) + packet.myObject = result122 return packet