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