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