57110
|
1 |
/*
|
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
|
|
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. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle 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 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.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package sun.nio.ch;
|
|
27 |
|
|
28 |
import java.io.FileDescriptor;
|
|
29 |
import java.io.IOException;
|
|
30 |
import java.io.InputStream;
|
|
31 |
import java.io.OutputStream;
|
|
32 |
import java.lang.invoke.MethodHandles;
|
|
33 |
import java.lang.invoke.VarHandle;
|
|
34 |
import java.lang.reflect.Field;
|
|
35 |
import java.net.InetAddress;
|
|
36 |
import java.net.InetSocketAddress;
|
|
37 |
import java.net.ProtocolFamily;
|
|
38 |
import java.net.SocketAddress;
|
|
39 |
import java.net.SocketException;
|
|
40 |
import java.net.SocketImpl;
|
|
41 |
import java.net.SocketOption;
|
|
42 |
import java.net.SocketTimeoutException;
|
|
43 |
import java.net.StandardProtocolFamily;
|
|
44 |
import java.net.StandardSocketOptions;
|
|
45 |
import java.net.UnknownHostException;
|
|
46 |
import java.nio.ByteBuffer;
|
|
47 |
import java.security.AccessController;
|
|
48 |
import java.security.PrivilegedActionException;
|
|
49 |
import java.security.PrivilegedExceptionAction;
|
|
50 |
import java.util.Collections;
|
|
51 |
import java.util.HashSet;
|
|
52 |
import java.util.Objects;
|
|
53 |
import java.util.Set;
|
|
54 |
import java.util.concurrent.TimeUnit;
|
|
55 |
import java.util.concurrent.locks.ReentrantLock;
|
|
56 |
|
|
57 |
import jdk.internal.access.SharedSecrets;
|
|
58 |
import jdk.internal.ref.CleanerFactory;
|
|
59 |
import sun.net.NetHooks;
|
|
60 |
import sun.net.ResourceManager;
|
|
61 |
import sun.net.ext.ExtendedSocketOptions;
|
|
62 |
import sun.net.util.SocketExceptions;
|
|
63 |
|
|
64 |
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
|
65 |
import static java.util.concurrent.TimeUnit.NANOSECONDS;
|
|
66 |
|
|
67 |
/**
|
|
68 |
* NIO based SocketImpl.
|
|
69 |
*
|
|
70 |
* This implementation attempts to be compatible with legacy PlainSocketImpl,
|
|
71 |
* including behavior and exceptions that are not specified by SocketImpl.
|
|
72 |
*
|
|
73 |
* The underlying socket used by this SocketImpl is initially configured
|
|
74 |
* blocking. If a connect, accept or read is attempted with a timeout then the
|
|
75 |
* socket is changed to non-blocking mode. When in non-blocking mode, operations
|
|
76 |
* that don't complete immediately will poll the socket.
|
|
77 |
*
|
|
78 |
* Behavior differences to examine:
|
57111
|
79 |
* - "Connection reset" handling differs to PlainSocketImpl for cases where
|
57110
|
80 |
* an application continues to call read or available after a reset.
|
57111
|
81 |
* - SocketInputStream extends FileInputStream so can cast to FIS and access FD.
|
57110
|
82 |
*/
|
|
83 |
|
|
84 |
public class NioSocketImpl extends SocketImpl {
|
|
85 |
private static final NativeDispatcher nd = new SocketDispatcher();
|
|
86 |
|
|
87 |
// The maximum number of bytes to read/write per syscall to avoid needing
|
|
88 |
// a huge buffer from the temporary buffer cache
|
|
89 |
private static final int MAX_BUFFER_SIZE = 128 * 1024;
|
|
90 |
|
|
91 |
// true if this is a SocketImpl for a ServerSocket
|
|
92 |
private final boolean server;
|
|
93 |
|
57111
|
94 |
// Lock held when reading (also used when accepting or connecting)
|
57110
|
95 |
private final ReentrantLock readLock = new ReentrantLock();
|
|
96 |
|
57111
|
97 |
// Lock held when writing
|
57110
|
98 |
private final ReentrantLock writeLock = new ReentrantLock();
|
|
99 |
|
|
100 |
// The stateLock for read/changing state
|
|
101 |
private final Object stateLock = new Object();
|
|
102 |
private static final int ST_NEW = 0;
|
|
103 |
private static final int ST_UNCONNECTED = 1;
|
|
104 |
private static final int ST_CONNECTING = 2;
|
|
105 |
private static final int ST_CONNECTED = 3;
|
|
106 |
private static final int ST_CLOSING = 4;
|
|
107 |
private static final int ST_CLOSED = 5;
|
|
108 |
private volatile int state; // need stateLock to change
|
|
109 |
|
|
110 |
// set by SocketImpl.create, protected by stateLock
|
|
111 |
private boolean stream;
|
|
112 |
private FileDescriptorCloser closer;
|
|
113 |
|
|
114 |
// lazily set to true when the socket is configured non-blocking
|
|
115 |
private volatile boolean nonBlocking;
|
|
116 |
|
|
117 |
// used by connect/read/write/accept, protected by stateLock
|
|
118 |
private long readerThread;
|
|
119 |
private long writerThread;
|
|
120 |
|
|
121 |
// used when SO_REUSEADDR is emulated
|
|
122 |
private boolean isReuseAddress;
|
|
123 |
|
|
124 |
// read or accept timeout in millis
|
|
125 |
private volatile int timeout;
|
|
126 |
|
|
127 |
// flags to indicate if the connection is shutdown for input and output
|
|
128 |
private volatile boolean isInputClosed;
|
|
129 |
private volatile boolean isOutputClosed;
|
|
130 |
|
|
131 |
/**
|
|
132 |
* Creates a instance of this SocketImpl.
|
|
133 |
* @param server true if this is a SocketImpl for a ServerSocket
|
|
134 |
*/
|
|
135 |
public NioSocketImpl(boolean server) {
|
|
136 |
this.server = server;
|
|
137 |
}
|
|
138 |
|
|
139 |
/**
|
|
140 |
* Returns true if the socket is open.
|
|
141 |
*/
|
|
142 |
private boolean isOpen() {
|
|
143 |
return state < ST_CLOSING;
|
|
144 |
}
|
|
145 |
|
|
146 |
/**
|
|
147 |
* Throws SocketException if the socket is not open.
|
|
148 |
*/
|
|
149 |
private void ensureOpen() throws SocketException {
|
|
150 |
if (state >= ST_CLOSING)
|
|
151 |
throw new SocketException("Socket closed");
|
|
152 |
}
|
|
153 |
|
|
154 |
/**
|
|
155 |
* Throws SocketException if the socket is not open and connected.
|
|
156 |
*/
|
|
157 |
private void ensureOpenAndConnected() throws SocketException {
|
|
158 |
int state = this.state;
|
|
159 |
if (state < ST_CONNECTED)
|
|
160 |
throw new SocketException("Not connected");
|
|
161 |
if (state > ST_CONNECTED)
|
|
162 |
throw new SocketException("Socket closed");
|
|
163 |
}
|
|
164 |
|
|
165 |
/**
|
|
166 |
* Disables the current thread for scheduling purposes until the socket is
|
|
167 |
* ready for I/O, or is asynchronously closed, for up to the specified
|
|
168 |
* waiting time.
|
|
169 |
* @throws IOException if an I/O error occurs
|
|
170 |
*/
|
|
171 |
private void park(int event, long nanos) throws IOException {
|
|
172 |
long millis;
|
|
173 |
if (nanos == 0) {
|
|
174 |
millis = -1;
|
|
175 |
} else {
|
|
176 |
millis = MILLISECONDS.convert(nanos, NANOSECONDS);
|
|
177 |
}
|
|
178 |
Net.poll(fd, event, millis);
|
|
179 |
}
|
|
180 |
|
|
181 |
/**
|
|
182 |
* Disables the current thread for scheduling purposes until the socket is
|
|
183 |
* ready for I/O or is asynchronously closed.
|
|
184 |
* @throws IOException if an I/O error occurs
|
|
185 |
*/
|
|
186 |
private void park(int event) throws IOException {
|
|
187 |
park(event, 0);
|
|
188 |
}
|
|
189 |
|
|
190 |
/**
|
|
191 |
* Ensures that the socket is configured non-blocking when a timeout is specified.
|
|
192 |
* @throws IOException if there is an I/O error changing the blocking mode
|
|
193 |
*/
|
|
194 |
private void maybeConfigureNonBlocking(FileDescriptor fd, int timeout)
|
|
195 |
throws IOException
|
|
196 |
{
|
|
197 |
assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread();
|
|
198 |
if (!nonBlocking && (timeout > 0)) {
|
|
199 |
IOUtil.configureBlocking(fd, false);
|
|
200 |
nonBlocking = true;
|
|
201 |
}
|
|
202 |
}
|
|
203 |
|
|
204 |
/**
|
|
205 |
* Marks the beginning of a read operation that might block.
|
|
206 |
* @throws SocketException if the socket is closed or not connected
|
|
207 |
*/
|
|
208 |
private FileDescriptor beginRead() throws SocketException {
|
|
209 |
synchronized (stateLock) {
|
|
210 |
ensureOpenAndConnected();
|
|
211 |
readerThread = NativeThread.current();
|
|
212 |
assert fd != null;
|
|
213 |
return fd;
|
|
214 |
}
|
|
215 |
}
|
|
216 |
|
|
217 |
/**
|
|
218 |
* Marks the end of a read operation that may have blocked.
|
|
219 |
* @throws SocketException is the socket is closed
|
|
220 |
*/
|
|
221 |
private void endRead(boolean completed) throws SocketException {
|
|
222 |
synchronized (stateLock) {
|
|
223 |
readerThread = 0;
|
|
224 |
int state = this.state;
|
|
225 |
if (state == ST_CLOSING)
|
|
226 |
stateLock.notifyAll();
|
|
227 |
if (!completed && state >= ST_CLOSING)
|
|
228 |
throw new SocketException("Socket closed");
|
|
229 |
}
|
|
230 |
}
|
|
231 |
|
|
232 |
/**
|
|
233 |
* Reads bytes from the socket into the given buffer.
|
|
234 |
* @throws IOException if the socket is closed or an I/O occurs
|
|
235 |
* @throws SocketTimeoutException if the read timeout elapses
|
|
236 |
*/
|
|
237 |
private int read(ByteBuffer dst) throws IOException {
|
|
238 |
readLock.lock();
|
|
239 |
try {
|
|
240 |
int n = 0;
|
|
241 |
FileDescriptor fd = beginRead();
|
|
242 |
try {
|
|
243 |
if (isInputClosed) {
|
|
244 |
return IOStatus.EOF;
|
|
245 |
}
|
|
246 |
int timeout = this.timeout;
|
|
247 |
maybeConfigureNonBlocking(fd, timeout);
|
|
248 |
n = IOUtil.read(fd, dst, -1, nd);
|
|
249 |
if (statusImpliesRetry(n) && isOpen()) {
|
|
250 |
if (timeout > 0) {
|
|
251 |
// read with timeout
|
|
252 |
assert nonBlocking;
|
|
253 |
long nanos = NANOSECONDS.convert(timeout, TimeUnit.MILLISECONDS);
|
|
254 |
do {
|
|
255 |
long startTime = System.nanoTime();
|
|
256 |
park(Net.POLLIN, nanos);
|
|
257 |
n = IOUtil.read(fd, dst, -1, nd);
|
|
258 |
if (n == IOStatus.UNAVAILABLE) {
|
|
259 |
nanos -= System.nanoTime() - startTime;
|
|
260 |
if (nanos <= 0)
|
|
261 |
throw new SocketTimeoutException("read timeout");
|
|
262 |
}
|
|
263 |
} while (n == IOStatus.UNAVAILABLE && isOpen());
|
|
264 |
} else {
|
|
265 |
// read, no timeout
|
|
266 |
do {
|
|
267 |
park(Net.POLLIN);
|
|
268 |
n = IOUtil.read(fd, dst, -1, nd);
|
|
269 |
} while (statusImpliesRetry(n) && isOpen());
|
|
270 |
}
|
|
271 |
}
|
|
272 |
return n;
|
|
273 |
} finally {
|
|
274 |
endRead(n > 0);
|
|
275 |
}
|
|
276 |
} finally {
|
|
277 |
readLock.unlock();
|
|
278 |
}
|
|
279 |
}
|
|
280 |
|
|
281 |
/**
|
|
282 |
* Marks the beginning of a write operation that might block.
|
|
283 |
* @throws SocketException if the socket is closed or not connected
|
|
284 |
*/
|
|
285 |
private FileDescriptor beginWrite() throws SocketException {
|
|
286 |
synchronized (stateLock) {
|
|
287 |
ensureOpenAndConnected();
|
|
288 |
writerThread = NativeThread.current();
|
|
289 |
assert fd != null;
|
|
290 |
return fd;
|
|
291 |
}
|
|
292 |
}
|
|
293 |
|
|
294 |
/**
|
|
295 |
* Marks the end of a write operation that may have blocked.
|
|
296 |
* @throws SocketException is the socket is closed
|
|
297 |
*/
|
|
298 |
private void endWrite(boolean completed) throws SocketException {
|
|
299 |
synchronized (stateLock) {
|
|
300 |
writerThread = 0;
|
|
301 |
int state = this.state;
|
|
302 |
if (state == ST_CLOSING)
|
|
303 |
stateLock.notifyAll();
|
|
304 |
if (!completed && state >= ST_CLOSING)
|
|
305 |
throw new SocketException("Socket closed");
|
|
306 |
}
|
|
307 |
}
|
|
308 |
|
|
309 |
/**
|
|
310 |
* Writes a sequence of bytes to this socket from the given buffer.
|
|
311 |
* @throws IOException if the socket is closed or an I/O occurs
|
|
312 |
*/
|
|
313 |
private int write(ByteBuffer dst) throws IOException {
|
|
314 |
writeLock.lock();
|
|
315 |
try {
|
|
316 |
int n = 0;
|
|
317 |
FileDescriptor fd = beginWrite();
|
|
318 |
try {
|
|
319 |
n = IOUtil.write(fd, dst, -1, nd);
|
|
320 |
while (statusImpliesRetry(n) && isOpen()) {
|
|
321 |
park(Net.POLLOUT);
|
|
322 |
n = IOUtil.write(fd, dst, -1, nd);
|
|
323 |
}
|
|
324 |
return n;
|
|
325 |
} finally {
|
|
326 |
endWrite(n > 0);
|
|
327 |
}
|
|
328 |
} finally {
|
|
329 |
writeLock.unlock();
|
|
330 |
}
|
|
331 |
}
|
|
332 |
|
|
333 |
/**
|
|
334 |
* Creates the socket.
|
|
335 |
* @param stream {@code true} for a streams socket
|
|
336 |
*/
|
|
337 |
@Override
|
|
338 |
protected void create(boolean stream) throws IOException {
|
|
339 |
synchronized (stateLock) {
|
|
340 |
assert state == ST_NEW;
|
|
341 |
if (!stream)
|
|
342 |
ResourceManager.beforeUdpCreate();
|
|
343 |
FileDescriptor fd;
|
|
344 |
try {
|
|
345 |
if (server) {
|
|
346 |
assert stream;
|
|
347 |
fd = Net.serverSocket(true);
|
|
348 |
} else {
|
|
349 |
fd = Net.socket(stream);
|
|
350 |
}
|
|
351 |
} catch (IOException ioe) {
|
|
352 |
if (!stream)
|
|
353 |
ResourceManager.afterUdpClose();
|
|
354 |
throw ioe;
|
|
355 |
}
|
|
356 |
this.fd = fd;
|
|
357 |
this.stream = stream;
|
|
358 |
this.closer = FileDescriptorCloser.create(this);
|
|
359 |
this.state = ST_UNCONNECTED;
|
|
360 |
}
|
|
361 |
}
|
|
362 |
|
|
363 |
/**
|
|
364 |
* For use by ServerSocket to set the state and other fields after a
|
|
365 |
* connection is accepted by a ServerSocket using a custom SocketImpl.
|
|
366 |
* The protected fields defined by SocketImpl should be set.
|
|
367 |
*/
|
|
368 |
public void postCustomAccept() throws IOException {
|
|
369 |
synchronized (stateLock) {
|
|
370 |
assert state == ST_NEW;
|
|
371 |
assert fd.valid() && localport != 0 && address != null && port != 0;
|
|
372 |
IOUtil.configureBlocking(fd, true);
|
|
373 |
stream = true;
|
|
374 |
closer = FileDescriptorCloser.create(this);
|
|
375 |
state = ST_CONNECTED;
|
|
376 |
}
|
|
377 |
}
|
|
378 |
|
|
379 |
/**
|
|
380 |
* For use by ServerSocket to copy the state from this connected SocketImpl
|
|
381 |
* to a target SocketImpl. If the target SocketImpl is not a newly created
|
|
382 |
* SocketImpl then it is first closed to release any resources. The target
|
|
383 |
* SocketImpl becomes the owner of the file descriptor, this SocketImpl
|
|
384 |
* is marked as closed and should be discarded.
|
|
385 |
*/
|
|
386 |
public void copyTo(SocketImpl si) {
|
|
387 |
if (si instanceof NioSocketImpl) {
|
|
388 |
NioSocketImpl nsi = (NioSocketImpl) si;
|
|
389 |
if (nsi.state != ST_NEW) {
|
|
390 |
try {
|
|
391 |
nsi.close();
|
|
392 |
} catch (IOException ignore) { }
|
|
393 |
}
|
|
394 |
synchronized (nsi.stateLock) {
|
|
395 |
assert nsi.state == ST_NEW || nsi.state == ST_CLOSED;
|
|
396 |
synchronized (this.stateLock) {
|
|
397 |
// this SocketImpl should be connected
|
|
398 |
assert state == ST_CONNECTED && fd.valid()
|
|
399 |
&& localport != 0 && address != null && port != 0;
|
|
400 |
|
|
401 |
// copy fields
|
|
402 |
nsi.fd = this.fd;
|
|
403 |
nsi.stream = this.stream;
|
|
404 |
nsi.closer = FileDescriptorCloser.create(nsi);
|
|
405 |
nsi.localport = this.localport;
|
|
406 |
nsi.address = this.address;
|
|
407 |
nsi.port = this.port;
|
|
408 |
nsi.state = ST_CONNECTED;
|
|
409 |
|
|
410 |
// disable closer to prevent GC'ing of this impl from
|
|
411 |
// closing the file descriptor
|
|
412 |
this.closer.disable();
|
|
413 |
this.state = ST_CLOSED;
|
|
414 |
}
|
|
415 |
}
|
|
416 |
} else {
|
|
417 |
synchronized (this.stateLock) {
|
|
418 |
// this SocketImpl should be connected
|
|
419 |
assert state == ST_CONNECTED && fd.valid()
|
|
420 |
&& localport != 0 && address != null && port != 0;
|
|
421 |
|
|
422 |
// set fields in foreign impl
|
|
423 |
setSocketImplFields(si, fd, localport, address, port);
|
|
424 |
|
|
425 |
// disable closer to prevent GC'ing of this impl from
|
|
426 |
// closing the file descriptor
|
|
427 |
this.closer.disable();
|
|
428 |
this.state = ST_CLOSED;
|
|
429 |
}
|
|
430 |
}
|
|
431 |
}
|
|
432 |
|
|
433 |
/**
|
|
434 |
* Marks the beginning of a connect operation that might block.
|
|
435 |
* @throws SocketException if the socket is closed or already connected
|
|
436 |
*/
|
|
437 |
private FileDescriptor beginConnect(InetAddress address, int port)
|
|
438 |
throws IOException
|
|
439 |
{
|
|
440 |
synchronized (stateLock) {
|
|
441 |
int state = this.state;
|
|
442 |
if (state >= ST_CLOSING)
|
|
443 |
throw new SocketException("Socket closed");
|
|
444 |
if (state == ST_CONNECTED)
|
|
445 |
throw new SocketException("Already connected");
|
|
446 |
assert state == ST_UNCONNECTED;
|
|
447 |
this.state = ST_CONNECTING;
|
|
448 |
|
|
449 |
// invoke beforeTcpConnect hook if not already bound
|
|
450 |
if (localport == 0) {
|
|
451 |
NetHooks.beforeTcpConnect(fd, address, port);
|
|
452 |
}
|
|
453 |
|
|
454 |
// save the remote address/port
|
|
455 |
this.address = address;
|
|
456 |
this.port = port;
|
|
457 |
|
|
458 |
readerThread = NativeThread.current();
|
|
459 |
assert fd != null;
|
|
460 |
return fd;
|
|
461 |
}
|
|
462 |
}
|
|
463 |
|
|
464 |
/**
|
|
465 |
* Marks the end of a connect operation that may have blocked.
|
|
466 |
* @throws SocketException is the socket is closed
|
|
467 |
*/
|
|
468 |
private void endConnect(boolean completed) throws IOException {
|
|
469 |
synchronized (stateLock) {
|
|
470 |
readerThread = 0;
|
|
471 |
int state = this.state;
|
|
472 |
if (state == ST_CLOSING)
|
|
473 |
stateLock.notifyAll();
|
|
474 |
if (completed && state == ST_CONNECTING) {
|
|
475 |
this.state = ST_CONNECTED;
|
|
476 |
localport = Net.localAddress(fd).getPort();
|
|
477 |
} else if (!completed && state >= ST_CLOSING) {
|
|
478 |
throw new SocketException("Socket closed");
|
|
479 |
}
|
|
480 |
}
|
|
481 |
}
|
|
482 |
|
|
483 |
/**
|
|
484 |
* Connect the socket. Closes the socket if connection cannot be established.
|
|
485 |
* @throws IllegalArgumentException if the address is not an InetSocketAddress
|
|
486 |
* @throws UnknownHostException if the InetSocketAddress is not resolved
|
|
487 |
* @throws IOException if the connection cannot be established
|
|
488 |
*/
|
|
489 |
private void implConnect(SocketAddress remote, int millis) throws IOException {
|
|
490 |
if (!(remote instanceof InetSocketAddress))
|
|
491 |
throw new IllegalArgumentException("Unsupported address type");
|
|
492 |
InetSocketAddress isa = (InetSocketAddress) remote;
|
|
493 |
if (isa.isUnresolved()) {
|
|
494 |
throw new UnknownHostException(isa.getHostName());
|
|
495 |
}
|
|
496 |
|
|
497 |
InetAddress address = isa.getAddress();
|
|
498 |
if (address.isAnyLocalAddress())
|
|
499 |
address = InetAddress.getLocalHost();
|
|
500 |
int port = isa.getPort();
|
|
501 |
|
|
502 |
try {
|
|
503 |
readLock.lock();
|
|
504 |
try {
|
57111
|
505 |
boolean connected = false;
|
|
506 |
FileDescriptor fd = beginConnect(address, port);
|
57110
|
507 |
try {
|
57111
|
508 |
maybeConfigureNonBlocking(fd, millis);
|
|
509 |
int n = Net.connect(fd, address, port);
|
|
510 |
if (statusImpliesRetry(n) && isOpen()) {
|
|
511 |
if (millis > 0) {
|
|
512 |
// connect with timeout
|
|
513 |
assert nonBlocking;
|
|
514 |
long nanos = NANOSECONDS.convert(millis, MILLISECONDS);
|
|
515 |
do {
|
|
516 |
long startTime = System.nanoTime();
|
|
517 |
park(Net.POLLOUT, nanos);
|
|
518 |
n = Net.polConnectlNow(fd);
|
|
519 |
if (n == 0) {
|
|
520 |
nanos -= System.nanoTime() - startTime;
|
|
521 |
if (nanos <= 0)
|
|
522 |
throw new SocketTimeoutException("connect timeout");
|
|
523 |
}
|
|
524 |
} while (n == 0 && isOpen());
|
|
525 |
} else {
|
|
526 |
// connect, no timeout
|
|
527 |
do {
|
|
528 |
park(Net.POLLOUT);
|
|
529 |
n = Net.polConnectlNow(fd);
|
|
530 |
} while ((n == 0 || n == IOStatus.INTERRUPTED) && isOpen());
|
57110
|
531 |
}
|
|
532 |
}
|
57111
|
533 |
connected = (n > 0) && isOpen();
|
57110
|
534 |
} finally {
|
57111
|
535 |
endConnect(connected);
|
57110
|
536 |
}
|
|
537 |
} finally {
|
|
538 |
readLock.unlock();
|
|
539 |
}
|
|
540 |
} catch (IOException ioe) {
|
|
541 |
close();
|
|
542 |
throw SocketExceptions.of(ioe, isa);
|
|
543 |
}
|
|
544 |
}
|
|
545 |
|
|
546 |
@Override
|
|
547 |
protected void connect(String host, int port) throws IOException {
|
|
548 |
implConnect(new InetSocketAddress(host, port), timeout);
|
|
549 |
}
|
|
550 |
|
|
551 |
@Override
|
|
552 |
protected void connect(InetAddress address, int port) throws IOException {
|
|
553 |
implConnect(new InetSocketAddress(address, port), timeout);
|
|
554 |
}
|
|
555 |
|
|
556 |
@Override
|
|
557 |
protected void connect(SocketAddress address, int timeout) throws IOException {
|
|
558 |
implConnect(address, timeout);
|
|
559 |
}
|
|
560 |
|
|
561 |
@Override
|
|
562 |
protected void bind(InetAddress host, int port) throws IOException {
|
|
563 |
synchronized (stateLock) {
|
|
564 |
ensureOpen();
|
|
565 |
if (localport != 0)
|
|
566 |
throw new SocketException("Already bound");
|
|
567 |
NetHooks.beforeTcpBind(fd, host, port);
|
|
568 |
Net.bind(fd, host, port);
|
|
569 |
// set the address field to the address specified to the method to
|
|
570 |
// keep compatibility with PlainSocketImpl. When binding to 0.0.0.0
|
|
571 |
// then the actual local address will be ::0 when IPv6 is enabled.
|
|
572 |
address = host;
|
|
573 |
localport = Net.localAddress(fd).getPort();
|
|
574 |
}
|
|
575 |
}
|
|
576 |
|
|
577 |
@Override
|
|
578 |
protected void listen(int backlog) throws IOException {
|
|
579 |
synchronized (stateLock) {
|
|
580 |
ensureOpen();
|
|
581 |
if (localport == 0)
|
|
582 |
throw new SocketException("Not bound");
|
|
583 |
Net.listen(fd, backlog < 1 ? 50 : backlog);
|
|
584 |
}
|
|
585 |
}
|
|
586 |
|
|
587 |
/**
|
|
588 |
* Marks the beginning of an accept operation that might block.
|
|
589 |
* @throws SocketException if the socket is closed
|
|
590 |
*/
|
|
591 |
private FileDescriptor beginAccept() throws SocketException {
|
|
592 |
synchronized (stateLock) {
|
|
593 |
ensureOpen();
|
|
594 |
if (!stream)
|
|
595 |
throw new SocketException("Not a stream socket");
|
|
596 |
if (localport == 0)
|
|
597 |
throw new SocketException("Not bound");
|
|
598 |
readerThread = NativeThread.current();
|
|
599 |
assert fd != null;
|
|
600 |
return fd;
|
|
601 |
}
|
|
602 |
}
|
|
603 |
|
|
604 |
/**
|
|
605 |
* Marks the end of an accept operation that may have blocked.
|
|
606 |
* @throws SocketException is the socket is closed
|
|
607 |
*/
|
|
608 |
private void endAccept(boolean completed) throws SocketException {
|
|
609 |
synchronized (stateLock) {
|
|
610 |
int state = this.state;
|
|
611 |
readerThread = 0;
|
|
612 |
if (state == ST_CLOSING)
|
|
613 |
stateLock.notifyAll();
|
|
614 |
if (!completed && state >= ST_CLOSING)
|
|
615 |
throw new SocketException("Socket closed");
|
|
616 |
}
|
|
617 |
}
|
|
618 |
|
|
619 |
@Override
|
|
620 |
protected void accept(SocketImpl si) throws IOException {
|
|
621 |
// accept a connection
|
|
622 |
FileDescriptor newfd = new FileDescriptor();
|
|
623 |
InetSocketAddress[] isaa = new InetSocketAddress[1];
|
|
624 |
readLock.lock();
|
|
625 |
try {
|
|
626 |
int n = 0;
|
|
627 |
FileDescriptor fd = beginAccept();
|
|
628 |
try {
|
|
629 |
int timeout = this.timeout;
|
|
630 |
maybeConfigureNonBlocking(fd, timeout);
|
|
631 |
n = ServerSocketChannelImpl.accept0(fd, newfd, isaa);
|
|
632 |
if (statusImpliesRetry(n) && isOpen()) {
|
|
633 |
if (timeout > 0) {
|
|
634 |
// accept with timeout
|
|
635 |
assert nonBlocking;
|
|
636 |
long nanos = NANOSECONDS.convert(timeout, TimeUnit.MILLISECONDS);
|
|
637 |
do {
|
|
638 |
long startTime = System.nanoTime();
|
|
639 |
park(Net.POLLIN, nanos);
|
|
640 |
n = ServerSocketChannelImpl.accept0(fd, newfd, isaa);
|
|
641 |
if (n == IOStatus.UNAVAILABLE) {
|
|
642 |
nanos -= System.nanoTime() - startTime;
|
|
643 |
if (nanos <= 0)
|
|
644 |
throw new SocketTimeoutException("accept timeout");
|
|
645 |
}
|
|
646 |
} while (n == IOStatus.UNAVAILABLE && isOpen());
|
|
647 |
} else {
|
|
648 |
// accept, no timeout
|
|
649 |
do {
|
|
650 |
park(Net.POLLIN);
|
|
651 |
n = ServerSocketChannelImpl.accept0(fd, newfd, isaa);
|
|
652 |
} while (statusImpliesRetry(n) && isOpen());
|
|
653 |
}
|
|
654 |
}
|
|
655 |
} finally {
|
|
656 |
endAccept(n > 0);
|
|
657 |
assert IOStatus.check(n);
|
|
658 |
}
|
|
659 |
} finally {
|
|
660 |
readLock.unlock();
|
|
661 |
}
|
|
662 |
|
|
663 |
// get local address and configure accepted socket to blocking mode
|
|
664 |
InetSocketAddress localAddress;
|
|
665 |
try {
|
|
666 |
localAddress = Net.localAddress(newfd);
|
|
667 |
IOUtil.configureBlocking(newfd, true);
|
|
668 |
} catch (IOException ioe) {
|
|
669 |
nd.close(newfd);
|
|
670 |
throw ioe;
|
|
671 |
}
|
|
672 |
|
|
673 |
// set the fields
|
|
674 |
InetSocketAddress remoteAddress = isaa[0];
|
|
675 |
if (si instanceof NioSocketImpl) {
|
|
676 |
NioSocketImpl nsi = (NioSocketImpl) si;
|
|
677 |
synchronized (nsi.stateLock) {
|
|
678 |
nsi.fd = newfd;
|
|
679 |
nsi.stream = true;
|
|
680 |
nsi.closer = FileDescriptorCloser.create(nsi);
|
|
681 |
nsi.localport = localAddress.getPort();
|
|
682 |
nsi.address = remoteAddress.getAddress();
|
|
683 |
nsi.port = remoteAddress.getPort();
|
|
684 |
nsi.state = ST_CONNECTED;
|
|
685 |
}
|
|
686 |
} else {
|
|
687 |
// set fields in foreign impl
|
|
688 |
setSocketImplFields(si, newfd,
|
|
689 |
localAddress.getPort(),
|
|
690 |
remoteAddress.getAddress(),
|
|
691 |
remoteAddress.getPort());
|
|
692 |
}
|
|
693 |
}
|
|
694 |
|
|
695 |
@Override
|
|
696 |
protected InputStream getInputStream() {
|
|
697 |
return new InputStream() {
|
|
698 |
private volatile boolean eof;
|
|
699 |
@Override
|
|
700 |
public int read() throws IOException {
|
|
701 |
byte[] a = new byte[1];
|
|
702 |
int n = read(a, 0, 1);
|
|
703 |
return (n > 0) ? (a[0] & 0xff) : -1;
|
|
704 |
}
|
|
705 |
@Override
|
|
706 |
public int read(byte b[], int off, int len) throws IOException {
|
|
707 |
Objects.checkFromIndexSize(off, len, b.length);
|
|
708 |
if (eof) {
|
|
709 |
return -1; // legacy SocketInputStream behavior
|
|
710 |
} else if (len == 0) {
|
|
711 |
return 0;
|
|
712 |
} else {
|
|
713 |
try {
|
|
714 |
// read up to MAX_BUFFER_SIZE bytes
|
|
715 |
int size = Math.min(len, MAX_BUFFER_SIZE);
|
|
716 |
ByteBuffer dst = ByteBuffer.wrap(b, off, size);
|
|
717 |
int n = NioSocketImpl.this.read(dst);
|
|
718 |
if (n == -1)
|
|
719 |
eof = true;
|
|
720 |
return n;
|
|
721 |
} catch (SocketTimeoutException e) {
|
|
722 |
throw e;
|
|
723 |
} catch (IOException ioe) {
|
|
724 |
throw new SocketException(ioe.getMessage());
|
|
725 |
}
|
|
726 |
}
|
|
727 |
}
|
|
728 |
@Override
|
|
729 |
public int available() throws IOException {
|
|
730 |
return NioSocketImpl.this.available();
|
|
731 |
}
|
|
732 |
@Override
|
|
733 |
public void close() throws IOException {
|
|
734 |
NioSocketImpl.this.close();
|
|
735 |
}
|
|
736 |
};
|
|
737 |
}
|
|
738 |
|
|
739 |
@Override
|
|
740 |
protected OutputStream getOutputStream() {
|
|
741 |
return new OutputStream() {
|
|
742 |
@Override
|
|
743 |
public void write(int b) throws IOException {
|
|
744 |
byte[] a = new byte[] { (byte) b };
|
|
745 |
write(a, 0, 1);
|
|
746 |
}
|
|
747 |
@Override
|
|
748 |
public void write(byte b[], int off, int len) throws IOException {
|
|
749 |
Objects.checkFromIndexSize(off, len, b.length);
|
|
750 |
if (len > 0) {
|
|
751 |
try {
|
|
752 |
ByteBuffer src = ByteBuffer.wrap(b, off, len);
|
|
753 |
int end = src.limit();
|
|
754 |
int pos;
|
|
755 |
// write up to MAX_BUFFER_SIZE bytes at a time
|
|
756 |
while ((pos = src.position()) < end) {
|
|
757 |
int size = Math.min((end - pos), MAX_BUFFER_SIZE);
|
|
758 |
src.limit(pos + size);
|
|
759 |
NioSocketImpl.this.write(src);
|
|
760 |
}
|
|
761 |
assert src.limit() == end && src.remaining() == 0;
|
|
762 |
} catch (IOException ioe) {
|
|
763 |
throw new SocketException(ioe.getMessage());
|
|
764 |
}
|
|
765 |
}
|
|
766 |
}
|
|
767 |
@Override
|
|
768 |
public void close() throws IOException {
|
|
769 |
NioSocketImpl.this.close();
|
|
770 |
}
|
|
771 |
};
|
|
772 |
}
|
|
773 |
|
|
774 |
@Override
|
|
775 |
protected int available() throws IOException {
|
|
776 |
readLock.lock();
|
|
777 |
try {
|
|
778 |
ensureOpenAndConnected();
|
|
779 |
if (isInputClosed) {
|
|
780 |
return 0;
|
|
781 |
} else {
|
|
782 |
return Net.available(fd);
|
|
783 |
}
|
|
784 |
} finally {
|
|
785 |
readLock.unlock();
|
|
786 |
}
|
|
787 |
}
|
|
788 |
|
|
789 |
/**
|
|
790 |
* Closes the socket, signalling and waiting for blocking I/O operations
|
|
791 |
* to complete.
|
|
792 |
*/
|
|
793 |
@Override
|
|
794 |
protected void close() throws IOException {
|
|
795 |
boolean interrupted = false;
|
|
796 |
|
|
797 |
synchronized (stateLock) {
|
|
798 |
int state = this.state;
|
|
799 |
if (state >= ST_CLOSING)
|
|
800 |
return;
|
|
801 |
if (state == ST_NEW) {
|
|
802 |
// stillborn
|
|
803 |
this.state = ST_CLOSED;
|
|
804 |
return;
|
|
805 |
}
|
|
806 |
this.state = ST_CLOSING;
|
|
807 |
assert fd != null && closer != null;
|
|
808 |
|
|
809 |
// shutdown output when linger interval not set
|
|
810 |
try {
|
|
811 |
var SO_LINGER = StandardSocketOptions.SO_LINGER;
|
|
812 |
if ((int) Net.getSocketOption(fd, SO_LINGER) != 0) {
|
|
813 |
Net.shutdown(fd, Net.SHUT_WR);
|
|
814 |
}
|
|
815 |
} catch (IOException ignore) { }
|
|
816 |
|
|
817 |
// interrupt and wait for kernel threads to complete I/O operations
|
|
818 |
long reader = readerThread;
|
|
819 |
long writer = writerThread;
|
|
820 |
if (reader != 0 || writer != 0) {
|
|
821 |
nd.preClose(fd);
|
|
822 |
|
|
823 |
if (reader != 0)
|
|
824 |
NativeThread.signal(reader);
|
|
825 |
if (writer != 0)
|
|
826 |
NativeThread.signal(writer);
|
|
827 |
|
|
828 |
// wait for blocking I/O operations to end
|
|
829 |
while (readerThread != 0 || writerThread != 0) {
|
|
830 |
try {
|
|
831 |
stateLock.wait();
|
|
832 |
} catch (InterruptedException e) {
|
|
833 |
interrupted = true;
|
|
834 |
}
|
|
835 |
}
|
|
836 |
}
|
|
837 |
|
|
838 |
// close file descriptor
|
|
839 |
try {
|
|
840 |
closer.run();
|
|
841 |
} finally {
|
|
842 |
this.state = ST_CLOSED;
|
|
843 |
}
|
|
844 |
}
|
|
845 |
|
|
846 |
// restore interrupt status
|
|
847 |
if (interrupted)
|
|
848 |
Thread.currentThread().interrupt();
|
|
849 |
}
|
|
850 |
|
|
851 |
@Override
|
|
852 |
protected Set<SocketOption<?>> supportedOptions() {
|
|
853 |
Set<SocketOption<?>> options = new HashSet<>();
|
|
854 |
options.addAll(super.supportedOptions());
|
|
855 |
if (server) {
|
|
856 |
options.addAll(ExtendedSocketOptions.serverSocketOptions());
|
|
857 |
} else {
|
|
858 |
options.addAll(ExtendedSocketOptions.clientSocketOptions());
|
|
859 |
}
|
|
860 |
if (Net.isReusePortAvailable())
|
|
861 |
options.add(StandardSocketOptions.SO_REUSEPORT);
|
|
862 |
return Collections.unmodifiableSet(options);
|
|
863 |
}
|
|
864 |
|
|
865 |
private boolean booleanValue(Object value, String desc) {
|
|
866 |
if (!(value instanceof Boolean))
|
|
867 |
throw new IllegalArgumentException("Bad value for " + desc);
|
|
868 |
return (boolean) value;
|
|
869 |
}
|
|
870 |
|
|
871 |
private int intValue(Object value, String desc) {
|
|
872 |
if (!(value instanceof Integer))
|
|
873 |
throw new IllegalArgumentException("Bad value for " + desc);
|
|
874 |
return (int) value;
|
|
875 |
}
|
|
876 |
|
|
877 |
@Override
|
|
878 |
public void setOption(int opt, Object value) throws SocketException {
|
|
879 |
synchronized (stateLock) {
|
|
880 |
ensureOpen();
|
|
881 |
try {
|
|
882 |
switch (opt) {
|
|
883 |
case SO_LINGER: {
|
|
884 |
// the value is "false" to disable, or linger interval to enable
|
|
885 |
int i;
|
|
886 |
if (value instanceof Boolean && ((boolean) value) == false) {
|
|
887 |
i = -1;
|
|
888 |
} else {
|
|
889 |
i = intValue(value, "SO_LINGER");
|
|
890 |
}
|
|
891 |
Net.setSocketOption(fd, StandardSocketOptions.SO_LINGER, i);
|
|
892 |
break;
|
|
893 |
}
|
|
894 |
case SO_TIMEOUT: {
|
|
895 |
int i = intValue(value, "SO_TIMEOUT");
|
|
896 |
if (i < 0)
|
|
897 |
throw new IllegalArgumentException("timeout < 0");
|
|
898 |
timeout = i;
|
|
899 |
break;
|
|
900 |
}
|
|
901 |
case IP_TOS: {
|
|
902 |
int i = intValue(value, "IP_TOS");
|
|
903 |
if (stream && Net.isIPv6Available()) {
|
|
904 |
// IP_TOS is not specified for stream sockets when IPv6 enabled
|
|
905 |
} else {
|
|
906 |
Net.setSocketOption(fd, family(), StandardSocketOptions.IP_TOS, i);
|
|
907 |
}
|
|
908 |
break;
|
|
909 |
}
|
|
910 |
case TCP_NODELAY: {
|
|
911 |
boolean b = booleanValue(value, "TCP_NODELAY");
|
|
912 |
Net.setSocketOption(fd, StandardSocketOptions.TCP_NODELAY, b);
|
|
913 |
break;
|
|
914 |
}
|
|
915 |
case SO_SNDBUF: {
|
|
916 |
int i = intValue(value, "SO_SNDBUF");
|
|
917 |
Net.setSocketOption(fd, StandardSocketOptions.SO_SNDBUF, i);
|
|
918 |
break;
|
|
919 |
}
|
|
920 |
case SO_RCVBUF: {
|
|
921 |
int i = intValue(value, "SO_RCVBUF");
|
|
922 |
Net.setSocketOption(fd, StandardSocketOptions.SO_RCVBUF, i);
|
|
923 |
break;
|
|
924 |
}
|
|
925 |
case SO_KEEPALIVE: {
|
|
926 |
boolean b = booleanValue(value, "SO_KEEPALIVE");
|
|
927 |
Net.setSocketOption(fd, StandardSocketOptions.SO_KEEPALIVE, b);
|
|
928 |
break;
|
|
929 |
}
|
|
930 |
case SO_OOBINLINE: {
|
|
931 |
boolean b = booleanValue(value, "SO_OOBINLINE");
|
|
932 |
Net.setSocketOption(fd, ExtendedSocketOption.SO_OOBINLINE, b);
|
|
933 |
break;
|
|
934 |
}
|
|
935 |
case SO_REUSEADDR: {
|
|
936 |
boolean b = booleanValue(value, "SO_REUSEADDR");
|
|
937 |
if (Net.useExclusiveBind()) {
|
|
938 |
isReuseAddress = b;
|
|
939 |
} else {
|
|
940 |
Net.setSocketOption(fd, StandardSocketOptions.SO_REUSEADDR, b);
|
|
941 |
}
|
|
942 |
break;
|
|
943 |
}
|
|
944 |
case SO_REUSEPORT: {
|
|
945 |
if (!Net.isReusePortAvailable())
|
|
946 |
throw new UnsupportedOperationException("SO_REUSEPORT not supported");
|
|
947 |
boolean b = booleanValue(value, "SO_REUSEPORT");
|
|
948 |
Net.setSocketOption(fd, StandardSocketOptions.SO_REUSEPORT, b);
|
|
949 |
break;
|
|
950 |
}
|
|
951 |
default:
|
|
952 |
throw new SocketException("Unknown option " + opt);
|
|
953 |
}
|
|
954 |
} catch (IOException ioe) {
|
|
955 |
throw new SocketException(ioe.getMessage());
|
|
956 |
}
|
|
957 |
}
|
|
958 |
}
|
|
959 |
|
|
960 |
@Override
|
|
961 |
public Object getOption(int opt) throws SocketException {
|
|
962 |
synchronized (stateLock) {
|
|
963 |
ensureOpen();
|
|
964 |
try {
|
|
965 |
switch (opt) {
|
|
966 |
case SO_TIMEOUT:
|
|
967 |
return timeout;
|
|
968 |
case TCP_NODELAY:
|
|
969 |
return Net.getSocketOption(fd, StandardSocketOptions.TCP_NODELAY);
|
|
970 |
case SO_OOBINLINE:
|
|
971 |
return Net.getSocketOption(fd, ExtendedSocketOption.SO_OOBINLINE);
|
|
972 |
case SO_LINGER: {
|
|
973 |
// return "false" when disabled, linger interval when enabled
|
|
974 |
int i = (int) Net.getSocketOption(fd, StandardSocketOptions.SO_LINGER);
|
|
975 |
if (i == -1) {
|
|
976 |
return Boolean.FALSE;
|
|
977 |
} else {
|
|
978 |
return i;
|
|
979 |
}
|
|
980 |
}
|
|
981 |
case SO_REUSEADDR:
|
|
982 |
if (Net.useExclusiveBind()) {
|
|
983 |
return isReuseAddress;
|
|
984 |
} else {
|
|
985 |
return Net.getSocketOption(fd, StandardSocketOptions.SO_REUSEADDR);
|
|
986 |
}
|
|
987 |
case SO_BINDADDR:
|
|
988 |
return Net.localAddress(fd).getAddress();
|
|
989 |
case SO_SNDBUF:
|
|
990 |
return Net.getSocketOption(fd, StandardSocketOptions.SO_SNDBUF);
|
|
991 |
case SO_RCVBUF:
|
|
992 |
return Net.getSocketOption(fd, StandardSocketOptions.SO_RCVBUF);
|
|
993 |
case IP_TOS:
|
|
994 |
if (stream && Net.isIPv6Available()) {
|
|
995 |
// IP_TOS is not specified for stream sockets when IPv6 enabled
|
|
996 |
return 0;
|
|
997 |
} else {
|
|
998 |
return Net.getSocketOption(fd, family(), StandardSocketOptions.IP_TOS);
|
|
999 |
}
|
|
1000 |
case SO_KEEPALIVE:
|
|
1001 |
return Net.getSocketOption(fd, StandardSocketOptions.SO_KEEPALIVE);
|
|
1002 |
case SO_REUSEPORT:
|
|
1003 |
if (!Net.isReusePortAvailable())
|
|
1004 |
throw new UnsupportedOperationException("SO_REUSEPORT not supported");
|
|
1005 |
return Net.getSocketOption(fd, StandardSocketOptions.SO_REUSEPORT);
|
|
1006 |
default:
|
|
1007 |
throw new SocketException("Unknown option " + opt);
|
|
1008 |
}
|
|
1009 |
} catch (IOException ioe) {
|
|
1010 |
throw new SocketException(ioe.getMessage());
|
|
1011 |
}
|
|
1012 |
}
|
|
1013 |
}
|
|
1014 |
|
|
1015 |
@Override
|
|
1016 |
protected <T> void setOption(SocketOption<T> opt, T value) throws IOException {
|
|
1017 |
synchronized (stateLock) {
|
|
1018 |
ensureOpen();
|
|
1019 |
if (supportedOptions().contains(opt)) {
|
|
1020 |
ExtendedSocketOptions extended = ExtendedSocketOptions.getInstance();
|
|
1021 |
if (extended.isOptionSupported(opt)) {
|
|
1022 |
extended.setOption(fd, opt, value);
|
|
1023 |
} else {
|
|
1024 |
super.setOption(opt, value);
|
|
1025 |
}
|
|
1026 |
} else {
|
|
1027 |
throw new UnsupportedOperationException(opt.name());
|
|
1028 |
}
|
|
1029 |
}
|
|
1030 |
}
|
|
1031 |
|
|
1032 |
@SuppressWarnings("unchecked")
|
|
1033 |
protected <T> T getOption(SocketOption<T> opt) throws IOException {
|
|
1034 |
synchronized (stateLock) {
|
|
1035 |
ensureOpen();
|
|
1036 |
if (supportedOptions().contains(opt)) {
|
|
1037 |
ExtendedSocketOptions extended = ExtendedSocketOptions.getInstance();
|
|
1038 |
if (extended.isOptionSupported(opt)) {
|
|
1039 |
return (T) extended.getOption(fd, opt);
|
|
1040 |
} else {
|
|
1041 |
return super.getOption(opt);
|
|
1042 |
}
|
|
1043 |
} else {
|
|
1044 |
throw new UnsupportedOperationException(opt.name());
|
|
1045 |
}
|
|
1046 |
}
|
|
1047 |
}
|
|
1048 |
|
|
1049 |
@Override
|
|
1050 |
protected void shutdownInput() throws IOException {
|
|
1051 |
synchronized (stateLock) {
|
|
1052 |
ensureOpenAndConnected();
|
|
1053 |
if (!isInputClosed) {
|
|
1054 |
Net.shutdown(fd, Net.SHUT_RD);
|
|
1055 |
long reader = readerThread;
|
|
1056 |
if (reader != 0)
|
|
1057 |
NativeThread.signal(reader);
|
|
1058 |
isInputClosed = true;
|
|
1059 |
}
|
|
1060 |
}
|
|
1061 |
}
|
|
1062 |
|
|
1063 |
@Override
|
|
1064 |
protected void shutdownOutput() throws IOException {
|
|
1065 |
synchronized (stateLock) {
|
|
1066 |
ensureOpenAndConnected();
|
|
1067 |
if (!isOutputClosed) {
|
|
1068 |
Net.shutdown(fd, Net.SHUT_WR);
|
|
1069 |
long writer = writerThread;
|
|
1070 |
if (writer != 0)
|
|
1071 |
NativeThread.signal(writer);
|
|
1072 |
isOutputClosed = true;
|
|
1073 |
}
|
|
1074 |
}
|
|
1075 |
}
|
|
1076 |
|
|
1077 |
@Override
|
|
1078 |
protected boolean supportsUrgentData() {
|
|
1079 |
return true;
|
|
1080 |
}
|
|
1081 |
|
|
1082 |
@Override
|
|
1083 |
protected void sendUrgentData(int data) throws IOException {
|
|
1084 |
writeLock.lock();
|
|
1085 |
try {
|
|
1086 |
int n = 0;
|
|
1087 |
FileDescriptor fd = beginWrite();
|
|
1088 |
try {
|
|
1089 |
maybeConfigureNonBlocking(fd, 0);
|
|
1090 |
do {
|
|
1091 |
n = Net.sendOOB(fd, (byte) data);
|
|
1092 |
} while (n == IOStatus.INTERRUPTED && isOpen());
|
|
1093 |
if (n == IOStatus.UNAVAILABLE) {
|
|
1094 |
throw new RuntimeException("not implemented yet");
|
|
1095 |
}
|
|
1096 |
} finally {
|
|
1097 |
endWrite(n > 0);
|
|
1098 |
}
|
|
1099 |
} finally {
|
|
1100 |
writeLock.unlock();
|
|
1101 |
}
|
|
1102 |
}
|
|
1103 |
|
|
1104 |
/**
|
|
1105 |
* A task that closes a SocketImpl's file descriptor. The task runs when the
|
|
1106 |
* SocketImpl is explicitly closed and when the SocketImpl becomes phantom
|
|
1107 |
* reachable.
|
|
1108 |
*/
|
|
1109 |
private static class FileDescriptorCloser implements Runnable {
|
|
1110 |
private static final VarHandle CLOSED;
|
|
1111 |
static {
|
|
1112 |
try {
|
|
1113 |
MethodHandles.Lookup l = MethodHandles.lookup();
|
|
1114 |
CLOSED = l.findVarHandle(FileDescriptorCloser.class,
|
|
1115 |
"closed",
|
|
1116 |
boolean.class);
|
|
1117 |
} catch (Exception e) {
|
|
1118 |
throw new InternalError(e);
|
|
1119 |
}
|
|
1120 |
}
|
|
1121 |
|
|
1122 |
private final FileDescriptor fd;
|
|
1123 |
private final boolean stream;
|
|
1124 |
private volatile boolean closed;
|
|
1125 |
|
|
1126 |
FileDescriptorCloser(FileDescriptor fd, boolean stream) {
|
|
1127 |
this.fd = fd;
|
|
1128 |
this.stream = stream;
|
|
1129 |
}
|
|
1130 |
|
|
1131 |
static FileDescriptorCloser create(NioSocketImpl impl) {
|
|
1132 |
assert Thread.holdsLock(impl.stateLock);
|
|
1133 |
var closer = new FileDescriptorCloser(impl.fd, impl.stream);
|
|
1134 |
CleanerFactory.cleaner().register(impl, closer);
|
|
1135 |
return closer;
|
|
1136 |
}
|
|
1137 |
|
|
1138 |
@Override
|
|
1139 |
public void run() {
|
|
1140 |
if (CLOSED.compareAndSet(this, false, true)) {
|
|
1141 |
try {
|
|
1142 |
nd.close(fd);
|
|
1143 |
} catch (IOException ioe) {
|
|
1144 |
throw new RuntimeException(ioe);
|
|
1145 |
} finally {
|
|
1146 |
if (!stream) {
|
|
1147 |
// decrement
|
|
1148 |
ResourceManager.afterUdpClose();
|
|
1149 |
}
|
|
1150 |
}
|
|
1151 |
}
|
|
1152 |
}
|
|
1153 |
|
|
1154 |
boolean disable() {
|
|
1155 |
return CLOSED.compareAndSet(this, false, true);
|
|
1156 |
}
|
|
1157 |
}
|
|
1158 |
|
|
1159 |
/**
|
|
1160 |
* Returns true if the error code is UNAVAILABLE or INTERRUPTED, the
|
|
1161 |
* error codes to indicate that an I/O operation should be retried.
|
|
1162 |
*/
|
|
1163 |
private static boolean statusImpliesRetry(int n) {
|
|
1164 |
return n == IOStatus.UNAVAILABLE || n == IOStatus.INTERRUPTED;
|
|
1165 |
}
|
|
1166 |
|
|
1167 |
/**
|
|
1168 |
* Returns the socket protocol family
|
|
1169 |
*/
|
|
1170 |
private static ProtocolFamily family() {
|
|
1171 |
if (Net.isIPv6Available()) {
|
|
1172 |
return StandardProtocolFamily.INET6;
|
|
1173 |
} else {
|
|
1174 |
return StandardProtocolFamily.INET;
|
|
1175 |
}
|
|
1176 |
}
|
|
1177 |
|
|
1178 |
/**
|
|
1179 |
* Returns the native file descriptor
|
|
1180 |
*/
|
|
1181 |
private static int fdVal(FileDescriptor fd) {
|
|
1182 |
int fdVal = SharedSecrets.getJavaIOFileDescriptorAccess().get(fd);
|
|
1183 |
assert fdVal == IOUtil.fdVal(fd);
|
|
1184 |
return fdVal;
|
|
1185 |
}
|
|
1186 |
|
|
1187 |
/**
|
|
1188 |
* Sets the SocketImpl fields to the given values.
|
|
1189 |
*/
|
|
1190 |
private static void setSocketImplFields(SocketImpl si,
|
|
1191 |
FileDescriptor fd,
|
|
1192 |
int localport,
|
|
1193 |
InetAddress address,
|
|
1194 |
int port)
|
|
1195 |
{
|
|
1196 |
PrivilegedExceptionAction<Void> pa = () -> {
|
|
1197 |
setSocketImplField(si, "fd", fd);
|
|
1198 |
setSocketImplField(si, "localport", localport);
|
|
1199 |
setSocketImplField(si, "address", address);
|
|
1200 |
setSocketImplField(si, "port", port);
|
|
1201 |
return null;
|
|
1202 |
};
|
|
1203 |
try {
|
|
1204 |
AccessController.doPrivileged(pa);
|
|
1205 |
} catch (PrivilegedActionException pae) {
|
|
1206 |
throw new InternalError(pae);
|
|
1207 |
}
|
|
1208 |
}
|
|
1209 |
|
|
1210 |
private static void setSocketImplField(SocketImpl si, String name, Object value)
|
|
1211 |
throws Exception
|
|
1212 |
{
|
|
1213 |
Field field = SocketImpl.class.getDeclaredField(name);
|
|
1214 |
field.setAccessible(true);
|
|
1215 |
field.set(si, value);
|
|
1216 |
}
|
|
1217 |
}
|