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