author | bpb |
Wed, 03 Jul 2019 14:37:19 -0700 | |
changeset 55578 | 8c19519114e7 |
parent 54620 | 13b67c1420b8 |
child 58899 | 5573a7098439 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
2 |
* Copyright (c) 2001, 2019, 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 |
||
48761 | 28 |
import java.io.IOException; |
29 |
import java.net.DatagramPacket; |
|
30 |
import java.net.DatagramSocket; |
|
31 |
import java.net.DatagramSocketImpl; |
|
32 |
import java.net.InetAddress; |
|
33 |
import java.net.InetSocketAddress; |
|
34 |
import java.net.NetworkInterface; |
|
35 |
import java.net.SocketAddress; |
|
36 |
import java.net.SocketException; |
|
37 |
import java.net.SocketOption; |
|
38 |
import java.net.SocketTimeoutException; |
|
39 |
import java.net.StandardSocketOptions; |
|
40 |
import java.nio.ByteBuffer; |
|
41 |
import java.nio.channels.ClosedChannelException; |
|
42 |
import java.nio.channels.DatagramChannel; |
|
43 |
import java.nio.channels.IllegalBlockingModeException; |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
44 |
import java.util.Objects; |
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
45 |
import java.util.Set; |
2 | 46 |
|
47 |
||
48 |
// Make a datagram-socket channel look like a datagram socket. |
|
49 |
// |
|
50 |
// The methods in this class are defined in exactly the same order as in |
|
51 |
// java.net.DatagramSocket so as to simplify tracking future changes to that |
|
52 |
// class. |
|
53 |
// |
|
54 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
55 |
class DatagramSocketAdaptor |
2 | 56 |
extends DatagramSocket |
57 |
{ |
|
58 |
// The channel being adapted |
|
59 |
private final DatagramChannelImpl dc; |
|
60 |
||
61 |
// Timeout "option" value for receives |
|
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
25859
diff
changeset
|
62 |
private volatile int timeout; |
2 | 63 |
|
64 |
// ## super will create a useless impl |
|
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
65 |
private DatagramSocketAdaptor(DatagramChannelImpl dc) { |
2 | 66 |
// Invoke the DatagramSocketAdaptor(SocketAddress) constructor, |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
67 |
// passing a dummy DatagramSocketImpl object to avoid any native |
2 | 68 |
// resource allocation in super class and invoking our bind method |
69 |
// before the dc field is initialized. |
|
70 |
super(dummyDatagramSocket); |
|
71 |
this.dc = dc; |
|
72 |
} |
|
73 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
74 |
static DatagramSocket create(DatagramChannelImpl dc) { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
75 |
return new DatagramSocketAdaptor(dc); |
2 | 76 |
} |
77 |
||
78 |
private void connectInternal(SocketAddress remote) |
|
79 |
throws SocketException |
|
80 |
{ |
|
81 |
InetSocketAddress isa = Net.asInetSocketAddress(remote); |
|
82 |
int port = isa.getPort(); |
|
83 |
if (port < 0 || port > 0xFFFF) |
|
84 |
throw new IllegalArgumentException("connect: " + port); |
|
85 |
if (remote == null) |
|
86 |
throw new IllegalArgumentException("connect: null address"); |
|
87 |
try { |
|
88 |
dc.connect(remote); |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
89 |
} catch (ClosedChannelException e) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
90 |
// ignore |
2 | 91 |
} catch (Exception x) { |
92 |
Net.translateToSocketException(x); |
|
93 |
} |
|
94 |
} |
|
95 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
96 |
@Override |
2 | 97 |
public void bind(SocketAddress local) throws SocketException { |
98 |
try { |
|
99 |
if (local == null) |
|
100 |
local = new InetSocketAddress(0); |
|
101 |
dc.bind(local); |
|
102 |
} catch (Exception x) { |
|
103 |
Net.translateToSocketException(x); |
|
104 |
} |
|
105 |
} |
|
106 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
107 |
@Override |
2 | 108 |
public void connect(InetAddress address, int port) { |
109 |
try { |
|
110 |
connectInternal(new InetSocketAddress(address, port)); |
|
111 |
} catch (SocketException x) { |
|
112 |
// Yes, j.n.DatagramSocket really does this |
|
113 |
} |
|
114 |
} |
|
115 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
116 |
@Override |
2 | 117 |
public void connect(SocketAddress remote) throws SocketException { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
118 |
Objects.requireNonNull(remote, "Address can't be null"); |
2 | 119 |
connectInternal(remote); |
120 |
} |
|
121 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
122 |
@Override |
2 | 123 |
public void disconnect() { |
124 |
try { |
|
125 |
dc.disconnect(); |
|
126 |
} catch (IOException x) { |
|
127 |
throw new Error(x); |
|
128 |
} |
|
129 |
} |
|
130 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
131 |
@Override |
2 | 132 |
public boolean isBound() { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
133 |
return dc.localAddress() != null; |
2 | 134 |
} |
135 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
136 |
@Override |
2 | 137 |
public boolean isConnected() { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
138 |
return dc.remoteAddress() != null; |
2 | 139 |
} |
140 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
141 |
@Override |
2 | 142 |
public InetAddress getInetAddress() { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
143 |
InetSocketAddress remote = dc.remoteAddress(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
144 |
return (remote != null) ? remote.getAddress() : null; |
2 | 145 |
} |
146 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
147 |
@Override |
2 | 148 |
public int getPort() { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
149 |
InetSocketAddress remote = dc.remoteAddress(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
150 |
return (remote != null) ? remote.getPort() : -1; |
2 | 151 |
} |
152 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
153 |
@Override |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
154 |
public SocketAddress getRemoteSocketAddress() { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
155 |
return dc.remoteAddress(); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
156 |
} |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
157 |
|
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
158 |
@Override |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
159 |
public SocketAddress getLocalSocketAddress() { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
160 |
return dc.localAddress(); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
161 |
} |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
162 |
|
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
163 |
@Override |
2 | 164 |
public void send(DatagramPacket p) throws IOException { |
165 |
synchronized (dc.blockingLock()) { |
|
166 |
if (!dc.isBlocking()) |
|
167 |
throw new IllegalBlockingModeException(); |
|
168 |
try { |
|
169 |
synchronized (p) { |
|
170 |
ByteBuffer bb = ByteBuffer.wrap(p.getData(), |
|
171 |
p.getOffset(), |
|
172 |
p.getLength()); |
|
173 |
if (dc.isConnected()) { |
|
174 |
if (p.getAddress() == null) { |
|
175 |
// Legacy DatagramSocket will send in this case |
|
176 |
// and set address and port of the packet |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
177 |
InetSocketAddress isa = dc.remoteAddress(); |
2 | 178 |
p.setPort(isa.getPort()); |
179 |
p.setAddress(isa.getAddress()); |
|
180 |
dc.write(bb); |
|
181 |
} else { |
|
182 |
// Target address may not match connected address |
|
183 |
dc.send(bb, p.getSocketAddress()); |
|
184 |
} |
|
185 |
} else { |
|
186 |
// Not connected so address must be valid or throw |
|
187 |
dc.send(bb, p.getSocketAddress()); |
|
188 |
} |
|
189 |
} |
|
190 |
} catch (IOException x) { |
|
191 |
Net.translateException(x); |
|
192 |
} |
|
193 |
} |
|
194 |
} |
|
195 |
||
5807
d34ed576e234
4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents:
5506
diff
changeset
|
196 |
private SocketAddress receive(ByteBuffer bb) throws IOException { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
197 |
assert Thread.holdsLock(dc.blockingLock()) && dc.isBlocking(); |
2 | 198 |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
199 |
long to = this.timeout; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
200 |
if (to == 0) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
201 |
return dc.receive(bb); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
202 |
} else { |
2 | 203 |
for (;;) { |
204 |
if (!dc.isOpen()) |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
205 |
throw new ClosedChannelException(); |
2 | 206 |
long st = System.currentTimeMillis(); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
207 |
if (dc.pollRead(to)) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
208 |
return dc.receive(bb); |
2 | 209 |
} |
210 |
to -= System.currentTimeMillis() - st; |
|
211 |
if (to <= 0) |
|
212 |
throw new SocketTimeoutException(); |
|
213 |
} |
|
214 |
} |
|
215 |
} |
|
216 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
217 |
@Override |
2 | 218 |
public void receive(DatagramPacket p) throws IOException { |
219 |
synchronized (dc.blockingLock()) { |
|
220 |
if (!dc.isBlocking()) |
|
221 |
throw new IllegalBlockingModeException(); |
|
222 |
try { |
|
223 |
synchronized (p) { |
|
224 |
ByteBuffer bb = ByteBuffer.wrap(p.getData(), |
|
225 |
p.getOffset(), |
|
226 |
p.getLength()); |
|
5807
d34ed576e234
4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents:
5506
diff
changeset
|
227 |
SocketAddress sender = receive(bb); |
d34ed576e234
4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents:
5506
diff
changeset
|
228 |
p.setSocketAddress(sender); |
2 | 229 |
p.setLength(bb.position() - p.getOffset()); |
230 |
} |
|
231 |
} catch (IOException x) { |
|
232 |
Net.translateException(x); |
|
233 |
} |
|
234 |
} |
|
235 |
} |
|
236 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
237 |
@Override |
2 | 238 |
public InetAddress getLocalAddress() { |
239 |
if (isClosed()) |
|
240 |
return null; |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
241 |
InetSocketAddress local = dc.localAddress(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
242 |
if (local == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
243 |
local = new InetSocketAddress(0); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
244 |
InetAddress result = local.getAddress(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
245 |
SecurityManager sm = System.getSecurityManager(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
246 |
if (sm != null) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
247 |
try { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
248 |
sm.checkConnect(result.getHostAddress(), -1); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
249 |
} catch (SecurityException x) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
250 |
return new InetSocketAddress(0).getAddress(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
251 |
} |
2 | 252 |
} |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
253 |
return result; |
2 | 254 |
} |
255 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
256 |
@Override |
2 | 257 |
public int getLocalPort() { |
258 |
if (isClosed()) |
|
259 |
return -1; |
|
260 |
try { |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
261 |
InetSocketAddress local = dc.localAddress(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
262 |
if (local != null) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
263 |
return local.getPort(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
264 |
} |
2 | 265 |
} catch (Exception x) { |
266 |
} |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
267 |
return 0; |
2 | 268 |
} |
269 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
270 |
@Override |
2 | 271 |
public void setSoTimeout(int timeout) throws SocketException { |
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
272 |
if (!dc.isOpen()) |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
273 |
throw new SocketException("Socket is closed"); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
274 |
if (timeout < 0) |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
275 |
throw new IllegalArgumentException("timeout < 0"); |
2 | 276 |
this.timeout = timeout; |
277 |
} |
|
278 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
279 |
@Override |
2 | 280 |
public int getSoTimeout() throws SocketException { |
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
281 |
if (!dc.isOpen()) |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
282 |
throw new SocketException("Socket is closed"); |
2 | 283 |
return timeout; |
284 |
} |
|
285 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
286 |
private void setBooleanOption(SocketOption<Boolean> name, boolean value) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
287 |
throws SocketException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
288 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
289 |
try { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
290 |
dc.setOption(name, value); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
291 |
} catch (IOException x) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
292 |
Net.translateToSocketException(x); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
293 |
} |
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 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
296 |
private void setIntOption(SocketOption<Integer> name, int value) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
297 |
throws SocketException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
298 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
299 |
try { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
300 |
dc.setOption(name, value); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
301 |
} catch (IOException x) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
302 |
Net.translateToSocketException(x); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
303 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
304 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
305 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
306 |
private boolean getBooleanOption(SocketOption<Boolean> name) throws SocketException { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
307 |
try { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
308 |
return dc.getOption(name).booleanValue(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
309 |
} catch (IOException x) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
310 |
Net.translateToSocketException(x); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
311 |
return false; // keep compiler happy |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
312 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
313 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
314 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
315 |
private int getIntOption(SocketOption<Integer> name) throws SocketException { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
316 |
try { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
317 |
return dc.getOption(name).intValue(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
318 |
} catch (IOException x) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
319 |
Net.translateToSocketException(x); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
320 |
return -1; // keep compiler happy |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
321 |
} |
2 | 322 |
} |
323 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
324 |
@Override |
2 | 325 |
public void setSendBufferSize(int size) throws SocketException { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
326 |
if (size <= 0) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
327 |
throw new IllegalArgumentException("Invalid send size"); |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
328 |
setIntOption(StandardSocketOptions.SO_SNDBUF, size); |
2 | 329 |
} |
330 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
331 |
@Override |
2 | 332 |
public int getSendBufferSize() throws SocketException { |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
333 |
return getIntOption(StandardSocketOptions.SO_SNDBUF); |
2 | 334 |
} |
335 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
336 |
@Override |
2 | 337 |
public void setReceiveBufferSize(int size) throws SocketException { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
338 |
if (size <= 0) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
339 |
throw new IllegalArgumentException("Invalid receive size"); |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
340 |
setIntOption(StandardSocketOptions.SO_RCVBUF, size); |
2 | 341 |
} |
342 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
343 |
@Override |
2 | 344 |
public int getReceiveBufferSize() throws SocketException { |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
345 |
return getIntOption(StandardSocketOptions.SO_RCVBUF); |
2 | 346 |
} |
347 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
348 |
@Override |
2 | 349 |
public void setReuseAddress(boolean on) throws SocketException { |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
350 |
setBooleanOption(StandardSocketOptions.SO_REUSEADDR, on); |
2 | 351 |
} |
352 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
353 |
@Override |
2 | 354 |
public boolean getReuseAddress() throws SocketException { |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
355 |
return getBooleanOption(StandardSocketOptions.SO_REUSEADDR); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
356 |
|
2 | 357 |
} |
358 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
359 |
@Override |
2 | 360 |
public void setBroadcast(boolean on) throws SocketException { |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
361 |
setBooleanOption(StandardSocketOptions.SO_BROADCAST, on); |
2 | 362 |
} |
363 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
364 |
@Override |
2 | 365 |
public boolean getBroadcast() throws SocketException { |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
366 |
return getBooleanOption(StandardSocketOptions.SO_BROADCAST); |
2 | 367 |
} |
368 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
369 |
@Override |
2 | 370 |
public void setTrafficClass(int tc) throws SocketException { |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
371 |
setIntOption(StandardSocketOptions.IP_TOS, tc); |
2 | 372 |
} |
373 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
374 |
@Override |
2 | 375 |
public int getTrafficClass() throws SocketException { |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
376 |
return getIntOption(StandardSocketOptions.IP_TOS); |
2 | 377 |
} |
378 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
379 |
@Override |
2 | 380 |
public void close() { |
381 |
try { |
|
382 |
dc.close(); |
|
383 |
} catch (IOException x) { |
|
384 |
throw new Error(x); |
|
385 |
} |
|
386 |
} |
|
387 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
388 |
@Override |
2 | 389 |
public boolean isClosed() { |
390 |
return !dc.isOpen(); |
|
391 |
} |
|
392 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
393 |
@Override |
2 | 394 |
public DatagramChannel getChannel() { |
395 |
return dc; |
|
396 |
} |
|
397 |
||
54620
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
398 |
@Override |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
399 |
public <T> DatagramSocket setOption(SocketOption<T> name, T value) throws IOException { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
400 |
dc.setOption(name, value); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
401 |
return this; |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
402 |
} |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
403 |
|
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
404 |
@Override |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
405 |
public <T> T getOption(SocketOption<T> name) throws IOException { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
406 |
return dc.getOption(name); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
407 |
} |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
408 |
|
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
409 |
@Override |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
410 |
public Set<SocketOption<?>> supportedOptions() { |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
411 |
return dc.supportedOptions(); |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
412 |
} |
13b67c1420b8
8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents:
49001
diff
changeset
|
413 |
|
2 | 414 |
/* |
415 |
* A dummy implementation of DatagramSocketImpl that can be passed to the |
|
416 |
* DatagramSocket constructor so that no native resources are allocated in |
|
417 |
* super class. |
|
418 |
*/ |
|
419 |
private static final DatagramSocketImpl dummyDatagramSocket |
|
420 |
= new DatagramSocketImpl() |
|
421 |
{ |
|
422 |
protected void create() throws SocketException {} |
|
423 |
||
424 |
protected void bind(int lport, InetAddress laddr) throws SocketException {} |
|
425 |
||
426 |
protected void send(DatagramPacket p) throws IOException {} |
|
427 |
||
428 |
protected int peek(InetAddress i) throws IOException { return 0; } |
|
429 |
||
430 |
protected int peekData(DatagramPacket p) throws IOException { return 0; } |
|
431 |
||
432 |
protected void receive(DatagramPacket p) throws IOException {} |
|
433 |
||
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9679
diff
changeset
|
434 |
@Deprecated |
2 | 435 |
protected void setTTL(byte ttl) throws IOException {} |
436 |
||
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9679
diff
changeset
|
437 |
@Deprecated |
2 | 438 |
protected byte getTTL() throws IOException { return 0; } |
439 |
||
440 |
protected void setTimeToLive(int ttl) throws IOException {} |
|
441 |
||
442 |
protected int getTimeToLive() throws IOException { return 0;} |
|
443 |
||
444 |
protected void join(InetAddress inetaddr) throws IOException {} |
|
445 |
||
446 |
protected void leave(InetAddress inetaddr) throws IOException {} |
|
447 |
||
448 |
protected void joinGroup(SocketAddress mcastaddr, |
|
449 |
NetworkInterface netIf) throws IOException {} |
|
450 |
||
451 |
protected void leaveGroup(SocketAddress mcastaddr, |
|
452 |
NetworkInterface netIf) throws IOException {} |
|
453 |
||
454 |
protected void close() {} |
|
455 |
||
456 |
public Object getOption(int optID) throws SocketException { return null;} |
|
457 |
||
458 |
public void setOption(int optID, Object value) throws SocketException {} |
|
459 |
}; |
|
460 |
} |