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