author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 36115 | jdk/src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java@0676e37a0b9c |
child 48750 | ffbb784a8873 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
36115 | 2 |
* Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.nio.ch; |
|
27 |
||
28 |
import java.io.FileDescriptor; |
|
29 |
import java.io.IOException; |
|
30 |
import java.net.*; |
|
31 |
import java.nio.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 |
* An implementation of ServerSocketChannels |
|
38 |
*/ |
|
39 |
||
40 |
class ServerSocketChannelImpl |
|
41 |
extends ServerSocketChannel |
|
42 |
implements SelChImpl |
|
43 |
{ |
|
44 |
||
45 |
// Used to make native close and configure calls |
|
46 |
private static NativeDispatcher nd; |
|
47 |
||
48 |
// Our file descriptor |
|
49 |
private final FileDescriptor fd; |
|
50 |
||
51 |
// fd value needed for dev/poll. This value will remain valid |
|
52 |
// even after the value in the file descriptor object has been set to -1 |
|
53 |
private int fdVal; |
|
54 |
||
55 |
// ID of native thread currently blocked in this channel, for signalling |
|
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
29986
diff
changeset
|
56 |
private volatile long thread; |
2 | 57 |
|
58 |
// Lock held by thread currently blocked in this channel |
|
59 |
private final Object lock = new Object(); |
|
60 |
||
61 |
// Lock held by any thread that modifies the state fields declared below |
|
62 |
// DO NOT invoke a blocking I/O operation while holding this lock! |
|
63 |
private final Object stateLock = new Object(); |
|
64 |
||
65 |
// -- The following fields are protected by stateLock |
|
66 |
||
67 |
// Channel state, increases monotonically |
|
68 |
private static final int ST_UNINITIALIZED = -1; |
|
69 |
private static final int ST_INUSE = 0; |
|
70 |
private static final int ST_KILLED = 1; |
|
71 |
private int state = ST_UNINITIALIZED; |
|
72 |
||
73 |
// Binding |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
74 |
private InetSocketAddress localAddress; // null => unbound |
2 | 75 |
|
18192 | 76 |
// set true when exclusive binding is on and SO_REUSEADDR is emulated |
77 |
private boolean isReuseAddress; |
|
78 |
||
2 | 79 |
// Our socket adaptor, if any |
80 |
ServerSocket socket; |
|
81 |
||
82 |
// -- End of fields protected by stateLock |
|
83 |
||
84 |
||
6525 | 85 |
ServerSocketChannelImpl(SelectorProvider sp) throws IOException { |
2 | 86 |
super(sp); |
87 |
this.fd = Net.serverSocket(true); |
|
88 |
this.fdVal = IOUtil.fdVal(fd); |
|
89 |
this.state = ST_INUSE; |
|
90 |
} |
|
91 |
||
6525 | 92 |
ServerSocketChannelImpl(SelectorProvider sp, |
93 |
FileDescriptor fd, |
|
94 |
boolean bound) |
|
2 | 95 |
throws IOException |
96 |
{ |
|
97 |
super(sp); |
|
98 |
this.fd = fd; |
|
99 |
this.fdVal = IOUtil.fdVal(fd); |
|
100 |
this.state = ST_INUSE; |
|
6525 | 101 |
if (bound) |
102 |
localAddress = Net.localAddress(fd); |
|
2 | 103 |
} |
104 |
||
105 |
public ServerSocket socket() { |
|
106 |
synchronized (stateLock) { |
|
107 |
if (socket == null) |
|
108 |
socket = ServerSocketAdaptor.create(this); |
|
109 |
return socket; |
|
110 |
} |
|
111 |
} |
|
112 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
113 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
114 |
public SocketAddress getLocalAddress() throws IOException { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
115 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
116 |
if (!isOpen()) |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
117 |
throw new ClosedChannelException(); |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
118 |
return localAddress == null ? localAddress |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
119 |
: Net.getRevealedLocalAddress( |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
120 |
Net.asInetSocketAddress(localAddress)); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
121 |
} |
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 |
@Override |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
125 |
public <T> ServerSocketChannel setOption(SocketOption<T> name, T value) |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
126 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
127 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
128 |
if (name == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
129 |
throw new NullPointerException(); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
130 |
if (!supportedOptions().contains(name)) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
131 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
132 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
133 |
if (!isOpen()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
134 |
throw new ClosedChannelException(); |
25170
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
135 |
|
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
136 |
if (name == StandardSocketOptions.IP_TOS) { |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
137 |
ProtocolFamily family = Net.isIPv6Available() ? |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
138 |
StandardProtocolFamily.INET6 : StandardProtocolFamily.INET; |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
139 |
Net.setSocketOption(fd, family, name, value); |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
140 |
return this; |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
141 |
} |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
142 |
|
18192 | 143 |
if (name == StandardSocketOptions.SO_REUSEADDR && |
144 |
Net.useExclusiveBind()) |
|
145 |
{ |
|
146 |
// SO_REUSEADDR emulated when using exclusive bind |
|
147 |
isReuseAddress = (Boolean)value; |
|
148 |
} else { |
|
149 |
// no options that require special handling |
|
150 |
Net.setSocketOption(fd, Net.UNSPEC, name, value); |
|
151 |
} |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
152 |
return this; |
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 |
} |
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 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
157 |
@SuppressWarnings("unchecked") |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
158 |
public <T> T getOption(SocketOption<T> name) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
159 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
160 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
161 |
if (name == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
162 |
throw new NullPointerException(); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
163 |
if (!supportedOptions().contains(name)) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
164 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
1152
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 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
167 |
if (!isOpen()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
168 |
throw new ClosedChannelException(); |
18192 | 169 |
if (name == StandardSocketOptions.SO_REUSEADDR && |
170 |
Net.useExclusiveBind()) |
|
171 |
{ |
|
172 |
// SO_REUSEADDR emulated when using exclusive bind |
|
173 |
return (T)Boolean.valueOf(isReuseAddress); |
|
174 |
} |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
175 |
// no options that require special handling |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
176 |
return (T) Net.getSocketOption(fd, Net.UNSPEC, name); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
177 |
} |
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 |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
180 |
private static class DefaultOptionsHolder { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
181 |
static final Set<SocketOption<?>> defaultOptions = defaultOptions(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
182 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
183 |
private static Set<SocketOption<?>> defaultOptions() { |
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
27300
diff
changeset
|
184 |
HashSet<SocketOption<?>> set = new HashSet<>(2); |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
185 |
set.add(StandardSocketOptions.SO_RCVBUF); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
186 |
set.add(StandardSocketOptions.SO_REUSEADDR); |
36115 | 187 |
if (Net.isReusePortAvailable()) { |
188 |
set.add(StandardSocketOptions.SO_REUSEPORT); |
|
189 |
} |
|
25170
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
190 |
set.add(StandardSocketOptions.IP_TOS); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
191 |
return Collections.unmodifiableSet(set); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
192 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
193 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
194 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
195 |
@Override |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
196 |
public final Set<SocketOption<?>> supportedOptions() { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
197 |
return DefaultOptionsHolder.defaultOptions; |
1152
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 |
|
2 | 200 |
public boolean isBound() { |
201 |
synchronized (stateLock) { |
|
202 |
return localAddress != null; |
|
203 |
} |
|
204 |
} |
|
205 |
||
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
206 |
public InetSocketAddress localAddress() { |
2 | 207 |
synchronized (stateLock) { |
208 |
return localAddress; |
|
209 |
} |
|
210 |
} |
|
211 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
212 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
213 |
public ServerSocketChannel bind(SocketAddress local, int backlog) throws IOException { |
2 | 214 |
synchronized (lock) { |
215 |
if (!isOpen()) |
|
216 |
throw new ClosedChannelException(); |
|
217 |
if (isBound()) |
|
218 |
throw new AlreadyBoundException(); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
219 |
InetSocketAddress isa = (local == null) ? new InetSocketAddress(0) : |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
220 |
Net.checkAddress(local); |
2 | 221 |
SecurityManager sm = System.getSecurityManager(); |
222 |
if (sm != null) |
|
223 |
sm.checkListen(isa.getPort()); |
|
2446 | 224 |
NetHooks.beforeTcpBind(fd, isa.getAddress(), isa.getPort()); |
2 | 225 |
Net.bind(fd, isa.getAddress(), isa.getPort()); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
226 |
Net.listen(fd, backlog < 1 ? 50 : backlog); |
2 | 227 |
synchronized (stateLock) { |
228 |
localAddress = Net.localAddress(fd); |
|
229 |
} |
|
230 |
} |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
231 |
return this; |
2 | 232 |
} |
233 |
||
234 |
public SocketChannel accept() throws IOException { |
|
235 |
synchronized (lock) { |
|
236 |
if (!isOpen()) |
|
237 |
throw new ClosedChannelException(); |
|
238 |
if (!isBound()) |
|
239 |
throw new NotYetBoundException(); |
|
240 |
SocketChannel sc = null; |
|
241 |
||
242 |
int n = 0; |
|
243 |
FileDescriptor newfd = new FileDescriptor(); |
|
244 |
InetSocketAddress[] isaa = new InetSocketAddress[1]; |
|
245 |
||
246 |
try { |
|
247 |
begin(); |
|
248 |
if (!isOpen()) |
|
249 |
return null; |
|
250 |
thread = NativeThread.current(); |
|
251 |
for (;;) { |
|
27300
c4394453e952
8062501: Modifications of server socket channel accept() methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
252 |
n = accept(this.fd, newfd, isaa); |
2 | 253 |
if ((n == IOStatus.INTERRUPTED) && isOpen()) |
254 |
continue; |
|
255 |
break; |
|
256 |
} |
|
257 |
} finally { |
|
258 |
thread = 0; |
|
259 |
end(n > 0); |
|
260 |
assert IOStatus.check(n); |
|
261 |
} |
|
262 |
||
263 |
if (n < 1) |
|
264 |
return null; |
|
265 |
||
266 |
IOUtil.configureBlocking(newfd, true); |
|
267 |
InetSocketAddress isa = isaa[0]; |
|
268 |
sc = new SocketChannelImpl(provider(), newfd, isa); |
|
269 |
SecurityManager sm = System.getSecurityManager(); |
|
270 |
if (sm != null) { |
|
271 |
try { |
|
272 |
sm.checkAccept(isa.getAddress().getHostAddress(), |
|
273 |
isa.getPort()); |
|
274 |
} catch (SecurityException x) { |
|
275 |
sc.close(); |
|
276 |
throw x; |
|
277 |
} |
|
278 |
} |
|
279 |
return sc; |
|
280 |
||
281 |
} |
|
282 |
} |
|
283 |
||
284 |
protected void implConfigureBlocking(boolean block) throws IOException { |
|
285 |
IOUtil.configureBlocking(fd, block); |
|
286 |
} |
|
287 |
||
288 |
protected void implCloseSelectableChannel() throws IOException { |
|
289 |
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
|
290 |
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
|
291 |
nd.preClose(fd); |
2 | 292 |
long th = thread; |
293 |
if (th != 0) |
|
294 |
NativeThread.signal(th); |
|
295 |
if (!isRegistered()) |
|
296 |
kill(); |
|
297 |
} |
|
298 |
} |
|
299 |
||
300 |
public void kill() throws IOException { |
|
301 |
synchronized (stateLock) { |
|
302 |
if (state == ST_KILLED) |
|
303 |
return; |
|
304 |
if (state == ST_UNINITIALIZED) { |
|
305 |
state = ST_KILLED; |
|
306 |
return; |
|
307 |
} |
|
308 |
assert !isOpen() && !isRegistered(); |
|
309 |
nd.close(fd); |
|
310 |
state = ST_KILLED; |
|
311 |
} |
|
312 |
} |
|
313 |
||
314 |
/** |
|
315 |
* Translates native poll revent set into a ready operation set |
|
316 |
*/ |
|
317 |
public boolean translateReadyOps(int ops, int initialOps, |
|
318 |
SelectionKeyImpl sk) { |
|
319 |
int intOps = sk.nioInterestOps(); // Do this just once, it synchronizes |
|
320 |
int oldOps = sk.nioReadyOps(); |
|
321 |
int newOps = initialOps; |
|
322 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
323 |
if ((ops & Net.POLLNVAL) != 0) { |
2 | 324 |
// This should only happen if this channel is pre-closed while a |
325 |
// selection operation is in progress |
|
326 |
// ## Throw an error if this channel has not been pre-closed |
|
327 |
return false; |
|
328 |
} |
|
329 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
330 |
if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) { |
2 | 331 |
newOps = intOps; |
332 |
sk.nioReadyOps(newOps); |
|
333 |
return (newOps & ~oldOps) != 0; |
|
334 |
} |
|
335 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
336 |
if (((ops & Net.POLLIN) != 0) && |
2 | 337 |
((intOps & SelectionKey.OP_ACCEPT) != 0)) |
338 |
newOps |= SelectionKey.OP_ACCEPT; |
|
339 |
||
340 |
sk.nioReadyOps(newOps); |
|
341 |
return (newOps & ~oldOps) != 0; |
|
342 |
} |
|
343 |
||
344 |
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) { |
|
345 |
return translateReadyOps(ops, sk.nioReadyOps(), sk); |
|
346 |
} |
|
347 |
||
348 |
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) { |
|
349 |
return translateReadyOps(ops, 0, sk); |
|
350 |
} |
|
351 |
||
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
352 |
// package-private |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
353 |
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
|
354 |
assert Thread.holdsLock(blockingLock()) && !isBlocking(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
355 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
356 |
synchronized (lock) { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
357 |
int n = 0; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
358 |
try { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
359 |
begin(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
360 |
synchronized (stateLock) { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
361 |
if (!isOpen()) |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
362 |
return 0; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
363 |
thread = NativeThread.current(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
364 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
365 |
n = Net.poll(fd, events, timeout); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
366 |
} finally { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
367 |
thread = 0; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
368 |
end(n > 0); |
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 |
return n; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
371 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
372 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12440
diff
changeset
|
373 |
|
2 | 374 |
/** |
375 |
* Translates an interest operation set into a native poll event set |
|
376 |
*/ |
|
377 |
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) { |
|
378 |
int newOps = 0; |
|
379 |
||
380 |
// Translate ops |
|
381 |
if ((ops & SelectionKey.OP_ACCEPT) != 0) |
|
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
382 |
newOps |= Net.POLLIN; |
2 | 383 |
// Place ops into pollfd array |
384 |
sk.selector.putEventOps(sk, newOps); |
|
385 |
} |
|
386 |
||
387 |
public FileDescriptor getFD() { |
|
388 |
return fd; |
|
389 |
} |
|
390 |
||
391 |
public int getFDVal() { |
|
392 |
return fdVal; |
|
393 |
} |
|
394 |
||
395 |
public String toString() { |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
23010
diff
changeset
|
396 |
StringBuilder sb = new StringBuilder(); |
2 | 397 |
sb.append(this.getClass().getName()); |
398 |
sb.append('['); |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
399 |
if (!isOpen()) { |
2 | 400 |
sb.append("closed"); |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
401 |
} else { |
2 | 402 |
synchronized (stateLock) { |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
403 |
InetSocketAddress addr = localAddress(); |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
404 |
if (addr == null) { |
2 | 405 |
sb.append("unbound"); |
406 |
} else { |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
407 |
sb.append(Net.getRevealedLocalAddressAsString(addr)); |
2 | 408 |
} |
409 |
} |
|
410 |
} |
|
411 |
sb.append(']'); |
|
412 |
return sb.toString(); |
|
413 |
} |
|
414 |
||
27300
c4394453e952
8062501: Modifications of server socket channel accept() methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
415 |
/** |
c4394453e952
8062501: Modifications of server socket channel accept() methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
416 |
* Accept a connection on a socket. |
c4394453e952
8062501: Modifications of server socket channel accept() methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
417 |
* |
c4394453e952
8062501: Modifications of server socket channel accept() methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
418 |
* @implNote Wrap native call to allow instrumentation. |
c4394453e952
8062501: Modifications of server socket channel accept() methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
419 |
*/ |
c4394453e952
8062501: Modifications of server socket channel accept() methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
420 |
private int accept(FileDescriptor ssfd, FileDescriptor newfd, |
c4394453e952
8062501: Modifications of server socket channel accept() methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
421 |
InetSocketAddress[] isaa) |
c4394453e952
8062501: Modifications of server socket channel accept() methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
422 |
throws IOException |
c4394453e952
8062501: Modifications of server socket channel accept() methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
423 |
{ |
c4394453e952
8062501: Modifications of server socket channel accept() methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
424 |
return accept0(ssfd, newfd, isaa); |
c4394453e952
8062501: Modifications of server socket channel accept() methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
425 |
} |
c4394453e952
8062501: Modifications of server socket channel accept() methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
426 |
|
2 | 427 |
// -- Native methods -- |
428 |
||
429 |
// Accepts a new connection, setting the given file descriptor to refer to |
|
430 |
// the new socket and setting isaa[0] to the socket's remote address. |
|
431 |
// Returns 1 on success, or IOStatus.UNAVAILABLE (if non-blocking and no |
|
432 |
// connections are pending) or IOStatus.INTERRUPTED. |
|
433 |
// |
|
434 |
private native int accept0(FileDescriptor ssfd, FileDescriptor newfd, |
|
435 |
InetSocketAddress[] isaa) |
|
436 |
throws IOException; |
|
437 |
||
438 |
private static native void initIDs(); |
|
439 |
||
440 |
static { |
|
19607
bee007586d06
8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents:
18212
diff
changeset
|
441 |
IOUtil.load(); |
2 | 442 |
initIDs(); |
443 |
nd = new SocketDispatcher(); |
|
444 |
} |
|
445 |
||
446 |
} |