예제 #1
0
def loop(file_in, sock_in, sock_out):
    next_ = 0
    exit_flag = False
    while not exit_flag:
        data = file_in.read(BLOCK_SIZE)
        packet = Packet(PacketType.data, next_, data)
        exit_flag = not data
        processing = True
        while processing:
            try:
                sock_out.send(packet.to_bytes())
            except ConnectionRefusedError:
                print('sender: connection lost')
                return
            ready, _, _ = select.select([sock_in], [], [], TIMEOUT)
            if not ready:
                continue
            rcvd = sock_in.recv(2**16)
            try:
                exploded = Packet.from_bytes(rcvd)
            except ValueError:
                continue
            if exploded.type_ != PacketType.ack:
                continue
            if exploded.data:
                continue
            if exploded.seqno != next_:
                continue
            next_ = 1 - next_
            processing = False
예제 #2
0
def loop(file_out, sock_in, sock_out):
    expected = 0
    while True:
        raw_rcvd = sock_in.recv(2**16)
        try:
            rcvd = Packet.from_bytes(raw_rcvd)
        except ValueError:
            continue
        if rcvd.type_ != PacketType.data:
            continue
        if rcvd.seqno != expected:
            ack = Packet(PacketType.ack, rcvd.seqno, b'')
            sock_out.send(ack.to_bytes())
        else:
            ack = Packet(PacketType.ack, rcvd.seqno, b'')
            sock_out.send(ack.to_bytes())
            expected = 1 - expected
            if rcvd.data:
                file_out.write(rcvd.data)
            else:
                break