author | lana |
Sun, 17 Apr 2011 16:19:29 -0700 | |
changeset 9275 | 1df1f7dfab7f |
parent 9237 | cbb5753e87e7 |
parent 9035 | 1255eb81cc2f |
child 9679 | d98ae8bc45fc |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
8817
diff
changeset
|
2 |
* Copyright (c) 2001, 2011, 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; |
2 | 36 |
|
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 |
|
61 |
private volatile long readerThread = 0; |
|
62 |
private volatile long writerThread = 0; |
|
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 |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
89 |
private SocketAddress localAddress; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
90 |
private SocketAddress 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 |
|
98 |
// -- End of fields protected by stateLock |
|
99 |
||
100 |
||
101 |
public DatagramChannelImpl(SelectorProvider sp) |
|
102 |
throws IOException |
|
103 |
{ |
|
104 |
super(sp); |
|
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
105 |
ResourceManager.beforeUdpCreate(); |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
106 |
try { |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
107 |
this.family = Net.isIPv6Available() ? |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
108 |
StandardProtocolFamily.INET6 : StandardProtocolFamily.INET; |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
109 |
this.fd = Net.socket(family, false); |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
110 |
this.fdVal = IOUtil.fdVal(fd); |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
111 |
this.state = ST_UNCONNECTED; |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
112 |
} catch (IOException ioe) { |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
113 |
ResourceManager.afterUdpClose(); |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
114 |
throw ioe; |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
115 |
} |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
116 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
117 |
|
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
118 |
public DatagramChannelImpl(SelectorProvider sp, ProtocolFamily family) |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
119 |
throws IOException |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
120 |
{ |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
121 |
super(sp); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
122 |
if ((family != StandardProtocolFamily.INET) && |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
123 |
(family != StandardProtocolFamily.INET6)) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
124 |
{ |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
125 |
if (family == null) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
126 |
throw new NullPointerException("'family' is null"); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
127 |
else |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
128 |
throw new UnsupportedOperationException("Protocol family not supported"); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
129 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
130 |
if (family == StandardProtocolFamily.INET6) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
131 |
if (!Net.isIPv6Available()) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
132 |
throw new UnsupportedOperationException("IPv6 not available"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
133 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
134 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
135 |
this.family = family; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
136 |
this.fd = Net.socket(family, false); |
2 | 137 |
this.fdVal = IOUtil.fdVal(fd); |
138 |
this.state = ST_UNCONNECTED; |
|
139 |
} |
|
140 |
||
141 |
public DatagramChannelImpl(SelectorProvider sp, FileDescriptor fd) |
|
142 |
throws IOException |
|
143 |
{ |
|
144 |
super(sp); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
145 |
this.family = Net.isIPv6Available() ? |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
146 |
StandardProtocolFamily.INET6 : StandardProtocolFamily.INET; |
2 | 147 |
this.fd = fd; |
148 |
this.fdVal = IOUtil.fdVal(fd); |
|
149 |
this.state = ST_UNCONNECTED; |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
150 |
this.localAddress = Net.localAddress(fd); |
2 | 151 |
} |
152 |
||
153 |
public DatagramSocket socket() { |
|
154 |
synchronized (stateLock) { |
|
155 |
if (socket == null) |
|
156 |
socket = DatagramSocketAdaptor.create(this); |
|
157 |
return socket; |
|
158 |
} |
|
159 |
} |
|
160 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
161 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
162 |
public SocketAddress getLocalAddress() throws IOException { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
163 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
164 |
if (!isOpen()) |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
165 |
throw new ClosedChannelException(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
166 |
return localAddress; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
167 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
168 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
169 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
170 |
@Override |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
171 |
public SocketAddress getRemoteAddress() throws IOException { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
172 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
173 |
if (!isOpen()) |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
174 |
throw new ClosedChannelException(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
175 |
return remoteAddress; |
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 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
178 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
179 |
@Override |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
180 |
public <T> DatagramChannel setOption(SocketOption<T> name, T value) |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
181 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
182 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
183 |
if (name == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
184 |
throw new NullPointerException(); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
185 |
if (!supportedOptions().contains(name)) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
186 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
187 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
188 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
189 |
ensureOpen(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
190 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
191 |
if (name == StandardSocketOption.IP_TOS) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
192 |
// IPv4 only; no-op for IPv6 |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
193 |
if (family == StandardProtocolFamily.INET) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
194 |
Net.setSocketOption(fd, family, name, value); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
195 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
196 |
return this; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
197 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
198 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
199 |
if (name == StandardSocketOption.IP_MULTICAST_TTL || |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
200 |
name == StandardSocketOption.IP_MULTICAST_LOOP) |
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 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
207 |
if (name == StandardSocketOption.IP_MULTICAST_IF) { |
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 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
226 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
227 |
// remaining options don't need any special handling |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
228 |
Net.setSocketOption(fd, Net.UNSPEC, name, value); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
229 |
return this; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
230 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
231 |
} |
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 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
234 |
@SuppressWarnings("unchecked") |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
235 |
public <T> T getOption(SocketOption<T> name) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
236 |
throws IOException |
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 |
if (name == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
239 |
throw new NullPointerException(); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
240 |
if (!supportedOptions().contains(name)) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
241 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
242 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
243 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
244 |
ensureOpen(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
245 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
246 |
if (name == StandardSocketOption.IP_TOS) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
247 |
// IPv4 only; always return 0 on IPv6 |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
248 |
if (family == StandardProtocolFamily.INET) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
249 |
return (T) Net.getSocketOption(fd, family, name); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
250 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
251 |
return (T) Integer.valueOf(0); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
252 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
253 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
254 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
255 |
if (name == StandardSocketOption.IP_MULTICAST_TTL || |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
256 |
name == StandardSocketOption.IP_MULTICAST_LOOP) |
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 |
return (T) Net.getSocketOption(fd, family, name); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
259 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
260 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
261 |
if (name == StandardSocketOption.IP_MULTICAST_IF) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
262 |
if (family == StandardProtocolFamily.INET) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
263 |
int address = Net.getInterface4(fd); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
264 |
if (address == 0) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
265 |
return null; // default interface |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
266 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
267 |
InetAddress ia = Net.inet4FromInt(address); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
268 |
NetworkInterface ni = NetworkInterface.getByInetAddress(ia); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
269 |
if (ni == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
270 |
throw new IOException("Unable to map address to interface"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
271 |
return (T) ni; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
272 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
273 |
int index = Net.getInterface6(fd); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
274 |
if (index == 0) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
275 |
return null; // default interface |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
276 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
277 |
NetworkInterface ni = NetworkInterface.getByIndex(index); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
278 |
if (ni == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
279 |
throw new IOException("Unable to map index to interface"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
280 |
return (T) ni; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
281 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
282 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
283 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
284 |
// no special handling |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
285 |
return (T) Net.getSocketOption(fd, Net.UNSPEC, name); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
286 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
287 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
288 |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
289 |
private static class DefaultOptionsHolder { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
290 |
static final Set<SocketOption<?>> defaultOptions = defaultOptions(); |
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 |
private static Set<SocketOption<?>> defaultOptions() { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
293 |
HashSet<SocketOption<?>> set = new HashSet<SocketOption<?>>(8); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
294 |
set.add(StandardSocketOption.SO_SNDBUF); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
295 |
set.add(StandardSocketOption.SO_RCVBUF); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
296 |
set.add(StandardSocketOption.SO_REUSEADDR); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
297 |
set.add(StandardSocketOption.SO_BROADCAST); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
298 |
set.add(StandardSocketOption.IP_TOS); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
299 |
set.add(StandardSocketOption.IP_MULTICAST_IF); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
300 |
set.add(StandardSocketOption.IP_MULTICAST_TTL); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
301 |
set.add(StandardSocketOption.IP_MULTICAST_LOOP); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
302 |
return Collections.unmodifiableSet(set); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
303 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
304 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
305 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
306 |
@Override |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
307 |
public final Set<SocketOption<?>> supportedOptions() { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
308 |
return DefaultOptionsHolder.defaultOptions; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
309 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
310 |
|
2 | 311 |
private void ensureOpen() throws ClosedChannelException { |
312 |
if (!isOpen()) |
|
313 |
throw new ClosedChannelException(); |
|
314 |
} |
|
315 |
||
316 |
private SocketAddress sender; // Set by receive0 (## ugh) |
|
317 |
||
318 |
public SocketAddress receive(ByteBuffer dst) throws IOException { |
|
319 |
if (dst.isReadOnly()) |
|
320 |
throw new IllegalArgumentException("Read-only buffer"); |
|
321 |
if (dst == null) |
|
322 |
throw new NullPointerException(); |
|
323 |
synchronized (readLock) { |
|
324 |
ensureOpen(); |
|
2427
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2057
diff
changeset
|
325 |
// 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
|
326 |
if (localAddress() == null) |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2057
diff
changeset
|
327 |
bind(null); |
2 | 328 |
int n = 0; |
329 |
ByteBuffer bb = null; |
|
330 |
try { |
|
331 |
begin(); |
|
332 |
if (!isOpen()) |
|
333 |
return null; |
|
334 |
SecurityManager security = System.getSecurityManager(); |
|
335 |
readerThread = NativeThread.current(); |
|
336 |
if (isConnected() || (security == null)) { |
|
337 |
do { |
|
338 |
n = receive(fd, dst); |
|
339 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
|
340 |
if (n == IOStatus.UNAVAILABLE) |
|
341 |
return null; |
|
342 |
} else { |
|
343 |
bb = Util.getTemporaryDirectBuffer(dst.remaining()); |
|
344 |
for (;;) { |
|
345 |
do { |
|
346 |
n = receive(fd, bb); |
|
347 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
|
348 |
if (n == IOStatus.UNAVAILABLE) |
|
349 |
return null; |
|
350 |
InetSocketAddress isa = (InetSocketAddress)sender; |
|
351 |
try { |
|
352 |
security.checkAccept( |
|
353 |
isa.getAddress().getHostAddress(), |
|
354 |
isa.getPort()); |
|
355 |
} catch (SecurityException se) { |
|
356 |
// Ignore packet |
|
357 |
bb.clear(); |
|
358 |
n = 0; |
|
359 |
continue; |
|
360 |
} |
|
361 |
bb.flip(); |
|
362 |
dst.put(bb); |
|
363 |
break; |
|
364 |
} |
|
365 |
} |
|
366 |
return sender; |
|
367 |
} finally { |
|
368 |
if (bb != null) |
|
369 |
Util.releaseTemporaryDirectBuffer(bb); |
|
370 |
readerThread = 0; |
|
371 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
372 |
assert IOStatus.check(n); |
|
373 |
} |
|
374 |
} |
|
375 |
} |
|
376 |
||
377 |
private int receive(FileDescriptor fd, ByteBuffer dst) |
|
378 |
throws IOException |
|
379 |
{ |
|
380 |
int pos = dst.position(); |
|
381 |
int lim = dst.limit(); |
|
382 |
assert (pos <= lim); |
|
383 |
int rem = (pos <= lim ? lim - pos : 0); |
|
384 |
if (dst instanceof DirectBuffer && rem > 0) |
|
385 |
return receiveIntoNativeBuffer(fd, dst, rem, pos); |
|
386 |
||
387 |
// Substitute a native buffer. If the supplied buffer is empty |
|
388 |
// we must instead use a nonempty buffer, otherwise the call |
|
389 |
// will not block waiting for a datagram on some platforms. |
|
390 |
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
|
391 |
ByteBuffer bb = Util.getTemporaryDirectBuffer(newSize); |
2 | 392 |
try { |
393 |
int n = receiveIntoNativeBuffer(fd, bb, newSize, 0); |
|
394 |
bb.flip(); |
|
395 |
if (n > 0 && rem > 0) |
|
396 |
dst.put(bb); |
|
397 |
return n; |
|
398 |
} finally { |
|
399 |
Util.releaseTemporaryDirectBuffer(bb); |
|
400 |
} |
|
401 |
} |
|
402 |
||
403 |
private int receiveIntoNativeBuffer(FileDescriptor fd, ByteBuffer bb, |
|
404 |
int rem, int pos) |
|
405 |
throws IOException |
|
406 |
{ |
|
407 |
int n = receive0(fd, ((DirectBuffer)bb).address() + pos, rem, |
|
408 |
isConnected()); |
|
409 |
if (n > 0) |
|
410 |
bb.position(pos + n); |
|
411 |
return n; |
|
412 |
} |
|
413 |
||
414 |
public int send(ByteBuffer src, SocketAddress target) |
|
415 |
throws IOException |
|
416 |
{ |
|
417 |
if (src == null) |
|
418 |
throw new NullPointerException(); |
|
419 |
||
420 |
synchronized (writeLock) { |
|
421 |
ensureOpen(); |
|
422 |
InetSocketAddress isa = (InetSocketAddress)target; |
|
423 |
InetAddress ia = isa.getAddress(); |
|
424 |
if (ia == null) |
|
425 |
throw new IOException("Target address not resolved"); |
|
426 |
synchronized (stateLock) { |
|
427 |
if (!isConnected()) { |
|
428 |
if (target == null) |
|
429 |
throw new NullPointerException(); |
|
430 |
SecurityManager sm = System.getSecurityManager(); |
|
431 |
if (sm != null) { |
|
432 |
if (ia.isMulticastAddress()) { |
|
433 |
sm.checkMulticast(isa.getAddress()); |
|
434 |
} else { |
|
435 |
sm.checkConnect(isa.getAddress().getHostAddress(), |
|
436 |
isa.getPort()); |
|
437 |
} |
|
438 |
} |
|
439 |
} else { // Connected case; Check address then write |
|
440 |
if (!target.equals(remoteAddress)) { |
|
441 |
throw new IllegalArgumentException( |
|
442 |
"Connected address not equal to target address"); |
|
443 |
} |
|
444 |
return write(src); |
|
445 |
} |
|
446 |
} |
|
447 |
||
448 |
int n = 0; |
|
449 |
try { |
|
450 |
begin(); |
|
451 |
if (!isOpen()) |
|
452 |
return 0; |
|
453 |
writerThread = NativeThread.current(); |
|
454 |
do { |
|
455 |
n = send(fd, src, target); |
|
456 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
457 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
458 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
459 |
if (isOpen() && (localAddress == null)) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
460 |
localAddress = Net.localAddress(fd); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
461 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
462 |
} |
2 | 463 |
return IOStatus.normalize(n); |
464 |
} finally { |
|
465 |
writerThread = 0; |
|
466 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
467 |
assert IOStatus.check(n); |
|
468 |
} |
|
469 |
} |
|
470 |
} |
|
471 |
||
472 |
private int send(FileDescriptor fd, ByteBuffer src, SocketAddress target) |
|
473 |
throws IOException |
|
474 |
{ |
|
475 |
if (src instanceof DirectBuffer) |
|
476 |
return sendFromNativeBuffer(fd, src, target); |
|
477 |
||
478 |
// Substitute a native buffer |
|
479 |
int pos = src.position(); |
|
480 |
int lim = src.limit(); |
|
481 |
assert (pos <= lim); |
|
482 |
int rem = (pos <= lim ? lim - pos : 0); |
|
483 |
||
9237
cbb5753e87e7
7034155: (ch) NullPointerException in sun.io.ch.IOUtil when OOM is thrown
alanb
parents:
8817
diff
changeset
|
484 |
ByteBuffer bb = Util.getTemporaryDirectBuffer(rem); |
2 | 485 |
try { |
486 |
bb.put(src); |
|
487 |
bb.flip(); |
|
488 |
// Do not update src until we see how many bytes were written |
|
489 |
src.position(pos); |
|
490 |
||
491 |
int n = sendFromNativeBuffer(fd, bb, target); |
|
492 |
if (n > 0) { |
|
493 |
// now update src |
|
494 |
src.position(pos + n); |
|
495 |
} |
|
496 |
return n; |
|
497 |
} finally { |
|
498 |
Util.releaseTemporaryDirectBuffer(bb); |
|
499 |
} |
|
500 |
} |
|
501 |
||
502 |
private int sendFromNativeBuffer(FileDescriptor fd, ByteBuffer bb, |
|
503 |
SocketAddress target) |
|
504 |
throws IOException |
|
505 |
{ |
|
506 |
int pos = bb.position(); |
|
507 |
int lim = bb.limit(); |
|
508 |
assert (pos <= lim); |
|
509 |
int rem = (pos <= lim ? lim - pos : 0); |
|
510 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
511 |
boolean preferIPv6 = (family != StandardProtocolFamily.INET); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
512 |
int written = send0(preferIPv6, fd, ((DirectBuffer)bb).address() + pos, |
2 | 513 |
rem, target); |
514 |
if (written > 0) |
|
515 |
bb.position(pos + written); |
|
516 |
return written; |
|
517 |
} |
|
518 |
||
519 |
public int read(ByteBuffer buf) throws IOException { |
|
520 |
if (buf == null) |
|
521 |
throw new NullPointerException(); |
|
522 |
synchronized (readLock) { |
|
523 |
synchronized (stateLock) { |
|
524 |
ensureOpen(); |
|
525 |
if (!isConnected()) |
|
526 |
throw new NotYetConnectedException(); |
|
527 |
} |
|
528 |
int n = 0; |
|
529 |
try { |
|
530 |
begin(); |
|
531 |
if (!isOpen()) |
|
532 |
return 0; |
|
533 |
readerThread = NativeThread.current(); |
|
534 |
do { |
|
535 |
n = IOUtil.read(fd, buf, -1, nd, readLock); |
|
536 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
|
537 |
return IOStatus.normalize(n); |
|
538 |
} finally { |
|
539 |
readerThread = 0; |
|
540 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
541 |
assert IOStatus.check(n); |
|
542 |
} |
|
543 |
} |
|
544 |
} |
|
545 |
||
6301 | 546 |
public long read(ByteBuffer[] dsts, int offset, int length) |
547 |
throws IOException |
|
548 |
{ |
|
549 |
if ((offset < 0) || (length < 0) || (offset > dsts.length - length)) |
|
550 |
throw new IndexOutOfBoundsException(); |
|
2 | 551 |
synchronized (readLock) { |
552 |
synchronized (stateLock) { |
|
553 |
ensureOpen(); |
|
554 |
if (!isConnected()) |
|
555 |
throw new NotYetConnectedException(); |
|
556 |
} |
|
557 |
long n = 0; |
|
558 |
try { |
|
559 |
begin(); |
|
560 |
if (!isOpen()) |
|
561 |
return 0; |
|
562 |
readerThread = NativeThread.current(); |
|
563 |
do { |
|
6301 | 564 |
n = IOUtil.read(fd, dsts, offset, length, nd); |
2 | 565 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
566 |
return IOStatus.normalize(n); |
|
567 |
} finally { |
|
568 |
readerThread = 0; |
|
569 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
570 |
assert IOStatus.check(n); |
|
571 |
} |
|
572 |
} |
|
573 |
} |
|
574 |
||
575 |
public int write(ByteBuffer buf) throws IOException { |
|
576 |
if (buf == null) |
|
577 |
throw new NullPointerException(); |
|
578 |
synchronized (writeLock) { |
|
579 |
synchronized (stateLock) { |
|
580 |
ensureOpen(); |
|
581 |
if (!isConnected()) |
|
582 |
throw new NotYetConnectedException(); |
|
583 |
} |
|
584 |
int n = 0; |
|
585 |
try { |
|
586 |
begin(); |
|
587 |
if (!isOpen()) |
|
588 |
return 0; |
|
589 |
writerThread = NativeThread.current(); |
|
590 |
do { |
|
591 |
n = IOUtil.write(fd, buf, -1, nd, writeLock); |
|
592 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
|
593 |
return IOStatus.normalize(n); |
|
594 |
} finally { |
|
595 |
writerThread = 0; |
|
596 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
597 |
assert IOStatus.check(n); |
|
598 |
} |
|
599 |
} |
|
600 |
} |
|
601 |
||
6301 | 602 |
public long write(ByteBuffer[] srcs, int offset, int length) |
603 |
throws IOException |
|
604 |
{ |
|
605 |
if ((offset < 0) || (length < 0) || (offset > srcs.length - length)) |
|
606 |
throw new IndexOutOfBoundsException(); |
|
2 | 607 |
synchronized (writeLock) { |
608 |
synchronized (stateLock) { |
|
609 |
ensureOpen(); |
|
610 |
if (!isConnected()) |
|
611 |
throw new NotYetConnectedException(); |
|
612 |
} |
|
613 |
long n = 0; |
|
614 |
try { |
|
615 |
begin(); |
|
616 |
if (!isOpen()) |
|
617 |
return 0; |
|
618 |
writerThread = NativeThread.current(); |
|
619 |
do { |
|
6301 | 620 |
n = IOUtil.write(fd, srcs, offset, length, nd); |
2 | 621 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
622 |
return IOStatus.normalize(n); |
|
623 |
} finally { |
|
624 |
writerThread = 0; |
|
625 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
626 |
assert IOStatus.check(n); |
|
627 |
} |
|
628 |
} |
|
629 |
} |
|
630 |
||
631 |
protected void implConfigureBlocking(boolean block) throws IOException { |
|
632 |
IOUtil.configureBlocking(fd, block); |
|
633 |
} |
|
634 |
||
635 |
public SocketAddress localAddress() { |
|
636 |
synchronized (stateLock) { |
|
637 |
return localAddress; |
|
638 |
} |
|
639 |
} |
|
640 |
||
641 |
public SocketAddress remoteAddress() { |
|
642 |
synchronized (stateLock) { |
|
643 |
return remoteAddress; |
|
644 |
} |
|
645 |
} |
|
646 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
647 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
648 |
public DatagramChannel bind(SocketAddress local) throws IOException { |
2 | 649 |
synchronized (readLock) { |
650 |
synchronized (writeLock) { |
|
651 |
synchronized (stateLock) { |
|
652 |
ensureOpen(); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
653 |
if (localAddress != null) |
2 | 654 |
throw new AlreadyBoundException(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
655 |
InetSocketAddress isa; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
656 |
if (local == null) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
657 |
isa = new InetSocketAddress(0); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
658 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
659 |
isa = Net.checkAddress(local); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
660 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
661 |
// only Inet4Address allowed with IPv4 socket |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
662 |
if (family == StandardProtocolFamily.INET) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
663 |
InetAddress addr = isa.getAddress(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
664 |
if (!(addr instanceof Inet4Address)) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
665 |
throw new UnsupportedAddressTypeException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
666 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
667 |
} |
2 | 668 |
SecurityManager sm = System.getSecurityManager(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
669 |
if (sm != null) { |
2 | 670 |
sm.checkListen(isa.getPort()); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
671 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
672 |
Net.bind(family, fd, isa.getAddress(), isa.getPort()); |
2 | 673 |
localAddress = Net.localAddress(fd); |
674 |
} |
|
675 |
} |
|
676 |
} |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
677 |
return this; |
2 | 678 |
} |
679 |
||
680 |
public boolean isConnected() { |
|
681 |
synchronized (stateLock) { |
|
682 |
return (state == ST_CONNECTED); |
|
683 |
} |
|
684 |
} |
|
685 |
||
686 |
void ensureOpenAndUnconnected() throws IOException { // package-private |
|
687 |
synchronized (stateLock) { |
|
688 |
if (!isOpen()) |
|
689 |
throw new ClosedChannelException(); |
|
690 |
if (state != ST_UNCONNECTED) |
|
691 |
throw new IllegalStateException("Connect already invoked"); |
|
692 |
} |
|
693 |
} |
|
694 |
||
695 |
public DatagramChannel connect(SocketAddress sa) throws IOException { |
|
696 |
int localPort = 0; |
|
697 |
||
698 |
synchronized(readLock) { |
|
699 |
synchronized(writeLock) { |
|
700 |
synchronized (stateLock) { |
|
701 |
ensureOpenAndUnconnected(); |
|
702 |
InetSocketAddress isa = Net.checkAddress(sa); |
|
703 |
SecurityManager sm = System.getSecurityManager(); |
|
704 |
if (sm != null) |
|
705 |
sm.checkConnect(isa.getAddress().getHostAddress(), |
|
706 |
isa.getPort()); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
707 |
int n = Net.connect(family, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
708 |
fd, |
2 | 709 |
isa.getAddress(), |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
710 |
isa.getPort()); |
2 | 711 |
if (n <= 0) |
712 |
throw new Error(); // Can't happen |
|
713 |
||
714 |
// Connection succeeded; disallow further invocation |
|
715 |
state = ST_CONNECTED; |
|
716 |
remoteAddress = sa; |
|
717 |
sender = isa; |
|
718 |
cachedSenderInetAddress = isa.getAddress(); |
|
719 |
cachedSenderPort = isa.getPort(); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
720 |
|
7045
525f00c555b2
6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
6301
diff
changeset
|
721 |
// 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
|
722 |
localAddress = Net.localAddress(fd); |
2 | 723 |
} |
724 |
} |
|
725 |
} |
|
726 |
return this; |
|
727 |
} |
|
728 |
||
729 |
public DatagramChannel disconnect() throws IOException { |
|
730 |
synchronized(readLock) { |
|
731 |
synchronized(writeLock) { |
|
732 |
synchronized (stateLock) { |
|
733 |
if (!isConnected() || !isOpen()) |
|
734 |
return this; |
|
735 |
InetSocketAddress isa = (InetSocketAddress)remoteAddress; |
|
736 |
SecurityManager sm = System.getSecurityManager(); |
|
737 |
if (sm != null) |
|
738 |
sm.checkConnect(isa.getAddress().getHostAddress(), |
|
739 |
isa.getPort()); |
|
740 |
disconnect0(fd); |
|
741 |
remoteAddress = null; |
|
742 |
state = ST_UNCONNECTED; |
|
7045
525f00c555b2
6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
6301
diff
changeset
|
743 |
|
525f00c555b2
6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
6301
diff
changeset
|
744 |
// 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); |
2 | 746 |
} |
747 |
} |
|
748 |
} |
|
749 |
return this; |
|
750 |
} |
|
751 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
752 |
/** |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
753 |
* Joins channel's socket to the given group/interface and |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
754 |
* optional source address. |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
755 |
*/ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
756 |
private MembershipKey innerJoin(InetAddress group, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
757 |
NetworkInterface interf, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
758 |
InetAddress source) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
759 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
760 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
761 |
if (!group.isMulticastAddress()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
762 |
throw new IllegalArgumentException("Group not a multicast address"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
763 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
764 |
// 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
|
765 |
if (group instanceof Inet4Address) { |
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
766 |
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
|
767 |
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
|
768 |
} else if (group instanceof Inet6Address) { |
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
769 |
if (family != StandardProtocolFamily.INET6) |
9237
cbb5753e87e7
7034155: (ch) NullPointerException in sun.io.ch.IOUtil when OOM is thrown
alanb
parents:
8817
diff
changeset
|
770 |
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
|
771 |
} else { |
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
772 |
throw new IllegalArgumentException("Address type not supported"); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
773 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
774 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
775 |
// check source address |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
776 |
if (source != null) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
777 |
if (source.isAnyLocalAddress()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
778 |
throw new IllegalArgumentException("Source address is a wildcard address"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
779 |
if (source.isMulticastAddress()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
780 |
throw new IllegalArgumentException("Source address is multicast address"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
781 |
if (source.getClass() != group.getClass()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
782 |
throw new IllegalArgumentException("Source address is different type to group"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
783 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
784 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
785 |
SecurityManager sm = System.getSecurityManager(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
786 |
if (sm != null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
787 |
sm.checkMulticast(group); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
788 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
789 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
790 |
if (!isOpen()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
791 |
throw new ClosedChannelException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
792 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
793 |
// 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
|
794 |
if (registry == null) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
795 |
registry = new MembershipRegistry(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
796 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
797 |
// return existing membership key |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
798 |
MembershipKey key = registry.checkMembership(group, interf, source); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
799 |
if (key != null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
800 |
return key; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
801 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
802 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
803 |
MembershipKeyImpl key; |
8788
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
804 |
if ((family == StandardProtocolFamily.INET6) && |
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
805 |
((group instanceof Inet6Address) || Net.canJoin6WithIPv4Group())) |
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
806 |
{ |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
807 |
int index = interf.getIndex(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
808 |
if (index == -1) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
809 |
throw new IOException("Network interface cannot be identified"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
810 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
811 |
// need multicast and source address as byte arrays |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
812 |
byte[] groupAddress = Net.inet6AsByteArray(group); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
813 |
byte[] sourceAddress = (source == null) ? null : |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
814 |
Net.inet6AsByteArray(source); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
815 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
816 |
// join the group |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
817 |
int n = Net.join6(fd, groupAddress, index, sourceAddress); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
818 |
if (n == IOStatus.UNAVAILABLE) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
819 |
throw new UnsupportedOperationException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
820 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
821 |
key = new MembershipKeyImpl.Type6(this, group, interf, source, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
822 |
groupAddress, index, sourceAddress); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
823 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
824 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
825 |
// need IPv4 address to identify interface |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
826 |
Inet4Address target = Net.anyInet4Address(interf); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
827 |
if (target == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
828 |
throw new IOException("Network interface not configured for IPv4"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
829 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
830 |
int groupAddress = Net.inet4AsInt(group); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
831 |
int targetAddress = Net.inet4AsInt(target); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
832 |
int sourceAddress = (source == null) ? 0 : Net.inet4AsInt(source); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
833 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
834 |
// join the group |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
835 |
int n = Net.join4(fd, groupAddress, targetAddress, sourceAddress); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
836 |
if (n == IOStatus.UNAVAILABLE) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
837 |
throw new UnsupportedOperationException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
838 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
839 |
key = new MembershipKeyImpl.Type4(this, group, interf, source, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
840 |
groupAddress, targetAddress, sourceAddress); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
841 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
842 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
843 |
registry.add(key); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
844 |
return key; |
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 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
847 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
848 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
849 |
public MembershipKey join(InetAddress group, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
850 |
NetworkInterface interf) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
851 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
852 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
853 |
return innerJoin(group, interf, null); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
854 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
855 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
856 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
857 |
public MembershipKey join(InetAddress group, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
858 |
NetworkInterface interf, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
859 |
InetAddress source) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
860 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
861 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
862 |
if (source == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
863 |
throw new NullPointerException("source address is null"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
864 |
return innerJoin(group, interf, source); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
865 |
} |
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 |
// package-private |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
868 |
void drop(MembershipKeyImpl key) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
869 |
assert key.channel() == this; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
870 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
871 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
872 |
if (!key.isValid()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
873 |
return; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
874 |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
875 |
try { |
8788
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
876 |
if (key instanceof MembershipKeyImpl.Type6) { |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
877 |
MembershipKeyImpl.Type6 key6 = |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
878 |
(MembershipKeyImpl.Type6)key; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
879 |
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
|
880 |
} else { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
881 |
MembershipKeyImpl.Type4 key4 = (MembershipKeyImpl.Type4)key; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
882 |
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
|
883 |
key4.source()); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
884 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
885 |
} catch (IOException ioe) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
886 |
// should not happen |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
887 |
throw new AssertionError(ioe); |
1152
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 |
key.invalidate(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
891 |
registry.remove(key); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
892 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
893 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
894 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
895 |
/** |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
896 |
* 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
|
897 |
* datagrams. |
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 |
void block(MembershipKeyImpl key, InetAddress source) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
900 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
901 |
{ |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
902 |
assert key.channel() == this; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
903 |
assert key.sourceAddress() == null; |
1152
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 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
906 |
if (!key.isValid()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
907 |
throw new IllegalStateException("key is no longer valid"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
908 |
if (source.isAnyLocalAddress()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
909 |
throw new IllegalArgumentException("Source address is a wildcard address"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
910 |
if (source.isMulticastAddress()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
911 |
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
|
912 |
if (source.getClass() != key.group().getClass()) |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
913 |
throw new IllegalArgumentException("Source address is different type to group"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
914 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
915 |
int n; |
8788
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
916 |
if (key instanceof MembershipKeyImpl.Type6) { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
917 |
MembershipKeyImpl.Type6 key6 = |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
918 |
(MembershipKeyImpl.Type6)key; |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
919 |
n = Net.block6(fd, key6.groupAddress(), key6.index(), |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
920 |
Net.inet6AsByteArray(source)); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
921 |
} else { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
922 |
MembershipKeyImpl.Type4 key4 = |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
923 |
(MembershipKeyImpl.Type4)key; |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
924 |
n = Net.block4(fd, key4.groupAddress(), key4.interfaceAddress(), |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
925 |
Net.inet4AsInt(source)); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
926 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
927 |
if (n == IOStatus.UNAVAILABLE) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
928 |
// ancient kernel |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
929 |
throw new UnsupportedOperationException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
930 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
931 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
932 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
933 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
934 |
/** |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
935 |
* Unblock given source. |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
936 |
*/ |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
937 |
void unblock(MembershipKeyImpl key, InetAddress source) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
938 |
assert key.channel() == this; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
939 |
assert key.sourceAddress() == null; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
940 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
941 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
942 |
if (!key.isValid()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
943 |
throw new IllegalStateException("key is no longer valid"); |
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 |
try { |
8788
b98f18278dc4
7026376: (dc) DatagramChannel created without specifying protocol family fails to join IPv4 group
alanb
parents:
7668
diff
changeset
|
946 |
if (key instanceof MembershipKeyImpl.Type6) { |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
947 |
MembershipKeyImpl.Type6 key6 = |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
948 |
(MembershipKeyImpl.Type6)key; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
949 |
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
|
950 |
Net.inet6AsByteArray(source)); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
951 |
} else { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
952 |
MembershipKeyImpl.Type4 key4 = |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
953 |
(MembershipKeyImpl.Type4)key; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
954 |
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
|
955 |
Net.inet4AsInt(source)); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
956 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
957 |
} catch (IOException ioe) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
958 |
// should not happen |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
959 |
throw new AssertionError(ioe); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
960 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
961 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
962 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
963 |
|
2 | 964 |
protected void implCloseSelectableChannel() throws IOException { |
965 |
synchronized (stateLock) { |
|
966 |
nd.preClose(fd); |
|
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
8788
diff
changeset
|
967 |
ResourceManager.afterUdpClose(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
968 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
969 |
// if member of mulitcast group then invalidate all keys |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
970 |
if (registry != null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
971 |
registry.invalidateAll(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
972 |
|
2 | 973 |
long th; |
974 |
if ((th = readerThread) != 0) |
|
975 |
NativeThread.signal(th); |
|
976 |
if ((th = writerThread) != 0) |
|
977 |
NativeThread.signal(th); |
|
978 |
if (!isRegistered()) |
|
979 |
kill(); |
|
980 |
} |
|
981 |
} |
|
982 |
||
983 |
public void kill() throws IOException { |
|
984 |
synchronized (stateLock) { |
|
985 |
if (state == ST_KILLED) |
|
986 |
return; |
|
987 |
if (state == ST_UNINITIALIZED) { |
|
988 |
state = ST_KILLED; |
|
989 |
return; |
|
990 |
} |
|
991 |
assert !isOpen() && !isRegistered(); |
|
992 |
nd.close(fd); |
|
993 |
state = ST_KILLED; |
|
994 |
} |
|
995 |
} |
|
996 |
||
997 |
protected void finalize() throws IOException { |
|
998 |
// fd is null if constructor threw exception |
|
999 |
if (fd != null) |
|
1000 |
close(); |
|
1001 |
} |
|
1002 |
||
1003 |
/** |
|
1004 |
* Translates native poll revent set into a ready operation set |
|
1005 |
*/ |
|
1006 |
public boolean translateReadyOps(int ops, int initialOps, |
|
1007 |
SelectionKeyImpl sk) { |
|
1008 |
int intOps = sk.nioInterestOps(); // Do this just once, it synchronizes |
|
1009 |
int oldOps = sk.nioReadyOps(); |
|
1010 |
int newOps = initialOps; |
|
1011 |
||
1012 |
if ((ops & PollArrayWrapper.POLLNVAL) != 0) { |
|
1013 |
// This should only happen if this channel is pre-closed while a |
|
1014 |
// selection operation is in progress |
|
1015 |
// ## Throw an error if this channel has not been pre-closed |
|
1016 |
return false; |
|
1017 |
} |
|
1018 |
||
1019 |
if ((ops & (PollArrayWrapper.POLLERR |
|
1020 |
| PollArrayWrapper.POLLHUP)) != 0) { |
|
1021 |
newOps = intOps; |
|
1022 |
sk.nioReadyOps(newOps); |
|
1023 |
return (newOps & ~oldOps) != 0; |
|
1024 |
} |
|
1025 |
||
1026 |
if (((ops & PollArrayWrapper.POLLIN) != 0) && |
|
1027 |
((intOps & SelectionKey.OP_READ) != 0)) |
|
1028 |
newOps |= SelectionKey.OP_READ; |
|
1029 |
||
1030 |
if (((ops & PollArrayWrapper.POLLOUT) != 0) && |
|
1031 |
((intOps & SelectionKey.OP_WRITE) != 0)) |
|
1032 |
newOps |= SelectionKey.OP_WRITE; |
|
1033 |
||
1034 |
sk.nioReadyOps(newOps); |
|
1035 |
return (newOps & ~oldOps) != 0; |
|
1036 |
} |
|
1037 |
||
1038 |
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) { |
|
1039 |
return translateReadyOps(ops, sk.nioReadyOps(), sk); |
|
1040 |
} |
|
1041 |
||
1042 |
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) { |
|
1043 |
return translateReadyOps(ops, 0, sk); |
|
1044 |
} |
|
1045 |
||
1046 |
/** |
|
1047 |
* Translates an interest operation set into a native poll event set |
|
1048 |
*/ |
|
1049 |
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) { |
|
1050 |
int newOps = 0; |
|
1051 |
||
1052 |
if ((ops & SelectionKey.OP_READ) != 0) |
|
1053 |
newOps |= PollArrayWrapper.POLLIN; |
|
1054 |
if ((ops & SelectionKey.OP_WRITE) != 0) |
|
1055 |
newOps |= PollArrayWrapper.POLLOUT; |
|
1056 |
if ((ops & SelectionKey.OP_CONNECT) != 0) |
|
1057 |
newOps |= PollArrayWrapper.POLLIN; |
|
1058 |
sk.selector.putEventOps(sk, newOps); |
|
1059 |
} |
|
1060 |
||
1061 |
public FileDescriptor getFD() { |
|
1062 |
return fd; |
|
1063 |
} |
|
1064 |
||
1065 |
public int getFDVal() { |
|
1066 |
return fdVal; |
|
1067 |
} |
|
1068 |
||
1069 |
||
1070 |
// -- Native methods -- |
|
1071 |
||
1072 |
private static native void initIDs(); |
|
1073 |
||
1074 |
private static native void disconnect0(FileDescriptor fd) |
|
1075 |
throws IOException; |
|
1076 |
||
1077 |
private native int receive0(FileDescriptor fd, long address, int len, |
|
1078 |
boolean connected) |
|
1079 |
throws IOException; |
|
1080 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1081 |
private native int send0(boolean preferIPv6, FileDescriptor fd, long address, int len, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
1082 |
SocketAddress sa) |
2 | 1083 |
throws IOException; |
1084 |
||
1085 |
static { |
|
1086 |
Util.load(); |
|
1087 |
initIDs(); |
|
1088 |
} |
|
1089 |
||
1090 |
} |