本文整理汇总了Python中panda3d.core.QueuedConnectionReader.setRawMode方法的典型用法代码示例。如果您正苦于以下问题:Python QueuedConnectionReader.setRawMode方法的具体用法?Python QueuedConnectionReader.setRawMode怎么用?Python QueuedConnectionReader.setRawMode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类panda3d.core.QueuedConnectionReader
的用法示例。
在下文中一共展示了QueuedConnectionReader.setRawMode方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Connection
# 需要导入模块: from panda3d.core import QueuedConnectionReader [as 别名]
# 或者: from panda3d.core.QueuedConnectionReader import setRawMode [as 别名]
class Connection(GlobalClockMixin, TaskMixin):
def __init__(self, base, host, port):
self.base = base
self.host = host
self.port = port
self._conn = None
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()
#.........这里部分代码省略.........