Пример #1
0
 def datagram_received(self, data, addr):
     try:
         packet = parse(data)
         if packet.blockid in self.acked:
             self.send_ack(packet.blockid, addr)
         else:
             if packet.blockid in self.ack_next:
                 self.acked.append(packet.blockid)
                 self.send_ack(packet.blockid, addr)
                 self.received.extend(packet.data)
                 if len(packet.data) < 512:
                     self._waiter.set_result(self.received)
                     self.transport.close()
             else:
                 if random.choice(["ack next", "don't"]) == "ack next":
                     self.ack_next.append(packet.blockid)
     except Exception as exc:
         self._waiter.set_exception(exc)
         self.transport.close()
Пример #2
0
def test_error_packets(pkt):
    data = bytes(pkt)
    assert packet.parse(data) == pkt
Пример #3
0
def test_ack_packets(pkt):
    ack = bytes(pkt)
    assert packet.parse(ack) == pkt
Пример #4
0
def test_request_packets(pkt):
    data = bytes(pkt)
    assert packet.parse(data) == pkt
Пример #5
0
def test_parse_invalid_packet(buf):
    with pytest.raises(ValueError):
        packet.parse(buf)
Пример #6
0
def test_parse_valid_packet(buf):
    assert packet.parse(buf)