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