Pass only expected data to parseFromString

Spectrum2/backend.py:handleDataRead is called when transwhat receives
data from Spectrum. The parameter `data' contains multiple (or pieces
of) WrapperMessages. Instead of passing all the data to
wrapper.ParseFromString. We should only pass a single WrapperMessage.
This commit is contained in:
moyamo 2015-10-09 18:31:57 +02:00
parent 4497b2c5c9
commit 652a9f0463

View file

@ -3,6 +3,7 @@ import socket
import struct import struct
import sys import sys
import os import os
import logging
import google.protobuf import google.protobuf
@ -24,6 +25,7 @@ class SpectrumBackend:
self.m_pingReceived = False self.m_pingReceived = False
self.m_data = "" self.m_data = ""
self.m_init_res = 0 self.m_init_res = 0
self.logger = logging.getLogger(self.__class__.__name__)
def handleMessage(self, user, legacyName, msg, nickname = "", xhtml = "", timestamp = ""): def handleMessage(self, user, legacyName, msg, nickname = "", xhtml = "", timestamp = ""):
m = protocol_pb2.ConversationMessage() m = protocol_pb2.ConversationMessage()
@ -347,12 +349,20 @@ class SpectrumBackend:
else: else:
return return
packet = self.m_data[4:4+expected_size]
wrapper = protocol_pb2.WrapperMessage() wrapper = protocol_pb2.WrapperMessage()
if (wrapper.ParseFromString(self.m_data[4:]) == False): try:
parseFromString = wrapper.ParseFromString(packet)
except:
self.m_data = self.m_data[expected_size+4:] self.m_data = self.m_data[expected_size+4:]
self.logger.error("Parse from String exception")
return return
if parseFromString == False:
self.m_data = self.m_data[expected_size+4:]
self.logger.error("Parse from String failed")
return
self.m_data = self.m_data[4+expected_size:] self.m_data = self.m_data[4+expected_size:]
if wrapper.type == protocol_pb2.WrapperMessage.TYPE_LOGIN: if wrapper.type == protocol_pb2.WrapperMessage.TYPE_LOGIN: