helpers.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:Pyrlang 作者: esl 项目源码 文件源码
def _handle_socket_read(handler, sock):
    collected = b''
    while True:
        # a full packet before calling on_packet in the handler class
        ready = select.select([sock], [], [], 0.0)
        try:
            if ready[0]:
                data = sock.recv(4096)
                # print("data in: %s" % hex_bytes(data))

                collected += data

                # Try and consume repeatedly if multiple messages arrived
                # in the same packet
                while True:
                    collected1 = handler.consume(collected)
                    if collected1 is None:
                        print("Protocol requested to disconnect the socket")
                        break
                    if collected1 == collected:
                        break  # could not consume any more

                    collected = collected1
            else:
                handler.handle_inbox()

        except select.error:
            # Disconnected probably or another error
            break

    sock.close()
    handler.on_connection_lost()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号