private boolean listen()

in extension-base/src/main/java/com/azure/autorest/extension/base/jsonrpc/Connection.java [156:209]


    private boolean listen() {
        while (isAlive) {
            try {
                int ch = reader.peekByte();
                if (-1 == ch) {
                    // didn't get anything. start again, it'll know if we're shutting down
                    break;
                }

                if ('{' == ch || '[' == ch) {
                    // looks like a json block or array. let's do this.
                    // don't wait for this to finish!
                    process(readJson(), '{' == ch);

                    // we're done here, start again.
                    continue;
                }

                // We're looking at headers
                Map<String, String> headers = new HashMap<>();
                String line = reader.readAsciiLine();
                while (line != null && !line.isEmpty()) {
                    String[] bits = line.split(":", 2);
                    headers.put(bits[0].trim(), bits[1].trim());
                    line = reader.readAsciiLine();
                }

                ch = reader.peekByte();
                // the next character had better be a { or [
                if ('{' == ch || '[' == ch) {
                    String contentLengthStr = headers.get("Content-Length");
                    if (contentLengthStr != null && !contentLengthStr.isEmpty()) {
                        int contentLength = Integer.parseInt(contentLengthStr);
                        // don't wait for this to finish!
                        process(readJson(contentLength), '{' == ch);
                        continue;
                    }
                    // looks like a json block or array. let's do this.
                    // don't wait for this to finish!
                    process(readJson(), '{' == ch);
                    // we're done here, start again.
                    continue;
                }

                return false;

            } catch (Exception e) {
                if (!isAlive) {
                    throw new RuntimeException(e);
                }
            }
        }
        return false;
    }