Commit 9dea11df authored by Vincent Pelletier's avatar Vincent Pelletier

Un-hardcode packet header format. Add a check about its packed size.

git-svn-id: https://svn.erp5.org/repos/neo/trunk@1853 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 9ab70be0
...@@ -26,7 +26,13 @@ PROTOCOL_VERSION = (4, 1) ...@@ -26,7 +26,13 @@ PROTOCOL_VERSION = (4, 1)
# Size restrictions. # Size restrictions.
MIN_PACKET_SIZE = 10 MIN_PACKET_SIZE = 10
MAX_PACKET_SIZE = 0x4000000 MAX_PACKET_SIZE = 0x4000000
PACKET_HEADER_SIZE = 10 PACKET_HEADER_FORMAT = '!LHL'
PACKET_HEADER_SIZE = calcsize(PACKET_HEADER_FORMAT)
# Check that header size is the expected value.
# If it is not, it means that struct module result is incompatible with
# "reference" platform (python 2.4 on x86-64).
assert PACKET_HEADER_SIZE == 10, \
'Unsupported platform, packet header length = %i' % (PACKET_HEADER_SIZE, )
RESPONSE_MASK = 0x8000 RESPONSE_MASK = 0x8000
class ErrorCodes(Enum): class ErrorCodes(Enum):
...@@ -261,7 +267,8 @@ class Packet(object): ...@@ -261,7 +267,8 @@ class Packet(object):
""" Encode a packet as a string to send it over the network """ """ Encode a packet as a string to send it over the network """
content = self._body content = self._body
length = PACKET_HEADER_SIZE + len(content) length = PACKET_HEADER_SIZE + len(content)
return pack('!LHL', self.getId(), self._code, length) + content return pack(PACKET_HEADER_FORMAT, self.getId(), self._code, length) \
+ content
def __len__(self): def __len__(self):
return PACKET_HEADER_SIZE + len(self._body) return PACKET_HEADER_SIZE + len(self._body)
...@@ -1510,7 +1517,8 @@ class PacketRegistry(dict): ...@@ -1510,7 +1517,8 @@ class PacketRegistry(dict):
def parse(self, msg): def parse(self, msg):
if len(msg) < MIN_PACKET_SIZE: if len(msg) < MIN_PACKET_SIZE:
return None return None
msg_id, msg_type, msg_len = unpack('!LHL', msg[:PACKET_HEADER_SIZE]) msg_id, msg_type, msg_len = unpack(PACKET_HEADER_FORMAT,
msg[:PACKET_HEADER_SIZE])
try: try:
packet_klass = self[msg_type] packet_klass = self[msg_type]
except KeyError: except KeyError:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment