author | alanb |
Wed, 20 Nov 2019 08:35:53 +0000 | |
changeset 59146 | 455612b3161a |
parent 59000 | 612c58965775 |
child 59329 | 289000934908 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
53419
eac105e3ec13
8217451: ExtendedSocketOptions should encapsulate support for SO_FLOW_SLA
alanb
parents:
50303
diff
changeset
|
2 |
* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.nio.ch; |
|
27 |
||
28 |
import java.io.FileDescriptor; |
|
29 |
import java.io.IOException; |
|
58808
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
30 |
import java.io.UncheckedIOException; |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
31 |
import java.lang.invoke.MethodHandles; |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
32 |
import java.lang.invoke.VarHandle; |
58808
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
33 |
import java.lang.ref.Cleaner.Cleanable; |
59146
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
34 |
import java.lang.reflect.Method; |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
35 |
import java.net.DatagramSocket; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
36 |
import java.net.Inet4Address; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
37 |
import java.net.Inet6Address; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
38 |
import java.net.InetAddress; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
39 |
import java.net.InetSocketAddress; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
40 |
import java.net.NetworkInterface; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
41 |
import java.net.PortUnreachableException; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
42 |
import java.net.ProtocolFamily; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
43 |
import java.net.SocketAddress; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
44 |
import java.net.SocketOption; |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
45 |
import java.net.SocketTimeoutException; |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
46 |
import java.net.StandardProtocolFamily; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
47 |
import java.net.StandardSocketOptions; |
2 | 48 |
import java.nio.ByteBuffer; |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
49 |
import java.nio.channels.AlreadyBoundException; |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
50 |
import java.nio.channels.AlreadyConnectedException; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
51 |
import java.nio.channels.AsynchronousCloseException; |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
52 |
import java.nio.channels.ClosedChannelException; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
53 |
import java.nio.channels.DatagramChannel; |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
54 |
import java.nio.channels.IllegalBlockingModeException; |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
55 |
import java.nio.channels.MembershipKey; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
56 |
import java.nio.channels.NotYetConnectedException; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
57 |
import java.nio.channels.SelectionKey; |
59146
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
58 |
import java.nio.channels.spi.AbstractSelectableChannel; |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
59 |
import java.nio.channels.spi.SelectorProvider; |
59146
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
60 |
import java.security.AccessController; |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
61 |
import java.security.PrivilegedExceptionAction; |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
62 |
import java.util.Collections; |
59146
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
63 |
import java.util.HashMap; |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
64 |
import java.util.HashSet; |
59146
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
65 |
import java.util.Map; |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
66 |
import java.util.Objects; |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
67 |
import java.util.Set; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
68 |
import java.util.concurrent.locks.ReentrantLock; |
59146
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
69 |
import java.util.function.Consumer; |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
70 |
|
58808
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
71 |
import jdk.internal.ref.CleanerFactory; |
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
72 |
import sun.net.ResourceManager; |
37676
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
36115
diff
changeset
|
73 |
import sun.net.ext.ExtendedSocketOptions; |
55375 | 74 |
import sun.net.util.IPAddressUtil; |
2 | 75 |
|
76 |
/** |
|
77 |
* An implementation of DatagramChannels. |
|
78 |
*/ |
|
79 |
||
80 |
class DatagramChannelImpl |
|
81 |
extends DatagramChannel |
|
82 |
implements SelChImpl |
|
83 |
{ |
|
84 |
// Used to make native read and write calls |
|
58808
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
85 |
private static final NativeDispatcher nd = new DatagramDispatcher(); |
2 | 86 |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
87 |
// The protocol family of the socket |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
88 |
private final ProtocolFamily family; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
89 |
|
2 | 90 |
// Our file descriptor |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
91 |
private final FileDescriptor fd; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
92 |
private final int fdVal; |
58808
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
93 |
private final Cleanable cleaner; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
94 |
|
2 | 95 |
// Cached InetAddress and port for unconnected DatagramChannels |
96 |
// used by receive0 |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
97 |
private InetAddress cachedSenderInetAddress; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
98 |
private int cachedSenderPort; |
2 | 99 |
|
100 |
// Lock held by current reading or connecting thread |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
101 |
private final ReentrantLock readLock = new ReentrantLock(); |
2 | 102 |
|
103 |
// Lock held by current writing or connecting thread |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
104 |
private final ReentrantLock writeLock = new ReentrantLock(); |
2 | 105 |
|
106 |
// Lock held by any thread that modifies the state fields declared below |
|
107 |
// DO NOT invoke a blocking I/O operation while holding this lock! |
|
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
108 |
private final Object stateLock = new Object(); |
2 | 109 |
|
110 |
// -- The following fields are protected by stateLock |
|
111 |
||
112 |
// State (does not necessarily increase monotonically) |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
113 |
private static final int ST_UNCONNECTED = 0; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
114 |
private static final int ST_CONNECTED = 1; |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
115 |
private static final int ST_CLOSING = 2; |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
116 |
private static final int ST_CLOSED = 3; |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
117 |
private int state; |
2 | 118 |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
119 |
// IDs of native threads doing reads and writes, for signalling |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
120 |
private long readerThread; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
121 |
private long writerThread; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
122 |
|
59146
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
123 |
// Local and remote (connected) address |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
124 |
private InetSocketAddress localAddress; |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
125 |
private InetSocketAddress remoteAddress; |
2 | 126 |
|
59146
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
127 |
// Local address prior to connecting |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
128 |
private InetSocketAddress initialLocalAddress; |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
129 |
|
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
130 |
// Socket adaptor, created lazily |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
131 |
private static final VarHandle SOCKET; |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
132 |
static { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
133 |
try { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
134 |
MethodHandles.Lookup l = MethodHandles.lookup(); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
135 |
SOCKET = l.findVarHandle(DatagramChannelImpl.class, "socket", DatagramSocket.class); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
136 |
} catch (Exception e) { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
137 |
throw new InternalError(e); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
138 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
139 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
140 |
private volatile DatagramSocket socket; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
141 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
142 |
// Multicast support |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
143 |
private MembershipRegistry registry; |
2 | 144 |
|
18192 | 145 |
// set true when socket is bound and SO_REUSEADDRESS is emulated |
146 |
private boolean reuseAddressEmulated; |
|
147 |
||
148 |
// set true/false when socket is already bound and SO_REUSEADDR is emulated |
|
149 |
private boolean isReuseAddress; |
|
150 |
||
2 | 151 |
// -- End of fields protected by stateLock |
152 |
||
153 |
public DatagramChannelImpl(SelectorProvider sp) |
|
154 |
throws IOException |
|
155 |
{ |
|
156 |
super(sp); |
|
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
157 |
ResourceManager.beforeUdpCreate(); |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
158 |
try { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
159 |
this.family = Net.isIPv6Available() |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
160 |
? StandardProtocolFamily.INET6 |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
161 |
: StandardProtocolFamily.INET; |
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
162 |
this.fd = Net.socket(family, false); |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
163 |
this.fdVal = IOUtil.fdVal(fd); |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
164 |
} catch (IOException ioe) { |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
165 |
ResourceManager.afterUdpClose(); |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
166 |
throw ioe; |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
167 |
} |
58808
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
168 |
this.cleaner = CleanerFactory.cleaner().register(this, closerFor(fd)); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
169 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
170 |
|
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
171 |
public DatagramChannelImpl(SelectorProvider sp, ProtocolFamily family) |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
172 |
throws IOException |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
173 |
{ |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
174 |
super(sp); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
175 |
Objects.requireNonNull(family, "'family' is null"); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
176 |
if ((family != StandardProtocolFamily.INET) && |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
177 |
(family != StandardProtocolFamily.INET6)) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
178 |
throw new UnsupportedOperationException("Protocol family not supported"); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
179 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
180 |
if (family == StandardProtocolFamily.INET6) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
181 |
if (!Net.isIPv6Available()) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
182 |
throw new UnsupportedOperationException("IPv6 not available"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
183 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
184 |
} |
48761 | 185 |
|
186 |
ResourceManager.beforeUdpCreate(); |
|
187 |
try { |
|
188 |
this.family = family; |
|
189 |
this.fd = Net.socket(family, false); |
|
190 |
this.fdVal = IOUtil.fdVal(fd); |
|
191 |
} catch (IOException ioe) { |
|
192 |
ResourceManager.afterUdpClose(); |
|
193 |
throw ioe; |
|
194 |
} |
|
58808
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
195 |
this.cleaner = CleanerFactory.cleaner().register(this, closerFor(fd)); |
2 | 196 |
} |
197 |
||
198 |
public DatagramChannelImpl(SelectorProvider sp, FileDescriptor fd) |
|
199 |
throws IOException |
|
200 |
{ |
|
201 |
super(sp); |
|
48761 | 202 |
|
203 |
// increment UDP count to match decrement when closing |
|
204 |
ResourceManager.beforeUdpCreate(); |
|
205 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
206 |
this.family = Net.isIPv6Available() |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
207 |
? StandardProtocolFamily.INET6 |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
208 |
: StandardProtocolFamily.INET; |
2 | 209 |
this.fd = fd; |
210 |
this.fdVal = IOUtil.fdVal(fd); |
|
58808
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
211 |
this.cleaner = CleanerFactory.cleaner().register(this, closerFor(fd)); |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
212 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
213 |
this.localAddress = Net.localAddress(fd); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
214 |
} |
2 | 215 |
} |
216 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
217 |
// @throws ClosedChannelException if channel is closed |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
218 |
private void ensureOpen() throws ClosedChannelException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
219 |
if (!isOpen()) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
220 |
throw new ClosedChannelException(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
221 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
222 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
223 |
@Override |
2 | 224 |
public DatagramSocket socket() { |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
225 |
DatagramSocket socket = this.socket; |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
226 |
if (socket == null) { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
227 |
socket = DatagramSocketAdaptor.create(this); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
228 |
if (!SOCKET.compareAndSet(this, null, socket)) { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
229 |
socket = this.socket; |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
230 |
} |
2 | 231 |
} |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
232 |
return socket; |
2 | 233 |
} |
234 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
235 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
236 |
public SocketAddress getLocalAddress() throws IOException { |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
237 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
238 |
ensureOpen(); |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
239 |
// Perform security check before returning address |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
240 |
return Net.getRevealedLocalAddress(localAddress); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
241 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
242 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
243 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
244 |
@Override |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
245 |
public SocketAddress getRemoteAddress() throws IOException { |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
246 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
247 |
ensureOpen(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
248 |
return remoteAddress; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
249 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
250 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
251 |
|
59000
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
252 |
/** |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
253 |
* Returns the protocol family to specify to set/getSocketOption for the |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
254 |
* given socket option. |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
255 |
*/ |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
256 |
private ProtocolFamily familyFor(SocketOption<?> name) { |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
257 |
assert Thread.holdsLock(stateLock); |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
258 |
|
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
259 |
// unspecified (most options) |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
260 |
if (SocketOptionRegistry.findOption(name, Net.UNSPEC) != null) |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
261 |
return Net.UNSPEC; |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
262 |
|
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
263 |
// IPv4 socket |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
264 |
if (family == StandardProtocolFamily.INET) |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
265 |
return StandardProtocolFamily.INET; |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
266 |
|
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
267 |
// IPv6 socket that is unbound |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
268 |
if (localAddress == null) |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
269 |
return StandardProtocolFamily.INET6; |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
270 |
|
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
271 |
// IPv6 socket bound to wildcard or IPv6 address |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
272 |
InetAddress address = localAddress.getAddress(); |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
273 |
if (address.isAnyLocalAddress() || (address instanceof Inet6Address)) |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
274 |
return StandardProtocolFamily.INET6; |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
275 |
|
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
276 |
// IPv6 socket bound to IPv4 address |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
277 |
if (Net.canUseIPv6OptionsWithIPv4LocalAddress()) { |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
278 |
// IPV6_XXX options can be used |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
279 |
return StandardProtocolFamily.INET6; |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
280 |
} else { |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
281 |
// IPV6_XXX options cannot be used |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
282 |
return StandardProtocolFamily.INET; |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
283 |
} |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
284 |
} |
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
285 |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
286 |
@Override |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
287 |
public <T> DatagramChannel setOption(SocketOption<T> name, T value) |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
288 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
289 |
{ |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
290 |
Objects.requireNonNull(name); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
291 |
if (!supportedOptions().contains(name)) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
292 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
55081
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54754
diff
changeset
|
293 |
if (!name.type().isInstance(value)) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54754
diff
changeset
|
294 |
throw new IllegalArgumentException("Invalid value '" + value + "'"); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
295 |
|
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
296 |
synchronized (stateLock) { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
297 |
ensureOpen(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
298 |
|
59000
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
299 |
ProtocolFamily family = familyFor(name); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
300 |
|
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
9275
diff
changeset
|
301 |
if (name == StandardSocketOptions.IP_MULTICAST_IF) { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
302 |
NetworkInterface interf = (NetworkInterface)value; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
303 |
if (family == StandardProtocolFamily.INET6) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
304 |
int index = interf.getIndex(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
305 |
if (index == -1) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
306 |
throw new IOException("Network interface cannot be identified"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
307 |
Net.setInterface6(fd, index); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
308 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
309 |
// need IPv4 address to identify interface |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
310 |
Inet4Address target = Net.anyInet4Address(interf); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
311 |
if (target == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
312 |
throw new IOException("Network interface not configured for IPv4"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
313 |
int targetAddress = Net.inet4AsInt(target); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
314 |
Net.setInterface4(fd, targetAddress); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
315 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
316 |
return this; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
317 |
} |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
318 |
if (name == StandardSocketOptions.SO_REUSEADDR |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
319 |
&& Net.useExclusiveBind() && localAddress != null) { |
18192 | 320 |
reuseAddressEmulated = true; |
321 |
this.isReuseAddress = (Boolean)value; |
|
322 |
} |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
323 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
324 |
// remaining options don't need any special handling |
59000
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
325 |
Net.setSocketOption(fd, family, name, value); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
326 |
return this; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
327 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
328 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
329 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
330 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
331 |
@SuppressWarnings("unchecked") |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
332 |
public <T> T getOption(SocketOption<T> name) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
333 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
334 |
{ |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
335 |
Objects.requireNonNull(name); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
336 |
if (!supportedOptions().contains(name)) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
337 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
338 |
|
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
339 |
synchronized (stateLock) { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
340 |
ensureOpen(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
341 |
|
59000
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
342 |
ProtocolFamily family = familyFor(name); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
343 |
|
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
9275
diff
changeset
|
344 |
if (name == StandardSocketOptions.IP_MULTICAST_IF) { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
345 |
if (family == StandardProtocolFamily.INET) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
346 |
int address = Net.getInterface4(fd); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
347 |
if (address == 0) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
348 |
return null; // default interface |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
349 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
350 |
InetAddress ia = Net.inet4FromInt(address); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
351 |
NetworkInterface ni = NetworkInterface.getByInetAddress(ia); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
352 |
if (ni == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
353 |
throw new IOException("Unable to map address to interface"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
354 |
return (T) ni; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
355 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
356 |
int index = Net.getInterface6(fd); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
357 |
if (index == 0) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
358 |
return null; // default interface |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
359 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
360 |
NetworkInterface ni = NetworkInterface.getByIndex(index); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
361 |
if (ni == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
362 |
throw new IOException("Unable to map index to interface"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
363 |
return (T) ni; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
364 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
365 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
366 |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
367 |
if (name == StandardSocketOptions.SO_REUSEADDR && reuseAddressEmulated) { |
59000
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
368 |
return (T) Boolean.valueOf(isReuseAddress); |
18192 | 369 |
} |
370 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
371 |
// no special handling |
59000
612c58965775
8233435: (dc) DatagramChannel should allow IPv6 socket join IPv4 multicast groups (macOS, win)
alanb
parents:
58899
diff
changeset
|
372 |
return (T) Net.getSocketOption(fd, family, name); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
373 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
374 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
375 |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
376 |
private static class DefaultOptionsHolder { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
377 |
static final Set<SocketOption<?>> defaultOptions = defaultOptions(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
378 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
379 |
private static Set<SocketOption<?>> defaultOptions() { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
380 |
HashSet<SocketOption<?>> set = new HashSet<>(); |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
9275
diff
changeset
|
381 |
set.add(StandardSocketOptions.SO_SNDBUF); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
9275
diff
changeset
|
382 |
set.add(StandardSocketOptions.SO_RCVBUF); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
9275
diff
changeset
|
383 |
set.add(StandardSocketOptions.SO_REUSEADDR); |
36115 | 384 |
if (Net.isReusePortAvailable()) { |
385 |
set.add(StandardSocketOptions.SO_REUSEPORT); |
|
386 |
} |
|
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
9275
diff
changeset
|
387 |
set.add(StandardSocketOptions.SO_BROADCAST); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
9275
diff
changeset
|
388 |
set.add(StandardSocketOptions.IP_TOS); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
9275
diff
changeset
|
389 |
set.add(StandardSocketOptions.IP_MULTICAST_IF); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
9275
diff
changeset
|
390 |
set.add(StandardSocketOptions.IP_MULTICAST_TTL); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
9275
diff
changeset
|
391 |
set.add(StandardSocketOptions.IP_MULTICAST_LOOP); |
53419
eac105e3ec13
8217451: ExtendedSocketOptions should encapsulate support for SO_FLOW_SLA
alanb
parents:
50303
diff
changeset
|
392 |
set.addAll(ExtendedSocketOptions.datagramSocketOptions()); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
393 |
return Collections.unmodifiableSet(set); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
394 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
395 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
396 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
397 |
@Override |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
398 |
public final Set<SocketOption<?>> supportedOptions() { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
399 |
return DefaultOptionsHolder.defaultOptions; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
400 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
401 |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
402 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
403 |
* Marks the beginning of a read operation that might block. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
404 |
* |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
405 |
* @param blocking true if configured blocking |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
406 |
* @param mustBeConnected true if the socket must be connected |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
407 |
* @return remote address if connected |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
408 |
* @throws ClosedChannelException if the channel is closed |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
409 |
* @throws NotYetConnectedException if mustBeConnected and not connected |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
410 |
* @throws IOException if socket not bound and cannot be bound |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
411 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
412 |
private SocketAddress beginRead(boolean blocking, boolean mustBeConnected) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
413 |
throws IOException |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
414 |
{ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
415 |
if (blocking) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
416 |
// set hook for Thread.interrupt |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
417 |
begin(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
418 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
419 |
SocketAddress remote; |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
420 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
421 |
ensureOpen(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
422 |
remote = remoteAddress; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
423 |
if ((remote == null) && mustBeConnected) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
424 |
throw new NotYetConnectedException(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
425 |
if (localAddress == null) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
426 |
bindInternal(null); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
427 |
if (blocking) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
428 |
readerThread = NativeThread.current(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
429 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
430 |
return remote; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
431 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
432 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
433 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
434 |
* Marks the end of a read operation that may have blocked. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
435 |
* |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
436 |
* @throws AsynchronousCloseException if the channel was closed asynchronously |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
437 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
438 |
private void endRead(boolean blocking, boolean completed) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
439 |
throws AsynchronousCloseException |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
440 |
{ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
441 |
if (blocking) { |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
442 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
443 |
readerThread = 0; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
444 |
if (state == ST_CLOSING) { |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
445 |
tryFinishClose(); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
446 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
447 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
448 |
// remove hook for Thread.interrupt |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
449 |
end(completed); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
450 |
} |
2 | 451 |
} |
452 |
||
453 |
private SocketAddress sender; // Set by receive0 (## ugh) |
|
454 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
455 |
@Override |
2 | 456 |
public SocketAddress receive(ByteBuffer dst) throws IOException { |
457 |
if (dst.isReadOnly()) |
|
458 |
throw new IllegalArgumentException("Read-only buffer"); |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
459 |
readLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
460 |
try { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
461 |
boolean blocking = isBlocking(); |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
462 |
boolean completed = false; |
2 | 463 |
int n = 0; |
464 |
try { |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
465 |
SocketAddress remote = beginRead(blocking, false); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
466 |
boolean connected = (remote != null); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
467 |
SecurityManager sm = System.getSecurityManager(); |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
468 |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
469 |
if (connected || (sm == null)) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
470 |
// connected or no security manager |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
471 |
n = receive(dst, connected); |
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
472 |
if (blocking) { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
473 |
while (IOStatus.okayToRetry(n) && isOpen()) { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
474 |
park(Net.POLLIN); |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
475 |
n = receive(dst, connected); |
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
476 |
} |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
477 |
} |
2 | 478 |
} else { |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
479 |
// security manager and unconnected |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
480 |
n = untrustedReceive(dst); |
2 | 481 |
} |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
482 |
if (n == IOStatus.UNAVAILABLE) |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
483 |
return null; |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
484 |
completed = (n > 0) || (n == 0 && isOpen()); |
2 | 485 |
return sender; |
486 |
} finally { |
|
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
487 |
endRead(blocking, completed); |
2 | 488 |
assert IOStatus.check(n); |
489 |
} |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
490 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
491 |
readLock.unlock(); |
2 | 492 |
} |
493 |
} |
|
494 |
||
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
495 |
/** |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
496 |
* Receives a datagram into an untrusted buffer. When there is a security |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
497 |
* manager set, and the socket is not connected, datagrams have to be received |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
498 |
* into a buffer that is not accessible to the user. The datagram is copied |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
499 |
* into the user's buffer when the sender address is accepted by the security |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
500 |
* manager. |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
501 |
* |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
502 |
* @return the size of the datagram or IOStatus.UNAVAILABLE |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
503 |
*/ |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
504 |
private int untrustedReceive(ByteBuffer dst) throws IOException { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
505 |
SecurityManager sm = System.getSecurityManager(); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
506 |
assert readLock.isHeldByCurrentThread() |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
507 |
&& sm != null && remoteAddress == null; |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
508 |
|
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
509 |
ByteBuffer bb = Util.getTemporaryDirectBuffer(dst.remaining()); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
510 |
try { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
511 |
boolean blocking = isBlocking(); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
512 |
for (;;) { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
513 |
int n = receive(bb, false); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
514 |
if (blocking) { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
515 |
while (IOStatus.okayToRetry(n) && isOpen()) { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
516 |
park(Net.POLLIN); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
517 |
n = receive(bb, false); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
518 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
519 |
} else if (n == IOStatus.UNAVAILABLE) { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
520 |
return n; |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
521 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
522 |
InetSocketAddress isa = (InetSocketAddress) sender; |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
523 |
try { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
524 |
sm.checkAccept(isa.getAddress().getHostAddress(), isa.getPort()); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
525 |
bb.flip(); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
526 |
dst.put(bb); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
527 |
return n; |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
528 |
} catch (SecurityException se) { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
529 |
// ignore datagram |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
530 |
bb.clear(); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
531 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
532 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
533 |
} finally { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
534 |
Util.releaseTemporaryDirectBuffer(bb); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
535 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
536 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
537 |
|
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
538 |
/** |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
539 |
* Receives a datagram into the given buffer. |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
540 |
* |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
541 |
* @apiNote This method is for use by the socket adaptor. The buffer is |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
542 |
* assumed to be trusted, meaning it is not accessible to user code. |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
543 |
* |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
544 |
* @throws IllegalBlockingModeException if the channel is non-blocking |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
545 |
* @throws SocketTimeoutException if the timeout elapses |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
546 |
*/ |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
547 |
SocketAddress blockingReceive(ByteBuffer dst, long nanos) throws IOException { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
548 |
readLock.lock(); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
549 |
try { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
550 |
ensureOpen(); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
551 |
if (!isBlocking()) |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
552 |
throw new IllegalBlockingModeException(); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
553 |
SecurityManager sm = System.getSecurityManager(); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
554 |
boolean connected = isConnected(); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
555 |
SocketAddress sender; |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
556 |
do { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
557 |
if (nanos > 0) { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
558 |
sender = trustedBlockingReceive(dst, nanos); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
559 |
} else { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
560 |
sender = trustedBlockingReceive(dst); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
561 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
562 |
// check sender when security manager set and not connected |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
563 |
if (sm != null && !connected) { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
564 |
InetSocketAddress isa = (InetSocketAddress) sender; |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
565 |
try { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
566 |
sm.checkAccept(isa.getAddress().getHostAddress(), isa.getPort()); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
567 |
} catch (SecurityException e) { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
568 |
sender = null; |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
569 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
570 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
571 |
} while (sender == null); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
572 |
return sender; |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
573 |
} finally { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
574 |
readLock.unlock(); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
575 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
576 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
577 |
|
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
578 |
/** |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
579 |
* Receives a datagram into given buffer. This method is used to support |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
580 |
* the socket adaptor. The buffer is assumed to be trusted. |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
581 |
* @throws SocketTimeoutException if the timeout elapses |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
582 |
*/ |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
583 |
private SocketAddress trustedBlockingReceive(ByteBuffer dst) |
2 | 584 |
throws IOException |
585 |
{ |
|
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
586 |
assert readLock.isHeldByCurrentThread() && isBlocking(); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
587 |
boolean completed = false; |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
588 |
int n = 0; |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
589 |
try { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
590 |
SocketAddress remote = beginRead(true, false); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
591 |
boolean connected = (remote != null); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
592 |
n = receive(dst, connected); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
593 |
while (n == IOStatus.UNAVAILABLE && isOpen()) { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
594 |
park(Net.POLLIN); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
595 |
n = receive(dst, connected); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
596 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
597 |
completed = (n > 0) || (n == 0 && isOpen()); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
598 |
return sender; |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
599 |
} finally { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
600 |
endRead(true, completed); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
601 |
assert IOStatus.check(n); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
602 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
603 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
604 |
|
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
605 |
/** |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
606 |
* Receives a datagram into given buffer with a timeout. This method is |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
607 |
* used to support the socket adaptor. The buffer is assumed to be trusted. |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
608 |
* @throws SocketTimeoutException if the timeout elapses |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
609 |
*/ |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
610 |
private SocketAddress trustedBlockingReceive(ByteBuffer dst, long nanos) |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
611 |
throws IOException |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
612 |
{ |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
613 |
assert readLock.isHeldByCurrentThread() && isBlocking(); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
614 |
boolean completed = false; |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
615 |
int n = 0; |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
616 |
try { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
617 |
SocketAddress remote = beginRead(true, false); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
618 |
boolean connected = (remote != null); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
619 |
|
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
620 |
// change socket to non-blocking |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
621 |
lockedConfigureBlocking(false); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
622 |
try { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
623 |
long startNanos = System.nanoTime(); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
624 |
n = receive(dst, connected); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
625 |
while (n == IOStatus.UNAVAILABLE && isOpen()) { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
626 |
long remainingNanos = nanos - (System.nanoTime() - startNanos); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
627 |
if (remainingNanos <= 0) { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
628 |
throw new SocketTimeoutException("Receive timed out"); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
629 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
630 |
park(Net.POLLIN, remainingNanos); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
631 |
n = receive(dst, connected); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
632 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
633 |
completed = (n > 0) || (n == 0 && isOpen()); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
634 |
return sender; |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
635 |
} finally { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
636 |
// restore socket to blocking mode (if channel is open) |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
637 |
tryLockedConfigureBlocking(true); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
638 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
639 |
|
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
640 |
} finally { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
641 |
endRead(true, completed); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
642 |
assert IOStatus.check(n); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
643 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
644 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
645 |
|
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
646 |
private int receive(ByteBuffer dst, boolean connected) throws IOException { |
2 | 647 |
int pos = dst.position(); |
648 |
int lim = dst.limit(); |
|
649 |
assert (pos <= lim); |
|
650 |
int rem = (pos <= lim ? lim - pos : 0); |
|
651 |
if (dst instanceof DirectBuffer && rem > 0) |
|
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
652 |
return receiveIntoNativeBuffer(dst, rem, pos, connected); |
2 | 653 |
|
654 |
// Substitute a native buffer. If the supplied buffer is empty |
|
655 |
// we must instead use a nonempty buffer, otherwise the call |
|
656 |
// will not block waiting for a datagram on some platforms. |
|
657 |
int newSize = Math.max(rem, 1); |
|
9237
cbb5753e87e7
7034155: (ch) NullPointerException in sun.io.ch.IOUtil when OOM is thrown
alanb
parents:
8817
diff
changeset
|
658 |
ByteBuffer bb = Util.getTemporaryDirectBuffer(newSize); |
2 | 659 |
try { |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
660 |
int n = receiveIntoNativeBuffer(bb, newSize, 0, connected); |
2 | 661 |
bb.flip(); |
662 |
if (n > 0 && rem > 0) |
|
663 |
dst.put(bb); |
|
664 |
return n; |
|
665 |
} finally { |
|
666 |
Util.releaseTemporaryDirectBuffer(bb); |
|
667 |
} |
|
668 |
} |
|
669 |
||
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
670 |
private int receiveIntoNativeBuffer(ByteBuffer bb, int rem, int pos, |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
671 |
boolean connected) |
2 | 672 |
throws IOException |
673 |
{ |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
674 |
int n = receive0(fd, ((DirectBuffer)bb).address() + pos, rem, connected); |
2 | 675 |
if (n > 0) |
676 |
bb.position(pos + n); |
|
677 |
return n; |
|
678 |
} |
|
679 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
680 |
@Override |
2 | 681 |
public int send(ByteBuffer src, SocketAddress target) |
682 |
throws IOException |
|
683 |
{ |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
684 |
Objects.requireNonNull(src); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
685 |
InetSocketAddress isa = Net.checkAddress(target, family); |
2 | 686 |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
687 |
writeLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
688 |
try { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
689 |
boolean blocking = isBlocking(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
690 |
int n = 0; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
691 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
692 |
SocketAddress remote = beginWrite(blocking, false); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
693 |
if (remote != null) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
694 |
// connected |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
695 |
if (!target.equals(remote)) { |
49284
a51ca91c2cde
8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
49001
diff
changeset
|
696 |
throw new AlreadyConnectedException(); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
697 |
} |
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
698 |
n = IOUtil.write(fd, src, -1, nd); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
699 |
if (blocking) { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
700 |
while (IOStatus.okayToRetry(n) && isOpen()) { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
701 |
park(Net.POLLOUT); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
702 |
n = IOUtil.write(fd, src, -1, nd); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
703 |
} |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
704 |
} |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
705 |
} else { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
706 |
// not connected |
2 | 707 |
SecurityManager sm = System.getSecurityManager(); |
55375 | 708 |
InetAddress ia = isa.getAddress(); |
2 | 709 |
if (sm != null) { |
710 |
if (ia.isMulticastAddress()) { |
|
16089 | 711 |
sm.checkMulticast(ia); |
2 | 712 |
} else { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
713 |
sm.checkConnect(ia.getHostAddress(), isa.getPort()); |
2 | 714 |
} |
715 |
} |
|
55375 | 716 |
if (ia.isLinkLocalAddress()) |
717 |
isa = IPAddressUtil.toScopedAddress(isa); |
|
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
718 |
n = send(fd, src, isa); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
719 |
if (blocking) { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
720 |
while (IOStatus.okayToRetry(n) && isOpen()) { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
721 |
park(Net.POLLOUT); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
722 |
n = send(fd, src, isa); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
723 |
} |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
724 |
} |
2 | 725 |
} |
726 |
} finally { |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
727 |
endWrite(blocking, n > 0); |
2 | 728 |
assert IOStatus.check(n); |
729 |
} |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
730 |
return IOStatus.normalize(n); |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
731 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
732 |
writeLock.unlock(); |
2 | 733 |
} |
734 |
} |
|
735 |
||
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
736 |
/** |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
737 |
* Sends a datagram from the bytes in given buffer. |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
738 |
* |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
739 |
* @apiNote This method is for use by the socket adaptor. |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
740 |
* |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
741 |
* @throws IllegalBlockingModeException if the channel is non-blocking |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
742 |
*/ |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
743 |
void blockingSend(ByteBuffer src, SocketAddress target) throws IOException { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
744 |
writeLock.lock(); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
745 |
try { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
746 |
ensureOpen(); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
747 |
if (!isBlocking()) |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
748 |
throw new IllegalBlockingModeException(); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
749 |
send(src, target); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
750 |
} finally { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
751 |
writeLock.unlock(); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
752 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
753 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
754 |
|
16089 | 755 |
private int send(FileDescriptor fd, ByteBuffer src, InetSocketAddress target) |
2 | 756 |
throws IOException |
757 |
{ |
|
758 |
if (src instanceof DirectBuffer) |
|
759 |
return sendFromNativeBuffer(fd, src, target); |
|
760 |
||
761 |
// Substitute a native buffer |
|
762 |
int pos = src.position(); |
|
763 |
int lim = src.limit(); |
|
764 |
assert (pos <= lim); |
|
765 |
int rem = (pos <= lim ? lim - pos : 0); |
|
766 |
||
9237
cbb5753e87e7
7034155: (ch) NullPointerException in sun.io.ch.IOUtil when OOM is thrown
alanb
parents:
8817
diff
changeset
|
767 |
ByteBuffer bb = Util.getTemporaryDirectBuffer(rem); |
2 | 768 |
try { |
769 |
bb.put(src); |
|
770 |
bb.flip(); |
|
771 |
// Do not update src until we see how many bytes were written |
|
772 |
src.position(pos); |
|
773 |
||
774 |
int n = sendFromNativeBuffer(fd, bb, target); |
|
775 |
if (n > 0) { |
|
776 |
// now update src |
|
777 |
src.position(pos + n); |
|
778 |
} |
|
779 |
return n; |
|
780 |
} finally { |
|
781 |
Util.releaseTemporaryDirectBuffer(bb); |
|
782 |
} |
|
783 |
} |
|
784 |
||
785 |
private int sendFromNativeBuffer(FileDescriptor fd, ByteBuffer bb, |
|
16089 | 786 |
InetSocketAddress target) |
2 | 787 |
throws IOException |
788 |
{ |
|
789 |
int pos = bb.position(); |
|
790 |
int lim = bb.limit(); |
|
791 |
assert (pos <= lim); |
|
792 |
int rem = (pos <= lim ? lim - pos : 0); |
|
793 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
794 |
boolean preferIPv6 = (family != StandardProtocolFamily.INET); |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9679
diff
changeset
|
795 |
int written; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9679
diff
changeset
|
796 |
try { |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9679
diff
changeset
|
797 |
written = send0(preferIPv6, fd, ((DirectBuffer)bb).address() + pos, |
16089 | 798 |
rem, target.getAddress(), target.getPort()); |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9679
diff
changeset
|
799 |
} catch (PortUnreachableException pue) { |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9679
diff
changeset
|
800 |
if (isConnected()) |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9679
diff
changeset
|
801 |
throw pue; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9679
diff
changeset
|
802 |
written = rem; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9679
diff
changeset
|
803 |
} |
2 | 804 |
if (written > 0) |
805 |
bb.position(pos + written); |
|
806 |
return written; |
|
807 |
} |
|
808 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
809 |
@Override |
2 | 810 |
public int read(ByteBuffer buf) throws IOException { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
811 |
Objects.requireNonNull(buf); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
812 |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
813 |
readLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
814 |
try { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
815 |
boolean blocking = isBlocking(); |
2 | 816 |
int n = 0; |
817 |
try { |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
818 |
beginRead(blocking, true); |
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
819 |
n = IOUtil.read(fd, buf, -1, nd); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
820 |
if (blocking) { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
821 |
while (IOStatus.okayToRetry(n) && isOpen()) { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
822 |
park(Net.POLLIN); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
823 |
n = IOUtil.read(fd, buf, -1, nd); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
824 |
} |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
825 |
} |
2 | 826 |
} finally { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
827 |
endRead(blocking, n > 0); |
2 | 828 |
assert IOStatus.check(n); |
829 |
} |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
830 |
return IOStatus.normalize(n); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
831 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
832 |
readLock.unlock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
833 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
834 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
835 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
836 |
@Override |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
837 |
public long read(ByteBuffer[] dsts, int offset, int length) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
838 |
throws IOException |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
839 |
{ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
840 |
Objects.checkFromIndexSize(offset, length, dsts.length); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
841 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
842 |
readLock.lock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
843 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
844 |
boolean blocking = isBlocking(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
845 |
long n = 0; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
846 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
847 |
beginRead(blocking, true); |
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
848 |
n = IOUtil.read(fd, dsts, offset, length, nd); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
849 |
if (blocking) { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
850 |
while (IOStatus.okayToRetry(n) && isOpen()) { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
851 |
park(Net.POLLIN); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
852 |
n = IOUtil.read(fd, dsts, offset, length, nd); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
853 |
} |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
854 |
} |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
855 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
856 |
endRead(blocking, n > 0); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
857 |
assert IOStatus.check(n); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
858 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
859 |
return IOStatus.normalize(n); |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
860 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
861 |
readLock.unlock(); |
2 | 862 |
} |
863 |
} |
|
864 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
865 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
866 |
* Marks the beginning of a write operation that might block. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
867 |
* @param blocking true if configured blocking |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
868 |
* @param mustBeConnected true if the socket must be connected |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
869 |
* @return remote address if connected |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
870 |
* @throws ClosedChannelException if the channel is closed |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
871 |
* @throws NotYetConnectedException if mustBeConnected and not connected |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
872 |
* @throws IOException if socket not bound and cannot be bound |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
873 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
874 |
private SocketAddress beginWrite(boolean blocking, boolean mustBeConnected) |
6301 | 875 |
throws IOException |
876 |
{ |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
877 |
if (blocking) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
878 |
// set hook for Thread.interrupt |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
879 |
begin(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
880 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
881 |
SocketAddress remote; |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
882 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
883 |
ensureOpen(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
884 |
remote = remoteAddress; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
885 |
if ((remote == null) && mustBeConnected) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
886 |
throw new NotYetConnectedException(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
887 |
if (localAddress == null) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
888 |
bindInternal(null); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
889 |
if (blocking) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
890 |
writerThread = NativeThread.current(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
891 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
892 |
return remote; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
893 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
894 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
895 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
896 |
* Marks the end of a write operation that may have blocked. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
897 |
* |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
898 |
* @throws AsynchronousCloseException if the channel was closed asynchronously |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
899 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
900 |
private void endWrite(boolean blocking, boolean completed) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
901 |
throws AsynchronousCloseException |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
902 |
{ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
903 |
if (blocking) { |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
904 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
905 |
writerThread = 0; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
906 |
if (state == ST_CLOSING) { |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
907 |
tryFinishClose(); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
908 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
909 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
910 |
// remove hook for Thread.interrupt |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
911 |
end(completed); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
912 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
913 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
914 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
915 |
@Override |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
916 |
public int write(ByteBuffer buf) throws IOException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
917 |
Objects.requireNonNull(buf); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
918 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
919 |
writeLock.lock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
920 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
921 |
boolean blocking = isBlocking(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
922 |
int n = 0; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
923 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
924 |
beginWrite(blocking, true); |
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
925 |
n = IOUtil.write(fd, buf, -1, nd); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
926 |
if (blocking) { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
927 |
while (IOStatus.okayToRetry(n) && isOpen()) { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
928 |
park(Net.POLLOUT); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
929 |
n = IOUtil.write(fd, buf, -1, nd); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
930 |
} |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
931 |
} |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
932 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
933 |
endWrite(blocking, n > 0); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
934 |
assert IOStatus.check(n); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
935 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
936 |
return IOStatus.normalize(n); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
937 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
938 |
writeLock.unlock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
939 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
940 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
941 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
942 |
@Override |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
943 |
public long write(ByteBuffer[] srcs, int offset, int length) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
944 |
throws IOException |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
945 |
{ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
946 |
Objects.checkFromIndexSize(offset, length, srcs.length); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
947 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
948 |
writeLock.lock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
949 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
950 |
boolean blocking = isBlocking(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
951 |
long n = 0; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
952 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
953 |
beginWrite(blocking, true); |
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
954 |
n = IOUtil.write(fd, srcs, offset, length, nd); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
955 |
if (blocking) { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
956 |
while (IOStatus.okayToRetry(n) && isOpen()) { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
957 |
park(Net.POLLOUT); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
958 |
n = IOUtil.write(fd, srcs, offset, length, nd); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
959 |
} |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
53419
diff
changeset
|
960 |
} |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
961 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
962 |
endWrite(blocking, n > 0); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
963 |
assert IOStatus.check(n); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
964 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
965 |
return IOStatus.normalize(n); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
966 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
967 |
writeLock.unlock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
968 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
969 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
970 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
971 |
@Override |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
972 |
protected void implConfigureBlocking(boolean block) throws IOException { |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
973 |
readLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
974 |
try { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
975 |
writeLock.lock(); |
2 | 976 |
try { |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
977 |
lockedConfigureBlocking(block); |
2 | 978 |
} finally { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
979 |
writeLock.unlock(); |
2 | 980 |
} |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
981 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
982 |
readLock.unlock(); |
2 | 983 |
} |
984 |
} |
|
985 |
||
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
986 |
/** |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
987 |
* Adjusts the blocking mode. readLock or writeLock must already be held. |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
988 |
*/ |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
989 |
private void lockedConfigureBlocking(boolean block) throws IOException { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
990 |
assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread(); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
991 |
synchronized (stateLock) { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
992 |
ensureOpen(); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
993 |
IOUtil.configureBlocking(fd, block); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
994 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
995 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
996 |
|
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
997 |
/** |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
998 |
* Adjusts the blocking mode if the channel is open. readLock or writeLock |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
999 |
* must already be held. |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1000 |
* |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1001 |
* @return {@code true} if the blocking mode was adjusted, {@code false} if |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1002 |
* the blocking mode was not adjusted because the channel is closed |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1003 |
*/ |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1004 |
private boolean tryLockedConfigureBlocking(boolean block) throws IOException { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1005 |
assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread(); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1006 |
synchronized (stateLock) { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1007 |
if (isOpen()) { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1008 |
IOUtil.configureBlocking(fd, block); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1009 |
return true; |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1010 |
} else { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1011 |
return false; |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1012 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1013 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1014 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1015 |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1016 |
InetSocketAddress localAddress() { |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1017 |
synchronized (stateLock) { |
2 | 1018 |
return localAddress; |
1019 |
} |
|
1020 |
} |
|
1021 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1022 |
InetSocketAddress remoteAddress() { |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1023 |
synchronized (stateLock) { |
2 | 1024 |
return remoteAddress; |
1025 |
} |
|
1026 |
} |
|
1027 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1028 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1029 |
public DatagramChannel bind(SocketAddress local) throws IOException { |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1030 |
readLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1031 |
try { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1032 |
writeLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1033 |
try { |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1034 |
synchronized (stateLock) { |
2 | 1035 |
ensureOpen(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1036 |
if (localAddress != null) |
2 | 1037 |
throw new AlreadyBoundException(); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1038 |
bindInternal(local); |
2 | 1039 |
} |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1040 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1041 |
writeLock.unlock(); |
2 | 1042 |
} |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1043 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1044 |
readLock.unlock(); |
2 | 1045 |
} |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1046 |
return this; |
2 | 1047 |
} |
1048 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1049 |
private void bindInternal(SocketAddress local) throws IOException { |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1050 |
assert Thread.holdsLock(stateLock )&& (localAddress == null); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1051 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1052 |
InetSocketAddress isa; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1053 |
if (local == null) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1054 |
// only Inet4Address allowed with IPv4 socket |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1055 |
if (family == StandardProtocolFamily.INET) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1056 |
isa = new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1057 |
} else { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1058 |
isa = new InetSocketAddress(0); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1059 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1060 |
} else { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1061 |
isa = Net.checkAddress(local, family); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1062 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1063 |
SecurityManager sm = System.getSecurityManager(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1064 |
if (sm != null) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1065 |
sm.checkListen(isa.getPort()); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1066 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1067 |
Net.bind(family, fd, isa.getAddress(), isa.getPort()); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1068 |
localAddress = Net.localAddress(fd); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1069 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1070 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1071 |
@Override |
2 | 1072 |
public boolean isConnected() { |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1073 |
synchronized (stateLock) { |
2 | 1074 |
return (state == ST_CONNECTED); |
1075 |
} |
|
1076 |
} |
|
1077 |
||
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
1078 |
@Override |
2 | 1079 |
public DatagramChannel connect(SocketAddress sa) throws IOException { |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1080 |
return connect(sa, true); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1081 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1082 |
|
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1083 |
/** |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1084 |
* Connects the channel's socket. |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1085 |
* |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1086 |
* @param sa the remote address to which this channel is to be connected |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1087 |
* @param check true to check if the channel is already connected. |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1088 |
*/ |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1089 |
DatagramChannel connect(SocketAddress sa, boolean check) throws IOException { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1090 |
InetSocketAddress isa = Net.checkAddress(sa, family); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1091 |
SecurityManager sm = System.getSecurityManager(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1092 |
if (sm != null) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1093 |
InetAddress ia = isa.getAddress(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1094 |
if (ia.isMulticastAddress()) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1095 |
sm.checkMulticast(ia); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1096 |
} else { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1097 |
sm.checkConnect(ia.getHostAddress(), isa.getPort()); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1098 |
sm.checkAccept(ia.getHostAddress(), isa.getPort()); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1099 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1100 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1101 |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1102 |
readLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1103 |
try { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1104 |
writeLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1105 |
try { |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1106 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1107 |
ensureOpen(); |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1108 |
if (check && state == ST_CONNECTED) |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1109 |
throw new AlreadyConnectedException(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1110 |
|
58518
705c3f88a409
8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
55375
diff
changeset
|
1111 |
// ensure that the socket is bound |
705c3f88a409
8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
55375
diff
changeset
|
1112 |
if (localAddress == null) { |
705c3f88a409
8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
55375
diff
changeset
|
1113 |
bindInternal(null); |
705c3f88a409
8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
55375
diff
changeset
|
1114 |
} |
705c3f88a409
8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
55375
diff
changeset
|
1115 |
|
59146
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1116 |
// capture local address before connect |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1117 |
initialLocalAddress = localAddress; |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1118 |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1119 |
int n = Net.connect(family, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1120 |
fd, |
2 | 1121 |
isa.getAddress(), |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1122 |
isa.getPort()); |
2 | 1123 |
if (n <= 0) |
1124 |
throw new Error(); // Can't happen |
|
1125 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1126 |
// connected |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
1127 |
remoteAddress = isa; |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1128 |
state = ST_CONNECTED; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1129 |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1130 |
// refresh local address |
7045
525f00c555b2
6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
6301
diff
changeset
|
1131 |
localAddress = Net.localAddress(fd); |
27078 | 1132 |
|
1133 |
// flush any packets already received. |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1134 |
boolean blocking = isBlocking(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1135 |
if (blocking) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1136 |
IOUtil.configureBlocking(fd, false); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1137 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1138 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1139 |
ByteBuffer buf = ByteBuffer.allocate(100); |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
58808
diff
changeset
|
1140 |
while (receive(buf, false) >= 0) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1141 |
buf.clear(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1142 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1143 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1144 |
if (blocking) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1145 |
IOUtil.configureBlocking(fd, true); |
27078 | 1146 |
} |
1147 |
} |
|
2 | 1148 |
} |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1149 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1150 |
writeLock.unlock(); |
2 | 1151 |
} |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1152 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1153 |
readLock.unlock(); |
2 | 1154 |
} |
1155 |
return this; |
|
1156 |
} |
|
1157 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1158 |
@Override |
2 | 1159 |
public DatagramChannel disconnect() throws IOException { |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1160 |
readLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1161 |
try { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1162 |
writeLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1163 |
try { |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1164 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1165 |
if (!isOpen() || (state != ST_CONNECTED)) |
2 | 1166 |
return this; |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1167 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1168 |
// disconnect socket |
12547
ae1b2051db5d
7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
12440
diff
changeset
|
1169 |
boolean isIPv6 = (family == StandardProtocolFamily.INET6); |
ae1b2051db5d
7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
12440
diff
changeset
|
1170 |
disconnect0(fd, isIPv6); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1171 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1172 |
// no longer connected |
2 | 1173 |
remoteAddress = null; |
1174 |
state = ST_UNCONNECTED; |
|
7045
525f00c555b2
6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
6301
diff
changeset
|
1175 |
|
59146
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1176 |
// refresh localAddress, should be same as it was prior to connect |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1177 |
localAddress = Net.localAddress(fd); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1178 |
try { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1179 |
if (!localAddress.equals(initialLocalAddress)) { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1180 |
// Workaround connect(2) issues on Linux and macOS |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1181 |
repairSocket(initialLocalAddress); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1182 |
assert (localAddress != null) |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1183 |
&& localAddress.equals(Net.localAddress(fd)) |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1184 |
&& localAddress.equals(initialLocalAddress); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1185 |
} |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1186 |
} finally { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1187 |
initialLocalAddress = null; |
58518
705c3f88a409
8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
55375
diff
changeset
|
1188 |
} |
2 | 1189 |
} |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1190 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1191 |
writeLock.unlock(); |
2 | 1192 |
} |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1193 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
1194 |
readLock.unlock(); |
2 | 1195 |
} |
1196 |
return this; |
|
1197 |
} |
|
1198 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1199 |
/** |
59146
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1200 |
* "Repair" the channel's socket after a disconnect that didn't restore the |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1201 |
* local address. |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1202 |
* |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1203 |
* On Linux, connect(2) dissolves the association but changes the local port |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1204 |
* to 0 when it was initially bound to an ephemeral port. The workaround here |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1205 |
* is to rebind to the original port. |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1206 |
* |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1207 |
* On macOS, connect(2) dissolves the association but rebinds the socket to |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1208 |
* the wildcard address when it was initially bound to a specific address. |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1209 |
* The workaround here is to re-create the socket. |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1210 |
*/ |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1211 |
private void repairSocket(InetSocketAddress target) |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1212 |
throws IOException |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1213 |
{ |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1214 |
assert Thread.holdsLock(stateLock); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1215 |
|
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1216 |
// Linux: try to bind the socket to the original address/port |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1217 |
if (localAddress.getPort() == 0) { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1218 |
assert localAddress.getAddress().equals(target.getAddress()); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1219 |
Net.bind(family, fd, target.getAddress(), target.getPort()); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1220 |
localAddress = Net.localAddress(fd); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1221 |
return; |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1222 |
} |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1223 |
|
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1224 |
// capture the value of all existing socket options |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1225 |
Map<SocketOption<?>, Object> map = new HashMap<>(); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1226 |
for (SocketOption<?> option : supportedOptions()) { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1227 |
Object value = getOption(option); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1228 |
if (value != null) { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1229 |
map.put(option, value); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1230 |
} |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1231 |
} |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1232 |
|
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1233 |
// macOS: re-create the socket. |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1234 |
FileDescriptor newfd = Net.socket(family, false); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1235 |
try { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1236 |
// copy the socket options that are protocol family agnostic |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1237 |
for (Map.Entry<SocketOption<?>, Object> e : map.entrySet()) { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1238 |
SocketOption<?> option = e.getKey(); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1239 |
if (SocketOptionRegistry.findOption(option, Net.UNSPEC) != null) { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1240 |
Object value = e.getValue(); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1241 |
try { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1242 |
Net.setSocketOption(newfd, Net.UNSPEC, option, value); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1243 |
} catch (IOException ignore) { } |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1244 |
} |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1245 |
} |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1246 |
|
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1247 |
// copy the blocking mode |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1248 |
if (!isBlocking()) { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1249 |
IOUtil.configureBlocking(newfd, false); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1250 |
} |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1251 |
|
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1252 |
// dup this channel's socket to the new socket. If this succeeds then |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1253 |
// fd will reference the new socket. If it fails then it will still |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1254 |
// reference the old socket. |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1255 |
nd.dup(newfd, fd); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1256 |
} finally { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1257 |
// release the file descriptor |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1258 |
nd.close(newfd); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1259 |
} |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1260 |
|
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1261 |
// bind to the original local address |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1262 |
try { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1263 |
Net.bind(family, fd, target.getAddress(), target.getPort()); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1264 |
} catch (IOException ioe) { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1265 |
// bind failed, socket is left unbound |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1266 |
localAddress = null; |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1267 |
throw ioe; |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1268 |
} |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1269 |
|
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1270 |
// restore local address |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1271 |
localAddress = Net.localAddress(fd); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1272 |
|
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1273 |
// restore all socket options (including those set in first pass) |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1274 |
for (Map.Entry<SocketOption<?>, Object> e : map.entrySet()) { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1275 |
@SuppressWarnings("unchecked") |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1276 |
SocketOption<Object> option = (SocketOption<Object>) e.getKey(); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1277 |
Object value = e.getValue(); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1278 |
try { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1279 |
setOption(option, value); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1280 |
} catch (IOException ignore) { } |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1281 |
} |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1282 |
|
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1283 |
// restore multicast group membership |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1284 |
MembershipRegistry registry = this.registry; |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1285 |
if (registry != null) { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1286 |
registry.forEach(k -> { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1287 |
if (k instanceof MembershipKeyImpl.Type6) { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1288 |
MembershipKeyImpl.Type6 key6 = (MembershipKeyImpl.Type6) k; |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1289 |
Net.join6(fd, key6.groupAddress(), key6.index(), key6.source()); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1290 |
} else { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1291 |
MembershipKeyImpl.Type4 key4 = (MembershipKeyImpl.Type4) k; |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1292 |
Net.join4(fd, key4.groupAddress(), key4.interfaceAddress(), key4.source()); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1293 |
} |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1294 |
}); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1295 |
} |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1296 |
|
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1297 |
// reset registration in all Selectors that this channel is registered with |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1298 |
AbstractSelectableChannels.forEach(this, SelectionKeyImpl::reset); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1299 |
} |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1300 |
|
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1301 |
/** |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1302 |
* Defines static methods to access AbstractSelectableChannel non-public members. |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1303 |
*/ |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1304 |
private static class AbstractSelectableChannels { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1305 |
private static final Method FOREACH; |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1306 |
static { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1307 |
try { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1308 |
PrivilegedExceptionAction<Method> pae = () -> { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1309 |
Method m = AbstractSelectableChannel.class.getDeclaredMethod("forEach", Consumer.class); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1310 |
m.setAccessible(true); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1311 |
return m; |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1312 |
}; |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1313 |
FOREACH = AccessController.doPrivileged(pae); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1314 |
} catch (Exception e) { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1315 |
throw new InternalError(e); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1316 |
} |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1317 |
} |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1318 |
static void forEach(AbstractSelectableChannel ch, Consumer<SelectionKeyImpl> action) { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1319 |
try { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1320 |
FOREACH.invoke(ch, action); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1321 |
} catch (Exception e) { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1322 |
throw new InternalError(e); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1323 |
} |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1324 |
} |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1325 |
} |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1326 |
|
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
59000
diff
changeset
|
1327 |
/** |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1328 |
* Joins channel's socket to the given group/interface and |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1329 |
* optional source address. |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1330 |
*/ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1331 |
private MembershipKey innerJoin(InetAddress group, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1332 |
NetworkInterface interf, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1333 |
InetAddress source) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1334 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1335 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1336 |
if (!group.isMulticastAddress()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1337 |
throw new IllegalArgumentException("Group not a multicast address"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1338 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1339 |
// check multicast address is compatible with this socket |
8788
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
1340 |
if (group instanceof Inet4Address) { |
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
1341 |
if (family == StandardProtocolFamily.INET6 && !Net.canIPv6SocketJoinIPv4Group()) |
9237
cbb5753e87e7
7034155: (ch) NullPointerException in sun.io.ch.IOUtil when OOM is thrown
alanb
parents:
8817
diff
changeset
|
1342 |
throw new IllegalArgumentException("IPv6 socket cannot join IPv4 multicast group"); |
8788
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
1343 |
} else if (group instanceof Inet6Address) { |
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
1344 |
if (family != StandardProtocolFamily.INET6) |
9237
cbb5753e87e7
7034155: (ch) NullPointerException in sun.io.ch.IOUtil when OOM is thrown
alanb
parents:
8817
diff
changeset
|
1345 |
throw new IllegalArgumentException("Only IPv6 sockets can join IPv6 multicast group"); |
8788
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
1346 |
} else { |
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
1347 |
throw new IllegalArgumentException("Address type not supported"); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1348 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1349 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1350 |
// check source address |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1351 |
if (source != null) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1352 |
if (source.isAnyLocalAddress()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1353 |
throw new IllegalArgumentException("Source address is a wildcard address"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1354 |
if (source.isMulticastAddress()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1355 |
throw new IllegalArgumentException("Source address is multicast address"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1356 |
if (source.getClass() != group.getClass()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1357 |
throw new IllegalArgumentException("Source address is different type to group"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1358 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1359 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1360 |
SecurityManager sm = System.getSecurityManager(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1361 |
if (sm != null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1362 |
sm.checkMulticast(group); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1363 |
|
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1364 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1365 |
ensureOpen(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1366 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1367 |
// check the registry to see if we are already a member of the group |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1368 |
if (registry == null) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1369 |
registry = new MembershipRegistry(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1370 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1371 |
// return existing membership key |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1372 |
MembershipKey key = registry.checkMembership(group, interf, source); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1373 |
if (key != null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1374 |
return key; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1375 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1376 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1377 |
MembershipKeyImpl key; |
8788
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
1378 |
if ((family == StandardProtocolFamily.INET6) && |
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
1379 |
((group instanceof Inet6Address) || Net.canJoin6WithIPv4Group())) |
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
1380 |
{ |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1381 |
int index = interf.getIndex(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1382 |
if (index == -1) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1383 |
throw new IOException("Network interface cannot be identified"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1384 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1385 |
// need multicast and source address as byte arrays |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1386 |
byte[] groupAddress = Net.inet6AsByteArray(group); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1387 |
byte[] sourceAddress = (source == null) ? null : |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1388 |
Net.inet6AsByteArray(source); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1389 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1390 |
// join the group |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1391 |
int n = Net.join6(fd, groupAddress, index, sourceAddress); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1392 |
if (n == IOStatus.UNAVAILABLE) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1393 |
throw new UnsupportedOperationException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1394 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1395 |
key = new MembershipKeyImpl.Type6(this, group, interf, source, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1396 |
groupAddress, index, sourceAddress); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1397 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1398 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1399 |
// need IPv4 address to identify interface |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1400 |
Inet4Address target = Net.anyInet4Address(interf); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1401 |
if (target == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1402 |
throw new IOException("Network interface not configured for IPv4"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1403 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1404 |
int groupAddress = Net.inet4AsInt(group); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1405 |
int targetAddress = Net.inet4AsInt(target); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1406 |
int sourceAddress = (source == null) ? 0 : Net.inet4AsInt(source); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1407 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1408 |
// join the group |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1409 |
int n = Net.join4(fd, groupAddress, targetAddress, sourceAddress); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1410 |
if (n == IOStatus.UNAVAILABLE) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1411 |
throw new UnsupportedOperationException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1412 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1413 |
key = new MembershipKeyImpl.Type4(this, group, interf, source, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1414 |
groupAddress, targetAddress, sourceAddress); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1415 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1416 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1417 |
registry.add(key); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1418 |
return key; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1419 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1420 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1421 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1422 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1423 |
public MembershipKey join(InetAddress group, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1424 |
NetworkInterface interf) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1425 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1426 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1427 |
return innerJoin(group, interf, null); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1428 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1429 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1430 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1431 |
public MembershipKey join(InetAddress group, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1432 |
NetworkInterface interf, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1433 |
InetAddress source) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1434 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1435 |
{ |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1436 |
Objects.requireNonNull(source); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1437 |
return innerJoin(group, interf, source); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1438 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1439 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1440 |
// package-private |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1441 |
void drop(MembershipKeyImpl key) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1442 |
assert key.channel() == this; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1443 |
|
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1444 |
synchronized (stateLock) { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1445 |
if (!key.isValid()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1446 |
return; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1447 |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1448 |
try { |
8788
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
1449 |
if (key instanceof MembershipKeyImpl.Type6) { |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1450 |
MembershipKeyImpl.Type6 key6 = |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1451 |
(MembershipKeyImpl.Type6)key; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1452 |
Net.drop6(fd, key6.groupAddress(), key6.index(), key6.source()); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1453 |
} else { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1454 |
MembershipKeyImpl.Type4 key4 = (MembershipKeyImpl.Type4)key; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1455 |
Net.drop4(fd, key4.groupAddress(), key4.interfaceAddress(), |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1456 |
key4.source()); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1457 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1458 |
} catch (IOException ioe) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1459 |
// should not happen |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1460 |
throw new AssertionError(ioe); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1461 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1462 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1463 |
key.invalidate(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1464 |
registry.remove(key); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1465 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1466 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1467 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1468 |
/** |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1469 |
* Block datagrams from given source if a memory to receive all |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1470 |
* datagrams. |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1471 |
*/ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1472 |
void block(MembershipKeyImpl key, InetAddress source) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1473 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1474 |
{ |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1475 |
assert key.channel() == this; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1476 |
assert key.sourceAddress() == null; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1477 |
|
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1478 |
synchronized (stateLock) { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1479 |
if (!key.isValid()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1480 |
throw new IllegalStateException("key is no longer valid"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1481 |
if (source.isAnyLocalAddress()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1482 |
throw new IllegalArgumentException("Source address is a wildcard address"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1483 |
if (source.isMulticastAddress()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1484 |
throw new IllegalArgumentException("Source address is multicast address"); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1485 |
if (source.getClass() != key.group().getClass()) |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1486 |
throw new IllegalArgumentException("Source address is different type to group"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1487 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1488 |
int n; |
8788
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
1489 |
if (key instanceof MembershipKeyImpl.Type6) { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1490 |
MembershipKeyImpl.Type6 key6 = |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1491 |
(MembershipKeyImpl.Type6)key; |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1492 |
n = Net.block6(fd, key6.groupAddress(), key6.index(), |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1493 |
Net.inet6AsByteArray(source)); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1494 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1495 |
MembershipKeyImpl.Type4 key4 = |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1496 |
(MembershipKeyImpl.Type4)key; |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1497 |
n = Net.block4(fd, key4.groupAddress(), key4.interfaceAddress(), |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1498 |
Net.inet4AsInt(source)); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1499 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1500 |
if (n == IOStatus.UNAVAILABLE) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1501 |
// ancient kernel |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1502 |
throw new UnsupportedOperationException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1503 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1504 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1505 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1506 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1507 |
/** |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1508 |
* Unblock given source. |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1509 |
*/ |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1510 |
void unblock(MembershipKeyImpl key, InetAddress source) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1511 |
assert key.channel() == this; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1512 |
assert key.sourceAddress() == null; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1513 |
|
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1514 |
synchronized (stateLock) { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1515 |
if (!key.isValid()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1516 |
throw new IllegalStateException("key is no longer valid"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1517 |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1518 |
try { |
8788
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
1519 |
if (key instanceof MembershipKeyImpl.Type6) { |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1520 |
MembershipKeyImpl.Type6 key6 = |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1521 |
(MembershipKeyImpl.Type6)key; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1522 |
Net.unblock6(fd, key6.groupAddress(), key6.index(), |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1523 |
Net.inet6AsByteArray(source)); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1524 |
} else { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1525 |
MembershipKeyImpl.Type4 key4 = |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1526 |
(MembershipKeyImpl.Type4)key; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1527 |
Net.unblock4(fd, key4.groupAddress(), key4.interfaceAddress(), |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1528 |
Net.inet4AsInt(source)); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1529 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1530 |
} catch (IOException ioe) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1531 |
// should not happen |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1532 |
throw new AssertionError(ioe); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1533 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1534 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1535 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1536 |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1537 |
/** |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1538 |
* Closes the socket if there are no I/O operations in progress and the |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1539 |
* channel is not registered with a Selector. |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1540 |
*/ |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1541 |
private boolean tryClose() throws IOException { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1542 |
assert Thread.holdsLock(stateLock) && state == ST_CLOSING; |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1543 |
if ((readerThread == 0) && (writerThread == 0) && !isRegistered()) { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1544 |
state = ST_CLOSED; |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1545 |
try { |
58808
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
1546 |
// close socket |
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
1547 |
cleaner.clean(); |
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
1548 |
} catch (UncheckedIOException ioe) { |
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
1549 |
throw ioe.getCause(); |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1550 |
} |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1551 |
return true; |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1552 |
} else { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1553 |
return false; |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1554 |
} |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1555 |
} |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1556 |
|
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1557 |
/** |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1558 |
* Invokes tryClose to attempt to close the socket. |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1559 |
* |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1560 |
* This method is used for deferred closing by I/O and Selector operations. |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1561 |
*/ |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1562 |
private void tryFinishClose() { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1563 |
try { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1564 |
tryClose(); |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1565 |
} catch (IOException ignore) { } |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1566 |
} |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1567 |
|
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1568 |
/** |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1569 |
* Closes this channel when configured in blocking mode. |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1570 |
* |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1571 |
* If there is an I/O operation in progress then the socket is pre-closed |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1572 |
* and the I/O threads signalled, in which case the final close is deferred |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1573 |
* until all I/O operations complete. |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1574 |
*/ |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1575 |
private void implCloseBlockingMode() throws IOException { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1576 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1577 |
assert state < ST_CLOSING; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1578 |
state = ST_CLOSING; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1579 |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1580 |
// if member of any multicast groups then invalidate the keys |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1581 |
if (registry != null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1582 |
registry.invalidateAll(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1583 |
|
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1584 |
if (!tryClose()) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1585 |
long reader = readerThread; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1586 |
long writer = writerThread; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1587 |
if (reader != 0 || writer != 0) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1588 |
nd.preClose(fd); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1589 |
if (reader != 0) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1590 |
NativeThread.signal(reader); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1591 |
if (writer != 0) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1592 |
NativeThread.signal(writer); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1593 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1594 |
} |
2 | 1595 |
} |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1596 |
} |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1597 |
|
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1598 |
/** |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1599 |
* Closes this channel when configured in non-blocking mode. |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1600 |
* |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1601 |
* If the channel is registered with a Selector then the close is deferred |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1602 |
* until the channel is flushed from all Selectors. |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1603 |
*/ |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1604 |
private void implCloseNonBlockingMode() throws IOException { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1605 |
synchronized (stateLock) { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1606 |
assert state < ST_CLOSING; |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1607 |
state = ST_CLOSING; |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1608 |
|
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1609 |
// if member of any multicast groups then invalidate the keys |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1610 |
if (registry != null) |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1611 |
registry.invalidateAll(); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1612 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1613 |
|
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1614 |
// wait for any read/write operations to complete before trying to close |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1615 |
readLock.lock(); |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1616 |
readLock.unlock(); |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1617 |
writeLock.lock(); |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1618 |
writeLock.unlock(); |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1619 |
synchronized (stateLock) { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1620 |
if (state == ST_CLOSING) { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1621 |
tryClose(); |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1622 |
} |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1623 |
} |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1624 |
} |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1625 |
|
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1626 |
/** |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1627 |
* Invoked by implCloseChannel to close the channel. |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1628 |
*/ |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1629 |
@Override |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1630 |
protected void implCloseSelectableChannel() throws IOException { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1631 |
assert !isOpen(); |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1632 |
if (isBlocking()) { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1633 |
implCloseBlockingMode(); |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1634 |
} else { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1635 |
implCloseNonBlockingMode(); |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1636 |
} |
2 | 1637 |
} |
1638 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1639 |
@Override |
54754
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1640 |
public void kill() { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1641 |
synchronized (stateLock) { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1642 |
if (state == ST_CLOSING) { |
193a8f1a4f3b
8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents:
54620
diff
changeset
|
1643 |
tryFinishClose(); |
2 | 1644 |
} |
1645 |
} |
|
1646 |
} |
|
1647 |
||
1648 |
/** |
|
1649 |
* Translates native poll revent set into a ready operation set |
|
1650 |
*/ |
|
49493 | 1651 |
public boolean translateReadyOps(int ops, int initialOps, SelectionKeyImpl ski) { |
1652 |
int intOps = ski.nioInterestOps(); |
|
1653 |
int oldOps = ski.nioReadyOps(); |
|
2 | 1654 |
int newOps = initialOps; |
1655 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1656 |
if ((ops & Net.POLLNVAL) != 0) { |
2 | 1657 |
// This should only happen if this channel is pre-closed while a |
1658 |
// selection operation is in progress |
|
1659 |
// ## Throw an error if this channel has not been pre-closed |
|
1660 |
return false; |
|
1661 |
} |
|
1662 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1663 |
if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) { |
2 | 1664 |
newOps = intOps; |
49493 | 1665 |
ski.nioReadyOps(newOps); |
2 | 1666 |
return (newOps & ~oldOps) != 0; |
1667 |
} |
|
1668 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1669 |
if (((ops & Net.POLLIN) != 0) && |
2 | 1670 |
((intOps & SelectionKey.OP_READ) != 0)) |
1671 |
newOps |= SelectionKey.OP_READ; |
|
1672 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1673 |
if (((ops & Net.POLLOUT) != 0) && |
2 | 1674 |
((intOps & SelectionKey.OP_WRITE) != 0)) |
1675 |
newOps |= SelectionKey.OP_WRITE; |
|
1676 |
||
49493 | 1677 |
ski.nioReadyOps(newOps); |
2 | 1678 |
return (newOps & ~oldOps) != 0; |
1679 |
} |
|
1680 |
||
49493 | 1681 |
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl ski) { |
1682 |
return translateReadyOps(ops, ski.nioReadyOps(), ski); |
|
2 | 1683 |
} |
1684 |
||
49493 | 1685 |
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl ski) { |
1686 |
return translateReadyOps(ops, 0, ski); |
|
2 | 1687 |
} |
1688 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1689 |
/** |
2 | 1690 |
* Translates an interest operation set into a native poll event set |
1691 |
*/ |
|
49493 | 1692 |
public int translateInterestOps(int ops) { |
2 | 1693 |
int newOps = 0; |
1694 |
if ((ops & SelectionKey.OP_READ) != 0) |
|
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1695 |
newOps |= Net.POLLIN; |
2 | 1696 |
if ((ops & SelectionKey.OP_WRITE) != 0) |
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1697 |
newOps |= Net.POLLOUT; |
2 | 1698 |
if ((ops & SelectionKey.OP_CONNECT) != 0) |
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1699 |
newOps |= Net.POLLIN; |
49493 | 1700 |
return newOps; |
2 | 1701 |
} |
1702 |
||
1703 |
public FileDescriptor getFD() { |
|
1704 |
return fd; |
|
1705 |
} |
|
1706 |
||
1707 |
public int getFDVal() { |
|
1708 |
return fdVal; |
|
1709 |
} |
|
1710 |
||
58808
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
1711 |
/** |
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
1712 |
* Returns an action to close the given file descriptor. |
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
1713 |
*/ |
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
1714 |
private static Runnable closerFor(FileDescriptor fd) { |
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
1715 |
return () -> { |
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
1716 |
try { |
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
1717 |
nd.close(fd); |
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
1718 |
} catch (IOException ioe) { |
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
1719 |
throw new UncheckedIOException(ioe); |
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
1720 |
} finally { |
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
1721 |
// decrement |
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
1722 |
ResourceManager.afterUdpClose(); |
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
1723 |
} |
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
1724 |
}; |
9261ad32cba9
8212132: (dc) Remove DatagramChannelImpl finalize method
alanb
parents:
58518
diff
changeset
|
1725 |
} |
2 | 1726 |
|
1727 |
// -- Native methods -- |
|
1728 |
||
1729 |
private static native void initIDs(); |
|
1730 |
||
12547
ae1b2051db5d
7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
12440
diff
changeset
|
1731 |
private static native void disconnect0(FileDescriptor fd, boolean isIPv6) |
2 | 1732 |
throws IOException; |
1733 |
||
1734 |
private native int receive0(FileDescriptor fd, long address, int len, |
|
1735 |
boolean connected) |
|
1736 |
throws IOException; |
|
1737 |
||
16089 | 1738 |
private native int send0(boolean preferIPv6, FileDescriptor fd, long address, |
1739 |
int len, InetAddress addr, int port) |
|
2 | 1740 |
throws IOException; |
1741 |
||
1742 |
static { |
|
19607
bee007586d06
8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents:
18212
diff
changeset
|
1743 |
IOUtil.load(); |
2 | 1744 |
initIDs(); |
1745 |
} |
|
1746 |
} |