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