author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 44534 | jdk/src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java@a076dffbc2c1 |
child 48750 | ffbb784a8873 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
44534 | 2 |
* Copyright (c) 2001, 2017, 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; |
|
30 |
import java.net.*; |
|
31 |
import java.nio.ByteBuffer; |
|
32 |
import java.nio.channels.*; |
|
33 |
import java.nio.channels.spi.*; |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
34 |
import java.util.*; |
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
35 |
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
|
36 |
import sun.net.ext.ExtendedSocketOptions; |
2 | 37 |
|
38 |
/** |
|
39 |
* An implementation of DatagramChannels. |
|
40 |
*/ |
|
41 |
||
42 |
class DatagramChannelImpl |
|
43 |
extends DatagramChannel |
|
44 |
implements SelChImpl |
|
45 |
{ |
|
46 |
||
47 |
// Used to make native read and write calls |
|
48 |
private static NativeDispatcher nd = new DatagramDispatcher(); |
|
49 |
||
50 |
// Our file descriptor |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
51 |
private final FileDescriptor fd; |
2 | 52 |
|
53 |
// fd value needed for dev/poll. This value will remain valid |
|
54 |
// even after the value in the file descriptor object has been set to -1 |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
55 |
private final int fdVal; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
56 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
57 |
// The protocol family of the socket |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
58 |
private final ProtocolFamily family; |
2 | 59 |
|
60 |
// IDs of native threads doing reads and writes, for signalling |
|
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
29986
diff
changeset
|
61 |
private volatile long readerThread; |
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
29986
diff
changeset
|
62 |
private volatile long writerThread; |
2 | 63 |
|
64 |
// Cached InetAddress and port for unconnected DatagramChannels |
|
65 |
// used by receive0 |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
66 |
private InetAddress cachedSenderInetAddress; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
67 |
private int cachedSenderPort; |
2 | 68 |
|
69 |
// Lock held by current reading or connecting thread |
|
70 |
private final Object readLock = new Object(); |
|
71 |
||
72 |
// Lock held by current writing or connecting thread |
|
73 |
private final Object writeLock = new Object(); |
|
74 |
||
75 |
// Lock held by any thread that modifies the state fields declared below |
|
76 |
// DO NOT invoke a blocking I/O operation while holding this lock! |
|
77 |
private final Object stateLock = new Object(); |
|
78 |
||
79 |
// -- The following fields are protected by stateLock |
|
80 |
||
81 |
// State (does not necessarily increase monotonically) |
|
82 |
private static final int ST_UNINITIALIZED = -1; |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
83 |
private static final int ST_UNCONNECTED = 0; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
84 |
private static final int ST_CONNECTED = 1; |
2 | 85 |
private static final int ST_KILLED = 2; |
86 |
private int state = ST_UNINITIALIZED; |
|
87 |
||
88 |
// Binding |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
89 |
private InetSocketAddress localAddress; |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
90 |
private InetSocketAddress remoteAddress; |
2 | 91 |
|
92 |
// Our socket adaptor, if any |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
93 |
private DatagramSocket socket; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
94 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
95 |
// Multicast support |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
96 |
private MembershipRegistry registry; |
2 | 97 |
|
18192 | 98 |
// set true when socket is bound and SO_REUSEADDRESS is emulated |
99 |
private boolean reuseAddressEmulated; |
|
100 |
||
101 |
// set true/false when socket is already bound and SO_REUSEADDR is emulated |
|
102 |
private boolean isReuseAddress; |
|
103 |
||
2 | 104 |
// -- End of fields protected by stateLock |
105 |
||
106 |
||
107 |
public DatagramChannelImpl(SelectorProvider sp) |
|
108 |
throws IOException |
|
109 |
{ |
|
110 |
super(sp); |
|
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
111 |
ResourceManager.beforeUdpCreate(); |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
112 |
try { |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
113 |
this.family = Net.isIPv6Available() ? |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
114 |
StandardProtocolFamily.INET6 : StandardProtocolFamily.INET; |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
115 |
this.fd = Net.socket(family, false); |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
116 |
this.fdVal = IOUtil.fdVal(fd); |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
117 |
this.state = ST_UNCONNECTED; |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
118 |
} catch (IOException ioe) { |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
119 |
ResourceManager.afterUdpClose(); |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
120 |
throw ioe; |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
121 |
} |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
122 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
123 |
|
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
124 |
public DatagramChannelImpl(SelectorProvider sp, ProtocolFamily family) |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
125 |
throws IOException |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
126 |
{ |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
127 |
super(sp); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
128 |
if ((family != StandardProtocolFamily.INET) && |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
129 |
(family != StandardProtocolFamily.INET6)) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
130 |
{ |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
131 |
if (family == null) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
132 |
throw new NullPointerException("'family' is null"); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
133 |
else |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
134 |
throw new UnsupportedOperationException("Protocol family not supported"); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
135 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
136 |
if (family == StandardProtocolFamily.INET6) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
137 |
if (!Net.isIPv6Available()) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
138 |
throw new UnsupportedOperationException("IPv6 not available"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
139 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
140 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
141 |
this.family = family; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
142 |
this.fd = Net.socket(family, false); |
2 | 143 |
this.fdVal = IOUtil.fdVal(fd); |
144 |
this.state = ST_UNCONNECTED; |
|
145 |
} |
|
146 |
||
147 |
public DatagramChannelImpl(SelectorProvider sp, FileDescriptor fd) |
|
148 |
throws IOException |
|
149 |
{ |
|
150 |
super(sp); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
151 |
this.family = Net.isIPv6Available() ? |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
152 |
StandardProtocolFamily.INET6 : StandardProtocolFamily.INET; |
2 | 153 |
this.fd = fd; |
154 |
this.fdVal = IOUtil.fdVal(fd); |
|
155 |
this.state = ST_UNCONNECTED; |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
156 |
this.localAddress = Net.localAddress(fd); |
2 | 157 |
} |
158 |
||
159 |
public DatagramSocket socket() { |
|
160 |
synchronized (stateLock) { |
|
161 |
if (socket == null) |
|
162 |
socket = DatagramSocketAdaptor.create(this); |
|
163 |
return socket; |
|
164 |
} |
|
165 |
} |
|
166 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
167 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
168 |
public SocketAddress getLocalAddress() throws IOException { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
169 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
170 |
if (!isOpen()) |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
171 |
throw new ClosedChannelException(); |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
172 |
// Perform security check before returning address |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
173 |
return Net.getRevealedLocalAddress(localAddress); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
174 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
175 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
176 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
177 |
@Override |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
178 |
public SocketAddress getRemoteAddress() throws IOException { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
179 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
180 |
if (!isOpen()) |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
181 |
throw new ClosedChannelException(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
182 |
return remoteAddress; |
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 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
185 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
186 |
@Override |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
187 |
public <T> DatagramChannel setOption(SocketOption<T> name, T value) |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
188 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
189 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
190 |
if (name == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
191 |
throw new NullPointerException(); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
192 |
if (!supportedOptions().contains(name)) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
193 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
194 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
195 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
196 |
ensureOpen(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
197 |
|
25170
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
23879
diff
changeset
|
198 |
if (name == StandardSocketOptions.IP_TOS || |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
23879
diff
changeset
|
199 |
name == StandardSocketOptions.IP_MULTICAST_TTL || |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
9275
diff
changeset
|
200 |
name == StandardSocketOptions.IP_MULTICAST_LOOP) |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
201 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
202 |
// options are protocol dependent |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
203 |
Net.setSocketOption(fd, family, name, value); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
204 |
return this; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
205 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
206 |
|
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
9275
diff
changeset
|
207 |
if (name == StandardSocketOptions.IP_MULTICAST_IF) { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
208 |
if (value == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
209 |
throw new IllegalArgumentException("Cannot set IP_MULTICAST_IF to 'null'"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
210 |
NetworkInterface interf = (NetworkInterface)value; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
211 |
if (family == StandardProtocolFamily.INET6) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
212 |
int index = interf.getIndex(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
213 |
if (index == -1) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
214 |
throw new IOException("Network interface cannot be identified"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
215 |
Net.setInterface6(fd, index); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
216 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
217 |
// need IPv4 address to identify interface |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
218 |
Inet4Address target = Net.anyInet4Address(interf); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
219 |
if (target == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
220 |
throw new IOException("Network interface not configured for IPv4"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
221 |
int targetAddress = Net.inet4AsInt(target); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
222 |
Net.setInterface4(fd, targetAddress); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
223 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
224 |
return this; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
225 |
} |
18192 | 226 |
if (name == StandardSocketOptions.SO_REUSEADDR && |
227 |
Net.useExclusiveBind() && localAddress != null) |
|
228 |
{ |
|
229 |
reuseAddressEmulated = true; |
|
230 |
this.isReuseAddress = (Boolean)value; |
|
231 |
} |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
232 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
233 |
// remaining options don't need any special handling |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
234 |
Net.setSocketOption(fd, Net.UNSPEC, name, value); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
235 |
return this; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
236 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
237 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
238 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
239 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
240 |
@SuppressWarnings("unchecked") |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
241 |
public <T> T getOption(SocketOption<T> name) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
242 |
throws IOException |
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 |
if (name == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
245 |
throw new NullPointerException(); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
246 |
if (!supportedOptions().contains(name)) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
247 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
248 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
249 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
250 |
ensureOpen(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
251 |
|
25170
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
23879
diff
changeset
|
252 |
if (name == StandardSocketOptions.IP_TOS || |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
23879
diff
changeset
|
253 |
name == StandardSocketOptions.IP_MULTICAST_TTL || |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
9275
diff
changeset
|
254 |
name == StandardSocketOptions.IP_MULTICAST_LOOP) |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
255 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
256 |
return (T) Net.getSocketOption(fd, family, name); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
257 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
258 |
|
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
9275
diff
changeset
|
259 |
if (name == StandardSocketOptions.IP_MULTICAST_IF) { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
260 |
if (family == StandardProtocolFamily.INET) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
261 |
int address = Net.getInterface4(fd); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
262 |
if (address == 0) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
263 |
return null; // default interface |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
264 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
265 |
InetAddress ia = Net.inet4FromInt(address); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
266 |
NetworkInterface ni = NetworkInterface.getByInetAddress(ia); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
267 |
if (ni == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
268 |
throw new IOException("Unable to map address to interface"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
269 |
return (T) ni; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
270 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
271 |
int index = Net.getInterface6(fd); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
272 |
if (index == 0) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
273 |
return null; // default interface |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
274 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
275 |
NetworkInterface ni = NetworkInterface.getByIndex(index); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
276 |
if (ni == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
277 |
throw new IOException("Unable to map index to interface"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
278 |
return (T) ni; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
279 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
280 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
281 |
|
18192 | 282 |
if (name == StandardSocketOptions.SO_REUSEADDR && |
283 |
reuseAddressEmulated) |
|
284 |
{ |
|
285 |
return (T)Boolean.valueOf(isReuseAddress); |
|
286 |
} |
|
287 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
288 |
// no special handling |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
289 |
return (T) Net.getSocketOption(fd, Net.UNSPEC, name); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
290 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
291 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
292 |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
293 |
private static class DefaultOptionsHolder { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
294 |
static final Set<SocketOption<?>> defaultOptions = defaultOptions(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
295 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
296 |
private static Set<SocketOption<?>> defaultOptions() { |
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
27724
diff
changeset
|
297 |
HashSet<SocketOption<?>> set = new HashSet<>(8); |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
9275
diff
changeset
|
298 |
set.add(StandardSocketOptions.SO_SNDBUF); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
9275
diff
changeset
|
299 |
set.add(StandardSocketOptions.SO_RCVBUF); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
9275
diff
changeset
|
300 |
set.add(StandardSocketOptions.SO_REUSEADDR); |
36115 | 301 |
if (Net.isReusePortAvailable()) { |
302 |
set.add(StandardSocketOptions.SO_REUSEPORT); |
|
303 |
} |
|
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
9275
diff
changeset
|
304 |
set.add(StandardSocketOptions.SO_BROADCAST); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
9275
diff
changeset
|
305 |
set.add(StandardSocketOptions.IP_TOS); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
9275
diff
changeset
|
306 |
set.add(StandardSocketOptions.IP_MULTICAST_IF); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
9275
diff
changeset
|
307 |
set.add(StandardSocketOptions.IP_MULTICAST_TTL); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
9275
diff
changeset
|
308 |
set.add(StandardSocketOptions.IP_MULTICAST_LOOP); |
37676
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
36115
diff
changeset
|
309 |
ExtendedSocketOptions extendedOptions = |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
36115
diff
changeset
|
310 |
ExtendedSocketOptions.getInstance(); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
36115
diff
changeset
|
311 |
set.addAll(extendedOptions.options()); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
312 |
return Collections.unmodifiableSet(set); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
313 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
314 |
} |
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 |
@Override |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
317 |
public final Set<SocketOption<?>> supportedOptions() { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
318 |
return DefaultOptionsHolder.defaultOptions; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
319 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
320 |
|
2 | 321 |
private void ensureOpen() throws ClosedChannelException { |
322 |
if (!isOpen()) |
|
323 |
throw new ClosedChannelException(); |
|
324 |
} |
|
325 |
||
326 |
private SocketAddress sender; // Set by receive0 (## ugh) |
|
327 |
||
328 |
public SocketAddress receive(ByteBuffer dst) throws IOException { |
|
329 |
if (dst.isReadOnly()) |
|
330 |
throw new IllegalArgumentException("Read-only buffer"); |
|
331 |
synchronized (readLock) { |
|
332 |
ensureOpen(); |
|
2427
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2057
diff
changeset
|
333 |
// Socket was not bound before attempting receive |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2057
diff
changeset
|
334 |
if (localAddress() == null) |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2057
diff
changeset
|
335 |
bind(null); |
2 | 336 |
int n = 0; |
337 |
ByteBuffer bb = null; |
|
338 |
try { |
|
339 |
begin(); |
|
340 |
if (!isOpen()) |
|
341 |
return null; |
|
342 |
SecurityManager security = System.getSecurityManager(); |
|
343 |
readerThread = NativeThread.current(); |
|
344 |
if (isConnected() || (security == null)) { |
|
345 |
do { |
|
346 |
n = receive(fd, dst); |
|
347 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
|
348 |
if (n == IOStatus.UNAVAILABLE) |
|
349 |
return null; |
|
350 |
} else { |
|
351 |
bb = Util.getTemporaryDirectBuffer(dst.remaining()); |
|
352 |
for (;;) { |
|
353 |
do { |
|
354 |
n = receive(fd, bb); |
|
355 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
|
356 |
if (n == IOStatus.UNAVAILABLE) |
|
357 |
return null; |
|
358 |
InetSocketAddress isa = (InetSocketAddress)sender; |
|
359 |
try { |
|
360 |
security.checkAccept( |
|
361 |
isa.getAddress().getHostAddress(), |
|
362 |
isa.getPort()); |
|
363 |
} catch (SecurityException se) { |
|
364 |
// Ignore packet |
|
365 |
bb.clear(); |
|
366 |
n = 0; |
|
367 |
continue; |
|
368 |
} |
|
369 |
bb.flip(); |
|
370 |
dst.put(bb); |
|
371 |
break; |
|
372 |
} |
|
373 |
} |
|
374 |
return sender; |
|
375 |
} finally { |
|
376 |
if (bb != null) |
|
377 |
Util.releaseTemporaryDirectBuffer(bb); |
|
378 |
readerThread = 0; |
|
379 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
380 |
assert IOStatus.check(n); |
|
381 |
} |
|
382 |
} |
|
383 |
} |
|
384 |
||
385 |
private int receive(FileDescriptor fd, ByteBuffer dst) |
|
386 |
throws IOException |
|
387 |
{ |
|
388 |
int pos = dst.position(); |
|
389 |
int lim = dst.limit(); |
|
390 |
assert (pos <= lim); |
|
391 |
int rem = (pos <= lim ? lim - pos : 0); |
|
392 |
if (dst instanceof DirectBuffer && rem > 0) |
|
393 |
return receiveIntoNativeBuffer(fd, dst, rem, pos); |
|
394 |
||
395 |
// Substitute a native buffer. If the supplied buffer is empty |
|
396 |
// we must instead use a nonempty buffer, otherwise the call |
|
397 |
// will not block waiting for a datagram on some platforms. |
|
398 |
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
|
399 |
ByteBuffer bb = Util.getTemporaryDirectBuffer(newSize); |
2 | 400 |
try { |
401 |
int n = receiveIntoNativeBuffer(fd, bb, newSize, 0); |
|
402 |
bb.flip(); |
|
403 |
if (n > 0 && rem > 0) |
|
404 |
dst.put(bb); |
|
405 |
return n; |
|
406 |
} finally { |
|
407 |
Util.releaseTemporaryDirectBuffer(bb); |
|
408 |
} |
|
409 |
} |
|
410 |
||
411 |
private int receiveIntoNativeBuffer(FileDescriptor fd, ByteBuffer bb, |
|
412 |
int rem, int pos) |
|
413 |
throws IOException |
|
414 |
{ |
|
415 |
int n = receive0(fd, ((DirectBuffer)bb).address() + pos, rem, |
|
416 |
isConnected()); |
|
417 |
if (n > 0) |
|
418 |
bb.position(pos + n); |
|
419 |
return n; |
|
420 |
} |
|
421 |
||
422 |
public int send(ByteBuffer src, SocketAddress target) |
|
423 |
throws IOException |
|
424 |
{ |
|
425 |
if (src == null) |
|
426 |
throw new NullPointerException(); |
|
427 |
||
428 |
synchronized (writeLock) { |
|
429 |
ensureOpen(); |
|
16089 | 430 |
InetSocketAddress isa = Net.checkAddress(target); |
2 | 431 |
InetAddress ia = isa.getAddress(); |
432 |
if (ia == null) |
|
433 |
throw new IOException("Target address not resolved"); |
|
434 |
synchronized (stateLock) { |
|
435 |
if (!isConnected()) { |
|
436 |
if (target == null) |
|
437 |
throw new NullPointerException(); |
|
438 |
SecurityManager sm = System.getSecurityManager(); |
|
439 |
if (sm != null) { |
|
440 |
if (ia.isMulticastAddress()) { |
|
16089 | 441 |
sm.checkMulticast(ia); |
2 | 442 |
} else { |
16089 | 443 |
sm.checkConnect(ia.getHostAddress(), |
2 | 444 |
isa.getPort()); |
445 |
} |
|
446 |
} |
|
447 |
} else { // Connected case; Check address then write |
|
448 |
if (!target.equals(remoteAddress)) { |
|
449 |
throw new IllegalArgumentException( |
|
450 |
"Connected address not equal to target address"); |
|
451 |
} |
|
452 |
return write(src); |
|
453 |
} |
|
454 |
} |
|
455 |
||
456 |
int n = 0; |
|
457 |
try { |
|
458 |
begin(); |
|
459 |
if (!isOpen()) |
|
460 |
return 0; |
|
461 |
writerThread = NativeThread.current(); |
|
462 |
do { |
|
16089 | 463 |
n = send(fd, src, isa); |
2 | 464 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
465 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
466 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
467 |
if (isOpen() && (localAddress == null)) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
468 |
localAddress = Net.localAddress(fd); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
469 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
470 |
} |
2 | 471 |
return IOStatus.normalize(n); |
472 |
} finally { |
|
473 |
writerThread = 0; |
|
474 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
475 |
assert IOStatus.check(n); |
|
476 |
} |
|
477 |
} |
|
478 |
} |
|
479 |
||
16089 | 480 |
private int send(FileDescriptor fd, ByteBuffer src, InetSocketAddress target) |
2 | 481 |
throws IOException |
482 |
{ |
|
483 |
if (src instanceof DirectBuffer) |
|
484 |
return sendFromNativeBuffer(fd, src, target); |
|
485 |
||
486 |
// Substitute a native buffer |
|
487 |
int pos = src.position(); |
|
488 |
int lim = src.limit(); |
|
489 |
assert (pos <= lim); |
|
490 |
int rem = (pos <= lim ? lim - pos : 0); |
|
491 |
||
9237
cbb5753e87e7
7034155: (ch) NullPointerException in sun.io.ch.IOUtil when OOM is thrown
alanb
parents:
8817
diff
changeset
|
492 |
ByteBuffer bb = Util.getTemporaryDirectBuffer(rem); |
2 | 493 |
try { |
494 |
bb.put(src); |
|
495 |
bb.flip(); |
|
496 |
// Do not update src until we see how many bytes were written |
|
497 |
src.position(pos); |
|
498 |
||
499 |
int n = sendFromNativeBuffer(fd, bb, target); |
|
500 |
if (n > 0) { |
|
501 |
// now update src |
|
502 |
src.position(pos + n); |
|
503 |
} |
|
504 |
return n; |
|
505 |
} finally { |
|
506 |
Util.releaseTemporaryDirectBuffer(bb); |
|
507 |
} |
|
508 |
} |
|
509 |
||
510 |
private int sendFromNativeBuffer(FileDescriptor fd, ByteBuffer bb, |
|
16089 | 511 |
InetSocketAddress target) |
2 | 512 |
throws IOException |
513 |
{ |
|
514 |
int pos = bb.position(); |
|
515 |
int lim = bb.limit(); |
|
516 |
assert (pos <= lim); |
|
517 |
int rem = (pos <= lim ? lim - pos : 0); |
|
518 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
519 |
boolean preferIPv6 = (family != StandardProtocolFamily.INET); |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9679
diff
changeset
|
520 |
int written; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9679
diff
changeset
|
521 |
try { |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9679
diff
changeset
|
522 |
written = send0(preferIPv6, fd, ((DirectBuffer)bb).address() + pos, |
16089 | 523 |
rem, target.getAddress(), target.getPort()); |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9679
diff
changeset
|
524 |
} catch (PortUnreachableException pue) { |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9679
diff
changeset
|
525 |
if (isConnected()) |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9679
diff
changeset
|
526 |
throw pue; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9679
diff
changeset
|
527 |
written = rem; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9679
diff
changeset
|
528 |
} |
2 | 529 |
if (written > 0) |
530 |
bb.position(pos + written); |
|
531 |
return written; |
|
532 |
} |
|
533 |
||
534 |
public int read(ByteBuffer buf) throws IOException { |
|
535 |
if (buf == null) |
|
536 |
throw new NullPointerException(); |
|
537 |
synchronized (readLock) { |
|
538 |
synchronized (stateLock) { |
|
539 |
ensureOpen(); |
|
540 |
if (!isConnected()) |
|
541 |
throw new NotYetConnectedException(); |
|
542 |
} |
|
543 |
int n = 0; |
|
544 |
try { |
|
545 |
begin(); |
|
546 |
if (!isOpen()) |
|
547 |
return 0; |
|
548 |
readerThread = NativeThread.current(); |
|
549 |
do { |
|
16921
e70261f11307
8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
16734
diff
changeset
|
550 |
n = IOUtil.read(fd, buf, -1, nd); |
2 | 551 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
552 |
return IOStatus.normalize(n); |
|
553 |
} finally { |
|
554 |
readerThread = 0; |
|
555 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
556 |
assert IOStatus.check(n); |
|
557 |
} |
|
558 |
} |
|
559 |
} |
|
560 |
||
6301 | 561 |
public long read(ByteBuffer[] dsts, int offset, int length) |
562 |
throws IOException |
|
563 |
{ |
|
564 |
if ((offset < 0) || (length < 0) || (offset > dsts.length - length)) |
|
565 |
throw new IndexOutOfBoundsException(); |
|
2 | 566 |
synchronized (readLock) { |
567 |
synchronized (stateLock) { |
|
568 |
ensureOpen(); |
|
569 |
if (!isConnected()) |
|
570 |
throw new NotYetConnectedException(); |
|
571 |
} |
|
572 |
long n = 0; |
|
573 |
try { |
|
574 |
begin(); |
|
575 |
if (!isOpen()) |
|
576 |
return 0; |
|
577 |
readerThread = NativeThread.current(); |
|
578 |
do { |
|
6301 | 579 |
n = IOUtil.read(fd, dsts, offset, length, nd); |
2 | 580 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
581 |
return IOStatus.normalize(n); |
|
582 |
} finally { |
|
583 |
readerThread = 0; |
|
584 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
585 |
assert IOStatus.check(n); |
|
586 |
} |
|
587 |
} |
|
588 |
} |
|
589 |
||
590 |
public int write(ByteBuffer buf) throws IOException { |
|
591 |
if (buf == null) |
|
592 |
throw new NullPointerException(); |
|
593 |
synchronized (writeLock) { |
|
594 |
synchronized (stateLock) { |
|
595 |
ensureOpen(); |
|
596 |
if (!isConnected()) |
|
597 |
throw new NotYetConnectedException(); |
|
598 |
} |
|
599 |
int n = 0; |
|
600 |
try { |
|
601 |
begin(); |
|
602 |
if (!isOpen()) |
|
603 |
return 0; |
|
604 |
writerThread = NativeThread.current(); |
|
605 |
do { |
|
16921
e70261f11307
8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
16734
diff
changeset
|
606 |
n = IOUtil.write(fd, buf, -1, nd); |
2 | 607 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
608 |
return IOStatus.normalize(n); |
|
609 |
} finally { |
|
610 |
writerThread = 0; |
|
611 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
612 |
assert IOStatus.check(n); |
|
613 |
} |
|
614 |
} |
|
615 |
} |
|
616 |
||
6301 | 617 |
public long write(ByteBuffer[] srcs, int offset, int length) |
618 |
throws IOException |
|
619 |
{ |
|
620 |
if ((offset < 0) || (length < 0) || (offset > srcs.length - length)) |
|
621 |
throw new IndexOutOfBoundsException(); |
|
2 | 622 |
synchronized (writeLock) { |
623 |
synchronized (stateLock) { |
|
624 |
ensureOpen(); |
|
625 |
if (!isConnected()) |
|
626 |
throw new NotYetConnectedException(); |
|
627 |
} |
|
628 |
long n = 0; |
|
629 |
try { |
|
630 |
begin(); |
|
631 |
if (!isOpen()) |
|
632 |
return 0; |
|
633 |
writerThread = NativeThread.current(); |
|
634 |
do { |
|
6301 | 635 |
n = IOUtil.write(fd, srcs, offset, length, nd); |
2 | 636 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
637 |
return IOStatus.normalize(n); |
|
638 |
} finally { |
|
639 |
writerThread = 0; |
|
640 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
641 |
assert IOStatus.check(n); |
|
642 |
} |
|
643 |
} |
|
644 |
} |
|
645 |
||
646 |
protected void implConfigureBlocking(boolean block) throws IOException { |
|
647 |
IOUtil.configureBlocking(fd, block); |
|
648 |
} |
|
649 |
||
650 |
public SocketAddress localAddress() { |
|
651 |
synchronized (stateLock) { |
|
652 |
return localAddress; |
|
653 |
} |
|
654 |
} |
|
655 |
||
656 |
public SocketAddress remoteAddress() { |
|
657 |
synchronized (stateLock) { |
|
658 |
return remoteAddress; |
|
659 |
} |
|
660 |
} |
|
661 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
662 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
663 |
public DatagramChannel bind(SocketAddress local) throws IOException { |
2 | 664 |
synchronized (readLock) { |
665 |
synchronized (writeLock) { |
|
666 |
synchronized (stateLock) { |
|
667 |
ensureOpen(); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
668 |
if (localAddress != null) |
2 | 669 |
throw new AlreadyBoundException(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
670 |
InetSocketAddress isa; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
671 |
if (local == null) { |
12889
3ecbb4f2b752
7161881: (dc) DatagramChannel.bind(null) fails if IPv4 socket and running with preferIPv6Addresses=true
robm
parents:
12547
diff
changeset
|
672 |
// only Inet4Address allowed with IPv4 socket |
3ecbb4f2b752
7161881: (dc) DatagramChannel.bind(null) fails if IPv4 socket and running with preferIPv6Addresses=true
robm
parents:
12547
diff
changeset
|
673 |
if (family == StandardProtocolFamily.INET) { |
3ecbb4f2b752
7161881: (dc) DatagramChannel.bind(null) fails if IPv4 socket and running with preferIPv6Addresses=true
robm
parents:
12547
diff
changeset
|
674 |
isa = new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0); |
3ecbb4f2b752
7161881: (dc) DatagramChannel.bind(null) fails if IPv4 socket and running with preferIPv6Addresses=true
robm
parents:
12547
diff
changeset
|
675 |
} else { |
3ecbb4f2b752
7161881: (dc) DatagramChannel.bind(null) fails if IPv4 socket and running with preferIPv6Addresses=true
robm
parents:
12547
diff
changeset
|
676 |
isa = new InetSocketAddress(0); |
3ecbb4f2b752
7161881: (dc) DatagramChannel.bind(null) fails if IPv4 socket and running with preferIPv6Addresses=true
robm
parents:
12547
diff
changeset
|
677 |
} |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
678 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
679 |
isa = Net.checkAddress(local); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
680 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
681 |
// only Inet4Address allowed with IPv4 socket |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
682 |
if (family == StandardProtocolFamily.INET) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
683 |
InetAddress addr = isa.getAddress(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
684 |
if (!(addr instanceof Inet4Address)) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
685 |
throw new UnsupportedAddressTypeException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
686 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
687 |
} |
2 | 688 |
SecurityManager sm = System.getSecurityManager(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
689 |
if (sm != null) { |
2 | 690 |
sm.checkListen(isa.getPort()); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
691 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
692 |
Net.bind(family, fd, isa.getAddress(), isa.getPort()); |
2 | 693 |
localAddress = Net.localAddress(fd); |
694 |
} |
|
695 |
} |
|
696 |
} |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
697 |
return this; |
2 | 698 |
} |
699 |
||
700 |
public boolean isConnected() { |
|
701 |
synchronized (stateLock) { |
|
702 |
return (state == ST_CONNECTED); |
|
703 |
} |
|
704 |
} |
|
705 |
||
706 |
void ensureOpenAndUnconnected() throws IOException { // package-private |
|
707 |
synchronized (stateLock) { |
|
708 |
if (!isOpen()) |
|
709 |
throw new ClosedChannelException(); |
|
710 |
if (state != ST_UNCONNECTED) |
|
711 |
throw new IllegalStateException("Connect already invoked"); |
|
712 |
} |
|
713 |
} |
|
714 |
||
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
715 |
@Override |
2 | 716 |
public DatagramChannel connect(SocketAddress sa) throws IOException { |
717 |
synchronized(readLock) { |
|
718 |
synchronized(writeLock) { |
|
719 |
synchronized (stateLock) { |
|
720 |
ensureOpenAndUnconnected(); |
|
721 |
InetSocketAddress isa = Net.checkAddress(sa); |
|
722 |
SecurityManager sm = System.getSecurityManager(); |
|
723 |
if (sm != null) |
|
724 |
sm.checkConnect(isa.getAddress().getHostAddress(), |
|
725 |
isa.getPort()); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
726 |
int n = Net.connect(family, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
727 |
fd, |
2 | 728 |
isa.getAddress(), |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
729 |
isa.getPort()); |
2 | 730 |
if (n <= 0) |
731 |
throw new Error(); // Can't happen |
|
732 |
||
733 |
// Connection succeeded; disallow further invocation |
|
734 |
state = ST_CONNECTED; |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
735 |
remoteAddress = isa; |
2 | 736 |
sender = isa; |
737 |
cachedSenderInetAddress = isa.getAddress(); |
|
738 |
cachedSenderPort = isa.getPort(); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
739 |
|
7045
525f00c555b2
6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
6301
diff
changeset
|
740 |
// set or refresh local address |
525f00c555b2
6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
6301
diff
changeset
|
741 |
localAddress = Net.localAddress(fd); |
27078 | 742 |
|
743 |
// flush any packets already received. |
|
744 |
boolean blocking = false; |
|
745 |
synchronized (blockingLock()) { |
|
746 |
try { |
|
747 |
blocking = isBlocking(); |
|
748 |
ByteBuffer tmpBuf = ByteBuffer.allocate(100); |
|
749 |
if (blocking) { |
|
750 |
configureBlocking(false); |
|
751 |
} |
|
752 |
do { |
|
753 |
tmpBuf.clear(); |
|
27724
9a42fe09eb61
8053963: (dc) Use DatagramChannel.receive() instead of read() in connect
michaelm
parents:
27078
diff
changeset
|
754 |
} while (receive(tmpBuf) != null); |
27078 | 755 |
} finally { |
756 |
if (blocking) { |
|
757 |
configureBlocking(true); |
|
758 |
} |
|
759 |
} |
|
760 |
} |
|
2 | 761 |
} |
762 |
} |
|
763 |
} |
|
764 |
return this; |
|
765 |
} |
|
766 |
||
767 |
public DatagramChannel disconnect() throws IOException { |
|
768 |
synchronized(readLock) { |
|
769 |
synchronized(writeLock) { |
|
770 |
synchronized (stateLock) { |
|
771 |
if (!isConnected() || !isOpen()) |
|
772 |
return this; |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
773 |
InetSocketAddress isa = remoteAddress; |
2 | 774 |
SecurityManager sm = System.getSecurityManager(); |
775 |
if (sm != null) |
|
776 |
sm.checkConnect(isa.getAddress().getHostAddress(), |
|
777 |
isa.getPort()); |
|
12547
ae1b2051db5d
7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
12440
diff
changeset
|
778 |
boolean isIPv6 = (family == StandardProtocolFamily.INET6); |
ae1b2051db5d
7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
12440
diff
changeset
|
779 |
disconnect0(fd, isIPv6); |
2 | 780 |
remoteAddress = null; |
781 |
state = ST_UNCONNECTED; |
|
7045
525f00c555b2
6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
6301
diff
changeset
|
782 |
|
525f00c555b2
6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
6301
diff
changeset
|
783 |
// refresh local address |
525f00c555b2
6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
6301
diff
changeset
|
784 |
localAddress = Net.localAddress(fd); |
2 | 785 |
} |
786 |
} |
|
787 |
} |
|
788 |
return this; |
|
789 |
} |
|
790 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
791 |
/** |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
792 |
* Joins channel's socket to the given group/interface and |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
793 |
* optional source address. |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
794 |
*/ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
795 |
private MembershipKey innerJoin(InetAddress group, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
796 |
NetworkInterface interf, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
797 |
InetAddress source) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
798 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
799 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
800 |
if (!group.isMulticastAddress()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
801 |
throw new IllegalArgumentException("Group not a multicast address"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
802 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
803 |
// 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
|
804 |
if (group instanceof Inet4Address) { |
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
805 |
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
|
806 |
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
|
807 |
} else if (group instanceof Inet6Address) { |
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
808 |
if (family != StandardProtocolFamily.INET6) |
9237
cbb5753e87e7
7034155: (ch) NullPointerException in sun.io.ch.IOUtil when OOM is thrown
alanb
parents:
8817
diff
changeset
|
809 |
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
|
810 |
} else { |
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
811 |
throw new IllegalArgumentException("Address type not supported"); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
812 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
813 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
814 |
// check source address |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
815 |
if (source != null) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
816 |
if (source.isAnyLocalAddress()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
817 |
throw new IllegalArgumentException("Source address is a wildcard address"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
818 |
if (source.isMulticastAddress()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
819 |
throw new IllegalArgumentException("Source address is multicast address"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
820 |
if (source.getClass() != group.getClass()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
821 |
throw new IllegalArgumentException("Source address is different type to group"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
822 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
823 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
824 |
SecurityManager sm = System.getSecurityManager(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
825 |
if (sm != null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
826 |
sm.checkMulticast(group); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
827 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
828 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
829 |
if (!isOpen()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
830 |
throw new ClosedChannelException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
831 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
832 |
// 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
|
833 |
if (registry == null) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
834 |
registry = new MembershipRegistry(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
835 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
836 |
// return existing membership key |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
837 |
MembershipKey key = registry.checkMembership(group, interf, source); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
838 |
if (key != null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
839 |
return key; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
840 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
841 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
842 |
MembershipKeyImpl key; |
8788
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
843 |
if ((family == StandardProtocolFamily.INET6) && |
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
844 |
((group instanceof Inet6Address) || Net.canJoin6WithIPv4Group())) |
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
845 |
{ |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
846 |
int index = interf.getIndex(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
847 |
if (index == -1) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
848 |
throw new IOException("Network interface cannot be identified"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
849 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
850 |
// need multicast and source address as byte arrays |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
851 |
byte[] groupAddress = Net.inet6AsByteArray(group); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
852 |
byte[] sourceAddress = (source == null) ? null : |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
853 |
Net.inet6AsByteArray(source); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
854 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
855 |
// join the group |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
856 |
int n = Net.join6(fd, groupAddress, index, sourceAddress); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
857 |
if (n == IOStatus.UNAVAILABLE) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
858 |
throw new UnsupportedOperationException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
859 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
860 |
key = new MembershipKeyImpl.Type6(this, group, interf, source, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
861 |
groupAddress, index, sourceAddress); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
862 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
863 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
864 |
// need IPv4 address to identify interface |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
865 |
Inet4Address target = Net.anyInet4Address(interf); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
866 |
if (target == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
867 |
throw new IOException("Network interface not configured for IPv4"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
868 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
869 |
int groupAddress = Net.inet4AsInt(group); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
870 |
int targetAddress = Net.inet4AsInt(target); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
871 |
int sourceAddress = (source == null) ? 0 : Net.inet4AsInt(source); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
872 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
873 |
// join the group |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
874 |
int n = Net.join4(fd, groupAddress, targetAddress, sourceAddress); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
875 |
if (n == IOStatus.UNAVAILABLE) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
876 |
throw new UnsupportedOperationException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
877 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
878 |
key = new MembershipKeyImpl.Type4(this, group, interf, source, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
879 |
groupAddress, targetAddress, sourceAddress); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
880 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
881 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
882 |
registry.add(key); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
883 |
return key; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
884 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
885 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
886 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
887 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
888 |
public MembershipKey join(InetAddress group, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
889 |
NetworkInterface interf) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
890 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
891 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
892 |
return innerJoin(group, interf, null); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
893 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
894 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
895 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
896 |
public MembershipKey join(InetAddress group, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
897 |
NetworkInterface interf, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
898 |
InetAddress source) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
899 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
900 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
901 |
if (source == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
902 |
throw new NullPointerException("source address is null"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
903 |
return innerJoin(group, interf, source); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
904 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
905 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
906 |
// package-private |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
907 |
void drop(MembershipKeyImpl key) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
908 |
assert key.channel() == this; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
909 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
910 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
911 |
if (!key.isValid()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
912 |
return; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
913 |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
914 |
try { |
8788
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
915 |
if (key instanceof MembershipKeyImpl.Type6) { |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
916 |
MembershipKeyImpl.Type6 key6 = |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
917 |
(MembershipKeyImpl.Type6)key; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
918 |
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
|
919 |
} else { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
920 |
MembershipKeyImpl.Type4 key4 = (MembershipKeyImpl.Type4)key; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
921 |
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
|
922 |
key4.source()); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
923 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
924 |
} catch (IOException ioe) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
925 |
// should not happen |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
926 |
throw new AssertionError(ioe); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
927 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
928 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
929 |
key.invalidate(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
930 |
registry.remove(key); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
931 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
932 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
933 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
934 |
/** |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
935 |
* 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
|
936 |
* datagrams. |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
937 |
*/ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
938 |
void block(MembershipKeyImpl key, InetAddress source) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
939 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
940 |
{ |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
941 |
assert key.channel() == this; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
942 |
assert key.sourceAddress() == null; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
943 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
944 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
945 |
if (!key.isValid()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
946 |
throw new IllegalStateException("key is no longer valid"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
947 |
if (source.isAnyLocalAddress()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
948 |
throw new IllegalArgumentException("Source address is a wildcard address"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
949 |
if (source.isMulticastAddress()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
950 |
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
|
951 |
if (source.getClass() != key.group().getClass()) |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
952 |
throw new IllegalArgumentException("Source address is different type to group"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
953 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
954 |
int n; |
8788
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
955 |
if (key instanceof MembershipKeyImpl.Type6) { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
956 |
MembershipKeyImpl.Type6 key6 = |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
957 |
(MembershipKeyImpl.Type6)key; |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
958 |
n = Net.block6(fd, key6.groupAddress(), key6.index(), |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
959 |
Net.inet6AsByteArray(source)); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
960 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
961 |
MembershipKeyImpl.Type4 key4 = |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
962 |
(MembershipKeyImpl.Type4)key; |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
963 |
n = Net.block4(fd, key4.groupAddress(), key4.interfaceAddress(), |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
964 |
Net.inet4AsInt(source)); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
965 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
966 |
if (n == IOStatus.UNAVAILABLE) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
967 |
// ancient kernel |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
968 |
throw new UnsupportedOperationException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
969 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
970 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
971 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
972 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
973 |
/** |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
974 |
* Unblock given source. |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
975 |
*/ |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
976 |
void unblock(MembershipKeyImpl key, InetAddress source) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
977 |
assert key.channel() == this; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
978 |
assert key.sourceAddress() == null; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
979 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
980 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
981 |
if (!key.isValid()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
982 |
throw new IllegalStateException("key is no longer valid"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
983 |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
984 |
try { |
8788
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
985 |
if (key instanceof MembershipKeyImpl.Type6) { |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
986 |
MembershipKeyImpl.Type6 key6 = |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
987 |
(MembershipKeyImpl.Type6)key; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
988 |
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
|
989 |
Net.inet6AsByteArray(source)); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
990 |
} else { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
991 |
MembershipKeyImpl.Type4 key4 = |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
992 |
(MembershipKeyImpl.Type4)key; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
993 |
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
|
994 |
Net.inet4AsInt(source)); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
995 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
996 |
} catch (IOException ioe) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
997 |
// should not happen |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
998 |
throw new AssertionError(ioe); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
999 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1000 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1001 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1002 |
|
2 | 1003 |
protected void implCloseSelectableChannel() throws IOException { |
1004 |
synchronized (stateLock) { |
|
12440
2689ca179e22
7118373: (se) Potential leak file descriptor when deregistrating at around the same time as an async close
robm
parents:
12047
diff
changeset
|
1005 |
if (state != ST_KILLED) |
2689ca179e22
7118373: (se) Potential leak file descriptor when deregistrating at around the same time as an async close
robm
parents:
12047
diff
changeset
|
1006 |
nd.preClose(fd); |
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
1007 |
ResourceManager.afterUdpClose(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1008 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1009 |
// if member of mulitcast group then invalidate all keys |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1010 |
if (registry != null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1011 |
registry.invalidateAll(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1012 |
|
2 | 1013 |
long th; |
1014 |
if ((th = readerThread) != 0) |
|
1015 |
NativeThread.signal(th); |
|
1016 |
if ((th = writerThread) != 0) |
|
1017 |
NativeThread.signal(th); |
|
1018 |
if (!isRegistered()) |
|
1019 |
kill(); |
|
1020 |
} |
|
1021 |
} |
|
1022 |
||
1023 |
public void kill() throws IOException { |
|
1024 |
synchronized (stateLock) { |
|
1025 |
if (state == ST_KILLED) |
|
1026 |
return; |
|
1027 |
if (state == ST_UNINITIALIZED) { |
|
1028 |
state = ST_KILLED; |
|
1029 |
return; |
|
1030 |
} |
|
1031 |
assert !isOpen() && !isRegistered(); |
|
1032 |
nd.close(fd); |
|
1033 |
state = ST_KILLED; |
|
1034 |
} |
|
1035 |
} |
|
1036 |
||
44534 | 1037 |
@SuppressWarnings("deprecation") |
2 | 1038 |
protected void finalize() throws IOException { |
1039 |
// fd is null if constructor threw exception |
|
1040 |
if (fd != null) |
|
1041 |
close(); |
|
1042 |
} |
|
1043 |
||
1044 |
/** |
|
1045 |
* Translates native poll revent set into a ready operation set |
|
1046 |
*/ |
|
1047 |
public boolean translateReadyOps(int ops, int initialOps, |
|
1048 |
SelectionKeyImpl sk) { |
|
1049 |
int intOps = sk.nioInterestOps(); // Do this just once, it synchronizes |
|
1050 |
int oldOps = sk.nioReadyOps(); |
|
1051 |
int newOps = initialOps; |
|
1052 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1053 |
if ((ops & Net.POLLNVAL) != 0) { |
2 | 1054 |
// This should only happen if this channel is pre-closed while a |
1055 |
// selection operation is in progress |
|
1056 |
// ## Throw an error if this channel has not been pre-closed |
|
1057 |
return false; |
|
1058 |
} |
|
1059 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1060 |
if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) { |
2 | 1061 |
newOps = intOps; |
1062 |
sk.nioReadyOps(newOps); |
|
1063 |
return (newOps & ~oldOps) != 0; |
|
1064 |
} |
|
1065 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1066 |
if (((ops & Net.POLLIN) != 0) && |
2 | 1067 |
((intOps & SelectionKey.OP_READ) != 0)) |
1068 |
newOps |= SelectionKey.OP_READ; |
|
1069 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1070 |
if (((ops & Net.POLLOUT) != 0) && |
2 | 1071 |
((intOps & SelectionKey.OP_WRITE) != 0)) |
1072 |
newOps |= SelectionKey.OP_WRITE; |
|
1073 |
||
1074 |
sk.nioReadyOps(newOps); |
|
1075 |
return (newOps & ~oldOps) != 0; |
|
1076 |
} |
|
1077 |
||
1078 |
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) { |
|
1079 |
return translateReadyOps(ops, sk.nioReadyOps(), sk); |
|
1080 |
} |
|
1081 |
||
1082 |
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) { |
|
1083 |
return translateReadyOps(ops, 0, sk); |
|
1084 |
} |
|
1085 |
||
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1086 |
// package-private |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1087 |
int poll(int events, long timeout) throws IOException { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1088 |
assert Thread.holdsLock(blockingLock()) && !isBlocking(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1089 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1090 |
synchronized (readLock) { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1091 |
int n = 0; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1092 |
try { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1093 |
begin(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1094 |
synchronized (stateLock) { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1095 |
if (!isOpen()) |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1096 |
return 0; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1097 |
readerThread = NativeThread.current(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1098 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1099 |
n = Net.poll(fd, events, timeout); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1100 |
} finally { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1101 |
readerThread = 0; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1102 |
end(n > 0); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1103 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1104 |
return n; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1105 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1106 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1107 |
|
2 | 1108 |
/** |
1109 |
* Translates an interest operation set into a native poll event set |
|
1110 |
*/ |
|
1111 |
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) { |
|
1112 |
int newOps = 0; |
|
1113 |
||
1114 |
if ((ops & SelectionKey.OP_READ) != 0) |
|
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1115 |
newOps |= Net.POLLIN; |
2 | 1116 |
if ((ops & SelectionKey.OP_WRITE) != 0) |
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1117 |
newOps |= Net.POLLOUT; |
2 | 1118 |
if ((ops & SelectionKey.OP_CONNECT) != 0) |
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1119 |
newOps |= Net.POLLIN; |
2 | 1120 |
sk.selector.putEventOps(sk, newOps); |
1121 |
} |
|
1122 |
||
1123 |
public FileDescriptor getFD() { |
|
1124 |
return fd; |
|
1125 |
} |
|
1126 |
||
1127 |
public int getFDVal() { |
|
1128 |
return fdVal; |
|
1129 |
} |
|
1130 |
||
1131 |
||
1132 |
// -- Native methods -- |
|
1133 |
||
1134 |
private static native void initIDs(); |
|
1135 |
||
12547
ae1b2051db5d
7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
12440
diff
changeset
|
1136 |
private static native void disconnect0(FileDescriptor fd, boolean isIPv6) |
2 | 1137 |
throws IOException; |
1138 |
||
1139 |
private native int receive0(FileDescriptor fd, long address, int len, |
|
1140 |
boolean connected) |
|
1141 |
throws IOException; |
|
1142 |
||
16089 | 1143 |
private native int send0(boolean preferIPv6, FileDescriptor fd, long address, |
1144 |
int len, InetAddress addr, int port) |
|
2 | 1145 |
throws IOException; |
1146 |
||
1147 |
static { |
|
19607
bee007586d06
8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents:
18212
diff
changeset
|
1148 |
IOUtil.load(); |
2 | 1149 |
initIDs(); |
1150 |
} |
|
1151 |
||
1152 |
} |