Commit 3442b793 authored by Vincent Pelletier's avatar Vincent Pelletier

Change back packet buffers to lists.

git-svn-id: https://svn.erp5.org/repos/neo/trunk@1878 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 68d203be
...@@ -246,8 +246,8 @@ class Connection(BaseConnection): ...@@ -246,8 +246,8 @@ class Connection(BaseConnection):
def __init__(self, event_manager, handler, def __init__(self, event_manager, handler,
connector = None, addr = None, connector = None, addr = None,
connector_handler = None): connector_handler = None):
self.read_buf = "" self.read_buf = []
self.write_buf = "" self.write_buf = []
self.cur_id = 0 self.cur_id = 0
self.peer_id = 0 self.peer_id = 0
self.event_dict = {} self.event_dict = {}
...@@ -295,8 +295,8 @@ class Connection(BaseConnection): ...@@ -295,8 +295,8 @@ class Connection(BaseConnection):
self._on_close() self._on_close()
self._on_close = None self._on_close = None
self.event_dict.clear() self.event_dict.clear()
self.write_buf = "" del self.write_buf[:]
self.read_buf = "" del self.read_buf[:]
self._handlers.clear() self._handlers.clear()
def abort(self): def abort(self):
...@@ -324,16 +324,21 @@ class Connection(BaseConnection): ...@@ -324,16 +324,21 @@ class Connection(BaseConnection):
def analyse(self): def analyse(self):
"""Analyse received data.""" """Analyse received data."""
read_buf = self.read_buf
if len(read_buf) == 1:
msg = read_buf[0]
else:
msg = ''.join(self.read_buf)
while True: while True:
# parse a packet # parse a packet
try: try:
packet = Packets.parse(self.read_buf) packet = Packets.parse(msg)
if packet is None: if packet is None:
break break
except PacketMalformedError, msg: except PacketMalformedError, msg:
self.getHandler()._packetMalformed(self, msg) self.getHandler()._packetMalformed(self, msg)
return return
self.read_buf = self.read_buf[len(packet):] msg = msg[len(packet):]
packet_type = packet.getType() packet_type = packet.getType()
# Remove idle events, if appropriate packets were received. # Remove idle events, if appropriate packets were received.
...@@ -353,6 +358,7 @@ class Connection(BaseConnection): ...@@ -353,6 +358,7 @@ class Connection(BaseConnection):
# Skip PONG packets, its only purpose is to drop IdleEvent # Skip PONG packets, its only purpose is to drop IdleEvent
# generated upong ping. # generated upong ping.
self._queue.append(packet) self._queue.append(packet)
self.read_buf = [msg]
def hasPendingMessages(self): def hasPendingMessages(self):
""" """
...@@ -388,7 +394,7 @@ class Connection(BaseConnection): ...@@ -388,7 +394,7 @@ class Connection(BaseConnection):
logging.debug('Connection %r closed in recv', self.connector) logging.debug('Connection %r closed in recv', self.connector)
self._closure() self._closure()
return return
self.read_buf += data self.read_buf.append(data)
except ConnectorTryAgainException: except ConnectorTryAgainException:
pass pass
except ConnectorConnectionRefusedException: except ConnectorConnectionRefusedException:
...@@ -410,12 +416,16 @@ class Connection(BaseConnection): ...@@ -410,12 +416,16 @@ class Connection(BaseConnection):
if not self.write_buf: if not self.write_buf:
return return
try: try:
n = self.connector.send(self.write_buf) msg = ''.join(self.write_buf)
n = self.connector.send(msg)
if not n: if not n:
logging.debug('Connection %r closed in send', self.connector) logging.debug('Connection %r closed in send', self.connector)
self._closure() self._closure()
return return
self.write_buf = self.write_buf[n:] if n == len(msg):
del self.write_buf[:]
else:
self.write_buf = [msg[n:]]
except ConnectorTryAgainException: except ConnectorTryAgainException:
pass pass
except ConnectorConnectionClosedException: except ConnectorConnectionClosedException:
...@@ -436,7 +446,7 @@ class Connection(BaseConnection): ...@@ -436,7 +446,7 @@ class Connection(BaseConnection):
was_empty = not bool(self.write_buf) was_empty = not bool(self.write_buf)
PACKET_LOGGER.dispatch(self, packet, ' to ') PACKET_LOGGER.dispatch(self, packet, ' to ')
self.write_buf += packet.encode() self.write_buf.extend(packet.encode())
if was_empty: if was_empty:
# enable polling for writing. # enable polling for writing.
......
...@@ -267,8 +267,8 @@ class Packet(object): ...@@ -267,8 +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(PACKET_HEADER_FORMAT, self.getId(), self._code, length) \ return (pack(PACKET_HEADER_FORMAT, self._id, self._code, length),
+ content content)
def __len__(self): def __len__(self):
return PACKET_HEADER_SIZE + len(self._body) return PACKET_HEADER_SIZE + len(self._body)
......
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