Skip to content

Commit

Permalink
test[py]: python protocol test
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Jul 8, 2024
1 parent c3da20e commit 2b7b8b5
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 21 deletions.
5 changes: 0 additions & 5 deletions protocol/src/test/python/ProtocolTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,3 @@ def test_buffer(self):
byteBuffer.writeString(str)
self.assertEqual(byteBuffer.readString(), str)

charStr = "A"
byteBuffer.writeChar(charStr)
self.assertEqual(byteBuffer.readChar(), charStr)
print("----------------------------------------------------------------")
print_bytearray(byteBuffer.buffer)
24 changes: 16 additions & 8 deletions protocol/src/test/python/zfoopy/ProtocolManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,28 @@
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)
Expand Down
5 changes: 4 additions & 1 deletion protocol/src/test/python/zfoopy/packet/ComplexObject.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion protocol/src/test/python/zfoopy/packet/EmptyObject.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@

class EmptyObject:

pass

def protocolId(self):
class EmptyObjectRegistration:
@classmethod
def protocolId(cls, self):
return 0

@classmethod
Expand Down
8 changes: 6 additions & 2 deletions protocol/src/test/python/zfoopy/packet/NormalObject.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

# 常规的对象,取所有语言语法的交集,基本上所有语言都支持下面的语法
class NormalObject:
a = 0 # byte
aaa = [] # byte[]
b = 0 # short
# 整数类型
c = 0 # int
d = 0 # long
e = 0.0 # float
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion protocol/src/test/python/zfoopy/packet/ObjectA.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion protocol/src/test/python/zfoopy/packet/ObjectB.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion protocol/src/test/python/zfoopy/packet/SimpleObject.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion protocol/src/test/python/zfoopy/packet/VeryBigObject.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2b7b8b5

Please sign in to comment.