def receive(self): if len(self.buffer) > 0: return self.buffer.pop() targetTime = time.time() + (Connection.TIMEOUT * 2) while targetTime > time.time(): data, address = self.sock.recvfrom(Packet.MAX_PACKET_LENGTH) otherConnection = next((x for x in Connection.ActiveConnections if x.destAddress == address and x.destAddress != self.destAddress), None) if address == self.destAddress: if not Packet.checkIntegrity(data): return None return Packet.fromRawString(data) elif otherConnection != None: otherConnection.buffer.append(Packet.fromRawString(data)) else: #if address is not in active connections. Throw packet away continue return None
def test_checkIntegrity(self): self.assertTrue( Packet.checkIntegrity(TestPacket.packet.serialize()), "Simple Packet Failed Integrity Test" )