author | michaelm |
Wed, 28 May 2014 14:51:24 +0100 | |
changeset 27078 | 39275d6a8cac |
parent 25859 | 3317bb8137f4 |
child 28852 | a581c7868768 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
19069 | 2 |
* Copyright (c) 1996, 2013, 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 java.net; |
|
27 |
||
28 |
import java.io.FileDescriptor; |
|
29 |
import java.io.IOException; |
|
30 |
import java.io.InterruptedIOException; |
|
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
31 |
import java.util.Set; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
32 |
import java.util.HashSet; |
2 | 33 |
|
34 |
/** |
|
35 |
* Abstract datagram and multicast socket implementation base class. |
|
36 |
* @author Pavani Diwanji |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24046
diff
changeset
|
37 |
* @since 1.1 |
2 | 38 |
*/ |
39 |
||
40 |
public abstract class DatagramSocketImpl implements SocketOptions { |
|
41 |
||
42 |
/** |
|
43 |
* The local port number. |
|
44 |
*/ |
|
45 |
protected int localPort; |
|
46 |
||
47 |
/** |
|
48 |
* The file descriptor object. |
|
49 |
*/ |
|
50 |
protected FileDescriptor fd; |
|
51 |
||
52 |
/** |
|
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
53 |
* The DatagramSocket or MulticastSocket |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
54 |
* that owns this impl |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
55 |
*/ |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
56 |
DatagramSocket socket; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
57 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
58 |
void setDatagramSocket(DatagramSocket socket) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
59 |
this.socket = socket; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
60 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
61 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
62 |
DatagramSocket getDatagramSocket() { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
63 |
return socket; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
64 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
65 |
|
27078 | 66 |
int dataAvailable() { |
67 |
// default impl returns zero, which disables the calling |
|
68 |
// functionality |
|
69 |
return 0; |
|
70 |
} |
|
71 |
||
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
72 |
/** |
2 | 73 |
* Creates a datagram socket. |
74 |
* @exception SocketException if there is an error in the |
|
75 |
* underlying protocol, such as a TCP error. |
|
76 |
*/ |
|
77 |
protected abstract void create() throws SocketException; |
|
78 |
||
79 |
/** |
|
80 |
* Binds a datagram socket to a local port and address. |
|
81 |
* @param lport the local port |
|
82 |
* @param laddr the local address |
|
83 |
* @exception SocketException if there is an error in the |
|
84 |
* underlying protocol, such as a TCP error. |
|
85 |
*/ |
|
86 |
protected abstract void bind(int lport, InetAddress laddr) throws SocketException; |
|
87 |
||
88 |
/** |
|
89 |
* Sends a datagram packet. The packet contains the data and the |
|
90 |
* destination address to send the packet to. |
|
91 |
* @param p the packet to be sent. |
|
92 |
* @exception IOException if an I/O exception occurs while sending the |
|
93 |
* datagram packet. |
|
94 |
* @exception PortUnreachableException may be thrown if the socket is connected |
|
95 |
* to a currently unreachable destination. Note, there is no guarantee that |
|
96 |
* the exception will be thrown. |
|
97 |
*/ |
|
98 |
protected abstract void send(DatagramPacket p) throws IOException; |
|
99 |
||
100 |
/** |
|
101 |
* Connects a datagram socket to a remote destination. This associates the remote |
|
102 |
* address with the local socket so that datagrams may only be sent to this destination |
|
103 |
* and received from this destination. This may be overridden to call a native |
|
104 |
* system connect. |
|
105 |
* |
|
106 |
* <p>If the remote destination to which the socket is connected does not |
|
107 |
* exist, or is otherwise unreachable, and if an ICMP destination unreachable |
|
108 |
* packet has been received for that address, then a subsequent call to |
|
109 |
* send or receive may throw a PortUnreachableException. |
|
110 |
* Note, there is no guarantee that the exception will be thrown. |
|
111 |
* @param address the remote InetAddress to connect to |
|
112 |
* @param port the remote port number |
|
113 |
* @exception SocketException may be thrown if the socket cannot be |
|
114 |
* connected to the remote destination |
|
115 |
* @since 1.4 |
|
116 |
*/ |
|
117 |
protected void connect(InetAddress address, int port) throws SocketException {} |
|
118 |
||
119 |
/** |
|
120 |
* Disconnects a datagram socket from its remote destination. |
|
121 |
* @since 1.4 |
|
122 |
*/ |
|
123 |
protected void disconnect() {} |
|
124 |
||
125 |
/** |
|
19069 | 126 |
* Peek at the packet to see who it is from. Updates the specified {@code InetAddress} |
2 | 127 |
* to the address which the packet came from. |
128 |
* @param i an InetAddress object |
|
129 |
* @return the port number which the packet came from. |
|
130 |
* @exception IOException if an I/O exception occurs |
|
131 |
* @exception PortUnreachableException may be thrown if the socket is connected |
|
132 |
* to a currently unreachable destination. Note, there is no guarantee that the |
|
133 |
* exception will be thrown. |
|
134 |
*/ |
|
135 |
protected abstract int peek(InetAddress i) throws IOException; |
|
136 |
||
137 |
/** |
|
138 |
* Peek at the packet to see who it is from. The data is copied into the specified |
|
19069 | 139 |
* {@code DatagramPacket}. The data is returned, |
2 | 140 |
* but not consumed, so that a subsequent peekData/receive operation |
141 |
* will see the same data. |
|
142 |
* @param p the Packet Received. |
|
143 |
* @return the port number which the packet came from. |
|
144 |
* @exception IOException if an I/O exception occurs |
|
145 |
* @exception PortUnreachableException may be thrown if the socket is connected |
|
146 |
* to a currently unreachable destination. Note, there is no guarantee that the |
|
147 |
* exception will be thrown. |
|
148 |
* @since 1.4 |
|
149 |
*/ |
|
150 |
protected abstract int peekData(DatagramPacket p) throws IOException; |
|
151 |
/** |
|
152 |
* Receive the datagram packet. |
|
153 |
* @param p the Packet Received. |
|
154 |
* @exception IOException if an I/O exception occurs |
|
155 |
* while receiving the datagram packet. |
|
156 |
* @exception PortUnreachableException may be thrown if the socket is connected |
|
157 |
* to a currently unreachable destination. Note, there is no guarantee that the |
|
158 |
* exception will be thrown. |
|
159 |
*/ |
|
160 |
protected abstract void receive(DatagramPacket p) throws IOException; |
|
161 |
||
162 |
/** |
|
163 |
* Set the TTL (time-to-live) option. |
|
164 |
* @param ttl a byte specifying the TTL value |
|
165 |
* |
|
166 |
* @deprecated use setTimeToLive instead. |
|
167 |
* @exception IOException if an I/O exception occurs while setting |
|
168 |
* the time-to-live option. |
|
169 |
* @see #getTTL() |
|
170 |
*/ |
|
171 |
@Deprecated |
|
172 |
protected abstract void setTTL(byte ttl) throws IOException; |
|
173 |
||
174 |
/** |
|
175 |
* Retrieve the TTL (time-to-live) option. |
|
176 |
* |
|
177 |
* @exception IOException if an I/O exception occurs |
|
178 |
* while retrieving the time-to-live option |
|
179 |
* @deprecated use getTimeToLive instead. |
|
180 |
* @return a byte representing the TTL value |
|
181 |
* @see #setTTL(byte) |
|
182 |
*/ |
|
183 |
@Deprecated |
|
184 |
protected abstract byte getTTL() throws IOException; |
|
185 |
||
186 |
/** |
|
187 |
* Set the TTL (time-to-live) option. |
|
19069 | 188 |
* @param ttl an {@code int} specifying the time-to-live value |
2 | 189 |
* @exception IOException if an I/O exception occurs |
190 |
* while setting the time-to-live option. |
|
191 |
* @see #getTimeToLive() |
|
192 |
*/ |
|
193 |
protected abstract void setTimeToLive(int ttl) throws IOException; |
|
194 |
||
195 |
/** |
|
196 |
* Retrieve the TTL (time-to-live) option. |
|
197 |
* @exception IOException if an I/O exception occurs |
|
198 |
* while retrieving the time-to-live option |
|
19069 | 199 |
* @return an {@code int} representing the time-to-live value |
2 | 200 |
* @see #setTimeToLive(int) |
201 |
*/ |
|
202 |
protected abstract int getTimeToLive() throws IOException; |
|
203 |
||
204 |
/** |
|
205 |
* Join the multicast group. |
|
206 |
* @param inetaddr multicast address to join. |
|
207 |
* @exception IOException if an I/O exception occurs |
|
208 |
* while joining the multicast group. |
|
209 |
*/ |
|
210 |
protected abstract void join(InetAddress inetaddr) throws IOException; |
|
211 |
||
212 |
/** |
|
213 |
* Leave the multicast group. |
|
214 |
* @param inetaddr multicast address to leave. |
|
215 |
* @exception IOException if an I/O exception occurs |
|
216 |
* while leaving the multicast group. |
|
217 |
*/ |
|
218 |
protected abstract void leave(InetAddress inetaddr) throws IOException; |
|
219 |
||
220 |
/** |
|
221 |
* Join the multicast group. |
|
222 |
* @param mcastaddr address to join. |
|
223 |
* @param netIf specifies the local interface to receive multicast |
|
224 |
* datagram packets |
|
225 |
* @throws IOException if an I/O exception occurs while joining |
|
226 |
* the multicast group |
|
227 |
* @since 1.4 |
|
228 |
*/ |
|
229 |
protected abstract void joinGroup(SocketAddress mcastaddr, |
|
230 |
NetworkInterface netIf) |
|
231 |
throws IOException; |
|
232 |
||
233 |
/** |
|
234 |
* Leave the multicast group. |
|
235 |
* @param mcastaddr address to leave. |
|
236 |
* @param netIf specified the local interface to leave the group at |
|
237 |
* @throws IOException if an I/O exception occurs while leaving |
|
238 |
* the multicast group |
|
239 |
* @since 1.4 |
|
240 |
*/ |
|
241 |
protected abstract void leaveGroup(SocketAddress mcastaddr, |
|
242 |
NetworkInterface netIf) |
|
243 |
throws IOException; |
|
244 |
||
245 |
/** |
|
246 |
* Close the socket. |
|
247 |
*/ |
|
248 |
protected abstract void close(); |
|
249 |
||
250 |
/** |
|
251 |
* Gets the local port. |
|
19069 | 252 |
* @return an {@code int} representing the local port value |
2 | 253 |
*/ |
254 |
protected int getLocalPort() { |
|
255 |
return localPort; |
|
256 |
} |
|
257 |
||
258 |
/** |
|
259 |
* Gets the datagram socket file descriptor. |
|
19069 | 260 |
* @return a {@code FileDescriptor} object representing the datagram socket |
2 | 261 |
* file descriptor |
262 |
*/ |
|
263 |
protected FileDescriptor getFileDescriptor() { |
|
264 |
return fd; |
|
265 |
} |
|
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
266 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
267 |
/** |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
268 |
* Called to set a socket option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
269 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
270 |
* @param name The socket option |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
271 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
272 |
* @param value The value of the socket option. A value of {@code null} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
273 |
* may be valid for some options. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
274 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
275 |
* @throws UnsupportedOperationException if the DatagramSocketImpl does not |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
276 |
* support the option |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
277 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
278 |
* @throws NullPointerException if name is {@code null} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
279 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
280 |
* @since 1.9 |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
281 |
*/ |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
282 |
protected <T> void setOption(SocketOption<T> name, T value) throws IOException { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
283 |
if (name == StandardSocketOptions.SO_SNDBUF) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
284 |
setOption(SocketOptions.SO_SNDBUF, value); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
285 |
} else if (name == StandardSocketOptions.SO_RCVBUF) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
286 |
setOption(SocketOptions.SO_RCVBUF, value); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
287 |
} else if (name == StandardSocketOptions.SO_REUSEADDR) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
288 |
setOption(SocketOptions.SO_REUSEADDR, value); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
289 |
} else if (name == StandardSocketOptions.IP_TOS) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
290 |
setOption(SocketOptions.IP_TOS, value); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
291 |
} else if (name == StandardSocketOptions.IP_MULTICAST_IF && |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
292 |
(getDatagramSocket() instanceof MulticastSocket)) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
293 |
setOption(SocketOptions.IP_MULTICAST_IF2, value); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
294 |
} else if (name == StandardSocketOptions.IP_MULTICAST_TTL && |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
295 |
(getDatagramSocket() instanceof MulticastSocket)) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
296 |
if (! (value instanceof Integer)) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
297 |
throw new IllegalArgumentException("not an integer"); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
298 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
299 |
setTimeToLive((Integer)value); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
300 |
} else if (name == StandardSocketOptions.IP_MULTICAST_LOOP && |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
301 |
(getDatagramSocket() instanceof MulticastSocket)) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
302 |
setOption(SocketOptions.IP_MULTICAST_LOOP, value); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
303 |
} else { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
304 |
throw new UnsupportedOperationException("unsupported option"); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
305 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
306 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
307 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
308 |
/** |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
309 |
* Called to get a socket option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
310 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
311 |
* @param name The socket option |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
312 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
313 |
* @throws UnsupportedOperationException if the DatagramSocketImpl does not |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
314 |
* support the option |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
315 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
316 |
* @throws NullPointerException if name is {@code null} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
317 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
318 |
* @since 1.9 |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
319 |
*/ |
24046
d1f6da65881a
8041397: Lint regression in java.net.SocketOption
michaelm
parents:
23879
diff
changeset
|
320 |
@SuppressWarnings("unchecked") |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
321 |
protected <T> T getOption(SocketOption<T> name) throws IOException { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
322 |
if (name == StandardSocketOptions.SO_SNDBUF) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
323 |
return (T) getOption(SocketOptions.SO_SNDBUF); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
324 |
} else if (name == StandardSocketOptions.SO_RCVBUF) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
325 |
return (T) getOption(SocketOptions.SO_RCVBUF); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
326 |
} else if (name == StandardSocketOptions.SO_REUSEADDR) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
327 |
return (T) getOption(SocketOptions.SO_REUSEADDR); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
328 |
} else if (name == StandardSocketOptions.IP_TOS) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
329 |
return (T) getOption(SocketOptions.IP_TOS); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
330 |
} else if (name == StandardSocketOptions.IP_MULTICAST_IF && |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
331 |
(getDatagramSocket() instanceof MulticastSocket)) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
332 |
return (T) getOption(SocketOptions.IP_MULTICAST_IF2); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
333 |
} else if (name == StandardSocketOptions.IP_MULTICAST_TTL && |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
334 |
(getDatagramSocket() instanceof MulticastSocket)) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
335 |
Integer ttl = getTimeToLive(); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
336 |
return (T)ttl; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
337 |
} else if (name == StandardSocketOptions.IP_MULTICAST_LOOP && |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
338 |
(getDatagramSocket() instanceof MulticastSocket)) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
339 |
return (T) getOption(SocketOptions.IP_MULTICAST_LOOP); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
340 |
} else { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
341 |
throw new UnsupportedOperationException("unsupported option"); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
342 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
343 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
344 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
345 |
private static final Set<SocketOption<?>> dgSocketOptions = |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
346 |
new HashSet<>(); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
347 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
348 |
private static final Set<SocketOption<?>> mcSocketOptions = |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
349 |
new HashSet<>(); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
350 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
351 |
static { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
352 |
dgSocketOptions.add(StandardSocketOptions.SO_SNDBUF); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
353 |
dgSocketOptions.add(StandardSocketOptions.SO_RCVBUF); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
354 |
dgSocketOptions.add(StandardSocketOptions.SO_REUSEADDR); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
355 |
dgSocketOptions.add(StandardSocketOptions.IP_TOS); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
356 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
357 |
mcSocketOptions.add(StandardSocketOptions.SO_SNDBUF); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
358 |
mcSocketOptions.add(StandardSocketOptions.SO_RCVBUF); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
359 |
mcSocketOptions.add(StandardSocketOptions.SO_REUSEADDR); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
360 |
mcSocketOptions.add(StandardSocketOptions.IP_TOS); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
361 |
mcSocketOptions.add(StandardSocketOptions.IP_MULTICAST_IF); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
362 |
mcSocketOptions.add(StandardSocketOptions.IP_MULTICAST_TTL); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
363 |
mcSocketOptions.add(StandardSocketOptions.IP_MULTICAST_LOOP); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
364 |
}; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
365 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
366 |
/** |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
367 |
* Returns a set of SocketOptions supported by this impl |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
368 |
* and by this impl's socket (DatagramSocket or MulticastSocket) |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
369 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
370 |
* @return a Set of SocketOptions |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
371 |
*/ |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
372 |
protected Set<SocketOption<?>> supportedOptions() { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
373 |
if (getDatagramSocket() instanceof MulticastSocket) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
374 |
return mcSocketOptions; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
375 |
} else { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
376 |
return dgSocketOptions; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
377 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
378 |
} |
2 | 379 |
} |