1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <jni.h>
7
8 #include <aws/io/socket.h>
9
10 #include "crt.h"
11 #include "java_class_ids.h"
12
13 /* on 32-bit platforms, casting pointers to longs throws a warning we don't need */
14 #if UINTPTR_MAX == 0xffffffff
15 # if defined(_MSC_VER)
16 # pragma warning(push)
17 # pragma warning(disable : 4305) /* 'type cast': truncation from 'jlong' to 'jni_tls_ctx_options *' */
18 # else
19 # pragma GCC diagnostic push
20 # pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
21 # pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
22 # endif
23 #endif
24
25 JNIEXPORT
Java_software_amazon_awssdk_crt_io_SocketOptions_socketOptionsNew(JNIEnv * env,jclass jni_class,jint domain,jint type,jint connect_timeout_ms,jint keep_alive_interval_secs,jint keep_alive_timeout_secs,jboolean keep_alive)26 jlong JNICALL Java_software_amazon_awssdk_crt_io_SocketOptions_socketOptionsNew(
27 JNIEnv *env,
28 jclass jni_class,
29 jint domain,
30 jint type,
31 jint connect_timeout_ms,
32 jint keep_alive_interval_secs,
33 jint keep_alive_timeout_secs,
34 jboolean keep_alive) {
35 (void)jni_class;
36 aws_cache_jni_ids(env);
37
38 struct aws_allocator *allocator = aws_jni_get_allocator();
39 struct aws_socket_options *options =
40 (struct aws_socket_options *)aws_mem_calloc(allocator, 1, sizeof(struct aws_socket_options));
41 AWS_FATAL_ASSERT(options);
42
43 options->domain = domain;
44 options->type = type;
45 options->connect_timeout_ms = connect_timeout_ms;
46 options->keep_alive_interval_sec = (short)keep_alive_interval_secs;
47 options->keep_alive_timeout_sec = (short)keep_alive_timeout_secs;
48 options->keepalive = keep_alive;
49
50 return (jlong)options;
51 }
52
53 JNIEXPORT
Java_software_amazon_awssdk_crt_io_SocketOptions_socketOptionsDestroy(JNIEnv * env,jclass jni_class,jlong jni_options)54 void JNICALL Java_software_amazon_awssdk_crt_io_SocketOptions_socketOptionsDestroy(
55 JNIEnv *env,
56 jclass jni_class,
57 jlong jni_options) {
58 (void)jni_class;
59 aws_cache_jni_ids(env);
60
61 struct aws_socket_options *options = (struct aws_socket_options *)jni_options;
62 if (!options) {
63 return;
64 }
65
66 struct aws_allocator *allocator = aws_jni_get_allocator();
67 aws_mem_release(allocator, options);
68 }
69
70 #if UINTPTR_MAX == 0xffffffff
71 # if defined(_MSC_VER)
72 # pragma warning(pop)
73 # else
74 # pragma GCC diagnostic pop
75 # endif
76 #endif
77