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