mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-21 08:25:28 +00:00
test[py]: python protocol test
This commit is contained in:
@@ -93,8 +93,3 @@ class ByteBufferTestCase(TestCase):
|
||||
byteBuffer.writeString(str)
|
||||
self.assertEqual(byteBuffer.readString(), str)
|
||||
|
||||
charStr = "A"
|
||||
byteBuffer.writeChar(charStr)
|
||||
self.assertEqual(byteBuffer.readChar(), charStr)
|
||||
print("----------------------------------------------------------------")
|
||||
print_bytearray(byteBuffer.buffer)
|
||||
|
||||
@@ -7,20 +7,28 @@ from .packet import ObjectB
|
||||
from .packet import SimpleObject
|
||||
|
||||
protocols = {}
|
||||
protocolIdMap = {}
|
||||
|
||||
protocols[0] = EmptyObject.EmptyObject
|
||||
protocols[1] = VeryBigObject.VeryBigObject
|
||||
protocols[100] = ComplexObject.ComplexObject
|
||||
protocols[101] = NormalObject.NormalObject
|
||||
protocols[102] = ObjectA.ObjectA
|
||||
protocols[103] = ObjectB.ObjectB
|
||||
protocols[104] = SimpleObject.SimpleObject
|
||||
protocols[0] = EmptyObject.EmptyObjectRegistration
|
||||
protocolIdMap[EmptyObject.EmptyObject] = 0
|
||||
protocols[1] = VeryBigObject.VeryBigObjectRegistration
|
||||
protocolIdMap[VeryBigObject.VeryBigObject] = 1
|
||||
protocols[100] = ComplexObject.ComplexObjectRegistration
|
||||
protocolIdMap[ComplexObject.ComplexObject] = 100
|
||||
protocols[101] = NormalObject.NormalObjectRegistration
|
||||
protocolIdMap[NormalObject.NormalObject] = 101
|
||||
protocols[102] = ObjectA.ObjectARegistration
|
||||
protocolIdMap[ObjectA.ObjectA] = 102
|
||||
protocols[103] = ObjectB.ObjectBRegistration
|
||||
protocolIdMap[ObjectB.ObjectB] = 103
|
||||
protocols[104] = SimpleObject.SimpleObjectRegistration
|
||||
protocolIdMap[SimpleObject.SimpleObject] = 104
|
||||
|
||||
def getProtocol(protocolId):
|
||||
return protocols[protocolId]
|
||||
|
||||
def write(buffer, packet):
|
||||
protocolId = packet.protocolId()
|
||||
protocolId = protocolIdMap[type(packet)]
|
||||
buffer.writeShort(protocolId)
|
||||
protocol = protocols[protocolId]
|
||||
protocol.write(buffer, packet)
|
||||
|
||||
@@ -53,8 +53,11 @@ class ComplexObject:
|
||||
# 如果要修改协议并且兼容老协议,需要加上Compatible注解,保持Compatible注解的value自增
|
||||
myCompatible = 0 # int
|
||||
myObject = None # ObjectA
|
||||
pass
|
||||
|
||||
def protocolId(self):
|
||||
class ComplexObjectRegistration:
|
||||
@classmethod
|
||||
def protocolId(cls, self):
|
||||
return 100
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
|
||||
class EmptyObject:
|
||||
|
||||
pass
|
||||
|
||||
def protocolId(self):
|
||||
class EmptyObjectRegistration:
|
||||
@classmethod
|
||||
def protocolId(cls, self):
|
||||
return 0
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
|
||||
# 常规的对象,取所有语言语法的交集,基本上所有语言都支持下面的语法
|
||||
class NormalObject:
|
||||
a = 0 # byte
|
||||
aaa = [] # byte[]
|
||||
b = 0 # short
|
||||
# 整数类型
|
||||
c = 0 # int
|
||||
d = 0 # long
|
||||
e = 0.0 # float
|
||||
@@ -20,8 +21,11 @@ class NormalObject:
|
||||
ssss = {} # HashSet<string>
|
||||
outCompatibleValue = 0 # int
|
||||
outCompatibleValue2 = 0 # int
|
||||
pass
|
||||
|
||||
def protocolId(self):
|
||||
class NormalObjectRegistration:
|
||||
@classmethod
|
||||
def protocolId(cls, self):
|
||||
return 101
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -4,8 +4,11 @@ class ObjectA:
|
||||
m = {} # Dictionary<int, string>
|
||||
objectB = None # ObjectB
|
||||
innerCompatibleValue = 0 # int
|
||||
pass
|
||||
|
||||
def protocolId(self):
|
||||
class ObjectARegistration:
|
||||
@classmethod
|
||||
def protocolId(cls, self):
|
||||
return 102
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -2,8 +2,11 @@
|
||||
class ObjectB:
|
||||
flag = False # bool
|
||||
innerCompatibleValue = 0 # int
|
||||
pass
|
||||
|
||||
def protocolId(self):
|
||||
class ObjectBRegistration:
|
||||
@classmethod
|
||||
def protocolId(cls, self):
|
||||
return 103
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -2,8 +2,11 @@
|
||||
class SimpleObject:
|
||||
c = 0 # int
|
||||
g = False # bool
|
||||
pass
|
||||
|
||||
def protocolId(self):
|
||||
class SimpleObjectRegistration:
|
||||
@classmethod
|
||||
def protocolId(cls, self):
|
||||
return 104
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -3344,8 +3344,11 @@ class VeryBigObject:
|
||||
mm88 = {} # Dictionary<int, ObjectA>
|
||||
s88 = {} # HashSet<int>
|
||||
ssss88 = {} # HashSet<string>
|
||||
pass
|
||||
|
||||
def protocolId(self):
|
||||
class VeryBigObjectRegistration:
|
||||
@classmethod
|
||||
def protocolId(cls, self):
|
||||
return 1
|
||||
|
||||
@classmethod
|
||||
|
||||
Reference in New Issue
Block a user