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