author | simonis |
Mon, 20 Jan 2014 09:24:25 +0100 | |
changeset 22604 | 9b394795e216 |
parent 19607 | bee007586d06 |
child 22607 | ba232b417248 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
12440
2689ca179e22
7118373: (se) Potential leak file descriptor when deregistrating at around the same time as an async close
robm
parents:
9679
diff
changeset
|
2 |
* Copyright (c) 2000, 2012, 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; |
2 | 36 |
|
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 |
|
58 |
private volatile long readerThread = 0; |
|
59 |
private volatile long writerThread = 0; |
|
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 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
175 |
// special handling for IP_TOS: no-op when IPv6 |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
176 |
if (name == StandardSocketOptions.IP_TOS) { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
177 |
if (!Net.isIPv6Available()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
178 |
Net.setSocketOption(fd, StandardProtocolFamily.INET, name, value); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
179 |
return this; |
18192 | 180 |
} else if (name == StandardSocketOptions.SO_REUSEADDR && |
181 |
Net.useExclusiveBind()) |
|
182 |
{ |
|
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) { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
217 |
return (Net.isIPv6Available()) ? (T) Integer.valueOf(0) : |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
218 |
(T) Net.getSocketOption(fd, StandardProtocolFamily.INET, name); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
219 |
} |
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 |
// no options that require special handling |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
222 |
return (T) Net.getSocketOption(fd, Net.UNSPEC, name); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
223 |
} |
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 |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
226 |
private static class DefaultOptionsHolder { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
227 |
static final Set<SocketOption<?>> defaultOptions = defaultOptions(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
228 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
229 |
private static Set<SocketOption<?>> defaultOptions() { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
230 |
HashSet<SocketOption<?>> set = new HashSet<SocketOption<?>>(8); |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
231 |
set.add(StandardSocketOptions.SO_SNDBUF); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
232 |
set.add(StandardSocketOptions.SO_RCVBUF); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
233 |
set.add(StandardSocketOptions.SO_KEEPALIVE); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
234 |
set.add(StandardSocketOptions.SO_REUSEADDR); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
235 |
set.add(StandardSocketOptions.SO_LINGER); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
236 |
set.add(StandardSocketOptions.TCP_NODELAY); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
237 |
// additional options required by socket adaptor |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
238 |
set.add(StandardSocketOptions.IP_TOS); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
239 |
set.add(ExtendedSocketOption.SO_OOBINLINE); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
240 |
return Collections.unmodifiableSet(set); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
241 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
242 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
243 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
244 |
@Override |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
245 |
public final Set<SocketOption<?>> supportedOptions() { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
246 |
return DefaultOptionsHolder.defaultOptions; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
247 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
248 |
|
2 | 249 |
private boolean ensureReadOpen() throws ClosedChannelException { |
250 |
synchronized (stateLock) { |
|
251 |
if (!isOpen()) |
|
252 |
throw new ClosedChannelException(); |
|
253 |
if (!isConnected()) |
|
254 |
throw new NotYetConnectedException(); |
|
255 |
if (!isInputOpen) |
|
256 |
return false; |
|
257 |
else |
|
258 |
return true; |
|
259 |
} |
|
260 |
} |
|
261 |
||
262 |
private void ensureWriteOpen() throws ClosedChannelException { |
|
263 |
synchronized (stateLock) { |
|
264 |
if (!isOpen()) |
|
265 |
throw new ClosedChannelException(); |
|
266 |
if (!isOutputOpen) |
|
267 |
throw new ClosedChannelException(); |
|
268 |
if (!isConnected()) |
|
269 |
throw new NotYetConnectedException(); |
|
270 |
} |
|
271 |
} |
|
272 |
||
273 |
private void readerCleanup() throws IOException { |
|
274 |
synchronized (stateLock) { |
|
275 |
readerThread = 0; |
|
276 |
if (state == ST_KILLPENDING) |
|
277 |
kill(); |
|
278 |
} |
|
279 |
} |
|
280 |
||
281 |
private void writerCleanup() throws IOException { |
|
282 |
synchronized (stateLock) { |
|
283 |
writerThread = 0; |
|
284 |
if (state == ST_KILLPENDING) |
|
285 |
kill(); |
|
286 |
} |
|
287 |
} |
|
288 |
||
289 |
public int read(ByteBuffer buf) throws IOException { |
|
290 |
||
291 |
if (buf == null) |
|
292 |
throw new NullPointerException(); |
|
293 |
||
294 |
synchronized (readLock) { |
|
295 |
if (!ensureReadOpen()) |
|
296 |
return -1; |
|
297 |
int n = 0; |
|
298 |
try { |
|
299 |
||
300 |
// Set up the interruption machinery; see |
|
301 |
// AbstractInterruptibleChannel for details |
|
302 |
// |
|
303 |
begin(); |
|
304 |
||
305 |
synchronized (stateLock) { |
|
306 |
if (!isOpen()) { |
|
307 |
// Either the current thread is already interrupted, so |
|
308 |
// begin() closed the channel, or another thread closed the |
|
309 |
// channel since we checked it a few bytecodes ago. In |
|
310 |
// either case the value returned here is irrelevant since |
|
311 |
// the invocation of end() in the finally block will throw |
|
312 |
// an appropriate exception. |
|
313 |
// |
|
314 |
return 0; |
|
315 |
||
316 |
} |
|
317 |
||
318 |
// Save this thread so that it can be signalled on those |
|
319 |
// platforms that require it |
|
320 |
// |
|
321 |
readerThread = NativeThread.current(); |
|
322 |
} |
|
323 |
||
324 |
// Between the previous test of isOpen() and the return of the |
|
325 |
// IOUtil.read invocation below, this channel might be closed |
|
326 |
// or this thread might be interrupted. We rely upon the |
|
327 |
// implicit synchronization point in the kernel read() call to |
|
328 |
// make sure that the right thing happens. In either case the |
|
329 |
// implCloseSelectableChannel method is ultimately invoked in |
|
330 |
// some other thread, so there are three possibilities: |
|
331 |
// |
|
332 |
// - implCloseSelectableChannel() invokes nd.preClose() |
|
333 |
// before this thread invokes read(), in which case the |
|
334 |
// read returns immediately with either EOF or an error, |
|
335 |
// the latter of which will cause an IOException to be |
|
336 |
// thrown. |
|
337 |
// |
|
338 |
// - implCloseSelectableChannel() invokes nd.preClose() after |
|
339 |
// this thread is blocked in read(). On some operating |
|
340 |
// systems (e.g., Solaris and Windows) this causes the read |
|
341 |
// to return immediately with either EOF or an error |
|
342 |
// indication. |
|
343 |
// |
|
344 |
// - implCloseSelectableChannel() invokes nd.preClose() after |
|
345 |
// this thread is blocked in read() but the operating |
|
346 |
// system (e.g., Linux) doesn't support preemptive close, |
|
347 |
// so implCloseSelectableChannel() proceeds to signal this |
|
348 |
// thread, thereby causing the read to return immediately |
|
349 |
// with IOStatus.INTERRUPTED. |
|
350 |
// |
|
351 |
// In all three cases the invocation of end() in the finally |
|
352 |
// clause will notice that the channel has been closed and |
|
353 |
// throw an appropriate exception (AsynchronousCloseException |
|
354 |
// or ClosedByInterruptException) if necessary. |
|
355 |
// |
|
356 |
// *There is A fourth possibility. implCloseSelectableChannel() |
|
357 |
// invokes nd.preClose(), signals reader/writer thred and quickly |
|
358 |
// moves on to nd.close() in kill(), which does a real close. |
|
359 |
// Then a third thread accepts a new connection, opens file or |
|
360 |
// whatever that causes the released "fd" to be recycled. All |
|
361 |
// above happens just between our last isOpen() check and the |
|
362 |
// next kernel read reached, with the recycled "fd". The solution |
|
363 |
// is to postpone the real kill() if there is a reader or/and |
|
364 |
// writer thread(s) over there "waiting", leave the cleanup/kill |
|
365 |
// to the reader or writer thread. (the preClose() still happens |
|
366 |
// so the connection gets cut off as usual). |
|
367 |
// |
|
368 |
// For socket channels there is the additional wrinkle that |
|
369 |
// asynchronous shutdown works much like asynchronous close, |
|
370 |
// except that the channel is shutdown rather than completely |
|
371 |
// closed. This is analogous to the first two cases above, |
|
372 |
// except that the shutdown operation plays the role of |
|
373 |
// nd.preClose(). |
|
374 |
for (;;) { |
|
16921
e70261f11307
8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
14025
diff
changeset
|
375 |
n = IOUtil.read(fd, buf, -1, nd); |
2 | 376 |
if ((n == IOStatus.INTERRUPTED) && isOpen()) { |
377 |
// The system call was interrupted but the channel |
|
378 |
// is still open, so retry |
|
379 |
continue; |
|
380 |
} |
|
381 |
return IOStatus.normalize(n); |
|
382 |
} |
|
383 |
||
384 |
} finally { |
|
385 |
readerCleanup(); // Clear reader thread |
|
386 |
// The end method, which is defined in our superclass |
|
387 |
// AbstractInterruptibleChannel, resets the interruption |
|
388 |
// machinery. If its argument is true then it returns |
|
389 |
// normally; otherwise it checks the interrupt and open state |
|
390 |
// of this channel and throws an appropriate exception if |
|
391 |
// necessary. |
|
392 |
// |
|
393 |
// So, if we actually managed to do any I/O in the above try |
|
394 |
// block then we pass true to the end method. We also pass |
|
395 |
// true if the channel was in non-blocking mode when the I/O |
|
396 |
// operation was initiated but no data could be transferred; |
|
397 |
// this prevents spurious exceptions from being thrown in the |
|
398 |
// rare event that a channel is closed or a thread is |
|
399 |
// interrupted at the exact moment that a non-blocking I/O |
|
400 |
// request is made. |
|
401 |
// |
|
402 |
end(n > 0 || (n == IOStatus.UNAVAILABLE)); |
|
403 |
||
404 |
// Extra case for socket channels: Asynchronous shutdown |
|
405 |
// |
|
406 |
synchronized (stateLock) { |
|
407 |
if ((n <= 0) && (!isInputOpen)) |
|
408 |
return IOStatus.EOF; |
|
409 |
} |
|
410 |
||
411 |
assert IOStatus.check(n); |
|
412 |
||
413 |
} |
|
414 |
} |
|
415 |
} |
|
416 |
||
6301 | 417 |
public long read(ByteBuffer[] dsts, int offset, int length) |
418 |
throws IOException |
|
419 |
{ |
|
420 |
if ((offset < 0) || (length < 0) || (offset > dsts.length - length)) |
|
421 |
throw new IndexOutOfBoundsException(); |
|
2 | 422 |
synchronized (readLock) { |
423 |
if (!ensureReadOpen()) |
|
424 |
return -1; |
|
425 |
long n = 0; |
|
426 |
try { |
|
427 |
begin(); |
|
428 |
synchronized (stateLock) { |
|
429 |
if (!isOpen()) |
|
430 |
return 0; |
|
431 |
readerThread = NativeThread.current(); |
|
432 |
} |
|
433 |
||
434 |
for (;;) { |
|
6301 | 435 |
n = IOUtil.read(fd, dsts, offset, length, nd); |
2 | 436 |
if ((n == IOStatus.INTERRUPTED) && isOpen()) |
437 |
continue; |
|
438 |
return IOStatus.normalize(n); |
|
439 |
} |
|
440 |
} finally { |
|
441 |
readerCleanup(); |
|
442 |
end(n > 0 || (n == IOStatus.UNAVAILABLE)); |
|
443 |
synchronized (stateLock) { |
|
444 |
if ((n <= 0) && (!isInputOpen)) |
|
445 |
return IOStatus.EOF; |
|
446 |
} |
|
447 |
assert IOStatus.check(n); |
|
448 |
} |
|
449 |
} |
|
450 |
} |
|
451 |
||
452 |
public int write(ByteBuffer buf) throws IOException { |
|
453 |
if (buf == null) |
|
454 |
throw new NullPointerException(); |
|
455 |
synchronized (writeLock) { |
|
456 |
ensureWriteOpen(); |
|
457 |
int n = 0; |
|
458 |
try { |
|
459 |
begin(); |
|
460 |
synchronized (stateLock) { |
|
461 |
if (!isOpen()) |
|
462 |
return 0; |
|
463 |
writerThread = NativeThread.current(); |
|
464 |
} |
|
465 |
for (;;) { |
|
16921
e70261f11307
8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
14025
diff
changeset
|
466 |
n = IOUtil.write(fd, buf, -1, nd); |
2 | 467 |
if ((n == IOStatus.INTERRUPTED) && isOpen()) |
468 |
continue; |
|
469 |
return IOStatus.normalize(n); |
|
470 |
} |
|
471 |
} finally { |
|
472 |
writerCleanup(); |
|
473 |
end(n > 0 || (n == IOStatus.UNAVAILABLE)); |
|
474 |
synchronized (stateLock) { |
|
475 |
if ((n <= 0) && (!isOutputOpen)) |
|
476 |
throw new AsynchronousCloseException(); |
|
477 |
} |
|
478 |
assert IOStatus.check(n); |
|
479 |
} |
|
480 |
} |
|
481 |
} |
|
482 |
||
6301 | 483 |
public long write(ByteBuffer[] srcs, int offset, int length) |
484 |
throws IOException |
|
485 |
{ |
|
486 |
if ((offset < 0) || (length < 0) || (offset > srcs.length - length)) |
|
487 |
throw new IndexOutOfBoundsException(); |
|
2 | 488 |
synchronized (writeLock) { |
489 |
ensureWriteOpen(); |
|
490 |
long n = 0; |
|
491 |
try { |
|
492 |
begin(); |
|
493 |
synchronized (stateLock) { |
|
494 |
if (!isOpen()) |
|
495 |
return 0; |
|
496 |
writerThread = NativeThread.current(); |
|
497 |
} |
|
498 |
for (;;) { |
|
6301 | 499 |
n = IOUtil.write(fd, srcs, offset, length, nd); |
2 | 500 |
if ((n == IOStatus.INTERRUPTED) && isOpen()) |
501 |
continue; |
|
502 |
return IOStatus.normalize(n); |
|
503 |
} |
|
504 |
} finally { |
|
505 |
writerCleanup(); |
|
506 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
507 |
synchronized (stateLock) { |
|
508 |
if ((n <= 0) && (!isOutputOpen)) |
|
509 |
throw new AsynchronousCloseException(); |
|
510 |
} |
|
511 |
assert IOStatus.check(n); |
|
512 |
} |
|
513 |
} |
|
514 |
} |
|
515 |
||
6117
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
516 |
// package-private |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
517 |
int sendOutOfBandData(byte b) throws IOException { |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
518 |
synchronized (writeLock) { |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
519 |
ensureWriteOpen(); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
520 |
int n = 0; |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
521 |
try { |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
522 |
begin(); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
523 |
synchronized (stateLock) { |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
524 |
if (!isOpen()) |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
525 |
return 0; |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
526 |
writerThread = NativeThread.current(); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
527 |
} |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
528 |
for (;;) { |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
529 |
n = sendOutOfBandData(fd, b); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
530 |
if ((n == IOStatus.INTERRUPTED) && isOpen()) |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
531 |
continue; |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
532 |
return IOStatus.normalize(n); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
533 |
} |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
534 |
} finally { |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
535 |
writerCleanup(); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
536 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
537 |
synchronized (stateLock) { |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
538 |
if ((n <= 0) && (!isOutputOpen)) |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
539 |
throw new AsynchronousCloseException(); |
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 |
assert IOStatus.check(n); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
542 |
} |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
543 |
} |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
544 |
} |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
545 |
|
2 | 546 |
protected void implConfigureBlocking(boolean block) throws IOException { |
547 |
IOUtil.configureBlocking(fd, block); |
|
548 |
} |
|
549 |
||
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
550 |
public InetSocketAddress localAddress() { |
2 | 551 |
synchronized (stateLock) { |
552 |
return localAddress; |
|
553 |
} |
|
554 |
} |
|
555 |
||
556 |
public SocketAddress remoteAddress() { |
|
557 |
synchronized (stateLock) { |
|
558 |
return remoteAddress; |
|
559 |
} |
|
560 |
} |
|
561 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
562 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
563 |
public SocketChannel bind(SocketAddress local) throws IOException { |
2 | 564 |
synchronized (readLock) { |
565 |
synchronized (writeLock) { |
|
566 |
synchronized (stateLock) { |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
567 |
if (!isOpen()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
568 |
throw new ClosedChannelException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
569 |
if (state == ST_PENDING) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
570 |
throw new ConnectionPendingException(); |
2 | 571 |
if (localAddress != null) |
572 |
throw new AlreadyBoundException(); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
573 |
InetSocketAddress isa = (local == null) ? |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
574 |
new InetSocketAddress(0) : Net.checkAddress(local); |
2446 | 575 |
NetHooks.beforeTcpBind(fd, isa.getAddress(), isa.getPort()); |
2 | 576 |
Net.bind(fd, isa.getAddress(), isa.getPort()); |
577 |
localAddress = Net.localAddress(fd); |
|
578 |
} |
|
579 |
} |
|
580 |
} |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
581 |
return this; |
2 | 582 |
} |
583 |
||
584 |
public boolean isConnected() { |
|
585 |
synchronized (stateLock) { |
|
586 |
return (state == ST_CONNECTED); |
|
587 |
} |
|
588 |
} |
|
589 |
||
590 |
public boolean isConnectionPending() { |
|
591 |
synchronized (stateLock) { |
|
592 |
return (state == ST_PENDING); |
|
593 |
} |
|
594 |
} |
|
595 |
||
596 |
void ensureOpenAndUnconnected() throws IOException { // package-private |
|
597 |
synchronized (stateLock) { |
|
598 |
if (!isOpen()) |
|
599 |
throw new ClosedChannelException(); |
|
600 |
if (state == ST_CONNECTED) |
|
601 |
throw new AlreadyConnectedException(); |
|
602 |
if (state == ST_PENDING) |
|
603 |
throw new ConnectionPendingException(); |
|
604 |
} |
|
605 |
} |
|
606 |
||
607 |
public boolean connect(SocketAddress sa) throws IOException { |
|
608 |
int localPort = 0; |
|
609 |
||
610 |
synchronized (readLock) { |
|
611 |
synchronized (writeLock) { |
|
612 |
ensureOpenAndUnconnected(); |
|
613 |
InetSocketAddress isa = Net.checkAddress(sa); |
|
614 |
SecurityManager sm = System.getSecurityManager(); |
|
615 |
if (sm != null) |
|
616 |
sm.checkConnect(isa.getAddress().getHostAddress(), |
|
617 |
isa.getPort()); |
|
618 |
synchronized (blockingLock()) { |
|
619 |
int n = 0; |
|
620 |
try { |
|
621 |
try { |
|
622 |
begin(); |
|
623 |
synchronized (stateLock) { |
|
624 |
if (!isOpen()) { |
|
625 |
return false; |
|
626 |
} |
|
2446 | 627 |
// notify hook only if unbound |
628 |
if (localAddress == null) { |
|
629 |
NetHooks.beforeTcpConnect(fd, |
|
630 |
isa.getAddress(), |
|
631 |
isa.getPort()); |
|
632 |
} |
|
2 | 633 |
readerThread = NativeThread.current(); |
634 |
} |
|
635 |
for (;;) { |
|
636 |
InetAddress ia = isa.getAddress(); |
|
637 |
if (ia.isAnyLocalAddress()) |
|
638 |
ia = InetAddress.getLocalHost(); |
|
639 |
n = Net.connect(fd, |
|
640 |
ia, |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
641 |
isa.getPort()); |
2 | 642 |
if ( (n == IOStatus.INTERRUPTED) |
643 |
&& isOpen()) |
|
644 |
continue; |
|
645 |
break; |
|
646 |
} |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
647 |
|
2 | 648 |
} finally { |
649 |
readerCleanup(); |
|
650 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
651 |
assert IOStatus.check(n); |
|
652 |
} |
|
653 |
} catch (IOException x) { |
|
654 |
// If an exception was thrown, close the channel after |
|
655 |
// invoking end() so as to avoid bogus |
|
656 |
// AsynchronousCloseExceptions |
|
657 |
close(); |
|
658 |
throw x; |
|
659 |
} |
|
660 |
synchronized (stateLock) { |
|
661 |
remoteAddress = isa; |
|
662 |
if (n > 0) { |
|
663 |
||
664 |
// Connection succeeded; disallow further |
|
665 |
// invocation |
|
666 |
state = ST_CONNECTED; |
|
12683
5c743840dd8a
7096436: (sc) SocketChannel.connect fails on Windows 8 when channel configured non-blocking
khazra
parents:
12440
diff
changeset
|
667 |
if (isOpen()) |
5c743840dd8a
7096436: (sc) SocketChannel.connect fails on Windows 8 when channel configured non-blocking
khazra
parents:
12440
diff
changeset
|
668 |
localAddress = Net.localAddress(fd); |
2 | 669 |
return true; |
670 |
} |
|
671 |
// If nonblocking and no exception then connection |
|
672 |
// pending; disallow another invocation |
|
673 |
if (!isBlocking()) |
|
674 |
state = ST_PENDING; |
|
675 |
else |
|
676 |
assert false; |
|
677 |
} |
|
678 |
} |
|
679 |
return false; |
|
680 |
} |
|
681 |
} |
|
682 |
} |
|
683 |
||
684 |
public boolean finishConnect() throws IOException { |
|
685 |
synchronized (readLock) { |
|
686 |
synchronized (writeLock) { |
|
687 |
synchronized (stateLock) { |
|
688 |
if (!isOpen()) |
|
689 |
throw new ClosedChannelException(); |
|
690 |
if (state == ST_CONNECTED) |
|
691 |
return true; |
|
692 |
if (state != ST_PENDING) |
|
693 |
throw new NoConnectionPendingException(); |
|
694 |
} |
|
695 |
int n = 0; |
|
696 |
try { |
|
697 |
try { |
|
698 |
begin(); |
|
699 |
synchronized (blockingLock()) { |
|
700 |
synchronized (stateLock) { |
|
701 |
if (!isOpen()) { |
|
702 |
return false; |
|
703 |
} |
|
704 |
readerThread = NativeThread.current(); |
|
705 |
} |
|
706 |
if (!isBlocking()) { |
|
707 |
for (;;) { |
|
708 |
n = checkConnect(fd, false, |
|
709 |
readyToConnect); |
|
710 |
if ( (n == IOStatus.INTERRUPTED) |
|
711 |
&& isOpen()) |
|
712 |
continue; |
|
713 |
break; |
|
714 |
} |
|
715 |
} else { |
|
716 |
for (;;) { |
|
717 |
n = checkConnect(fd, true, |
|
718 |
readyToConnect); |
|
719 |
if (n == 0) { |
|
720 |
// Loop in case of |
|
721 |
// spurious notifications |
|
722 |
continue; |
|
723 |
} |
|
724 |
if ( (n == IOStatus.INTERRUPTED) |
|
725 |
&& isOpen()) |
|
726 |
continue; |
|
727 |
break; |
|
728 |
} |
|
729 |
} |
|
730 |
} |
|
731 |
} finally { |
|
732 |
synchronized (stateLock) { |
|
733 |
readerThread = 0; |
|
734 |
if (state == ST_KILLPENDING) { |
|
735 |
kill(); |
|
736 |
// poll()/getsockopt() does not report |
|
737 |
// error (throws exception, with n = 0) |
|
738 |
// on Linux platform after dup2 and |
|
739 |
// signal-wakeup. Force n to 0 so the |
|
740 |
// end() can throw appropriate exception |
|
741 |
n = 0; |
|
742 |
} |
|
743 |
} |
|
744 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
745 |
assert IOStatus.check(n); |
|
746 |
} |
|
747 |
} catch (IOException x) { |
|
748 |
// If an exception was thrown, close the channel after |
|
749 |
// invoking end() so as to avoid bogus |
|
750 |
// AsynchronousCloseExceptions |
|
751 |
close(); |
|
752 |
throw x; |
|
753 |
} |
|
754 |
if (n > 0) { |
|
755 |
synchronized (stateLock) { |
|
756 |
state = ST_CONNECTED; |
|
12683
5c743840dd8a
7096436: (sc) SocketChannel.connect fails on Windows 8 when channel configured non-blocking
khazra
parents:
12440
diff
changeset
|
757 |
if (isOpen()) |
5c743840dd8a
7096436: (sc) SocketChannel.connect fails on Windows 8 when channel configured non-blocking
khazra
parents:
12440
diff
changeset
|
758 |
localAddress = Net.localAddress(fd); |
2 | 759 |
} |
760 |
return true; |
|
761 |
} |
|
762 |
return false; |
|
763 |
} |
|
764 |
} |
|
765 |
} |
|
766 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
767 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
768 |
public SocketChannel shutdownInput() throws IOException { |
2 | 769 |
synchronized (stateLock) { |
770 |
if (!isOpen()) |
|
771 |
throw new ClosedChannelException(); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
772 |
if (!isConnected()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
773 |
throw new NotYetConnectedException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
774 |
if (isInputOpen) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
775 |
Net.shutdown(fd, Net.SHUT_RD); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
776 |
if (readerThread != 0) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
777 |
NativeThread.signal(readerThread); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
778 |
isInputOpen = false; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
779 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
780 |
return this; |
2 | 781 |
} |
782 |
} |
|
783 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
784 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
785 |
public SocketChannel shutdownOutput() throws IOException { |
2 | 786 |
synchronized (stateLock) { |
787 |
if (!isOpen()) |
|
788 |
throw new ClosedChannelException(); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
789 |
if (!isConnected()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
790 |
throw new NotYetConnectedException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
791 |
if (isOutputOpen) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
792 |
Net.shutdown(fd, Net.SHUT_WR); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
793 |
if (writerThread != 0) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
794 |
NativeThread.signal(writerThread); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
795 |
isOutputOpen = false; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
796 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
797 |
return this; |
2 | 798 |
} |
799 |
} |
|
800 |
||
801 |
public boolean isInputOpen() { |
|
802 |
synchronized (stateLock) { |
|
803 |
return isInputOpen; |
|
804 |
} |
|
805 |
} |
|
806 |
||
807 |
public boolean isOutputOpen() { |
|
808 |
synchronized (stateLock) { |
|
809 |
return isOutputOpen; |
|
810 |
} |
|
811 |
} |
|
812 |
||
813 |
// AbstractInterruptibleChannel synchronizes invocations of this method |
|
814 |
// using AbstractInterruptibleChannel.closeLock, and also ensures that this |
|
815 |
// method is only ever invoked once. Before we get to this method, isOpen |
|
816 |
// (which is volatile) will have been set to false. |
|
817 |
// |
|
818 |
protected void implCloseSelectableChannel() throws IOException { |
|
819 |
synchronized (stateLock) { |
|
820 |
isInputOpen = false; |
|
821 |
isOutputOpen = false; |
|
822 |
||
823 |
// Close the underlying file descriptor and dup it to a known fd |
|
824 |
// that's already closed. This prevents other operations on this |
|
825 |
// channel from using the old fd, which might be recycled in the |
|
826 |
// meantime and allocated to an entirely different channel. |
|
827 |
// |
|
12440
2689ca179e22
7118373: (se) Potential leak file descriptor when deregistrating at around the same time as an async close
robm
parents:
9679
diff
changeset
|
828 |
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
|
829 |
nd.preClose(fd); |
2 | 830 |
|
831 |
// Signal native threads, if needed. If a target thread is not |
|
832 |
// currently blocked in an I/O operation then no harm is done since |
|
833 |
// the signal handler doesn't actually do anything. |
|
834 |
// |
|
835 |
if (readerThread != 0) |
|
836 |
NativeThread.signal(readerThread); |
|
837 |
||
838 |
if (writerThread != 0) |
|
839 |
NativeThread.signal(writerThread); |
|
840 |
||
841 |
// If this channel is not registered then it's safe to close the fd |
|
842 |
// immediately since we know at this point that no thread is |
|
843 |
// blocked in an I/O operation upon the channel and, since the |
|
844 |
// channel is marked closed, no thread will start another such |
|
845 |
// operation. If this channel is registered then we don't close |
|
846 |
// the fd since it might be in use by a selector. In that case |
|
847 |
// closing this channel caused its keys to be cancelled, so the |
|
848 |
// last selector to deregister a key for this channel will invoke |
|
849 |
// kill() to close the fd. |
|
850 |
// |
|
851 |
if (!isRegistered()) |
|
852 |
kill(); |
|
853 |
} |
|
854 |
} |
|
855 |
||
856 |
public void kill() throws IOException { |
|
857 |
synchronized (stateLock) { |
|
858 |
if (state == ST_KILLED) |
|
859 |
return; |
|
860 |
if (state == ST_UNINITIALIZED) { |
|
861 |
state = ST_KILLED; |
|
862 |
return; |
|
863 |
} |
|
864 |
assert !isOpen() && !isRegistered(); |
|
865 |
||
866 |
// Postpone the kill if there is a waiting reader |
|
867 |
// or writer thread. See the comments in read() for |
|
868 |
// more detailed explanation. |
|
869 |
if (readerThread == 0 && writerThread == 0) { |
|
870 |
nd.close(fd); |
|
871 |
state = ST_KILLED; |
|
872 |
} else { |
|
873 |
state = ST_KILLPENDING; |
|
874 |
} |
|
875 |
} |
|
876 |
} |
|
877 |
||
878 |
/** |
|
879 |
* Translates native poll revent ops into a ready operation ops |
|
880 |
*/ |
|
881 |
public boolean translateReadyOps(int ops, int initialOps, |
|
882 |
SelectionKeyImpl sk) { |
|
883 |
int intOps = sk.nioInterestOps(); // Do this just once, it synchronizes |
|
884 |
int oldOps = sk.nioReadyOps(); |
|
885 |
int newOps = initialOps; |
|
886 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
887 |
if ((ops & Net.POLLNVAL) != 0) { |
2 | 888 |
// This should only happen if this channel is pre-closed while a |
889 |
// selection operation is in progress |
|
890 |
// ## Throw an error if this channel has not been pre-closed |
|
891 |
return false; |
|
892 |
} |
|
893 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
894 |
if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) { |
2 | 895 |
newOps = intOps; |
896 |
sk.nioReadyOps(newOps); |
|
897 |
// No need to poll again in checkConnect, |
|
898 |
// the error will be detected there |
|
899 |
readyToConnect = true; |
|
900 |
return (newOps & ~oldOps) != 0; |
|
901 |
} |
|
902 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
903 |
if (((ops & Net.POLLIN) != 0) && |
2 | 904 |
((intOps & SelectionKey.OP_READ) != 0) && |
905 |
(state == ST_CONNECTED)) |
|
906 |
newOps |= SelectionKey.OP_READ; |
|
907 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
908 |
if (((ops & Net.POLLCONN) != 0) && |
2 | 909 |
((intOps & SelectionKey.OP_CONNECT) != 0) && |
910 |
((state == ST_UNCONNECTED) || (state == ST_PENDING))) { |
|
911 |
newOps |= SelectionKey.OP_CONNECT; |
|
912 |
readyToConnect = true; |
|
913 |
} |
|
914 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
915 |
if (((ops & Net.POLLOUT) != 0) && |
2 | 916 |
((intOps & SelectionKey.OP_WRITE) != 0) && |
917 |
(state == ST_CONNECTED)) |
|
918 |
newOps |= SelectionKey.OP_WRITE; |
|
919 |
||
920 |
sk.nioReadyOps(newOps); |
|
921 |
return (newOps & ~oldOps) != 0; |
|
922 |
} |
|
923 |
||
924 |
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) { |
|
925 |
return translateReadyOps(ops, sk.nioReadyOps(), sk); |
|
926 |
} |
|
927 |
||
928 |
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) { |
|
929 |
return translateReadyOps(ops, 0, sk); |
|
930 |
} |
|
931 |
||
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
932 |
// package-private |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
933 |
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
|
934 |
assert Thread.holdsLock(blockingLock()) && !isBlocking(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
935 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
936 |
synchronized (readLock) { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
937 |
int n = 0; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
938 |
try { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
939 |
begin(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
940 |
synchronized (stateLock) { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
941 |
if (!isOpen()) |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
942 |
return 0; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
943 |
readerThread = NativeThread.current(); |
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 |
n = Net.poll(fd, events, timeout); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
946 |
} finally { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
947 |
readerCleanup(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
948 |
end(n > 0); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
949 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
950 |
return n; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
951 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
952 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12683
diff
changeset
|
953 |
|
2 | 954 |
/** |
955 |
* Translates an interest operation set into a native poll event set |
|
956 |
*/ |
|
957 |
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) { |
|
958 |
int newOps = 0; |
|
959 |
if ((ops & SelectionKey.OP_READ) != 0) |
|
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
960 |
newOps |= Net.POLLIN; |
2 | 961 |
if ((ops & SelectionKey.OP_WRITE) != 0) |
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
962 |
newOps |= Net.POLLOUT; |
2 | 963 |
if ((ops & SelectionKey.OP_CONNECT) != 0) |
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
964 |
newOps |= Net.POLLCONN; |
2 | 965 |
sk.selector.putEventOps(sk, newOps); |
966 |
} |
|
967 |
||
968 |
public FileDescriptor getFD() { |
|
969 |
return fd; |
|
970 |
} |
|
971 |
||
972 |
public int getFDVal() { |
|
973 |
return fdVal; |
|
974 |
} |
|
975 |
||
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
976 |
@Override |
2 | 977 |
public String toString() { |
978 |
StringBuffer sb = new StringBuffer(); |
|
979 |
sb.append(this.getClass().getSuperclass().getName()); |
|
980 |
sb.append('['); |
|
981 |
if (!isOpen()) |
|
982 |
sb.append("closed"); |
|
983 |
else { |
|
984 |
synchronized (stateLock) { |
|
985 |
switch (state) { |
|
986 |
case ST_UNCONNECTED: |
|
987 |
sb.append("unconnected"); |
|
988 |
break; |
|
989 |
case ST_PENDING: |
|
990 |
sb.append("connection-pending"); |
|
991 |
break; |
|
992 |
case ST_CONNECTED: |
|
993 |
sb.append("connected"); |
|
994 |
if (!isInputOpen) |
|
995 |
sb.append(" ishut"); |
|
996 |
if (!isOutputOpen) |
|
997 |
sb.append(" oshut"); |
|
998 |
break; |
|
999 |
} |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
1000 |
InetSocketAddress addr = localAddress(); |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
1001 |
if (addr != null) { |
2 | 1002 |
sb.append(" local="); |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
1003 |
sb.append(Net.getRevealedLocalAddressAsString(addr)); |
2 | 1004 |
} |
1005 |
if (remoteAddress() != null) { |
|
1006 |
sb.append(" remote="); |
|
1007 |
sb.append(remoteAddress().toString()); |
|
1008 |
} |
|
1009 |
} |
|
1010 |
} |
|
1011 |
sb.append(']'); |
|
1012 |
return sb.toString(); |
|
1013 |
} |
|
1014 |
||
1015 |
||
1016 |
// -- Native methods -- |
|
1017 |
||
1018 |
private static native int checkConnect(FileDescriptor fd, |
|
1019 |
boolean block, boolean ready) |
|
1020 |
throws IOException; |
|
1021 |
||
6117
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
1022 |
private static native int sendOutOfBandData(FileDescriptor fd, byte data) |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
1023 |
throws IOException; |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
1024 |
|
2 | 1025 |
static { |
19607
bee007586d06
8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents:
18212
diff
changeset
|
1026 |
IOUtil.load(); |
2 | 1027 |
nd = new SocketDispatcher(); |
1028 |
} |
|
1029 |
||
1030 |
} |