author | chegar |
Wed, 27 Apr 2016 20:36:02 +0100 | |
changeset 37676 | 24ef455da1b0 |
parent 36115 | 0676e37a0b9c |
child 41134 | e0b8f9336c65 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
36115 | 2 |
* Copyright (c) 2001, 2016, 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 |
if (dst == null) |
|
332 |
throw new NullPointerException(); |
|
333 |
synchronized (readLock) { |
|
334 |
ensureOpen(); |
|
2427
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2057
diff
changeset
|
335 |
// 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
|
336 |
if (localAddress() == null) |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2057
diff
changeset
|
337 |
bind(null); |
2 | 338 |
int n = 0; |
339 |
ByteBuffer bb = null; |
|
340 |
try { |
|
341 |
begin(); |
|
342 |
if (!isOpen()) |
|
343 |
return null; |
|
344 |
SecurityManager security = System.getSecurityManager(); |
|
345 |
readerThread = NativeThread.current(); |
|
346 |
if (isConnected() || (security == null)) { |
|
347 |
do { |
|
348 |
n = receive(fd, dst); |
|
349 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
|
350 |
if (n == IOStatus.UNAVAILABLE) |
|
351 |
return null; |
|
352 |
} else { |
|
353 |
bb = Util.getTemporaryDirectBuffer(dst.remaining()); |
|
354 |
for (;;) { |
|
355 |
do { |
|
356 |
n = receive(fd, bb); |
|
357 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
|
358 |
if (n == IOStatus.UNAVAILABLE) |
|
359 |
return null; |
|
360 |
InetSocketAddress isa = (InetSocketAddress)sender; |
|
361 |
try { |
|
362 |
security.checkAccept( |
|
363 |
isa.getAddress().getHostAddress(), |
|
364 |
isa.getPort()); |
|
365 |
} catch (SecurityException se) { |
|
366 |
// Ignore packet |
|
367 |
bb.clear(); |
|
368 |
n = 0; |
|
369 |
continue; |
|
370 |
} |
|
371 |
bb.flip(); |
|
372 |
dst.put(bb); |
|
373 |
break; |
|
374 |
} |
|
375 |
} |
|
376 |
return sender; |
|
377 |
} finally { |
|
378 |
if (bb != null) |
|
379 |
Util.releaseTemporaryDirectBuffer(bb); |
|
380 |
readerThread = 0; |
|
381 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
382 |
assert IOStatus.check(n); |
|
383 |
} |
|
384 |
} |
|
385 |
} |
|
386 |
||
387 |
private int receive(FileDescriptor fd, ByteBuffer dst) |
|
388 |
throws IOException |
|
389 |
{ |
|
390 |
int pos = dst.position(); |
|
391 |
int lim = dst.limit(); |
|
392 |
assert (pos <= lim); |
|
393 |
int rem = (pos <= lim ? lim - pos : 0); |
|
394 |
if (dst instanceof DirectBuffer && rem > 0) |
|
395 |
return receiveIntoNativeBuffer(fd, dst, rem, pos); |
|
396 |
||
397 |
// Substitute a native buffer. If the supplied buffer is empty |
|
398 |
// we must instead use a nonempty buffer, otherwise the call |
|
399 |
// will not block waiting for a datagram on some platforms. |
|
400 |
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
|
401 |
ByteBuffer bb = Util.getTemporaryDirectBuffer(newSize); |
2 | 402 |
try { |
403 |
int n = receiveIntoNativeBuffer(fd, bb, newSize, 0); |
|
404 |
bb.flip(); |
|
405 |
if (n > 0 && rem > 0) |
|
406 |
dst.put(bb); |
|
407 |
return n; |
|
408 |
} finally { |
|
409 |
Util.releaseTemporaryDirectBuffer(bb); |
|
410 |
} |
|
411 |
} |
|
412 |
||
413 |
private int receiveIntoNativeBuffer(FileDescriptor fd, ByteBuffer bb, |
|
414 |
int rem, int pos) |
|
415 |
throws IOException |
|
416 |
{ |
|
417 |
int n = receive0(fd, ((DirectBuffer)bb).address() + pos, rem, |
|
418 |
isConnected()); |
|
419 |
if (n > 0) |
|
420 |
bb.position(pos + n); |
|
421 |
return n; |
|
422 |
} |
|
423 |
||
424 |
public int send(ByteBuffer src, SocketAddress target) |
|
425 |
throws IOException |
|
426 |
{ |
|
427 |
if (src == null) |
|
428 |
throw new NullPointerException(); |
|
429 |
||
430 |
synchronized (writeLock) { |
|
431 |
ensureOpen(); |
|
16089 | 432 |
InetSocketAddress isa = Net.checkAddress(target); |
2 | 433 |
InetAddress ia = isa.getAddress(); |
434 |
if (ia == null) |
|
435 |
throw new IOException("Target address not resolved"); |
|
436 |
synchronized (stateLock) { |
|
437 |
if (!isConnected()) { |
|
438 |
if (target == null) |
|
439 |
throw new NullPointerException(); |
|
440 |
SecurityManager sm = System.getSecurityManager(); |
|
441 |
if (sm != null) { |
|
442 |
if (ia.isMulticastAddress()) { |
|
16089 | 443 |
sm.checkMulticast(ia); |
2 | 444 |
} else { |
16089 | 445 |
sm.checkConnect(ia.getHostAddress(), |
2 | 446 |
isa.getPort()); |
447 |
} |
|
448 |
} |
|
449 |
} else { // Connected case; Check address then write |
|
450 |
if (!target.equals(remoteAddress)) { |
|
451 |
throw new IllegalArgumentException( |
|
452 |
"Connected address not equal to target address"); |
|
453 |
} |
|
454 |
return write(src); |
|
455 |
} |
|
456 |
} |
|
457 |
||
458 |
int n = 0; |
|
459 |
try { |
|
460 |
begin(); |
|
461 |
if (!isOpen()) |
|
462 |
return 0; |
|
463 |
writerThread = NativeThread.current(); |
|
464 |
do { |
|
16089 | 465 |
n = send(fd, src, isa); |
2 | 466 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
467 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
468 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
469 |
if (isOpen() && (localAddress == null)) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
470 |
localAddress = Net.localAddress(fd); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
471 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
472 |
} |
2 | 473 |
return IOStatus.normalize(n); |
474 |
} finally { |
|
475 |
writerThread = 0; |
|
476 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
477 |
assert IOStatus.check(n); |
|
478 |
} |
|
479 |
} |
|
480 |
} |
|
481 |
||
16089 | 482 |
private int send(FileDescriptor fd, ByteBuffer src, InetSocketAddress target) |
2 | 483 |
throws IOException |
484 |
{ |
|
485 |
if (src instanceof DirectBuffer) |
|
486 |
return sendFromNativeBuffer(fd, src, target); |
|
487 |
||
488 |
// Substitute a native buffer |
|
489 |
int pos = src.position(); |
|
490 |
int lim = src.limit(); |
|
491 |
assert (pos <= lim); |
|
492 |
int rem = (pos <= lim ? lim - pos : 0); |
|
493 |
||
9237
cbb5753e87e7
7034155: (ch) NullPointerException in sun.io.ch.IOUtil when OOM is thrown
alanb
parents:
8817
diff
changeset
|
494 |
ByteBuffer bb = Util.getTemporaryDirectBuffer(rem); |
2 | 495 |
try { |
496 |
bb.put(src); |
|
497 |
bb.flip(); |
|
498 |
// Do not update src until we see how many bytes were written |
|
499 |
src.position(pos); |
|
500 |
||
501 |
int n = sendFromNativeBuffer(fd, bb, target); |
|
502 |
if (n > 0) { |
|
503 |
// now update src |
|
504 |
src.position(pos + n); |
|
505 |
} |
|
506 |
return n; |
|
507 |
} finally { |
|
508 |
Util.releaseTemporaryDirectBuffer(bb); |
|
509 |
} |
|
510 |
} |
|
511 |
||
512 |
private int sendFromNativeBuffer(FileDescriptor fd, ByteBuffer bb, |
|
16089 | 513 |
InetSocketAddress target) |
2 | 514 |
throws IOException |
515 |
{ |
|
516 |
int pos = bb.position(); |
|
517 |
int lim = bb.limit(); |
|
518 |
assert (pos <= lim); |
|
519 |
int rem = (pos <= lim ? lim - pos : 0); |
|
520 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
521 |
boolean preferIPv6 = (family != StandardProtocolFamily.INET); |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9679
diff
changeset
|
522 |
int written; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9679
diff
changeset
|
523 |
try { |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9679
diff
changeset
|
524 |
written = send0(preferIPv6, fd, ((DirectBuffer)bb).address() + pos, |
16089 | 525 |
rem, target.getAddress(), target.getPort()); |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9679
diff
changeset
|
526 |
} catch (PortUnreachableException pue) { |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9679
diff
changeset
|
527 |
if (isConnected()) |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9679
diff
changeset
|
528 |
throw pue; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9679
diff
changeset
|
529 |
written = rem; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9679
diff
changeset
|
530 |
} |
2 | 531 |
if (written > 0) |
532 |
bb.position(pos + written); |
|
533 |
return written; |
|
534 |
} |
|
535 |
||
536 |
public int read(ByteBuffer buf) throws IOException { |
|
537 |
if (buf == null) |
|
538 |
throw new NullPointerException(); |
|
539 |
synchronized (readLock) { |
|
540 |
synchronized (stateLock) { |
|
541 |
ensureOpen(); |
|
542 |
if (!isConnected()) |
|
543 |
throw new NotYetConnectedException(); |
|
544 |
} |
|
545 |
int n = 0; |
|
546 |
try { |
|
547 |
begin(); |
|
548 |
if (!isOpen()) |
|
549 |
return 0; |
|
550 |
readerThread = NativeThread.current(); |
|
551 |
do { |
|
16921
e70261f11307
8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
16734
diff
changeset
|
552 |
n = IOUtil.read(fd, buf, -1, nd); |
2 | 553 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
554 |
return IOStatus.normalize(n); |
|
555 |
} finally { |
|
556 |
readerThread = 0; |
|
557 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
558 |
assert IOStatus.check(n); |
|
559 |
} |
|
560 |
} |
|
561 |
} |
|
562 |
||
6301 | 563 |
public long read(ByteBuffer[] dsts, int offset, int length) |
564 |
throws IOException |
|
565 |
{ |
|
566 |
if ((offset < 0) || (length < 0) || (offset > dsts.length - length)) |
|
567 |
throw new IndexOutOfBoundsException(); |
|
2 | 568 |
synchronized (readLock) { |
569 |
synchronized (stateLock) { |
|
570 |
ensureOpen(); |
|
571 |
if (!isConnected()) |
|
572 |
throw new NotYetConnectedException(); |
|
573 |
} |
|
574 |
long n = 0; |
|
575 |
try { |
|
576 |
begin(); |
|
577 |
if (!isOpen()) |
|
578 |
return 0; |
|
579 |
readerThread = NativeThread.current(); |
|
580 |
do { |
|
6301 | 581 |
n = IOUtil.read(fd, dsts, offset, length, nd); |
2 | 582 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
583 |
return IOStatus.normalize(n); |
|
584 |
} finally { |
|
585 |
readerThread = 0; |
|
586 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
587 |
assert IOStatus.check(n); |
|
588 |
} |
|
589 |
} |
|
590 |
} |
|
591 |
||
592 |
public int write(ByteBuffer buf) throws IOException { |
|
593 |
if (buf == null) |
|
594 |
throw new NullPointerException(); |
|
595 |
synchronized (writeLock) { |
|
596 |
synchronized (stateLock) { |
|
597 |
ensureOpen(); |
|
598 |
if (!isConnected()) |
|
599 |
throw new NotYetConnectedException(); |
|
600 |
} |
|
601 |
int n = 0; |
|
602 |
try { |
|
603 |
begin(); |
|
604 |
if (!isOpen()) |
|
605 |
return 0; |
|
606 |
writerThread = NativeThread.current(); |
|
607 |
do { |
|
16921
e70261f11307
8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
16734
diff
changeset
|
608 |
n = IOUtil.write(fd, buf, -1, nd); |
2 | 609 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
610 |
return IOStatus.normalize(n); |
|
611 |
} finally { |
|
612 |
writerThread = 0; |
|
613 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
614 |
assert IOStatus.check(n); |
|
615 |
} |
|
616 |
} |
|
617 |
} |
|
618 |
||
6301 | 619 |
public long write(ByteBuffer[] srcs, int offset, int length) |
620 |
throws IOException |
|
621 |
{ |
|
622 |
if ((offset < 0) || (length < 0) || (offset > srcs.length - length)) |
|
623 |
throw new IndexOutOfBoundsException(); |
|
2 | 624 |
synchronized (writeLock) { |
625 |
synchronized (stateLock) { |
|
626 |
ensureOpen(); |
|
627 |
if (!isConnected()) |
|
628 |
throw new NotYetConnectedException(); |
|
629 |
} |
|
630 |
long n = 0; |
|
631 |
try { |
|
632 |
begin(); |
|
633 |
if (!isOpen()) |
|
634 |
return 0; |
|
635 |
writerThread = NativeThread.current(); |
|
636 |
do { |
|
6301 | 637 |
n = IOUtil.write(fd, srcs, offset, length, nd); |
2 | 638 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
639 |
return IOStatus.normalize(n); |
|
640 |
} finally { |
|
641 |
writerThread = 0; |
|
642 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
643 |
assert IOStatus.check(n); |
|
644 |
} |
|
645 |
} |
|
646 |
} |
|
647 |
||
648 |
protected void implConfigureBlocking(boolean block) throws IOException { |
|
649 |
IOUtil.configureBlocking(fd, block); |
|
650 |
} |
|
651 |
||
652 |
public SocketAddress localAddress() { |
|
653 |
synchronized (stateLock) { |
|
654 |
return localAddress; |
|
655 |
} |
|
656 |
} |
|
657 |
||
658 |
public SocketAddress remoteAddress() { |
|
659 |
synchronized (stateLock) { |
|
660 |
return remoteAddress; |
|
661 |
} |
|
662 |
} |
|
663 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
664 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
665 |
public DatagramChannel bind(SocketAddress local) throws IOException { |
2 | 666 |
synchronized (readLock) { |
667 |
synchronized (writeLock) { |
|
668 |
synchronized (stateLock) { |
|
669 |
ensureOpen(); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
670 |
if (localAddress != null) |
2 | 671 |
throw new AlreadyBoundException(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
672 |
InetSocketAddress isa; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
673 |
if (local == null) { |
12889
3ecbb4f2b752
7161881: (dc) DatagramChannel.bind(null) fails if IPv4 socket and running with preferIPv6Addresses=true
robm
parents:
12547
diff
changeset
|
674 |
// 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
|
675 |
if (family == StandardProtocolFamily.INET) { |
3ecbb4f2b752
7161881: (dc) DatagramChannel.bind(null) fails if IPv4 socket and running with preferIPv6Addresses=true
robm
parents:
12547
diff
changeset
|
676 |
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
|
677 |
} else { |
3ecbb4f2b752
7161881: (dc) DatagramChannel.bind(null) fails if IPv4 socket and running with preferIPv6Addresses=true
robm
parents:
12547
diff
changeset
|
678 |
isa = new InetSocketAddress(0); |
3ecbb4f2b752
7161881: (dc) DatagramChannel.bind(null) fails if IPv4 socket and running with preferIPv6Addresses=true
robm
parents:
12547
diff
changeset
|
679 |
} |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
680 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
681 |
isa = Net.checkAddress(local); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
682 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
683 |
// only Inet4Address allowed with IPv4 socket |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
684 |
if (family == StandardProtocolFamily.INET) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
685 |
InetAddress addr = isa.getAddress(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
686 |
if (!(addr instanceof Inet4Address)) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
687 |
throw new UnsupportedAddressTypeException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
688 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
689 |
} |
2 | 690 |
SecurityManager sm = System.getSecurityManager(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
691 |
if (sm != null) { |
2 | 692 |
sm.checkListen(isa.getPort()); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
693 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
694 |
Net.bind(family, fd, isa.getAddress(), isa.getPort()); |
2 | 695 |
localAddress = Net.localAddress(fd); |
696 |
} |
|
697 |
} |
|
698 |
} |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
699 |
return this; |
2 | 700 |
} |
701 |
||
702 |
public boolean isConnected() { |
|
703 |
synchronized (stateLock) { |
|
704 |
return (state == ST_CONNECTED); |
|
705 |
} |
|
706 |
} |
|
707 |
||
708 |
void ensureOpenAndUnconnected() throws IOException { // package-private |
|
709 |
synchronized (stateLock) { |
|
710 |
if (!isOpen()) |
|
711 |
throw new ClosedChannelException(); |
|
712 |
if (state != ST_UNCONNECTED) |
|
713 |
throw new IllegalStateException("Connect already invoked"); |
|
714 |
} |
|
715 |
} |
|
716 |
||
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
717 |
@Override |
2 | 718 |
public DatagramChannel connect(SocketAddress sa) throws IOException { |
719 |
int localPort = 0; |
|
720 |
||
721 |
synchronized(readLock) { |
|
722 |
synchronized(writeLock) { |
|
723 |
synchronized (stateLock) { |
|
724 |
ensureOpenAndUnconnected(); |
|
725 |
InetSocketAddress isa = Net.checkAddress(sa); |
|
726 |
SecurityManager sm = System.getSecurityManager(); |
|
727 |
if (sm != null) |
|
728 |
sm.checkConnect(isa.getAddress().getHostAddress(), |
|
729 |
isa.getPort()); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
730 |
int n = Net.connect(family, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
731 |
fd, |
2 | 732 |
isa.getAddress(), |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
733 |
isa.getPort()); |
2 | 734 |
if (n <= 0) |
735 |
throw new Error(); // Can't happen |
|
736 |
||
737 |
// Connection succeeded; disallow further invocation |
|
738 |
state = ST_CONNECTED; |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
739 |
remoteAddress = isa; |
2 | 740 |
sender = isa; |
741 |
cachedSenderInetAddress = isa.getAddress(); |
|
742 |
cachedSenderPort = isa.getPort(); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
743 |
|
7045
525f00c555b2
6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
6301
diff
changeset
|
744 |
// 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
|
745 |
localAddress = Net.localAddress(fd); |
27078 | 746 |
|
747 |
// flush any packets already received. |
|
748 |
boolean blocking = false; |
|
749 |
synchronized (blockingLock()) { |
|
750 |
try { |
|
751 |
blocking = isBlocking(); |
|
752 |
ByteBuffer tmpBuf = ByteBuffer.allocate(100); |
|
753 |
if (blocking) { |
|
754 |
configureBlocking(false); |
|
755 |
} |
|
756 |
do { |
|
757 |
tmpBuf.clear(); |
|
27724
9a42fe09eb61
8053963: (dc) Use DatagramChannel.receive() instead of read() in connect
michaelm
parents:
27078
diff
changeset
|
758 |
} while (receive(tmpBuf) != null); |
27078 | 759 |
} finally { |
760 |
if (blocking) { |
|
761 |
configureBlocking(true); |
|
762 |
} |
|
763 |
} |
|
764 |
} |
|
2 | 765 |
} |
766 |
} |
|
767 |
} |
|
768 |
return this; |
|
769 |
} |
|
770 |
||
771 |
public DatagramChannel disconnect() throws IOException { |
|
772 |
synchronized(readLock) { |
|
773 |
synchronized(writeLock) { |
|
774 |
synchronized (stateLock) { |
|
775 |
if (!isConnected() || !isOpen()) |
|
776 |
return this; |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
777 |
InetSocketAddress isa = remoteAddress; |
2 | 778 |
SecurityManager sm = System.getSecurityManager(); |
779 |
if (sm != null) |
|
780 |
sm.checkConnect(isa.getAddress().getHostAddress(), |
|
781 |
isa.getPort()); |
|
12547
ae1b2051db5d
7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
12440
diff
changeset
|
782 |
boolean isIPv6 = (family == StandardProtocolFamily.INET6); |
ae1b2051db5d
7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
12440
diff
changeset
|
783 |
disconnect0(fd, isIPv6); |
2 | 784 |
remoteAddress = null; |
785 |
state = ST_UNCONNECTED; |
|
7045
525f00c555b2
6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
6301
diff
changeset
|
786 |
|
525f00c555b2
6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
6301
diff
changeset
|
787 |
// refresh local address |
525f00c555b2
6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
6301
diff
changeset
|
788 |
localAddress = Net.localAddress(fd); |
2 | 789 |
} |
790 |
} |
|
791 |
} |
|
792 |
return this; |
|
793 |
} |
|
794 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
795 |
/** |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
796 |
* Joins channel's socket to the given group/interface and |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
797 |
* optional source address. |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
798 |
*/ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
799 |
private MembershipKey innerJoin(InetAddress group, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
800 |
NetworkInterface interf, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
801 |
InetAddress source) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
802 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
803 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
804 |
if (!group.isMulticastAddress()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
805 |
throw new IllegalArgumentException("Group not a multicast address"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
806 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
807 |
// 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
|
808 |
if (group instanceof Inet4Address) { |
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
809 |
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
|
810 |
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
|
811 |
} else if (group instanceof Inet6Address) { |
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
812 |
if (family != StandardProtocolFamily.INET6) |
9237
cbb5753e87e7
7034155: (ch) NullPointerException in sun.io.ch.IOUtil when OOM is thrown
alanb
parents:
8817
diff
changeset
|
813 |
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
|
814 |
} else { |
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
815 |
throw new IllegalArgumentException("Address type not supported"); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
816 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
817 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
818 |
// check source address |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
819 |
if (source != null) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
820 |
if (source.isAnyLocalAddress()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
821 |
throw new IllegalArgumentException("Source address is a wildcard address"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
822 |
if (source.isMulticastAddress()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
823 |
throw new IllegalArgumentException("Source address is multicast address"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
824 |
if (source.getClass() != group.getClass()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
825 |
throw new IllegalArgumentException("Source address is different type to group"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
826 |
} |
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 |
SecurityManager sm = System.getSecurityManager(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
829 |
if (sm != null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
830 |
sm.checkMulticast(group); |
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 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
833 |
if (!isOpen()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
834 |
throw new ClosedChannelException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
835 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
836 |
// 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
|
837 |
if (registry == null) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
838 |
registry = new MembershipRegistry(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
839 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
840 |
// return existing membership key |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
841 |
MembershipKey key = registry.checkMembership(group, interf, source); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
842 |
if (key != null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
843 |
return key; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
844 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
845 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
846 |
MembershipKeyImpl key; |
8788
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
847 |
if ((family == StandardProtocolFamily.INET6) && |
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
848 |
((group instanceof Inet6Address) || Net.canJoin6WithIPv4Group())) |
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
849 |
{ |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
850 |
int index = interf.getIndex(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
851 |
if (index == -1) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
852 |
throw new IOException("Network interface cannot be identified"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
853 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
854 |
// need multicast and source address as byte arrays |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
855 |
byte[] groupAddress = Net.inet6AsByteArray(group); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
856 |
byte[] sourceAddress = (source == null) ? null : |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
857 |
Net.inet6AsByteArray(source); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
858 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
859 |
// join the group |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
860 |
int n = Net.join6(fd, groupAddress, index, sourceAddress); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
861 |
if (n == IOStatus.UNAVAILABLE) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
862 |
throw new UnsupportedOperationException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
863 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
864 |
key = new MembershipKeyImpl.Type6(this, group, interf, source, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
865 |
groupAddress, index, sourceAddress); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
866 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
867 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
868 |
// need IPv4 address to identify interface |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
869 |
Inet4Address target = Net.anyInet4Address(interf); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
870 |
if (target == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
871 |
throw new IOException("Network interface not configured for IPv4"); |
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 |
int groupAddress = Net.inet4AsInt(group); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
874 |
int targetAddress = Net.inet4AsInt(target); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
875 |
int sourceAddress = (source == null) ? 0 : Net.inet4AsInt(source); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
876 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
877 |
// join the group |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
878 |
int n = Net.join4(fd, groupAddress, targetAddress, sourceAddress); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
879 |
if (n == IOStatus.UNAVAILABLE) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
880 |
throw new UnsupportedOperationException(); |
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 |
key = new MembershipKeyImpl.Type4(this, group, interf, source, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
883 |
groupAddress, targetAddress, sourceAddress); |
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 |
registry.add(key); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
887 |
return key; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
888 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
889 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
890 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
891 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
892 |
public MembershipKey join(InetAddress group, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
893 |
NetworkInterface interf) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
894 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
895 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
896 |
return innerJoin(group, interf, null); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
897 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
898 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
899 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
900 |
public MembershipKey join(InetAddress group, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
901 |
NetworkInterface interf, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
902 |
InetAddress source) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
903 |
throws IOException |
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 |
if (source == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
906 |
throw new NullPointerException("source address is null"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
907 |
return innerJoin(group, interf, source); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
908 |
} |
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 |
// package-private |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
911 |
void drop(MembershipKeyImpl key) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
912 |
assert key.channel() == this; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
913 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
914 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
915 |
if (!key.isValid()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
916 |
return; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
917 |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
918 |
try { |
8788
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
919 |
if (key instanceof MembershipKeyImpl.Type6) { |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
920 |
MembershipKeyImpl.Type6 key6 = |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
921 |
(MembershipKeyImpl.Type6)key; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
922 |
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
|
923 |
} else { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
924 |
MembershipKeyImpl.Type4 key4 = (MembershipKeyImpl.Type4)key; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
925 |
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
|
926 |
key4.source()); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
927 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
928 |
} catch (IOException ioe) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
929 |
// should not happen |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
930 |
throw new AssertionError(ioe); |
1152
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 |
key.invalidate(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
934 |
registry.remove(key); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
935 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
936 |
} |
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 |
/** |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
939 |
* 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
|
940 |
* datagrams. |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
941 |
*/ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
942 |
void block(MembershipKeyImpl key, InetAddress source) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
943 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
944 |
{ |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
945 |
assert key.channel() == this; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
946 |
assert key.sourceAddress() == null; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
947 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
948 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
949 |
if (!key.isValid()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
950 |
throw new IllegalStateException("key is no longer valid"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
951 |
if (source.isAnyLocalAddress()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
952 |
throw new IllegalArgumentException("Source address is a wildcard address"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
953 |
if (source.isMulticastAddress()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
954 |
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
|
955 |
if (source.getClass() != key.group().getClass()) |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
956 |
throw new IllegalArgumentException("Source address is different type to group"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
957 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
958 |
int n; |
8788
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
959 |
if (key instanceof MembershipKeyImpl.Type6) { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
960 |
MembershipKeyImpl.Type6 key6 = |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
961 |
(MembershipKeyImpl.Type6)key; |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
962 |
n = Net.block6(fd, key6.groupAddress(), key6.index(), |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
963 |
Net.inet6AsByteArray(source)); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
964 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
965 |
MembershipKeyImpl.Type4 key4 = |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
966 |
(MembershipKeyImpl.Type4)key; |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
967 |
n = Net.block4(fd, key4.groupAddress(), key4.interfaceAddress(), |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
968 |
Net.inet4AsInt(source)); |
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 |
if (n == IOStatus.UNAVAILABLE) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
971 |
// ancient kernel |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
972 |
throw new UnsupportedOperationException(); |
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 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
975 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
976 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
977 |
/** |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
978 |
* Unblock given source. |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
979 |
*/ |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
980 |
void unblock(MembershipKeyImpl key, InetAddress source) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
981 |
assert key.channel() == this; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
982 |
assert key.sourceAddress() == null; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
983 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
984 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
985 |
if (!key.isValid()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
986 |
throw new IllegalStateException("key is no longer valid"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
987 |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
988 |
try { |
8788
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
989 |
if (key instanceof MembershipKeyImpl.Type6) { |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
990 |
MembershipKeyImpl.Type6 key6 = |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
991 |
(MembershipKeyImpl.Type6)key; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
992 |
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
|
993 |
Net.inet6AsByteArray(source)); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
994 |
} else { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
995 |
MembershipKeyImpl.Type4 key4 = |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
996 |
(MembershipKeyImpl.Type4)key; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
997 |
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
|
998 |
Net.inet4AsInt(source)); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
999 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1000 |
} catch (IOException ioe) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1001 |
// should not happen |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
1002 |
throw new AssertionError(ioe); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1003 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1004 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1005 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1006 |
|
2 | 1007 |
protected void implCloseSelectableChannel() throws IOException { |
1008 |
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
|
1009 |
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
|
1010 |
nd.preClose(fd); |
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
1011 |
ResourceManager.afterUdpClose(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1012 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1013 |
// if member of mulitcast group then invalidate all keys |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1014 |
if (registry != null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1015 |
registry.invalidateAll(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1016 |
|
2 | 1017 |
long th; |
1018 |
if ((th = readerThread) != 0) |
|
1019 |
NativeThread.signal(th); |
|
1020 |
if ((th = writerThread) != 0) |
|
1021 |
NativeThread.signal(th); |
|
1022 |
if (!isRegistered()) |
|
1023 |
kill(); |
|
1024 |
} |
|
1025 |
} |
|
1026 |
||
1027 |
public void kill() throws IOException { |
|
1028 |
synchronized (stateLock) { |
|
1029 |
if (state == ST_KILLED) |
|
1030 |
return; |
|
1031 |
if (state == ST_UNINITIALIZED) { |
|
1032 |
state = ST_KILLED; |
|
1033 |
return; |
|
1034 |
} |
|
1035 |
assert !isOpen() && !isRegistered(); |
|
1036 |
nd.close(fd); |
|
1037 |
state = ST_KILLED; |
|
1038 |
} |
|
1039 |
} |
|
1040 |
||
1041 |
protected void finalize() throws IOException { |
|
1042 |
// fd is null if constructor threw exception |
|
1043 |
if (fd != null) |
|
1044 |
close(); |
|
1045 |
} |
|
1046 |
||
1047 |
/** |
|
1048 |
* Translates native poll revent set into a ready operation set |
|
1049 |
*/ |
|
1050 |
public boolean translateReadyOps(int ops, int initialOps, |
|
1051 |
SelectionKeyImpl sk) { |
|
1052 |
int intOps = sk.nioInterestOps(); // Do this just once, it synchronizes |
|
1053 |
int oldOps = sk.nioReadyOps(); |
|
1054 |
int newOps = initialOps; |
|
1055 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1056 |
if ((ops & Net.POLLNVAL) != 0) { |
2 | 1057 |
// This should only happen if this channel is pre-closed while a |
1058 |
// selection operation is in progress |
|
1059 |
// ## Throw an error if this channel has not been pre-closed |
|
1060 |
return false; |
|
1061 |
} |
|
1062 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1063 |
if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) { |
2 | 1064 |
newOps = intOps; |
1065 |
sk.nioReadyOps(newOps); |
|
1066 |
return (newOps & ~oldOps) != 0; |
|
1067 |
} |
|
1068 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1069 |
if (((ops & Net.POLLIN) != 0) && |
2 | 1070 |
((intOps & SelectionKey.OP_READ) != 0)) |
1071 |
newOps |= SelectionKey.OP_READ; |
|
1072 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1073 |
if (((ops & Net.POLLOUT) != 0) && |
2 | 1074 |
((intOps & SelectionKey.OP_WRITE) != 0)) |
1075 |
newOps |= SelectionKey.OP_WRITE; |
|
1076 |
||
1077 |
sk.nioReadyOps(newOps); |
|
1078 |
return (newOps & ~oldOps) != 0; |
|
1079 |
} |
|
1080 |
||
1081 |
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) { |
|
1082 |
return translateReadyOps(ops, sk.nioReadyOps(), sk); |
|
1083 |
} |
|
1084 |
||
1085 |
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) { |
|
1086 |
return translateReadyOps(ops, 0, sk); |
|
1087 |
} |
|
1088 |
||
14025
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1089 |
// package-private |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1090 |
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
|
1091 |
assert Thread.holdsLock(blockingLock()) && !isBlocking(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1092 |
|
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1093 |
synchronized (readLock) { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1094 |
int n = 0; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1095 |
try { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1096 |
begin(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1097 |
synchronized (stateLock) { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1098 |
if (!isOpen()) |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1099 |
return 0; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1100 |
readerThread = NativeThread.current(); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1101 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1102 |
n = Net.poll(fd, events, timeout); |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1103 |
} finally { |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1104 |
readerThread = 0; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1105 |
end(n > 0); |
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 |
return n; |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1108 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1109 |
} |
fbebe005a3ee
7184932: Remove the temporary Selector usage in the NIO socket adapters
robm
parents:
12890
diff
changeset
|
1110 |
|
2 | 1111 |
/** |
1112 |
* Translates an interest operation set into a native poll event set |
|
1113 |
*/ |
|
1114 |
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) { |
|
1115 |
int newOps = 0; |
|
1116 |
||
1117 |
if ((ops & SelectionKey.OP_READ) != 0) |
|
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1118 |
newOps |= Net.POLLIN; |
2 | 1119 |
if ((ops & SelectionKey.OP_WRITE) != 0) |
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1120 |
newOps |= Net.POLLOUT; |
2 | 1121 |
if ((ops & SelectionKey.OP_CONNECT) != 0) |
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1122 |
newOps |= Net.POLLIN; |
2 | 1123 |
sk.selector.putEventOps(sk, newOps); |
1124 |
} |
|
1125 |
||
1126 |
public FileDescriptor getFD() { |
|
1127 |
return fd; |
|
1128 |
} |
|
1129 |
||
1130 |
public int getFDVal() { |
|
1131 |
return fdVal; |
|
1132 |
} |
|
1133 |
||
1134 |
||
1135 |
// -- Native methods -- |
|
1136 |
||
1137 |
private static native void initIDs(); |
|
1138 |
||
12547
ae1b2051db5d
7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
12440
diff
changeset
|
1139 |
private static native void disconnect0(FileDescriptor fd, boolean isIPv6) |
2 | 1140 |
throws IOException; |
1141 |
||
1142 |
private native int receive0(FileDescriptor fd, long address, int len, |
|
1143 |
boolean connected) |
|
1144 |
throws IOException; |
|
1145 |
||
16089 | 1146 |
private native int send0(boolean preferIPv6, FileDescriptor fd, long address, |
1147 |
int len, InetAddress addr, int port) |
|
2 | 1148 |
throws IOException; |
1149 |
||
1150 |
static { |
|
19607
bee007586d06
8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents:
18212
diff
changeset
|
1151 |
IOUtil.load(); |
2 | 1152 |
initIDs(); |
1153 |
} |
|
1154 |
||
1155 |
} |