jint ProfilerSocket::readMessage()

in jvmti-access/src/main/jni/ProfilerSocket.cpp [97:121]


        jint ProfilerSocket::readMessage(JNIEnv* jniEnv, jobject outputBuffer) {
            std::lock_guard<std::mutex> guard(mutex);
            if (state == nullptr) {
                return raiseExceptionAndReturn(jniEnv, -1, "Profiler socket has not been opened yet!");
            }
            if (outputBuffer == nullptr) {
                return raiseExceptionAndReturn(jniEnv, -1, "No profiler socket active!");
            }

            jsize arrayLen = jniEnv->GetDirectBufferCapacity(outputBuffer);
            uint8_t* output = static_cast<uint8_t*>(jniEnv->GetDirectBufferAddress(outputBuffer));
            if (output == nullptr || arrayLen == -1) {
                return raiseExceptionAndReturn(jniEnv, -1, "Provided bytebuffer is not a direct buffer");
            }

            int n = recv(state->socketFd, output, arrayLen, 0);
            if (n == -1) {
                if(errno == EAGAIN || errno == EWOULDBLOCK) {
                    return 0; //no data to read available
                } else {
                    return raiseExceptionAndReturn(jniEnv, -1, "Failed to read from socket, error code is ", errno);
                }
            }
            return n;
        }