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