author | rriggs |
Fri, 01 Dec 2017 16:40:08 -0500 | |
changeset 48224 | be0df5ab3093 |
parent 47216 | 71c04702a3d5 |
child 48750 | ffbb784a8873 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
36115 | 2 |
* Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.nio.ch; |
|
27 |
||
28 |
import java.io.FileDescriptor; |
|
29 |
import java.io.IOException; |
|
30 |
import java.net.*; |
|
31 |
import java.nio.ByteBuffer; |
|
32 |
import java.nio.channels.*; |
|
33 |
import java.nio.channels.spi.*; |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
34 |
import java.util.*; |
2446 | 35 |
import sun.net.NetHooks; |
37676
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
36115
diff
changeset
|
36 |
import sun.net.ext.ExtendedSocketOptions; |
2 | 37 |
|
38 |
/** |
|
39 |
* An implementation of SocketChannels |
|
40 |
*/ |
|
41 |
||
42 |
class SocketChannelImpl |
|
43 |
extends SocketChannel |
|
44 |
implements SelChImpl |
|
45 |
{ |
|
46 |
||
47 |
// Used to make native read and write calls |
|
48 |
private static NativeDispatcher nd; |
|
49 |
||
50 |
// Our file descriptor object |
|
51 |
private final FileDescriptor fd; |
|
52 |
||
53 |
// fd value needed for dev/poll. This value will remain valid |
|
54 |
// even after the value in the file descriptor object has been set to -1 |
|
55 |
private final int fdVal; |
|
56 |
||
57 |
// IDs of native threads doing reads and writes, for signalling |
|
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
29986
diff
changeset
|
58 |
private volatile long readerThread; |
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
29986
diff
changeset
|
59 |
private volatile long writerThread; |
2 | 60 |
|
61 |
// Lock held by current reading or connecting thread |
|
62 |
private final Object readLock = new Object(); |
|
63 |
||
64 |
// Lock held by current writing or connecting thread |
|
65 |
private final Object writeLock = new Object(); |
|
66 |
||
67 |
// Lock held by any thread that modifies the state fields declared below |
|
68 |
// DO NOT invoke a blocking I/O operation while holding this lock! |
|
69 |
private final Object stateLock = new Object(); |
|
70 |
||
71 |
// -- The following fields are protected by stateLock |
|
72 |
||
18192 | 73 |
// set true when exclusive binding is on and SO_REUSEADDR is emulated |
74 |
private boolean isReuseAddress; |
|
75 |
||
2 | 76 |
// State, increases monotonically |
77 |
private static final int ST_UNINITIALIZED = -1; |
|
78 |
private static final int ST_UNCONNECTED = 0; |
|
79 |
private static final int ST_PENDING = 1; |
|
80 |
private static final int ST_CONNECTED = 2; |
|
81 |
private static final int ST_KILLPENDING = 3; |
|
82 |
private static final int ST_KILLED = 4; |
|
83 |
private int state = ST_UNINITIALIZED; |
|
84 |
||
85 |
// Binding |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
86 |
private InetSocketAddress localAddress; |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
87 |
private InetSocketAddress remoteAddress; |
2 | 88 |
|
89 |
// Input/Output open |
|
90 |
private boolean isInputOpen = true; |
|
91 |
private boolean isOutputOpen = true; |
|
92 |
private boolean readyToConnect = false; |
|
93 |
||
94 |
// Socket adaptor, created on demand |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
95 |
private Socket socket; |
2 | 96 |
|
97 |
// -- End of fields protected by stateLock |
|
98 |
||
99 |
||
100 |
// Constructor for normal connecting sockets |
|
101 |
// |
|
102 |
SocketChannelImpl(SelectorProvider sp) throws IOException { |
|
103 |
super(sp); |
|
104 |
this.fd = Net.socket(true); |
|
105 |
this.fdVal = IOUtil.fdVal(fd); |
|
106 |
this.state = ST_UNCONNECTED; |
|
107 |
} |
|
108 |
||
6525 | 109 |
SocketChannelImpl(SelectorProvider sp, |
110 |
FileDescriptor fd, |
|
111 |
boolean bound) |
|
112 |
throws IOException |
|
113 |
{ |
|
114 |
super(sp); |
|
115 |
this.fd = fd; |
|
116 |
this.fdVal = IOUtil.fdVal(fd); |
|
117 |
this.state = ST_UNCONNECTED; |
|
118 |
if (bound) |
|
119 |
this.localAddress = Net.localAddress(fd); |
|
120 |
} |
|
121 |
||
2 | 122 |
// Constructor for sockets obtained from server sockets |
123 |
// |
|
124 |
SocketChannelImpl(SelectorProvider sp, |
|
125 |
FileDescriptor fd, InetSocketAddress remote) |
|
126 |
throws IOException |
|
127 |
{ |
|
128 |
super(sp); |
|
129 |
this.fd = fd; |
|
130 |
this.fdVal = IOUtil.fdVal(fd); |
|
131 |
this.state = ST_CONNECTED; |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
132 |
this.localAddress = Net.localAddress(fd); |
2 | 133 |
this.remoteAddress = remote; |
134 |
} |
|
135 |
||
136 |
public Socket socket() { |
|
137 |
synchronized (stateLock) { |
|
138 |
if (socket == null) |
|
139 |
socket = SocketAdaptor.create(this); |
|
140 |
return socket; |
|
141 |
} |
|
142 |
} |
|
143 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
144 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
145 |
public SocketAddress getLocalAddress() throws IOException { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
146 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
147 |
if (!isOpen()) |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
148 |
throw new ClosedChannelException(); |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
149 |
return Net.getRevealedLocalAddress(localAddress); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
150 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
151 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
152 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
153 |
@Override |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
154 |
public SocketAddress getRemoteAddress() throws IOException { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
155 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
156 |
if (!isOpen()) |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
157 |
throw new ClosedChannelException(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
158 |
return remoteAddress; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
159 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
160 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
161 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
162 |
@Override |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
163 |
public <T> SocketChannel setOption(SocketOption<T> name, T value) |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
164 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
165 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
166 |
if (name == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
167 |
throw new NullPointerException(); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
168 |
if (!supportedOptions().contains(name)) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
169 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
170 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
171 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
172 |
if (!isOpen()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
173 |
throw new ClosedChannelException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
174 |
|
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
175 |
if (name == StandardSocketOptions.IP_TOS) { |
25170
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
176 |
ProtocolFamily family = Net.isIPv6Available() ? |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
177 |
StandardProtocolFamily.INET6 : StandardProtocolFamily.INET; |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
178 |
Net.setSocketOption(fd, family, name, value); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
179 |
return this; |
25170
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
180 |
} |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
181 |
|
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
182 |
if (name == StandardSocketOptions.SO_REUSEADDR && Net.useExclusiveBind()) { |
18192 | 183 |
// SO_REUSEADDR emulated when using exclusive bind |
184 |
isReuseAddress = (Boolean)value; |
|
185 |
return this; |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
186 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
187 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
188 |
// no options that require special handling |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
189 |
Net.setSocketOption(fd, Net.UNSPEC, name, value); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
190 |
return this; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
191 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
192 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
193 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
194 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
195 |
@SuppressWarnings("unchecked") |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
196 |
public <T> T getOption(SocketOption<T> name) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
197 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
198 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
199 |
if (name == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
200 |
throw new NullPointerException(); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
201 |
if (!supportedOptions().contains(name)) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
202 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
203 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
204 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
205 |
if (!isOpen()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
206 |
throw new ClosedChannelException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
207 |
|
18192 | 208 |
if (name == StandardSocketOptions.SO_REUSEADDR && |
209 |
Net.useExclusiveBind()) |
|
210 |
{ |
|
211 |
// SO_REUSEADDR emulated when using exclusive bind |
|
212 |
return (T)Boolean.valueOf(isReuseAddress); |
|
213 |
} |
|
214 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
215 |
// special handling for IP_TOS: always return 0 when IPv6 |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
216 |
if (name == StandardSocketOptions.IP_TOS) { |
25170
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
217 |
ProtocolFamily family = Net.isIPv6Available() ? |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
218 |
StandardProtocolFamily.INET6 : StandardProtocolFamily.INET; |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
219 |
return (T) Net.getSocketOption(fd, family, name); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
220 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
221 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
222 |
// no options that require special handling |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
223 |
return (T) Net.getSocketOption(fd, Net.UNSPEC, name); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
224 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
225 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
226 |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
227 |
private static class DefaultOptionsHolder { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
228 |
static final Set<SocketOption<?>> defaultOptions = defaultOptions(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
229 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
230 |
private static Set<SocketOption<?>> defaultOptions() { |
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
25859
diff
changeset
|
231 |
HashSet<SocketOption<?>> set = new HashSet<>(8); |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
232 |
set.add(StandardSocketOptions.SO_SNDBUF); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
233 |
set.add(StandardSocketOptions.SO_RCVBUF); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
234 |
set.add(StandardSocketOptions.SO_KEEPALIVE); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
235 |
set.add(StandardSocketOptions.SO_REUSEADDR); |
36115 | 236 |
if (Net.isReusePortAvailable()) { |
237 |
set.add(StandardSocketOptions.SO_REUSEPORT); |
|
238 |
} |
|
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
239 |
set.add(StandardSocketOptions.SO_LINGER); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
240 |
set.add(StandardSocketOptions.TCP_NODELAY); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
241 |
// additional options required by socket adaptor |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
242 |
set.add(StandardSocketOptions.IP_TOS); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
243 |
set.add(ExtendedSocketOption.SO_OOBINLINE); |
37676
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
36115
diff
changeset
|
244 |
ExtendedSocketOptions extendedOptions = |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
36115
diff
changeset
|
245 |
ExtendedSocketOptions.getInstance(); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
36115
diff
changeset
|
246 |
set.addAll(extendedOptions.options()); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
247 |
return Collections.unmodifiableSet(set); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
248 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
249 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
250 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
251 |
@Override |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
252 |
public final Set<SocketOption<?>> supportedOptions() { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
253 |
return DefaultOptionsHolder.defaultOptions; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
254 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
255 |
|
2 | 256 |
private boolean ensureReadOpen() throws ClosedChannelException { |
257 |
synchronized (stateLock) { |
|
258 |
if (!isOpen()) |
|
259 |
throw new ClosedChannelException(); |
|
260 |
if (!isConnected()) |
|
261 |
throw new NotYetConnectedException(); |
|
262 |
if (!isInputOpen) |
|
263 |
return false; |
|
264 |
else |
|
265 |
return true; |
|
266 |
} |
|
267 |
} |
|
268 |
||
269 |
private void ensureWriteOpen() throws ClosedChannelException { |
|
270 |
synchronized (stateLock) { |
|
271 |
if (!isOpen()) |
|
272 |
throw new ClosedChannelException(); |
|
273 |
if (!isOutputOpen) |
|
274 |
throw new ClosedChannelException(); |
|
275 |
if (!isConnected()) |
|
276 |
throw new NotYetConnectedException(); |
|
277 |
} |
|
278 |
} |
|
279 |
||
280 |
private void readerCleanup() throws IOException { |
|
281 |
synchronized (stateLock) { |
|
282 |
readerThread = 0; |
|
283 |
if (state == ST_KILLPENDING) |
|
284 |
kill(); |
|
285 |
} |
|
286 |
} |
|
287 |
||
288 |
private void writerCleanup() throws IOException { |
|
289 |
synchronized (stateLock) { |
|
290 |
writerThread = 0; |
|
291 |
if (state == ST_KILLPENDING) |
|
292 |
kill(); |
|
293 |
} |
|
294 |
} |
|
295 |
||
296 |
public int read(ByteBuffer buf) throws IOException { |
|
297 |
||
298 |
if (buf == null) |
|
299 |
throw new NullPointerException(); |
|
300 |
||
301 |
synchronized (readLock) { |
|
302 |
if (!ensureReadOpen()) |
|
303 |
return -1; |
|
304 |
int n = 0; |
|
305 |
try { |
|
306 |
||
307 |
// Set up the interruption machinery; see |
|
308 |
// AbstractInterruptibleChannel for details |
|
309 |
// |
|
310 |
begin(); |
|
311 |
||
312 |
synchronized (stateLock) { |
|
313 |
if (!isOpen()) { |
|
314 |
// Either the current thread is already interrupted, so |
|
315 |
// begin() closed the channel, or another thread closed the |
|
316 |
// channel since we checked it a few bytecodes ago. In |
|
317 |
// either case the value returned here is irrelevant since |
|
318 |
// the invocation of end() in the finally block will throw |
|
319 |
// an appropriate exception. |
|
320 |
// |
|
321 |
return 0; |
|
322 |
||
323 |
} |
|
324 |
||
325 |
// Save this thread so that it can be signalled on those |
|
326 |
// platforms that require it |
|
327 |
// |
|
328 |
readerThread = NativeThread.current(); |
|
329 |
} |
|
330 |
||
331 |
// Between the previous test of isOpen() and the return of the |
|
332 |
// IOUtil.read invocation below, this channel might be closed |
|
333 |
// or this thread might be interrupted. We rely upon the |
|
334 |
// implicit synchronization point in the kernel read() call to |
|
335 |
// make sure that the right thing happens. In either case the |
|
336 |
// implCloseSelectableChannel method is ultimately invoked in |
|
337 |
// some other thread, so there are three possibilities: |
|
338 |
// |
|
339 |
// - implCloseSelectableChannel() invokes nd.preClose() |
|
340 |
// before this thread invokes read(), in which case the |
|
341 |
// read returns immediately with either EOF or an error, |
|
342 |
// the latter of which will cause an IOException to be |
|
343 |
// thrown. |
|
344 |
// |
|
345 |
// - implCloseSelectableChannel() invokes nd.preClose() after |
|
346 |
// this thread is blocked in read(). On some operating |
|
347 |
// systems (e.g., Solaris and Windows) this causes the read |
|
348 |
// to return immediately with either EOF or an error |
|
349 |
// indication. |
|
350 |
// |
|
351 |
// - implCloseSelectableChannel() invokes nd.preClose() after |
|
352 |
// this thread is blocked in read() but the operating |
|
353 |
// system (e.g., Linux) doesn't support preemptive close, |
|
354 |
// so implCloseSelectableChannel() proceeds to signal this |
|
355 |
// thread, thereby causing the read to return immediately |
|
356 |
// with IOStatus.INTERRUPTED. |
|
357 |
// |
|
358 |
// In all three cases the invocation of end() in the finally |
|
359 |
// clause will notice that the channel has been closed and |
|
360 |
// throw an appropriate exception (AsynchronousCloseException |
|
361 |
// or ClosedByInterruptException) if necessary. |
|
362 |
// |
|
363 |
// *There is A fourth possibility. implCloseSelectableChannel() |
|
364 |
// invokes nd.preClose(), signals reader/writer thred and quickly |
|
365 |
// moves on to nd.close() in kill(), which does a real close. |
|
366 |
// Then a third thread accepts a new connection, opens file or |
|
367 |
// whatever that causes the released "fd" to be recycled. All |
|
368 |
// above happens just between our last isOpen() check and the |
|
369 |
// next kernel read reached, with the recycled "fd". The solution |
|
370 |
// is to postpone the real kill() if there is a reader or/and |
|
371 |
// writer thread(s) over there "waiting", leave the cleanup/kill |
|
372 |
// to the reader or writer thread. (the preClose() still happens |
|
373 |
// so the connection gets cut off as usual). |
|
374 |
// |
|
375 |
// For socket channels there is the additional wrinkle that |
|
376 |
// asynchronous shutdown works much like asynchronous close, |
|
377 |
// except that the channel is shutdown rather than completely |
|
378 |
// closed. This is analogous to the first two cases above, |
|
379 |
// except that the shutdown operation plays the role of |
|
380 |
// nd.preClose(). |
|
381 |
for (;;) { |
|
16921
e70261f11307
8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
14025
diff
changeset
|
382 |
n = IOUtil.read(fd, buf, -1, nd); |
2 | 383 |
if ((n == IOStatus.INTERRUPTED) && isOpen()) { |
384 |
// The system call was interrupted but the channel |
|
385 |
// is still open, so retry |
|
386 |
continue; |
|
387 |
} |
|
388 |
return IOStatus.normalize(n); |
|
389 |
} |
|
390 |
||
391 |
} finally { |
|
392 |
readerCleanup(); // Clear reader thread |
|
393 |
// The end method, which is defined in our superclass |
|
394 |
// AbstractInterruptibleChannel, resets the interruption |
|
395 |
// machinery. If its argument is true then it returns |
|
396 |
// normally; otherwise it checks the interrupt and open state |
|
397 |
// of this channel and throws an appropriate exception if |
|
398 |
// necessary. |
|
399 |
// |
|
400 |
// So, if we actually managed to do any I/O in the above try |
|
401 |
// block then we pass true to the end method. We also pass |
|
402 |
// true if the channel was in non-blocking mode when the I/O |
|
403 |
// operation was initiated but no data could be transferred; |
|
404 |
// this prevents spurious exceptions from being thrown in the |
|
405 |
// rare event that a channel is closed or a thread is |
|
406 |
// interrupted at the exact moment that a non-blocking I/O |
|
407 |
// request is made. |
|
408 |
// |
|
409 |
end(n > 0 || (n == IOStatus.UNAVAILABLE)); |
|
410 |
||
411 |
// Extra case for socket channels: Asynchronous shutdown |
|
412 |
// |
|
413 |
synchronized (stateLock) { |
|
414 |
if ((n <= 0) && (!isInputOpen)) |
|
415 |
return IOStatus.EOF; |
|
416 |
} |
|
417 |
||
418 |
assert IOStatus.check(n); |
|
419 |
||
420 |
} |
|
421 |
} |
|
422 |
} |
|
423 |
||
6301 | 424 |
public long read(ByteBuffer[] dsts, int offset, int length) |
425 |
throws IOException |
|
426 |
{ |
|
427 |
if ((offset < 0) || (length < 0) || (offset > dsts.length - length)) |
|
428 |
throw new IndexOutOfBoundsException(); |
|
2 | 429 |
synchronized (readLock) { |
430 |
if (!ensureReadOpen()) |
|
431 |
return -1; |
|
432 |
long n = 0; |
|
433 |
try { |
|
434 |
begin(); |
|
435 |
synchronized (stateLock) { |
|
436 |
if (!isOpen()) |
|
437 |
return 0; |
|
438 |
readerThread = NativeThread.current(); |
|
439 |
} |
|
440 |
||
441 |
for (;;) { |
|
6301 | 442 |
n = IOUtil.read(fd, dsts, offset, length, nd); |
2 | 443 |
if ((n == IOStatus.INTERRUPTED) && isOpen()) |
444 |
continue; |
|
445 |
return IOStatus.normalize(n); |
|
446 |
} |
|
447 |
} finally { |
|
448 |
readerCleanup(); |
|
449 |
end(n > 0 || (n == IOStatus.UNAVAILABLE)); |
|
450 |
synchronized (stateLock) { |
|
451 |
if ((n <= 0) && (!isInputOpen)) |
|
452 |
return IOStatus.EOF; |
|
453 |
} |
|
454 |
assert IOStatus.check(n); |
|
455 |
} |
|
456 |
} |
|
457 |
} |
|
458 |
||
459 |
public int write(ByteBuffer buf) throws IOException { |
|
460 |
if (buf == null) |
|
461 |
throw new NullPointerException(); |
|
462 |
synchronized (writeLock) { |
|
463 |
ensureWriteOpen(); |
|
464 |
int n = 0; |
|
465 |
try { |
|
466 |
begin(); |
|
467 |
synchronized (stateLock) { |
|
468 |
if (!isOpen()) |
|
469 |
return 0; |
|
470 |
writerThread = NativeThread.current(); |
|
471 |
} |
|
472 |
for (;;) { |
|
16921
e70261f11307
8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
14025
diff
changeset
|
473 |
n = IOUtil.write(fd, buf, -1, nd); |
2 | 474 |
if ((n == IOStatus.INTERRUPTED) && isOpen()) |
475 |
continue; |
|
476 |
return IOStatus.normalize(n); |
|
477 |
} |
|
478 |
} finally { |
|
479 |
writerCleanup(); |
|
480 |
end(n > 0 || (n == IOStatus.UNAVAILABLE)); |
|
481 |
synchronized (stateLock) { |
|
482 |
if ((n <= 0) && (!isOutputOpen)) |
|
483 |
throw new AsynchronousCloseException(); |
|
484 |
} |
|
485 |
assert IOStatus.check(n); |
|
486 |
} |
|
487 |
} |
|
488 |
} |
|
489 |
||
6301 | 490 |
public long write(ByteBuffer[] srcs, int offset, int length) |
491 |
throws IOException |
|
492 |
{ |
|
493 |
if ((offset < 0) || (length < 0) || (offset > srcs.length - length)) |
|
494 |
throw new IndexOutOfBoundsException(); |
|
2 | 495 |
synchronized (writeLock) { |
496 |
ensureWriteOpen(); |
|
497 |
long n = 0; |
|
498 |
try { |
|
499 |
begin(); |
|
500 |
synchronized (stateLock) { |
|
501 |
if (!isOpen()) |
|
502 |
return 0; |
|
503 |
writerThread = NativeThread.current(); |
|
504 |
} |
|
505 |
for (;;) { |
|
6301 | 506 |
n = IOUtil.write(fd, srcs, offset, length, nd); |
2 | 507 |
if ((n == IOStatus.INTERRUPTED) && isOpen()) |
508 |
continue; |
|
509 |
return IOStatus.normalize(n); |
|
510 |
} |
|
511 |
} finally { |
|
512 |
writerCleanup(); |
|
513 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
514 |
synchronized (stateLock) { |
|
515 |
if ((n <= 0) && (!isOutputOpen)) |
|
516 |
throw new AsynchronousCloseException(); |
|
517 |
} |
|
518 |
assert IOStatus.check(n); |
|
519 |
} |
|
520 |
} |
|
521 |
} |
|
522 |
||
6117
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
523 |
// package-private |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
524 |
int sendOutOfBandData(byte b) throws IOException { |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
525 |
synchronized (writeLock) { |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
526 |
ensureWriteOpen(); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
527 |
int n = 0; |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
528 |
try { |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
529 |
begin(); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
530 |
synchronized (stateLock) { |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
531 |
if (!isOpen()) |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
532 |
return 0; |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
533 |
writerThread = NativeThread.current(); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
534 |
} |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
535 |
for (;;) { |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
536 |
n = sendOutOfBandData(fd, b); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
537 |
if ((n == IOStatus.INTERRUPTED) && isOpen()) |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
538 |
continue; |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
539 |
return IOStatus.normalize(n); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
540 |
} |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
541 |
} finally { |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
542 |
writerCleanup(); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
543 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
544 |
synchronized (stateLock) { |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
545 |
if ((n <= 0) && (!isOutputOpen)) |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
546 |
throw new AsynchronousCloseException(); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
547 |
} |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
548 |
assert IOStatus.check(n); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
549 |
} |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
550 |
} |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
551 |
} |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
552 |
|
2 | 553 |
protected void implConfigureBlocking(boolean block) throws IOException { |
554 |
IOUtil.configureBlocking(fd, block); |
|
555 |
} |
|
556 |
||
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
557 |
public InetSocketAddress localAddress() { |
2 | 558 |
synchronized (stateLock) { |
559 |
return localAddress; |
|
560 |
} |
|
561 |
} |
|
562 |
||
563 |
public SocketAddress remoteAddress() { |
|
564 |
synchronized (stateLock) { |
|
565 |
return remoteAddress; |
|
566 |
} |
|
567 |
} |
|
568 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
569 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
570 |
public SocketChannel bind(SocketAddress local) throws IOException { |
2 | 571 |
synchronized (readLock) { |
572 |
synchronized (writeLock) { |
|
573 |
synchronized (stateLock) { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
574 |
if (!isOpen()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
575 |
throw new ClosedChannelException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
576 |
if (state == ST_PENDING) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
577 |
throw new ConnectionPendingException(); |
2 | 578 |
if (localAddress != null) |
579 |
throw new AlreadyBoundException(); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
580 |
InetSocketAddress isa = (local == null) ? |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
581 |
new InetSocketAddress(0) : Net.checkAddress(local); |
22339 | 582 |
SecurityManager sm = System.getSecurityManager(); |
583 |
if (sm != null) { |
|
584 |
sm.checkListen(isa.getPort()); |
|
585 |
} |
|
2446 | 586 |
NetHooks.beforeTcpBind(fd, isa.getAddress(), isa.getPort()); |
2 | 587 |
Net.bind(fd, isa.getAddress(), isa.getPort()); |
588 |
localAddress = Net.localAddress(fd); |
|
589 |
} |
|
590 |
} |
|
591 |
} |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
592 |
return this; |
2 | 593 |
} |
594 |
||
595 |
public boolean isConnected() { |
|
596 |
synchronized (stateLock) { |
|
597 |
return (state == ST_CONNECTED); |
|
598 |
} |
|
599 |
} |
|
600 |
||
601 |
public boolean isConnectionPending() { |
|
602 |
synchronized (stateLock) { |
|
603 |
return (state == ST_PENDING); |
|
604 |
} |
|
605 |
} |
|
606 |
||
607 |
void ensureOpenAndUnconnected() throws IOException { // package-private |
|
608 |
synchronized (stateLock) { |
|
609 |
if (!isOpen()) |
|
610 |
throw new ClosedChannelException(); |
|
611 |
if (state == ST_CONNECTED) |
|
612 |
throw new AlreadyConnectedException(); |
|
613 |
if (state == ST_PENDING) |
|
614 |
throw new ConnectionPendingException(); |
|
615 |
} |
|
616 |
} |
|
617 |
||
618 |
public boolean connect(SocketAddress sa) throws IOException { |
|
619 |
synchronized (readLock) { |
|
620 |
synchronized (writeLock) { |
|
621 |
ensureOpenAndUnconnected(); |
|
622 |
InetSocketAddress isa = Net.checkAddress(sa); |
|
623 |
SecurityManager sm = System.getSecurityManager(); |
|
624 |
if (sm != null) |
|
625 |
sm.checkConnect(isa.getAddress().getHostAddress(), |
|
626 |
isa.getPort()); |
|
627 |
synchronized (blockingLock()) { |
|
628 |
int n = 0; |
|
629 |
try { |
|
630 |
try { |
|
631 |
begin(); |
|
632 |
synchronized (stateLock) { |
|
633 |
if (!isOpen()) { |
|
634 |
return false; |
|
635 |
} |
|
2446 | 636 |
// notify hook only if unbound |
637 |
if (localAddress == null) { |
|
638 |
NetHooks.beforeTcpConnect(fd, |
|
639 |
isa.getAddress(), |
|
640 |
isa.getPort()); |
|
641 |
} |
|
2 | 642 |
readerThread = NativeThread.current(); |
643 |
} |
|
644 |
for (;;) { |
|
645 |
InetAddress ia = isa.getAddress(); |
|
646 |
if (ia.isAnyLocalAddress()) |
|
647 |
ia = InetAddress.getLocalHost(); |
|
648 |
n = Net.connect(fd, |
|
649 |
ia, |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
650 |
isa.getPort()); |
2 | 651 |
if ( (n == IOStatus.INTERRUPTED) |
652 |
&& isOpen()) |
|
653 |
continue; |
|
654 |
break; |
|
655 |
} |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
656 |
|
2 | 657 |
} finally { |
658 |
readerCleanup(); |
|
659 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
660 |
assert IOStatus.check(n); |
|
661 |
} |
|
662 |
} catch (IOException x) { |
|
663 |
// If an exception was thrown, close the channel after |
|
664 |
// invoking end() so as to avoid bogus |
|
665 |
// AsynchronousCloseExceptions |
|
666 |
close(); |
|
667 |
throw x; |
|
668 |
} |
|
669 |
synchronized (stateLock) { |
|
670 |
remoteAddress = isa; |
|
671 |
if (n > 0) { |
|
672 |
||
673 |
// Connection succeeded; disallow further |
|
674 |
// invocation |
|
675 |
state = ST_CONNECTED; |
|
12683
5c743840dd8a
7096436: (sc) SocketChannel.connect fails on Windows 8 when channel configured non-blocking
khazra
parents:
12440
diff
changeset
|
676 |
if (isOpen()) |
5c743840dd8a
7096436: (sc) SocketChannel.connect fails on Windows 8 when channel configured non-blocking
khazra
parents:
12440
diff
changeset
|
677 |
localAddress = Net.localAddress(fd); |
2 | 678 |
return true; |
679 |
} |
|
680 |
// If nonblocking and no exception then connection |
|
681 |
// pending; disallow another invocation |
|
682 |
if (!isBlocking()) |
|
683 |
state = ST_PENDING; |
|
684 |
else |
|
685 |
assert false; |
|
686 |
} |
|
687 |
} |
|
688 |
return false; |
|
689 |
} |
|
690 |
} |
|
691 |
} |
|
692 |
||
693 |
public boolean finishConnect() throws IOException { |
|
694 |
synchronized (readLock) { |
|
695 |
synchronized (writeLock) { |
|
696 |
synchronized (stateLock) { |
|
697 |
if (!isOpen()) |
|
698 |
throw new ClosedChannelException(); |
|
699 |
if (state == ST_CONNECTED) |
|
700 |
return true; |
|
701 |
if (state != ST_PENDING) |
|
702 |
throw new NoConnectionPendingException(); |
|
703 |
} |
|
704 |
int n = 0; |
|
705 |
try { |
|
706 |
try { |
|
707 |
begin(); |
|
708 |
synchronized (blockingLock()) { |
|
709 |
synchronized (stateLock) { |
|
710 |
if (!isOpen()) { |
|
711 |
return false; |
|
712 |
} |
|
713 |
readerThread = NativeThread.current(); |
|
714 |
} |
|
715 |
if (!isBlocking()) { |
|
716 |
for (;;) { |
|
717 |
n = checkConnect(fd, false, |
|
718 |
readyToConnect); |
|
719 |
if ( (n == IOStatus.INTERRUPTED) |
|
720 |
&& isOpen()) |
|
721 |
continue; |
|
722 |
break; |
|
723 |
} |
|
724 |
} else { |
|
725 |
for (;;) { |
|
726 |
n = checkConnect(fd, true, |
|
727 |
readyToConnect); |
|
728 |
if (n == 0) { |
|
729 |
// Loop in case of |
|
730 |
// spurious notifications |
|
731 |
continue; |
|
732 |
} |
|
733 |
if ( (n == IOStatus.INTERRUPTED) |
|
734 |
&& isOpen()) |
|
735 |
continue; |
|
736 |
break; |
|
737 |
} |
|
738 |
} |
|
739 |
} |
|
740 |
} finally { |
|
741 |
synchronized (stateLock) { |
|
742 |
readerThread = 0; |
|
743 |
if (state == ST_KILLPENDING) { |
|
744 |
kill(); |
|
745 |
// poll()/getsockopt() does not report |
|
746 |
// error (throws exception, with n = 0) |
|
747 |
// on Linux platform after dup2 and |
|
748 |
// signal-wakeup. Force n to 0 so the |
|
749 |
// end() can throw appropriate exception |
|
750 |
n = 0; |
|
751 |
} |
|
752 |
} |
|
753 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
754 |
assert IOStatus.check(n); |
|
755 |
} |
|
756 |
} catch (IOException x) { |
|
757 |
// If an exception was thrown, close the channel after |
|
758 |
// invoking end() so as to avoid bogus |
|
759 |
// AsynchronousCloseExceptions |
|
760 |
close(); |
|
761 |
throw x; |
|
762 |
} |
|
763 |
if (n > 0) { |
|
764 |
synchronized (stateLock) { |
|
765 |
state = ST_CONNECTED; |
|
12683
5c743840dd8a
7096436: (sc) SocketChannel.connect fails on Windows 8 when channel configured non-blocking
khazra
parents:
12440
diff
changeset
|
766 |
if (isOpen()) |
5c743840dd8a
7096436: (sc) SocketChannel.connect fails on Windows 8 when channel configured non-blocking
khazra
parents:
12440
diff
changeset
|
767 |
localAddress = Net.localAddress(fd); |
2 | 768 |
} |
769 |
return true; |
|
770 |
} |
|
771 |
return false; |
|
772 |
} |
|
773 |
} |
|
774 |
} |
|
775 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
776 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
777 |
public SocketChannel shutdownInput() throws IOException { |
2 | 778 |
synchronized (stateLock) { |
779 |
if (!isOpen()) |
|
780 |
throw new ClosedChannelException(); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
781 |
if (!isConnected()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
782 |
throw new NotYetConnectedException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
783 |
if (isInputOpen) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
784 |
Net.shutdown(fd, Net.SHUT_RD); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
785 |
if (readerThread != 0) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
786 |
NativeThread.signal(readerThread); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
787 |
isInputOpen = false; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
788 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
789 |
return this; |
2 | 790 |
} |
791 |
} |
|
792 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
793 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
794 |
public SocketChannel shutdownOutput() throws IOException { |
2 | 795 |
synchronized (stateLock) { |
796 |
if (!isOpen()) |
|
797 |
throw new ClosedChannelException(); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
798 |
if (!isConnected()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
799 |
throw new NotYetConnectedException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
800 |
if (isOutputOpen) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
801 |
Net.shutdown(fd, Net.SHUT_WR); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
802 |
if (writerThread != 0) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
803 |
NativeThread.signal(writerThread); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
804 |
isOutputOpen = false; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
805 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
806 |
return this; |
2 | 807 |
} |
808 |
} |
|
809 |
||
810 |
public boolean isInputOpen() { |
|
811 |
synchronized (stateLock) { |
|
812 |
return isInputOpen; |
|
813 |
} |
|
814 |
} |
|
815 |
||
816 |
public boolean isOutputOpen() { |
|
817 |
synchronized (stateLock) { |
|
818 |
return isOutputOpen; |
|
819 |
} |
|
820 |
} |
|
821 |
||
822 |
// AbstractInterruptibleChannel synchronizes invocations of this method |
|
823 |
// using AbstractInterruptibleChannel.closeLock, and also ensures that this |
|
824 |
// method is only ever invoked once. Before we get to this method, isOpen |
|
825 |
// (which is volatile) will have been set to false. |
|
826 |
// |
|
827 |
protected void implCloseSelectableChannel() throws IOException { |
|
828 |
synchronized (stateLock) { |
|
829 |
isInputOpen = false; |
|
830 |
isOutputOpen = false; |
|
831 |
||
832 |
// Close the underlying file descriptor and dup it to a known fd |
|
833 |
// that's already closed. This prevents other operations on this |
|
834 |
// channel from using the old fd, which might be recycled in the |
|
835 |
// meantime and allocated to an entirely different channel. |
|
836 |
// |
|
12440
2689ca179e22
7118373: (se) Potential leak file descriptor when deregistrating at around the same time as an async close
robm
parents:
9679
diff
changeset
|
837 |
if (state != ST_KILLED) |
2689ca179e22
7118373: (se) Potential leak file descriptor when deregistrating at around the same time as an async close
robm
parents:
9679
diff
changeset
|
838 |
nd.preClose(fd); |
2 | 839 |
|
840 |
// Signal native threads, if needed. If a target thread is not |
|
841 |
// currently blocked in an I/O operation then no harm is done since |
|
842 |
// the signal handler doesn't actually do anything. |
|
843 |
// |
|
844 |
if (readerThread != 0) |
|
845 |
NativeThread.signal(readerThread); |
|
846 |
||
847 |
if (writerThread != 0) |
|
848 |
NativeThread.signal(writerThread); |
|
849 |
||
850 |
// If this channel is not registered then it's safe to close the fd |
|
851 |
// immediately since we know at this point that no thread is |
|
852 |
// blocked in an I/O operation upon the channel and, since the |
|
853 |
// channel is marked closed, no thread will start another such |
|
854 |
// operation. If this channel is registered then we don't close |
|
855 |
// the fd since it might be in use by a selector. In that case |
|
856 |
// closing this channel caused its keys to be cancelled, so the |
|
857 |
// last selector to deregister a key for this channel will invoke |
|
858 |
// kill() to close the fd. |
|
859 |
// |
|
860 |
if (!isRegistered()) |
|
861 |
kill(); |
|
862 |
} |
|
863 |
} |
|
864 |
||
865 |
public void kill() throws IOException { |
|
866 |
synchronized (stateLock) { |
|
867 |
if (state == ST_KILLED) |
|
868 |
return; |
|
869 |
if (state == ST_UNINITIALIZED) { |
|
870 |
state = ST_KILLED; |
|
871 |
return; |
|
872 |
} |
|
873 |
assert !isOpen() && !isRegistered(); |
|
874 |
||
875 |
// Postpone the kill if there is a waiting reader |
|
876 |
// or writer thread. See the comments in read() for |
|
877 |
// more detailed explanation. |
|
878 |
if (readerThread == 0 && writerThread == 0) { |
|
879 |
nd.close(fd); |
|
880 |
state = ST_KILLED; |
|
881 |
} else { |
|
882 |
state = ST_KILLPENDING; |
|
883 |
} |
|
884 |
} |
|
885 |
} |
|
886 |
||
887 |
/** |
|
888 |
* Translates native poll revent ops into a ready operation ops |
|
889 |
*/ |
|
890 |
public boolean translateReadyOps(int ops, int initialOps, |
|
891 |
SelectionKeyImpl sk) { |
|
892 |
int intOps = sk.nioInterestOps(); // Do this just once, it synchronizes |
|
893 |
int oldOps = sk.nioReadyOps(); |
|
894 |
int newOps = initialOps; |
|
895 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
896 |
if ((ops & Net.POLLNVAL) != 0) { |
2 | 897 |
// This should only happen if this channel is pre-closed while a |
898 |
// selection operation is in progress |
|
899 |
// ## Throw an error if this channel has not been pre-closed |
|
900 |
return false; |
|
901 |
} |
|
902 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
903 |
if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) { |
2 | 904 |
newOps = intOps; |
905 |
sk.nioReadyOps(newOps); |
|
906 |
// No need to poll again in checkConnect, |
|
907 |
// the error will be detected there |
|
908 |
readyToConnect = true; |
|
909 |
return (newOps & ~oldOps) != 0; |
|
910 |
} |
|
911 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
912 |
if (((ops & Net.POLLIN) != 0) && |
2 | 913 |
((intOps & SelectionKey.OP_READ) != 0) && |
914 |
(state == ST_CONNECTED)) |
|
915 |
newOps |= SelectionKey.OP_READ; |
|
916 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
917 |
if (((ops & Net.POLLCONN) != 0) && |
2 | 918 |
((intOps & SelectionKey.OP_CONNECT) != 0) && |
919 |
((state == ST_UNCONNECTED) || (state == ST_PENDING))) { |
|
920 |
newOps |= SelectionKey.OP_CONNECT; |
|
921 |
readyToConnect = true; |
|
922 |
} |
|
923 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
924 |
if (((ops & Net.POLLOUT) != 0) && |
2 | 925 |
((intOps & SelectionKey.OP_WRITE) != 0) && |
926 |
(state == ST_CONNECTED)) |
|
927 |
newOps |= SelectionKey.OP_WRITE; |
|
928 |
||
929 |
sk.nioReadyOps(newOps); |
|
930 |
return (newOps & ~oldOps) != 0; |
|
931 |
} |
|
932 |
||
933 |
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) { |
|
934 |
return translateReadyOps(ops, sk.nioReadyOps(), sk); |
|
935 |
} |
|
936 |
||
937 |
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) { |
|
938 |
return translateReadyOps(ops, 0, sk); |
|
939 |
} |
|
940 |
||
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
941 |
// package-private |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
942 |
int poll(int events, long timeout) throws IOException { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
943 |
assert Thread.holdsLock(blockingLock()) && !isBlocking(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
944 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
945 |
synchronized (readLock) { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
946 |
int n = 0; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
947 |
try { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
948 |
begin(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
949 |
synchronized (stateLock) { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
950 |
if (!isOpen()) |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
951 |
return 0; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
952 |
readerThread = NativeThread.current(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
953 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
954 |
n = Net.poll(fd, events, timeout); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
955 |
} finally { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
956 |
readerCleanup(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
957 |
end(n > 0); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
958 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
959 |
return n; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
960 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
961 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
962 |
|
2 | 963 |
/** |
964 |
* Translates an interest operation set into a native poll event set |
|
965 |
*/ |
|
966 |
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) { |
|
967 |
int newOps = 0; |
|
968 |
if ((ops & SelectionKey.OP_READ) != 0) |
|
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
969 |
newOps |= Net.POLLIN; |
2 | 970 |
if ((ops & SelectionKey.OP_WRITE) != 0) |
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
971 |
newOps |= Net.POLLOUT; |
2 | 972 |
if ((ops & SelectionKey.OP_CONNECT) != 0) |
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
973 |
newOps |= Net.POLLCONN; |
2 | 974 |
sk.selector.putEventOps(sk, newOps); |
975 |
} |
|
976 |
||
977 |
public FileDescriptor getFD() { |
|
978 |
return fd; |
|
979 |
} |
|
980 |
||
981 |
public int getFDVal() { |
|
982 |
return fdVal; |
|
983 |
} |
|
984 |
||
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
985 |
@Override |
2 | 986 |
public String toString() { |
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
23879
diff
changeset
|
987 |
StringBuilder sb = new StringBuilder(); |
2 | 988 |
sb.append(this.getClass().getSuperclass().getName()); |
989 |
sb.append('['); |
|
990 |
if (!isOpen()) |
|
991 |
sb.append("closed"); |
|
992 |
else { |
|
993 |
synchronized (stateLock) { |
|
994 |
switch (state) { |
|
995 |
case ST_UNCONNECTED: |
|
996 |
sb.append("unconnected"); |
|
997 |
break; |
|
998 |
case ST_PENDING: |
|
999 |
sb.append("connection-pending"); |
|
1000 |
break; |
|
1001 |
case ST_CONNECTED: |
|
1002 |
sb.append("connected"); |
|
1003 |
if (!isInputOpen) |
|
1004 |
sb.append(" ishut"); |
|
1005 |
if (!isOutputOpen) |
|
1006 |
sb.append(" oshut"); |
|
1007 |
break; |
|
1008 |
} |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
1009 |
InetSocketAddress addr = localAddress(); |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
1010 |
if (addr != null) { |
2 | 1011 |
sb.append(" local="); |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
1012 |
sb.append(Net.getRevealedLocalAddressAsString(addr)); |
2 | 1013 |
} |
1014 |
if (remoteAddress() != null) { |
|
1015 |
sb.append(" remote="); |
|
1016 |
sb.append(remoteAddress().toString()); |
|
1017 |
} |
|
1018 |
} |
|
1019 |
} |
|
1020 |
sb.append(']'); |
|
1021 |
return sb.toString(); |
|
1022 |
} |
|
1023 |
||
1024 |
||
1025 |
// -- Native methods -- |
|
1026 |
||
1027 |
private static native int checkConnect(FileDescriptor fd, |
|
1028 |
boolean block, boolean ready) |
|
1029 |
throws IOException; |
|
1030 |
||
6117
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
1031 |
private static native int sendOutOfBandData(FileDescriptor fd, byte data) |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
1032 |
throws IOException; |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
1033 |
|
2 | 1034 |
static { |
19607
bee007586d06
8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents:
18212
diff
changeset
|
1035 |
IOUtil.load(); |
2 | 1036 |
nd = new SocketDispatcher(); |
1037 |
} |
|
1038 |
||
1039 |
} |