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