async _sendAndReceive()

in aws-greengrass-core-sdk/stream-manager/client.js [353:392]


    async _sendAndReceive(operation, data) {
        if (this.#closed) {
            throw new exceptions.StreamManagerException('Client is closed and cannot be reopened');
        }

        if (data.requestId === null) {
            // eslint-disable-next-line no-param-reassign
            data.requestId = utilInternal.uuidv4();
        }

        const validation = utilInternal.isInvalid(data);
        if (validation) {
            throw new exceptions.ValidationException(validation);
        }

        // If we're not connected, immediately try to reconnect
        if (!this.#connected) {
            await this.__connect();
        }

        const promise = new Promise(((resolve, reject) => {
            this.#requestMap[data.requestId] = (result) => {
                // Drop async queue from request map
                delete this.#requestMap[data.requestId];
                if (result instanceof smData.MessageFrame && result.operation === smData.Operation.Unknown) {
                    reject(new exceptions.ClientException('Received response with unknown operation from server'));
                }

                resolve(result);
            };
        }));

        // Write request to socket
        const frame = new smData.MessageFrame(operation, cbor.encode(data.asMap()));
        const byteFrame = utilInternal.encodeFrame(frame);
        this.#socket.write(byteFrame.header);
        this.#socket.write(byteFrame.payload);

        return promise;
    }