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


Python Stream.set_on_closed方法代码示例

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


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

示例1: _on_decode_error

# 需要导入模块: from stream import Stream [as 别名]
# 或者: from stream.Stream import set_on_closed [as 别名]
    def _on_decode_error(self, received):
        self._disable_heartbeat()
        self._stream._encoders = []
        backend = Stream(prefix="SIMPLE")

        def tunnel_ready_to_send(_):
            backend.start_receiving()

        def tunnel_send_buffer_full(_):
            backend.stop_receiving()

        def tunnel_received(_, data, _addr):
            backend.send(data)
            return backend.is_ready_to_send()

        def tunnel_closed(_):
            backend.close()

        def backend_received(_, data, _addr):
            self._stream.send(data)
            return self._stream.is_ready_to_send()

        def backend_closed(_self):
            self._stream.close()

        self._stream.set_on_ready_to_send(tunnel_ready_to_send)
        self._stream.set_on_send_buffer_full(tunnel_send_buffer_full)
        self._stream.set_on_received(tunnel_received)
        self._stream.set_on_closed(tunnel_closed)
        backend.set_on_received(backend_received)
        backend.set_on_closed(backend_closed)
        if received is not None and len(received) > 0:
            backend.send(received)
        backend.connect(UNKNOWN_CONN_ADDR, UNKNOWN_CONN_PORT)
开发者ID:binasc,项目名称:utils,代码行数:36,代码来源:tunnel.py

示例2: Tunnel

# 需要导入模块: from stream import Stream [as 别名]
# 或者: from stream.Stream import set_on_closed [as 别名]

#.........这里部分代码省略.........
    def _disable_heartbeat(self):
        if self._hb_event is not None:
            self._hb_event.del_timer()
            self._hb_event = None

    def _on_fin_received(self):
        self._disable_heartbeat()
        self._stream.close()

    def initialize(self):
        if self._stream is None:
            self._stream = Stream(prefix='TUNNEL')

        # self._stream.set_buffer_size(BUFF_SIZE)
        self._stream.set_tcp_no_delay()

        self._stream.append_send_handler(obscure.pack_data)
        self._stream.append_send_handler(obscure.random_padding)
        # self._stream.append_send_handler(obscure.gen_aes_encrypt())
        self._stream.append_send_handler(obscure.gen_xor_encrypt())
        # self._stream.append_send_handler(obscure.base64_encode)
        self._stream.append_send_handler(obscure.gen_http_encode(self._connect_to is not None))
        self._stream.append_receive_handler(obscure.gen_http_decode(self._connect_to is not None))
        # self._stream.append_receive_handler(obscure.base64_decode)
        self._stream.append_receive_handler(obscure.gen_xor_decrypt())
        # self._stream.append_receive_handler(obscure.gen_aes_decrypt())
        self._stream.append_receive_handler(obscure.unpad_random)
        self._stream.append_receive_handler(obscure.unpack_data)

        self._stream.set_on_ready_to_send(lambda _: self._on_tunnel_ready_to_send())
        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()
开发者ID:binasc,项目名称:utils,代码行数:69,代码来源:tunnel.py


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