-
Notifications
You must be signed in to change notification settings - Fork 39
/
SocketOptions.java
135 lines (117 loc) · 3.54 KB
/
SocketOptions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
package software.amazon.awssdk.crt.io;
import software.amazon.awssdk.crt.CrtResource;
import software.amazon.awssdk.crt.CrtRuntimeException;
/**
* This class wraps the aws_socket_options from aws-c-io to provide
* access to TCP/UDP socket configuration in the AWS Common Runtime.
*/
public final class SocketOptions extends CrtResource {
/**
* Socket communications domain
*/
public enum SocketDomain {
/**
* Corresponds to PF_INET in Berkeley sockets
*/
IPv4(0),
/**
* Corresponds to PF_INET6 in Berkeley sockets
*/
IPv6(1),
/**
* Corresponds to PF_LOCAL in Berkeley sockets, usually UNIX domain sockets or named pipes
*/
LOCAL(2);
private int domain;
SocketDomain(int val) {
domain = val;
}
int getValue() {
return domain;
}
}
/**
* Socket type
*/
public enum SocketType {
/**
* Corresponds to SOCK_STREAM in Berkeley sockets (TCP)
*/
STREAM(0),
/**
* Corresponds to SOCK_DGRAM in Berkeley sockets (UDP)
*/
DGRAM(1);
private int type;
SocketType(int val) {
type = val;
}
int getValue() {
return type;
}
}
/**
* Sets the socket domain
*/
public SocketDomain domain = SocketDomain.IPv6;
/**
* Sets the socket type
*/
public SocketType type = SocketType.STREAM;
/**
* Sets the number of milliseconds before a connection will be considered timed out
*/
public int connectTimeoutMs = 3000;
/**
* Sets the number of seconds between TCP keepalive packets being sent to the peer
* 0 disables keepalive
*/
public int keepAliveIntervalSecs = 0;
/**
* Sets the number of seconds to wait for a keepalive response before considering the connection timed out
* 0 disables keepalive
*/
public int keepAliveTimeoutSecs = 0;
/**
* Creates a new set of socket options
*/
public SocketOptions() {
}
@Override
public long getNativeHandle() {
if (super.getNativeHandle() == 0) {
acquireNativeHandle(socketOptionsNew(
domain.getValue(),
type.getValue(),
connectTimeoutMs,
keepAliveIntervalSecs,
keepAliveTimeoutSecs
));
}
return super.getNativeHandle();
}
/**
* Determines whether a resource releases its dependencies at the same time the native handle is released or if it waits.
* Resources that wait are responsible for calling releaseReferences() manually.
*/
@Override
protected boolean canReleaseReferencesImmediately() { return true; }
/**
* Frees the native resources for this set of socket options
*/
@Override
protected void releaseNativeHandle() {
if (!isNull()) {
socketOptionsDestroy(getNativeHandle());
}
}
/*******************************************************************************
* native methods
******************************************************************************/
private static native long socketOptionsNew(int domain, int type, int connectTimeoutMs, int keepAliveIntervalSecs, int keepAliveTimeoutSecs);
private static native void socketOptionsDestroy(long elg);
};