author | michaelm |
Wed, 25 Jun 2014 15:55:42 +0100 | |
changeset 25170 | f58832169add |
parent 24969 | afa6934dd8e8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
22604
diff
changeset
|
2 |
* Copyright (c) 2000, 2013, 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.channels.*; |
|
32 |
import java.nio.channels.spi.*; |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
33 |
import java.util.*; |
2446 | 34 |
import sun.net.NetHooks; |
2 | 35 |
|
36 |
||
37 |
/** |
|
38 |
* An implementation of ServerSocketChannels |
|
39 |
*/ |
|
40 |
||
41 |
class ServerSocketChannelImpl |
|
42 |
extends ServerSocketChannel |
|
43 |
implements SelChImpl |
|
44 |
{ |
|
45 |
||
46 |
// Used to make native close and configure calls |
|
47 |
private static NativeDispatcher nd; |
|
48 |
||
49 |
// Our file descriptor |
|
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 int fdVal; |
|
55 |
||
56 |
// ID of native thread currently blocked in this channel, for signalling |
|
57 |
private volatile long thread = 0; |
|
58 |
||
59 |
// Lock held by thread currently blocked in this channel |
|
60 |
private final Object lock = new Object(); |
|
61 |
||
62 |
// Lock held by any thread that modifies the state fields declared below |
|
63 |
// DO NOT invoke a blocking I/O operation while holding this lock! |
|
64 |
private final Object stateLock = new Object(); |
|
65 |
||
66 |
// -- The following fields are protected by stateLock |
|
67 |
||
68 |
// Channel state, increases monotonically |
|
69 |
private static final int ST_UNINITIALIZED = -1; |
|
70 |
private static final int ST_INUSE = 0; |
|
71 |
private static final int ST_KILLED = 1; |
|
72 |
private int state = ST_UNINITIALIZED; |
|
73 |
||
74 |
// Binding |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
75 |
private InetSocketAddress localAddress; // null => unbound |
2 | 76 |
|
18192 | 77 |
// set true when exclusive binding is on and SO_REUSEADDR is emulated |
78 |
private boolean isReuseAddress; |
|
79 |
||
2 | 80 |
// Our socket adaptor, if any |
81 |
ServerSocket socket; |
|
82 |
||
83 |
// -- End of fields protected by stateLock |
|
84 |
||
85 |
||
6525 | 86 |
ServerSocketChannelImpl(SelectorProvider sp) throws IOException { |
2 | 87 |
super(sp); |
88 |
this.fd = Net.serverSocket(true); |
|
89 |
this.fdVal = IOUtil.fdVal(fd); |
|
90 |
this.state = ST_INUSE; |
|
91 |
} |
|
92 |
||
6525 | 93 |
ServerSocketChannelImpl(SelectorProvider sp, |
94 |
FileDescriptor fd, |
|
95 |
boolean bound) |
|
2 | 96 |
throws IOException |
97 |
{ |
|
98 |
super(sp); |
|
99 |
this.fd = fd; |
|
100 |
this.fdVal = IOUtil.fdVal(fd); |
|
101 |
this.state = ST_INUSE; |
|
6525 | 102 |
if (bound) |
103 |
localAddress = Net.localAddress(fd); |
|
2 | 104 |
} |
105 |
||
106 |
public ServerSocket socket() { |
|
107 |
synchronized (stateLock) { |
|
108 |
if (socket == null) |
|
109 |
socket = ServerSocketAdaptor.create(this); |
|
110 |
return socket; |
|
111 |
} |
|
112 |
} |
|
113 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
114 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
115 |
public SocketAddress getLocalAddress() throws IOException { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
116 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
117 |
if (!isOpen()) |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
118 |
throw new ClosedChannelException(); |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
119 |
return localAddress == null ? localAddress |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
120 |
: Net.getRevealedLocalAddress( |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
121 |
Net.asInetSocketAddress(localAddress)); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
122 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
123 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
124 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
125 |
@Override |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
126 |
public <T> ServerSocketChannel setOption(SocketOption<T> name, T value) |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
127 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
128 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
129 |
if (name == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
130 |
throw new NullPointerException(); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
131 |
if (!supportedOptions().contains(name)) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
132 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
133 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
134 |
if (!isOpen()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
135 |
throw new ClosedChannelException(); |
25170
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
136 |
|
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
137 |
if (name == StandardSocketOptions.IP_TOS) { |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
138 |
ProtocolFamily family = Net.isIPv6Available() ? |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
139 |
StandardProtocolFamily.INET6 : StandardProtocolFamily.INET; |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
140 |
Net.setSocketOption(fd, family, name, value); |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
141 |
return this; |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
142 |
} |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
143 |
|
18192 | 144 |
if (name == StandardSocketOptions.SO_REUSEADDR && |
145 |
Net.useExclusiveBind()) |
|
146 |
{ |
|
147 |
// SO_REUSEADDR emulated when using exclusive bind |
|
148 |
isReuseAddress = (Boolean)value; |
|
149 |
} else { |
|
150 |
// no options that require special handling |
|
151 |
Net.setSocketOption(fd, Net.UNSPEC, name, value); |
|
152 |
} |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
153 |
return this; |
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 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
156 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
157 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
158 |
@SuppressWarnings("unchecked") |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
159 |
public <T> T getOption(SocketOption<T> name) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
160 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
161 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
162 |
if (name == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
163 |
throw new NullPointerException(); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
164 |
if (!supportedOptions().contains(name)) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
165 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
166 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
167 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
168 |
if (!isOpen()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
169 |
throw new ClosedChannelException(); |
18192 | 170 |
if (name == StandardSocketOptions.SO_REUSEADDR && |
171 |
Net.useExclusiveBind()) |
|
172 |
{ |
|
173 |
// SO_REUSEADDR emulated when using exclusive bind |
|
174 |
return (T)Boolean.valueOf(isReuseAddress); |
|
175 |
} |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
176 |
// no options that require special handling |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
177 |
return (T) Net.getSocketOption(fd, Net.UNSPEC, name); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
178 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
179 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
180 |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
181 |
private static class DefaultOptionsHolder { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
182 |
static final Set<SocketOption<?>> defaultOptions = defaultOptions(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
183 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
184 |
private static Set<SocketOption<?>> defaultOptions() { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
185 |
HashSet<SocketOption<?>> set = new HashSet<SocketOption<?>>(2); |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
186 |
set.add(StandardSocketOptions.SO_RCVBUF); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
187 |
set.add(StandardSocketOptions.SO_REUSEADDR); |
25170
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
188 |
set.add(StandardSocketOptions.IP_TOS); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
189 |
return Collections.unmodifiableSet(set); |
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 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
193 |
@Override |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
194 |
public final Set<SocketOption<?>> supportedOptions() { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
195 |
return DefaultOptionsHolder.defaultOptions; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
196 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
197 |
|
2 | 198 |
public boolean isBound() { |
199 |
synchronized (stateLock) { |
|
200 |
return localAddress != null; |
|
201 |
} |
|
202 |
} |
|
203 |
||
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
204 |
public InetSocketAddress localAddress() { |
2 | 205 |
synchronized (stateLock) { |
206 |
return localAddress; |
|
207 |
} |
|
208 |
} |
|
209 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
210 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
211 |
public ServerSocketChannel bind(SocketAddress local, int backlog) throws IOException { |
2 | 212 |
synchronized (lock) { |
213 |
if (!isOpen()) |
|
214 |
throw new ClosedChannelException(); |
|
215 |
if (isBound()) |
|
216 |
throw new AlreadyBoundException(); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
217 |
InetSocketAddress isa = (local == null) ? new InetSocketAddress(0) : |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
218 |
Net.checkAddress(local); |
2 | 219 |
SecurityManager sm = System.getSecurityManager(); |
220 |
if (sm != null) |
|
221 |
sm.checkListen(isa.getPort()); |
|
2446 | 222 |
NetHooks.beforeTcpBind(fd, isa.getAddress(), isa.getPort()); |
2 | 223 |
Net.bind(fd, isa.getAddress(), isa.getPort()); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
224 |
Net.listen(fd, backlog < 1 ? 50 : backlog); |
2 | 225 |
synchronized (stateLock) { |
226 |
localAddress = Net.localAddress(fd); |
|
227 |
} |
|
228 |
} |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
229 |
return this; |
2 | 230 |
} |
231 |
||
232 |
public SocketChannel accept() throws IOException { |
|
233 |
synchronized (lock) { |
|
234 |
if (!isOpen()) |
|
235 |
throw new ClosedChannelException(); |
|
236 |
if (!isBound()) |
|
237 |
throw new NotYetBoundException(); |
|
238 |
SocketChannel sc = null; |
|
239 |
||
240 |
int n = 0; |
|
241 |
FileDescriptor newfd = new FileDescriptor(); |
|
242 |
InetSocketAddress[] isaa = new InetSocketAddress[1]; |
|
243 |
||
244 |
try { |
|
245 |
begin(); |
|
246 |
if (!isOpen()) |
|
247 |
return null; |
|
248 |
thread = NativeThread.current(); |
|
249 |
for (;;) { |
|
250 |
n = accept0(this.fd, newfd, isaa); |
|
251 |
if ((n == IOStatus.INTERRUPTED) && isOpen()) |
|
252 |
continue; |
|
253 |
break; |
|
254 |
} |
|
255 |
} finally { |
|
256 |
thread = 0; |
|
257 |
end(n > 0); |
|
258 |
assert IOStatus.check(n); |
|
259 |
} |
|
260 |
||
261 |
if (n < 1) |
|
262 |
return null; |
|
263 |
||
264 |
IOUtil.configureBlocking(newfd, true); |
|
265 |
InetSocketAddress isa = isaa[0]; |
|
266 |
sc = new SocketChannelImpl(provider(), newfd, isa); |
|
267 |
SecurityManager sm = System.getSecurityManager(); |
|
268 |
if (sm != null) { |
|
269 |
try { |
|
270 |
sm.checkAccept(isa.getAddress().getHostAddress(), |
|
271 |
isa.getPort()); |
|
272 |
} catch (SecurityException x) { |
|
273 |
sc.close(); |
|
274 |
throw x; |
|
275 |
} |
|
276 |
} |
|
277 |
return sc; |
|
278 |
||
279 |
} |
|
280 |
} |
|
281 |
||
282 |
protected void implConfigureBlocking(boolean block) throws IOException { |
|
283 |
IOUtil.configureBlocking(fd, block); |
|
284 |
} |
|
285 |
||
286 |
protected void implCloseSelectableChannel() throws IOException { |
|
287 |
synchronized (stateLock) { |
|
12440
2689ca179e22
7118373: (se) Potential leak file descriptor when deregistrating at around the same time as an async close
robm
parents:
9679
diff
changeset
|
288 |
if (state != ST_KILLED) |
2689ca179e22
7118373: (se) Potential leak file descriptor when deregistrating at around the same time as an async close
robm
parents:
9679
diff
changeset
|
289 |
nd.preClose(fd); |
2 | 290 |
long th = thread; |
291 |
if (th != 0) |
|
292 |
NativeThread.signal(th); |
|
293 |
if (!isRegistered()) |
|
294 |
kill(); |
|
295 |
} |
|
296 |
} |
|
297 |
||
298 |
public void kill() throws IOException { |
|
299 |
synchronized (stateLock) { |
|
300 |
if (state == ST_KILLED) |
|
301 |
return; |
|
302 |
if (state == ST_UNINITIALIZED) { |
|
303 |
state = ST_KILLED; |
|
304 |
return; |
|
305 |
} |
|
306 |
assert !isOpen() && !isRegistered(); |
|
307 |
nd.close(fd); |
|
308 |
state = ST_KILLED; |
|
309 |
} |
|
310 |
} |
|
311 |
||
312 |
/** |
|
313 |
* Translates native poll revent set into a ready operation set |
|
314 |
*/ |
|
315 |
public boolean translateReadyOps(int ops, int initialOps, |
|
316 |
SelectionKeyImpl sk) { |
|
317 |
int intOps = sk.nioInterestOps(); // Do this just once, it synchronizes |
|
318 |
int oldOps = sk.nioReadyOps(); |
|
319 |
int newOps = initialOps; |
|
320 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
321 |
if ((ops & Net.POLLNVAL) != 0) { |
2 | 322 |
// This should only happen if this channel is pre-closed while a |
323 |
// selection operation is in progress |
|
324 |
// ## Throw an error if this channel has not been pre-closed |
|
325 |
return false; |
|
326 |
} |
|
327 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
328 |
if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) { |
2 | 329 |
newOps = intOps; |
330 |
sk.nioReadyOps(newOps); |
|
331 |
return (newOps & ~oldOps) != 0; |
|
332 |
} |
|
333 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
334 |
if (((ops & Net.POLLIN) != 0) && |
2 | 335 |
((intOps & SelectionKey.OP_ACCEPT) != 0)) |
336 |
newOps |= SelectionKey.OP_ACCEPT; |
|
337 |
||
338 |
sk.nioReadyOps(newOps); |
|
339 |
return (newOps & ~oldOps) != 0; |
|
340 |
} |
|
341 |
||
342 |
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) { |
|
343 |
return translateReadyOps(ops, sk.nioReadyOps(), sk); |
|
344 |
} |
|
345 |
||
346 |
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) { |
|
347 |
return translateReadyOps(ops, 0, sk); |
|
348 |
} |
|
349 |
||
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
350 |
// package-private |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
351 |
int poll(int events, long timeout) throws IOException { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
352 |
assert Thread.holdsLock(blockingLock()) && !isBlocking(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
353 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
354 |
synchronized (lock) { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
355 |
int n = 0; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
356 |
try { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
357 |
begin(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
358 |
synchronized (stateLock) { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
359 |
if (!isOpen()) |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
360 |
return 0; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
361 |
thread = NativeThread.current(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
362 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
363 |
n = Net.poll(fd, events, timeout); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
364 |
} finally { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
365 |
thread = 0; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
366 |
end(n > 0); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
367 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
368 |
return n; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
369 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
370 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
371 |
|
2 | 372 |
/** |
373 |
* Translates an interest operation set into a native poll event set |
|
374 |
*/ |
|
375 |
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) { |
|
376 |
int newOps = 0; |
|
377 |
||
378 |
// Translate ops |
|
379 |
if ((ops & SelectionKey.OP_ACCEPT) != 0) |
|
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
380 |
newOps |= Net.POLLIN; |
2 | 381 |
// Place ops into pollfd array |
382 |
sk.selector.putEventOps(sk, newOps); |
|
383 |
} |
|
384 |
||
385 |
public FileDescriptor getFD() { |
|
386 |
return fd; |
|
387 |
} |
|
388 |
||
389 |
public int getFDVal() { |
|
390 |
return fdVal; |
|
391 |
} |
|
392 |
||
393 |
public String toString() { |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
23010
diff
changeset
|
394 |
StringBuilder sb = new StringBuilder(); |
2 | 395 |
sb.append(this.getClass().getName()); |
396 |
sb.append('['); |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
397 |
if (!isOpen()) { |
2 | 398 |
sb.append("closed"); |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
399 |
} else { |
2 | 400 |
synchronized (stateLock) { |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
401 |
InetSocketAddress addr = localAddress(); |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
402 |
if (addr == null) { |
2 | 403 |
sb.append("unbound"); |
404 |
} else { |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
405 |
sb.append(Net.getRevealedLocalAddressAsString(addr)); |
2 | 406 |
} |
407 |
} |
|
408 |
} |
|
409 |
sb.append(']'); |
|
410 |
return sb.toString(); |
|
411 |
} |
|
412 |
||
413 |
// -- Native methods -- |
|
414 |
||
415 |
// Accepts a new connection, setting the given file descriptor to refer to |
|
416 |
// the new socket and setting isaa[0] to the socket's remote address. |
|
417 |
// Returns 1 on success, or IOStatus.UNAVAILABLE (if non-blocking and no |
|
418 |
// connections are pending) or IOStatus.INTERRUPTED. |
|
419 |
// |
|
420 |
private native int accept0(FileDescriptor ssfd, FileDescriptor newfd, |
|
421 |
InetSocketAddress[] isaa) |
|
422 |
throws IOException; |
|
423 |
||
424 |
private static native void initIDs(); |
|
425 |
||
426 |
static { |
|
19607
bee007586d06
8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents:
18212
diff
changeset
|
427 |
IOUtil.load(); |
2 | 428 |
initIDs(); |
429 |
nd = new SocketDispatcher(); |
|
430 |
} |
|
431 |
||
432 |
} |