Commit 2985fbce authored by Vincent Pelletier's avatar Vincent Pelletier

Avoid parsing packet header multiple times when there is not enough data.

git-svn-id: https://svn.erp5.org/repos/neo/trunk@2001 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent c1101e85
...@@ -20,7 +20,7 @@ from time import time ...@@ -20,7 +20,7 @@ from time import time
from neo import logging from neo import logging
from neo.locking import RLock from neo.locking import RLock
from neo.protocol import PacketMalformedError, Packets from neo.protocol import PacketMalformedError, Packets, ParserState
from neo.connector import ConnectorException, ConnectorTryAgainException, \ from neo.connector import ConnectorException, ConnectorTryAgainException, \
ConnectorInProgressException, ConnectorConnectionRefusedException, \ ConnectorInProgressException, ConnectorConnectionRefusedException, \
ConnectorConnectionClosedException ConnectorConnectionClosedException
...@@ -293,6 +293,7 @@ class Connection(BaseConnection): ...@@ -293,6 +293,7 @@ class Connection(BaseConnection):
self.uuid = None self.uuid = None
self._queue = [] self._queue = []
self._on_close = None self._on_close = None
self._parser_state = ParserState()
event_manager.addReader(self) event_manager.addReader(self)
def setOnClose(self, callback): def setOnClose(self, callback):
...@@ -359,7 +360,7 @@ class Connection(BaseConnection): ...@@ -359,7 +360,7 @@ class Connection(BaseConnection):
while True: while True:
# parse a packet # parse a packet
try: try:
packet = Packets.parse(self.read_buf) packet = Packets.parse(self.read_buf, self._parser_state)
if packet is None: if packet is None:
break break
except PacketMalformedError, msg: except PacketMalformedError, msg:
......
...@@ -1575,6 +1575,21 @@ def register(code, request, answer=None): ...@@ -1575,6 +1575,21 @@ def register(code, request, answer=None):
return (request, answer) return (request, answer)
return request return request
class ParserState(object):
"""
Parser internal state.
To be considered opaque datatype outside of PacketRegistry.parse .
"""
payload = None
def set(self, payload):
self.payload = payload
def get(self):
return self.payload
def clear(self):
self.payload = None
class PacketRegistry(dict): class PacketRegistry(dict):
""" """
...@@ -1586,28 +1601,32 @@ class PacketRegistry(dict): ...@@ -1586,28 +1601,32 @@ class PacketRegistry(dict):
# load packet classes # load packet classes
self.update(StaticRegistry) self.update(StaticRegistry)
def parse(self, buf): def parse(self, buf, state_container):
if len(buf) < PACKET_HEADER_SIZE: state = state_container.get()
return None if state is None:
header = buf.peek(PACKET_HEADER_SIZE) header = buf.read(PACKET_HEADER_SIZE)
assert header is not None if header is None:
msg_id, msg_type, msg_len = unpack(PACKET_HEADER_FORMAT, header) return None
try: msg_id, msg_type, msg_len = unpack(PACKET_HEADER_FORMAT, header)
packet_klass = self[msg_type] try:
except KeyError: packet_klass = self[msg_type]
raise PacketMalformedError('Unknown packet type') except KeyError:
if msg_len > MAX_PACKET_SIZE: raise PacketMalformedError('Unknown packet type')
raise PacketMalformedError('message too big (%d)' % msg_len) if msg_len > MAX_PACKET_SIZE:
if msg_len < MIN_PACKET_SIZE: raise PacketMalformedError('message too big (%d)' % msg_len)
raise PacketMalformedError('message too small (%d)' % msg_len) if msg_len < MIN_PACKET_SIZE:
if len(buf) < msg_len: raise PacketMalformedError('message too small (%d)' % msg_len)
msg_len -= PACKET_HEADER_SIZE
else:
msg_id, packet_klass, msg_len = state
data = buf.read(msg_len)
if data is None:
# Not enough. # Not enough.
state_container.set((msg_id, packet_klass, msg_len))
return None return None
buf.skip(PACKET_HEADER_SIZE) if state:
msg_len -= PACKET_HEADER_SIZE state_container.clear()
packet = packet_klass() packet = packet_klass()
data = buf.read(msg_len)
assert data is not None
packet.setContent(msg_id, data) packet.setContent(msg_id, data)
return packet return packet
......
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