in src/native/event_stream_message.c [93:167]
int aws_event_stream_rpc_marshall_message_args_init(
struct aws_event_stream_rpc_marshalled_message *message_args,
struct aws_allocator *allocator,
JNIEnv *env,
jbyteArray headers,
jbyteArray payload,
jbyteArray operation_name,
jint message_flags,
jint message_type) {
AWS_ZERO_STRUCT(*message_args);
message_args->allocator = allocator;
if (headers) {
if (aws_event_stream_headers_list_init(&message_args->headers_list, allocator)) {
aws_jni_throw_runtime_exception(env, "EventStreamRPCMessage: headers allocation failed.");
return AWS_OP_ERR;
}
message_args->headers_init = true;
struct aws_byte_cursor headers_cur = aws_jni_byte_cursor_from_jbyteArray_acquire(env, headers);
/* copy because JNI is stupid and the buffer that the headers parser runs from needs the memory to stick around
* until the final message creation happens. */
aws_byte_buf_init_copy_from_cursor(&message_args->headers_buf, allocator, headers_cur);
int headers_parse_error = aws_event_stream_read_headers_from_buffer(
&message_args->headers_list, message_args->headers_buf.buffer, message_args->headers_buf.len);
aws_jni_byte_cursor_from_jbyteArray_release(env, headers, headers_cur);
if (headers_parse_error) {
aws_jni_throw_runtime_exception(env, "EventStreamRPCMessage: headers allocation failed.");
goto clean_up;
}
}
if (payload) {
struct aws_byte_cursor payload_cur = aws_jni_byte_cursor_from_jbyteArray_acquire(env, payload);
aws_byte_buf_init_copy_from_cursor(&message_args->payload_buf, allocator, payload_cur);
aws_jni_byte_cursor_from_jbyteArray_release(env, payload, payload_cur);
if (!message_args->payload_buf.buffer) {
aws_jni_throw_runtime_exception(env, "EventStreamRPCMessage: allocation failed.");
goto clean_up;
}
}
message_args->message_args.message_type = message_type;
message_args->message_args.message_flags = message_flags;
message_args->message_args.headers = message_args->headers_list.data;
message_args->message_args.headers_count = message_args->headers_list.length;
message_args->message_args.payload = &message_args->payload_buf;
if (operation_name) {
struct aws_byte_cursor operation_cur = aws_jni_byte_cursor_from_jbyteArray_acquire(env, operation_name);
aws_byte_buf_init_copy_from_cursor(&message_args->operation_buf, allocator, operation_cur);
aws_jni_byte_cursor_from_jbyteArray_release(env, operation_name, operation_cur);
if (!message_args->operation_buf.buffer) {
aws_jni_throw_runtime_exception(env, "CEventStreamRPCMessage: allocation failed.");
goto clean_up;
}
}
return AWS_OP_SUCCESS;
clean_up:
aws_byte_buf_clean_up(&message_args->headers_buf);
aws_byte_buf_clean_up(&message_args->payload_buf);
aws_byte_buf_clean_up(&message_args->operation_buf);
if (message_args->headers_init) {
aws_event_stream_headers_list_cleanup(&message_args->headers_list);
}
return AWS_OP_ERR;
}