jint JNICALL Java_software_amazon_awssdk_crt_eventstream_ClientConnectionContinuation_activateContinuation()

in src/native/event_stream_rpc_client.c [464:545]


jint JNICALL Java_software_amazon_awssdk_crt_eventstream_ClientConnectionContinuation_activateContinuation(
    JNIEnv *env,
    jclass jni_class,
    jlong jni_continuation_ptr,
    jobject continuation,
    jbyteArray operation_name,
    jbyteArray headers,
    jbyteArray payload,
    jint message_type,
    jint message_flags,
    jobject callback) {
    (void)jni_class;

    struct aws_event_stream_rpc_client_continuation_token *continuation_token =
        (struct aws_event_stream_rpc_client_continuation_token *)jni_continuation_ptr;

    struct continuation_callback_data *continuation_callback_data =
        aws_event_stream_rpc_client_continuation_get_user_data(continuation_token);

    struct message_flush_callback_args *callback_data = NULL;

    int ret_val = AWS_OP_ERR;

    continuation_callback_data->java_continuation = (*env)->NewGlobalRef(env, continuation);

    if (!continuation_callback_data->java_continuation) {
        aws_jni_throw_runtime_exception(
            env, "ClientConnectionContinuation.activateContinuation: Unable to create reference");
        goto clean_up;
    }

    struct aws_event_stream_rpc_marshalled_message marshalled_message;
    if (aws_event_stream_rpc_marshall_message_args_init(
            &marshalled_message,
            aws_jni_get_allocator(),
            env,
            headers,
            payload,
            operation_name,
            message_flags,
            message_type)) {
        goto clean_up;
    }

    callback_data = aws_mem_calloc(aws_jni_get_allocator(), 1, sizeof(struct message_flush_callback_args));

    if (!callback_data) {
        aws_jni_throw_runtime_exception(env, "ClientConnectionContinuation.activateContinuation: allocation failed.");
        goto clean_up;
    }

    jint jvmresult = (*env)->GetJavaVM(env, &callback_data->jvm);
    if (jvmresult != 0) {
        aws_jni_throw_runtime_exception(env, "ClientConnectionContinuation.activateContinuation: Unable to get JVM");
        goto clean_up;
    }

    callback_data->callback = (*env)->NewGlobalRef(env, callback);

    struct aws_byte_cursor operation_cursor = aws_byte_cursor_from_buf(&marshalled_message.operation_buf);

    if (aws_event_stream_rpc_client_continuation_activate(
            continuation_token,
            operation_cursor,
            &marshalled_message.message_args,
            s_message_flush_fn,
            callback_data)) {
        aws_jni_throw_runtime_exception(env, "ClientConnectionContinuation.activateContinuation: send message failed");
        goto clean_up;
    }

    ret_val = AWS_OP_SUCCESS;

clean_up:
    aws_event_stream_rpc_marshall_message_args_clean_up(&marshalled_message);

    if (callback_data && ret_val) {
        (*env)->DeleteGlobalRef(env, callback_data->callback);
        aws_mem_release(aws_jni_get_allocator(), callback_data);
    }
    return ret_val;
}