Пример #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(sender_in, sender_out, receiver_in, receiver_out, p_rate):
    while True:
        ready, _, _ = select.select([sender_in, receiver_in], [], [])
        for sock in ready:
            raw_packet = sock.recv(2**16)
            if random.random() < p_rate:
                continue
            elif sock == sender_in:
                try:
                    receiver_out.send(raw_packet)
                except ConnectionRefusedError:
                    print('channel: lost connection to receiver')
                    return
                else:
                    print('sender->receiver:', Packet.from_bytes(raw_packet))
            elif sock == receiver_in:
                try:
                    sender_out.send(raw_packet)
                except ConnectionRefusedError:
                    print('channel: lost connection to sender')
                    return
                else:
                    print('receiver->sender:', Packet.from_bytes(raw_packet))
Пример #3
0
def loop(sender_in, sender_out, receiver_in, receiver_out, p_rate):
    while True:
        ready, _, _ = select.select([sender_in, receiver_in], [], [])
        for sock in ready:
            raw_packet = sock.recv(2**16)
            if random.random() < p_rate:
                continue
            elif sock == sender_in:
                try:
                    receiver_out.send(raw_packet)
                except ConnectionRefusedError:
                    print('channel: lost connection to receiver')
                    return
                else:
                    print('sender->receiver:', Packet.from_bytes(raw_packet))
            elif sock == receiver_in:
                try:
                    sender_out.send(raw_packet)
                except ConnectionRefusedError:
                    print('channel: lost connection to sender')
                    return
                else:
                    print('receiver->sender:', Packet.from_bytes(raw_packet))
Пример #4
0
    def proc_byte(self, c):
        if not self.buf:
            if c in [0x00, 0x80, 0x08, 0x88]:
                self.buf.append(c)
            return None
        elif len(self.buf) == 1:
            self.buf.append(c)
            self.packet_len = 4 + (self.buf[0] & 0x07) + self.buf[1]
            return None
        else:
            self.buf.append(c)

        if self.packet_len and len(self.buf) == self.packet_len:
            p = Packet(self.buf)
            self.buf = []
            return p
        return None
Пример #5
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