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