def command_buffer(debugger):
    "Buffer input from a socket, yielding complete command packets."
    remainder = b''
    while True:
        new_buffer = debugger.socket.recv(1024)

        if not new_buffer:
            # If recv() returns None, the socket has closed
            break
        else:
            # print "CLIENT NEW BUFFER: %s >%s<" % (len(new_buffer), new_buffer[:50])
            if new_buffer[-1] == debugger.ETX:
                terminator = new_buffer[-1]
                full_buffer = remainder + new_buffer[:-1]
            else:
                terminator = None
                full_buffer = remainder + new_buffer

            messages = full_buffer.split(debugger.ETX)
            if terminator is None:
                remainder = messages.pop()
            else:
                remainder = ''
            for message in messages:
                # print "READ %s bytes" % len(message)
                message = message.decode('utf8')
                event, data = json.loads(message)

                if hasattr(debugger, 'on_%s' % event):
                    getattr(debugger, 'on_%s' % event)(**data)
                else:
                    print("Unknown server event:", event)

    # print "FINISH PROCESSING CLIENT COMMAND BUFFER"
dataset/ETHPy150Open pybee/bugjar/bugjar/connection.py/command_buffer/original