def receive_packet(self): data,addr = self.sock.recvfrom(self.BUFFER_SIZE) packet = RxPacket.deserialize(data) self.logger.debug("Received packet: %s" % str(packet)) '''Check if we have a non corrupt packet''' if packet.checksum != RxPacket.calculate_checksum(packet): self.logger.error("Corrupt Packet Detected, dropping packet %s", packet) return None # if packet contains an ACK, remove a waiting to be ACK'ed packet from the unacked list if RxPFlags.ACK in packet.flags: self.logger.debug("Waiting To be acked going to remove %s, these are the current keys: %s" % (packet.ack, self.waiting_to_be_acked.keys())) del self.waiting_to_be_acked[packet.ack] return packet
def send_packet(self, packet): packet.checksum = RxPacket.calculate_checksum(packet) self.logger.debug("Sending packet to %s at port %s: " % (packet.destinationip, packet.destport)) # set the packet send time packet.sent_time = time.time() # if the packet needs to be acked then it has to be added to the waiting to be acked list if any(flag in [RxPFlags.SYN, RxPFlags.FIN, RxPFlags.DATA] for flag in packet.flags): self.waiting_to_be_acked[packet.sequence] = packet # if the RETRY Thread is not running, kick it off if not self.RETRY_THREAD: t = Timer(self.RETRY_DELAY, self.resend_unacked_packets) t.setDaemon(True) t.start() self.RETRY_THREAD = True # Send packet over UDP self.sock.sendto(RxPacket.serialize(packet), (packet.destinationip, packet.destport)) self.logger.debug("Sent Packet, returning to calling function")
def test_receive_packet(self): test_packet = TestPacket([RxPFlags.DATA], 10, data=bytearray("Hello", 'utf-8')) test_packet.checksum = RxPacket.calculate_checksum(test_packet) self.communicator.sock.test_packet = RxPacket.serialize(test_packet) self.assertIsNotNone(self.communicator.receive_packet())