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