Пример #1
0
 def listen(self) -> bytes:
     while True:
         packet = self.duplex.listen()
         if packet.syn:
             if self.state == TCPState.CLOSED:
                 self.duplex.send(Packet(b'', syn=True))
                 self.state = TCPState.SYN_SENT
                 logging.info('set to SYN_SENT')
             elif self.state == TCPState.SYN_SENT:
                 self.state = TCPState.ESTABLISHED
                 self.duplex.send(Packet(b'', syn=True))
                 logging.info('set to ESTABLISHED')
                 self.connection_established.release()
         else:
             return packet.data
Пример #2
0
 def __init__(self, socket: Socket):
     self.socket = socket
     self.receiver_expected_seq_num = 1
     self.receiver_send_packet = Packet(b'', ack=True, acknum=0)
     self.base = 1
     self.sender_next_seq_num = 1
     self.sender_send_packets: List[Optional[Packet]] = [None] * N
     self.send_lock = Lock()
     self.timer = Timer(self.timeout)
Пример #3
0
def spam(sender: Duplex):
    for d in [
            b'Line numero 1',
            b'Line numero 2',
            b'Line numero 3',
            b'Line numero 4',
            b'Line numero 5',
            b'Line numero 6',
            b'Line numero 7',
    ]:
        logging.info(f'sent data {d}')
        sender.send(Packet(d))
        sleep(1)
Пример #4
0
    def send(self, orig_packet: Packet) -> bool:
        if self.sender_next_seq_num < self.base + N:
            self.send_lock.acquire()
            orig_packet.seqnum = self.sender_next_seq_num
            self.sender_send_packets[self.sender_next_seq_num] = orig_packet
            self.socket.send(self.sender_send_packets[self.sender_next_seq_num])
            if not self.timer.is_running():
                self.timer.start_timer()
            self.sender_next_seq_num += 1

            self.send_lock.release()
        else:
            return False
        return True
Пример #5
0
    def listen(self) -> Packet:
        while True:
            packet = self.socket.listen()

            if packet.ack:
                for p in self.sender_send_packets:
                    if p:
                        if p.seqnum > packet.acknum:
                            break
                        p.is_acked = True
                        logging.debug(f'Got ack ({packet}) for {p}')
                if packet.acknum > self.base:
                    self.base = packet.acknum
                    if any(map(lambda p: p and p.is_acked, self.sender_send_packets)):
                        self.timer.start_timer()
            else:
                logging.debug(f'received packet {packet}, {self.receiver_expected_seq_num}')
                if packet.seqnum == self.receiver_expected_seq_num:
                    self.receiver_send_packet = Packet(b'', ack=True, acknum=self.receiver_expected_seq_num)
                    self.socket.send(self.receiver_send_packet)
                    self.receiver_expected_seq_num += 1
                    return packet
                else:
                    self.socket.send(self.receiver_send_packet)
Пример #6
0
def spam(sender: Duplex):
    for d in [b'kek', b'mem']:
        logging.info(f'sent data {d}')
        sender.send(Packet(d))
        sleep(1)
Пример #7
0
 def establish(self):
     self.duplex.send(Packet(b'', syn=True))
     self.state = TCPState.SYN_SENT
     self.connection_established.acquire()
     self.connection_established.release()
Пример #8
0
 def send(self, data: bytes) -> bool:
     if self.state == TCPState.ESTABLISHED:
         return self.duplex.send(Packet(data))
     return False