Beispiel #1
0
    def test_by_value(self):
        cls = Cell.by_value(8)
        self.assertEqual('NETINFO', cls.NAME)
        self.assertEqual(8, cls.VALUE)
        self.assertEqual(True, cls.IS_FIXED_SIZE)

        self.assertRaises(ValueError, Cell.by_value, 'NOPE')
        self.assertRaises(ValueError, Cell.by_value, 85)
        self.assertRaises(ValueError, Cell.by_value, None)
Beispiel #2
0
    def _recv(self, raw=False):
        """
    Reads the next cell from our ORPort. If none is present this blocks
    until one is available.

    :param bool raw: provides bytes rather than parsing as a cell if **True**

    :returns: next :class:`~stem.client.cell.Cell`
    """

        with self._orport_lock:
            # cells begin with [circ_id][cell_type][...]

            circ_id_size = self.link_protocol.circ_id_size.size

            while len(self._orport_buffer) < (circ_id_size +
                                              CELL_TYPE_SIZE.size):
                self._orport_buffer += self._orport.recv(
                )  # read until we know the cell type

            cell_type = Cell.by_value(
                CELL_TYPE_SIZE.pop(self._orport_buffer[circ_id_size:])[0])

            if cell_type.IS_FIXED_SIZE:
                cell_size = circ_id_size + CELL_TYPE_SIZE.size + FIXED_PAYLOAD_LEN
            else:
                # variable length, our next field is the payload size

                while len(self._orport_buffer) < (circ_id_size +
                                                  CELL_TYPE_SIZE.size +
                                                  FIXED_PAYLOAD_LEN.size):
                    self._orport_buffer += self._orport.recv(
                    )  # read until we know the cell size

                payload_len = FIXED_PAYLOAD_LEN.pop(
                    self._orport_buffer[circ_id_size +
                                        CELL_TYPE_SIZE.size:])[0]
                cell_size = circ_id_size + CELL_TYPE_SIZE.size + FIXED_PAYLOAD_LEN.size + payload_len

            while len(self._orport_buffer) < cell_size:
                self._orport_buffer += self._orport.recv(
                )  # read until we have the full cell

            if raw:
                content, self._orport_buffer = split(self._orport_buffer,
                                                     cell_size)
                return content
            else:
                cell, self._orport_buffer = Cell.pop(self._orport_buffer,
                                                     self.link_protocol)
                return cell