Pymet fix transport automatic roll over

bug/bundler_fix
Spencer McIntyre 2015-07-14 15:18:11 -04:00
parent 00da619556
commit 18cb55f1fa
1 changed files with 28 additions and 23 deletions

View File

@ -469,9 +469,10 @@ class Transport(object):
pkt = self._get_packet() pkt = self._get_packet()
except: except:
return None return None
if pkt is None:
return None
self.communication_last = time.time() self.communication_last = time.time()
if pkt: self.communication_active = True
self.communication_active = True
return pkt return pkt
def send_packet(self, pkt): def send_packet(self, pkt):
@ -536,11 +537,13 @@ class HttpTransport(Transport):
request = urllib.Request(self.url, bytes('RECV', 'UTF-8'), self._http_request_headers) request = urllib.Request(self.url, bytes('RECV', 'UTF-8'), self._http_request_headers)
url_h = urllib.urlopen(request, timeout=self.communication_timeout) url_h = urllib.urlopen(request, timeout=self.communication_timeout)
packet = url_h.read() packet = url_h.read()
if not packet or len(packet) < 8: if packet == '':
return None return ''
if len(packet) < 8:
return None # looks corrupt
pkt_length, _ = struct.unpack('>II', packet[:8]) pkt_length, _ = struct.unpack('>II', packet[:8])
if len(packet) != pkt_length: if len(packet) != pkt_length:
return None return None # looks corrupt
return packet[8:] return packet[8:]
def _send_packet(self, packet): def _send_packet(self, packet):
@ -609,26 +612,28 @@ class TcpTransport(Transport):
self.socket = None self.socket = None
def _get_packet(self): def _get_packet(self):
packet = None
first = self._first_packet first = self._first_packet
self._first_packet = False self._first_packet = False
if select.select([self.socket], [], [], 0.5)[0]: if not select.select([self.socket], [], [], 0.5)[0]:
packet = self.socket.recv(8) return ''
if len(packet) != 8: packet = self.socket.recv(8)
if first and len(packet) == 4: if packet == '': # remote is closed
received = 0 return None
pkt_length = struct.unpack('>I', packet)[0] if len(packet) != 8:
self.socket.settimeout(max(self.communication_timeout, 30)) if first and len(packet) == 4:
while received < pkt_length: received = 0
received += len(self.socket.recv(pkt_length - received)) pkt_length = struct.unpack('>I', packet)[0]
self.socket.settimeout(None) self.socket.settimeout(max(self.communication_timeout, 30))
return self._get_packet() while received < pkt_length:
return None received += len(self.socket.recv(pkt_length - received))
pkt_length, pkt_type = struct.unpack('>II', packet) self.socket.settimeout(None)
pkt_length -= 8 return self._get_packet()
packet = bytes() return None
while len(packet) < pkt_length: pkt_length, pkt_type = struct.unpack('>II', packet)
packet += self.socket.recv(pkt_length - len(packet)) pkt_length -= 8
packet = bytes()
while len(packet) < pkt_length:
packet += self.socket.recv(pkt_length - len(packet))
return packet return packet
def _send_packet(self, packet): def _send_packet(self, packet):