当前位置: 首页>>代码示例>>Python>>正文


Python QueuedConnectionReader.shutdown方法代码示例

本文整理汇总了Python中panda3d.core.QueuedConnectionReader.shutdown方法的典型用法代码示例。如果您正苦于以下问题:Python QueuedConnectionReader.shutdown方法的具体用法?Python QueuedConnectionReader.shutdown怎么用?Python QueuedConnectionReader.shutdown使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在panda3d.core.QueuedConnectionReader的用法示例。


在下文中一共展示了QueuedConnectionReader.shutdown方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Connection

# 需要导入模块: from panda3d.core import QueuedConnectionReader [as 别名]
# 或者: from panda3d.core.QueuedConnectionReader import shutdown [as 别名]

#.........这里部分代码省略.........
        self._retry_elapsed = 0
        self._retry_next = 0
        self._data_last_received = 0

        self.manager = QueuedConnectionManager()

        self.reader = QueuedConnectionReader(
            self.manager,
            0,  # number of threads
        )

        # we're using our own protocol so we don't want panda reading our headers and getting
        # all foobared by it (not setting raw mode causes `dataAvailable` to block
        # once there is actually data to process)
        self.reader.setRawMode(True)

        self.writer = ConnectionWriter(
            self.manager,
            0,  # number of threads
        )

    def connect(self, connected_callback, task):
        """
        Attempts to connect to the server and if unable to will retry adding
        a second to the wait time to each consecutive failure.
        """
        self._retry_elapsed += self.get_dt()
        if self._retry_elapsed < self._retry_next:
            return task.cont

        logging.debug(
            'attempting to connect to server at %s:%d',
            self.host,
            self.port
        )
        self._conn = self.manager.openTCPClientConnection(
            self.host,
            self.port,
            10000,  # timeout ms
        )

        if self._conn:
            logging.debug('connection established')
            self.reader.addConnection(self._conn)

            # reset the counters in case the connection drops and we have to restart the
            # connection process
            self._retry_elapsed = 0
            self._retry_next = 0

            # add a task to poll the connection for data
            self.add_task(
                self.task_read_polling,
                name='connection_reader_poll',
                sort=-39,
            )

            connected_callback()
            return

        # no connection so retry in a bit
        self._retry_elapsed = 0
        if self._retry_next == 0:
            self._retry_next = 1
        elif self._retry_next > 9:
            self._retry_next = 10
        else:
            self._retry_next += 1

        logging.error(
            'Unable to connect to server %s:%s, will retry in %d seconds',
            self.host,
            self.port,
            self._retry_next,
        )
        return task.cont

    def task_read_polling(self, task):
        if self.reader.dataAvailable():
            logging.debug('data available from server')
            datagram = Datagram()
            if self.reader.getData(datagram):
                logging.debug('received data from server: %s', datagram)
                logging.debug('received data from server: %s', datagram.getMessage())
                # TODO: provide a way to supply a data callback
            self._data_last_received = 0
        else:
            # no data received
            logging.debug('no data')
            self._data_last_received += self.get_dt()

        if self._data_last_received >= 10:
            logging.error('connection to server lost')
            return

        return task.cont

    def shutdown(self):
        logging.info('connection reader shutting down')
        self.reader.shutdown()
开发者ID:atatsu,项目名称:panda3d-fun,代码行数:104,代码来源:client.py


注:本文中的panda3d.core.QueuedConnectionReader.shutdown方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。