本文整理汇总了Python中stream.Stream.is_ready_to_send方法的典型用法代码示例。如果您正苦于以下问题:Python Stream.is_ready_to_send方法的具体用法?Python Stream.is_ready_to_send怎么用?Python Stream.is_ready_to_send使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stream.Stream
的用法示例。
在下文中一共展示了Stream.is_ready_to_send方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Tunnel
# 需要导入模块: from stream import Stream [as 别名]
# 或者: from stream.Stream import is_ready_to_send [as 别名]
#.........这里部分代码省略.........
self._stream.set_on_send_buffer_full(lambda _: self._on_tunnel_send_buffer_full())
self._stream.set_on_received(lambda _, data, addr: self._on_received(data, addr))
self._stream.set_on_fin_received(lambda _: self._on_fin_received())
self._stream.set_on_closed(lambda _: self._on_closed())
if self._connect_to is not None:
self._stream.connect(*self._connect_to)
else:
self._stream.set_on_decode_error(lambda _, received: self._on_decode_error(received))
self._stream.start_receiving()
self._enable_heartbeat()
def register(self, key, conn):
_logger.debug('%s, register: %s(%s)', str(self), str(key), str(conn))
assert(key not in self.connections)
self.connections[key] = conn
def deregister(self, key):
_logger.debug('%s, deregister(%s)', str(self), str(key))
if key in self.connections:
del self.connections[key]
def get_connection(self, key):
if key in self.connections:
return self.connections[key]
else:
_logger.debug('no such connection: %s', str(key))
return None
def clear_connections(self):
_logger.debug('%s, clear_connections (%d)', str(self), len(self.connections))
self.connections.clear()
def is_ready_to_send(self):
return self._stream.is_ready_to_send()
def _send_content(self, type_, id_, content):
if id_ is None:
to_send = struct.pack('!HI', type_, 0) + '\x00' * 16
else:
to_send = struct.pack('!HI', type_, len(content)) + id_.get_bytes() + content
self._stream.send(to_send)
def _on_tunnel_ready_to_send(self):
if self._on_ready_to_send is not None:
self._on_ready_to_send(self)
def _on_tunnel_send_buffer_full(self):
if self._on_send_buffer_full is not None:
self._on_send_buffer_full(self)
def _on_received(self, data, _addr):
_logger.debug("tunnel %s received %d bytes" % (str(self), len(data)))
if len(data) < 6 + 16:
raise Exception('corrupted data')
type_, content_length = struct.unpack('!HI', data[: 6])
id_ = uuid.UUID(bytes=data[6: 6 + 16])
if type_ not in self._handlers or self._handlers[type_] is None:
_logger.warning("tunnel message type %d can not be handled", type_)
if len(data) - 6 - 16 != content_length:
raise Exception('corrupted data')
self._handlers[type_](self, id_, data[6 + 16:])