Exemplo n.º 1
0
    def _get_file(self):
        print(f'Downloading File {self._file_name}')

        read_bytes = 0
        probes = 0
        with open(self._file_name, 'wb') as file:
            while read_bytes < self._file_size:
                try:
                    data = self._connection.recv(PACKET_SIZE)

                    self._connection.send(pk.dumps({'ACK': True}))

                    read_bytes += len(data)

                    progress(read_bytes, self._file_size, self._file_name)

                    if not data:
                        raise DisconnectedException

                    file.write(data)
                except socket.timeout:
                    probes += 1

                    if probes > MAX_PROBES:
                        raise socket.timeout

                    print(f'\nConnection timeout. Waiting for {probes} of {MAX_PROBES}')

                    continue
Exemplo n.º 2
0
    def _send_file(self, file_handle, file_size: int):
        print(f'Uploading File {self._file_name}')

        file_handle.seek(0)
        file_portion = file_handle.read(PACKET_SIZE)

        probes = 0
        sent_bytes = 0
        while file_portion:
            try:
                self._connection.send(file_portion)

                sent_bytes += len(file_portion)

                file_portion = file_handle.read(PACKET_SIZE)

                progress(sent_bytes, file_size)
            except socket.timeout:
                probes += 1

                if probes > MAX_PROBES:
                    raise socket.timeout

                print(
                    f'\nConnection timeout. Waiting for {probes} of {MAX_PROBES}'
                )

                continue
Exemplo n.º 3
0
    def _send_file(self, file_handle, file_size: int):
        print(f'Uploading File {self._file_name}')

        file_handle.seek(0)
        file_portion = file_handle.read(TCP_PACKET_SIZE)

        probes = 0
        sent_bytes = 0
        while file_portion:
            try:
                self._connection.send(file_portion)

                response = pk.loads(self._connection.recv(TCP_PACKET_SIZE))

                if 'ACK' not in response:
                    print('Server denied upload!')

                    raise FileNotFoundError

                sent_bytes += len(file_portion)

                file_portion = file_handle.read(TCP_PACKET_SIZE)

                progress(sent_bytes, file_size, self._file_name)
            except socket.timeout:
                probes += 1

                if probes > MAX_PROBES:
                    raise socket.timeout

                print(
                    f'\nConnection timeout. Waiting for {probes} of {MAX_PROBES}'
                )

                continue
Exemplo n.º 4
0
def send(sock: socket.socket, init_address: Tuple[str, int], packets: List[Packet], window_size: int = 0, show_progress: bool = False):
    if window_size == 0:
        window_size = len(packets) if len(packets) < WINDOWS_SIZE else WINDOWS_SIZE

    length = len(packets)
    timeout = 0.4
    sequence_number = 0
    unacknowledged_number = 0

    while sequence_number < length:
        try:
            ready = select.select([sock], [], [], timeout)
            if ready[0]:
                ack_data, address = sock.recvfrom(UDP_MAX_PACKET_SIZE)
                ack_data: Acknowledgment = pickle.loads(ack_data)
                if ack_data.ack_field == 0b1010101010101010:
                    if ack_data.sequence_number == sequence_number:
                        sequence_number += 1
                        unacknowledged_number -= 1
                        if show_progress:
                            progress(ack_data.sequence_number, length)
                    elif ack_data.sequence_number > sequence_number:
                        # in case client received part of the window
                        # but his ack was dropped (due to disconnect)
                        sequence_number = ack_data.sequence_number
                        unacknowledged_number = 0
                    else:
                        # window has probably been dropped
                        unacknowledged_number = 0
            else:
                # timed out
                unacknowledged_number = 0
            if unacknowledged_number < window_size and (unacknowledged_number + sequence_number) < length:
                sock.sendto(pickle.dumps(packets[unacknowledged_number + sequence_number]), init_address)
                unacknowledged_number += 1
                continue
        except socket.error:
            continue

    if show_progress:
        progress(length, length)
        print('\n')