author | alanb |
Thu, 21 Mar 2019 08:41:10 +0000 | |
branch | niosocketimpl-branch |
changeset 57275 | 222fa5ed1c91 |
parent 57268 | adcdd45830a0 |
child 57301 | 66c3aaf31f2b |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
2 |
* Copyright (c) 1995, 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 java.net; |
|
27 |
||
57173 | 28 |
import java.io.FileDescriptor; |
2 | 29 |
import java.io.IOException; |
30 |
import java.io.InputStream; |
|
31 |
import java.io.OutputStream; |
|
57174 | 32 |
import java.security.AccessController; |
33 |
import java.security.PrivilegedAction; |
|
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
34 |
import java.util.Set; |
2 | 35 |
|
57174 | 36 |
import sun.net.NetProperties; |
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
37 |
import sun.net.PlatformSocketImpl; |
57167
82874527373e
Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents:
47478
diff
changeset
|
38 |
import sun.nio.ch.NioSocketImpl; |
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
39 |
|
2 | 40 |
/** |
19069 | 41 |
* The abstract class {@code SocketImpl} is a common superclass |
2 | 42 |
* of all classes that actually implement sockets. It is used to |
43 |
* create both client and server sockets. |
|
57275
222fa5ed1c91
Add @systemProperty ref to system property to configure old impl
alanb
parents:
57268
diff
changeset
|
44 |
* |
222fa5ed1c91
Add @systemProperty ref to system property to configure old impl
alanb
parents:
57268
diff
changeset
|
45 |
* @implNote The JDK historically used a {@code SocketImpl} implementation named |
222fa5ed1c91
Add @systemProperty ref to system property to configure old impl
alanb
parents:
57268
diff
changeset
|
46 |
* <em>PlainSocketImpl</em>. This has been replaced by a simpler and more modern |
222fa5ed1c91
Add @systemProperty ref to system property to configure old impl
alanb
parents:
57268
diff
changeset
|
47 |
* implementation but the JDK continues to ship with the older implementation |
222fa5ed1c91
Add @systemProperty ref to system property to configure old impl
alanb
parents:
57268
diff
changeset
|
48 |
* to allow code that depends on unspecified behavior, that differs between the |
222fa5ed1c91
Add @systemProperty ref to system property to configure old impl
alanb
parents:
57268
diff
changeset
|
49 |
* old and new implementations, to continue to run. The old implementation |
222fa5ed1c91
Add @systemProperty ref to system property to configure old impl
alanb
parents:
57268
diff
changeset
|
50 |
* will be used if the Java virtual machine is started with the system property |
222fa5ed1c91
Add @systemProperty ref to system property to configure old impl
alanb
parents:
57268
diff
changeset
|
51 |
* {@systemProperty jdk.net.usePlainSocketImpl} set on the command line. The |
222fa5ed1c91
Add @systemProperty ref to system property to configure old impl
alanb
parents:
57268
diff
changeset
|
52 |
* value of the system property is a boolean. If set without a value then it |
222fa5ed1c91
Add @systemProperty ref to system property to configure old impl
alanb
parents:
57268
diff
changeset
|
53 |
* defaults to {@code true}, hence running with {@code -Djdk.net.usePlainSocketImpl} |
222fa5ed1c91
Add @systemProperty ref to system property to configure old impl
alanb
parents:
57268
diff
changeset
|
54 |
* or {@code -Djdk.net.usePlainSocketImpl=true} will configure the Java virtual |
222fa5ed1c91
Add @systemProperty ref to system property to configure old impl
alanb
parents:
57268
diff
changeset
|
55 |
* machine use the old implementation. |
2 | 56 |
* |
57 |
* @author unascribed |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24046
diff
changeset
|
58 |
* @since 1.0 |
2 | 59 |
*/ |
60 |
public abstract class SocketImpl implements SocketOptions { |
|
57173 | 61 |
private static final boolean USE_PLAINSOCKETIMPL = usePlainSocketImpl(); |
62 |
||
63 |
private static boolean usePlainSocketImpl() { |
|
57177
0b5ebb227a8d
-Djdk.net.usePlainSocketImpl without value should use PlainSocketImpl
alanb
parents:
57176
diff
changeset
|
64 |
PrivilegedAction<String> pa = () -> NetProperties.get("jdk.net.usePlainSocketImpl"); |
0b5ebb227a8d
-Djdk.net.usePlainSocketImpl without value should use PlainSocketImpl
alanb
parents:
57176
diff
changeset
|
65 |
String s = AccessController.doPrivileged(pa); |
0b5ebb227a8d
-Djdk.net.usePlainSocketImpl without value should use PlainSocketImpl
alanb
parents:
57176
diff
changeset
|
66 |
return (s != null) && !s.equalsIgnoreCase("false"); |
57173 | 67 |
} |
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
68 |
|
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
69 |
/** |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
70 |
* Creates an instance of platform's SocketImpl |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
71 |
*/ |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
72 |
@SuppressWarnings("unchecked") |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
73 |
static <S extends SocketImpl & PlatformSocketImpl> S createPlatformSocketImpl(boolean server) { |
57173 | 74 |
if (USE_PLAINSOCKETIMPL) { |
57178
738431fa8fb2
Improve clarity of implAccept, align timeout messages with PSI
alanb
parents:
57177
diff
changeset
|
75 |
return (S) new PlainSocketImpl(); |
57173 | 76 |
} else { |
57178
738431fa8fb2
Improve clarity of implAccept, align timeout messages with PSI
alanb
parents:
57177
diff
changeset
|
77 |
return (S) new NioSocketImpl(server); |
57173 | 78 |
} |
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
79 |
} |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
80 |
|
2 | 81 |
/** |
82 |
* The actual Socket object. |
|
83 |
*/ |
|
84 |
Socket socket = null; |
|
85 |
ServerSocket serverSocket = null; |
|
86 |
||
87 |
/** |
|
88 |
* The file descriptor object for this socket. |
|
89 |
*/ |
|
90 |
protected FileDescriptor fd; |
|
91 |
||
92 |
/** |
|
93 |
* The IP address of the remote end of this socket. |
|
94 |
*/ |
|
95 |
protected InetAddress address; |
|
96 |
||
97 |
/** |
|
98 |
* The port number on the remote host to which this socket is connected. |
|
99 |
*/ |
|
100 |
protected int port; |
|
101 |
||
102 |
/** |
|
103 |
* The local port number to which this socket is connected. |
|
104 |
*/ |
|
105 |
protected int localport; |
|
106 |
||
107 |
/** |
|
108 |
* Creates either a stream or a datagram socket. |
|
109 |
* |
|
19069 | 110 |
* @param stream if {@code true}, create a stream socket; |
2 | 111 |
* otherwise, create a datagram socket. |
112 |
* @exception IOException if an I/O error occurs while creating the |
|
113 |
* socket. |
|
114 |
*/ |
|
115 |
protected abstract void create(boolean stream) throws IOException; |
|
116 |
||
117 |
/** |
|
118 |
* Connects this socket to the specified port on the named host. |
|
119 |
* |
|
120 |
* @param host the name of the remote host. |
|
121 |
* @param port the port number. |
|
122 |
* @exception IOException if an I/O error occurs when connecting to the |
|
123 |
* remote host. |
|
124 |
*/ |
|
125 |
protected abstract void connect(String host, int port) throws IOException; |
|
126 |
||
127 |
/** |
|
128 |
* Connects this socket to the specified port number on the specified host. |
|
129 |
* |
|
130 |
* @param address the IP address of the remote host. |
|
131 |
* @param port the port number. |
|
132 |
* @exception IOException if an I/O error occurs when attempting a |
|
133 |
* connection. |
|
134 |
*/ |
|
135 |
protected abstract void connect(InetAddress address, int port) throws IOException; |
|
136 |
||
137 |
/** |
|
138 |
* Connects this socket to the specified port number on the specified host. |
|
139 |
* A timeout of zero is interpreted as an infinite timeout. The connection |
|
140 |
* will then block until established or an error occurs. |
|
141 |
* |
|
142 |
* @param address the Socket address of the remote host. |
|
143 |
* @param timeout the timeout value, in milliseconds, or zero for no timeout. |
|
144 |
* @exception IOException if an I/O error occurs when attempting a |
|
145 |
* connection. |
|
146 |
* @since 1.4 |
|
147 |
*/ |
|
148 |
protected abstract void connect(SocketAddress address, int timeout) throws IOException; |
|
149 |
||
150 |
/** |
|
151 |
* Binds this socket to the specified local IP address and port number. |
|
152 |
* |
|
153 |
* @param host an IP address that belongs to a local interface. |
|
154 |
* @param port the port number. |
|
155 |
* @exception IOException if an I/O error occurs when binding this socket. |
|
156 |
*/ |
|
157 |
protected abstract void bind(InetAddress host, int port) throws IOException; |
|
158 |
||
159 |
/** |
|
160 |
* Sets the maximum queue length for incoming connection indications |
|
19069 | 161 |
* (a request to connect) to the {@code count} argument. If a |
2 | 162 |
* connection indication arrives when the queue is full, the |
163 |
* connection is refused. |
|
164 |
* |
|
165 |
* @param backlog the maximum length of the queue. |
|
166 |
* @exception IOException if an I/O error occurs when creating the queue. |
|
167 |
*/ |
|
168 |
protected abstract void listen(int backlog) throws IOException; |
|
169 |
||
170 |
/** |
|
171 |
* Accepts a connection. |
|
172 |
* |
|
173 |
* @param s the accepted connection. |
|
174 |
* @exception IOException if an I/O error occurs when accepting the |
|
175 |
* connection. |
|
176 |
*/ |
|
177 |
protected abstract void accept(SocketImpl s) throws IOException; |
|
178 |
||
179 |
/** |
|
180 |
* Returns an input stream for this socket. |
|
181 |
* |
|
182 |
* @return a stream for reading from this socket. |
|
183 |
* @exception IOException if an I/O error occurs when creating the |
|
184 |
* input stream. |
|
185 |
*/ |
|
186 |
protected abstract InputStream getInputStream() throws IOException; |
|
187 |
||
188 |
/** |
|
189 |
* Returns an output stream for this socket. |
|
190 |
* |
|
191 |
* @return an output stream for writing to this socket. |
|
192 |
* @exception IOException if an I/O error occurs when creating the |
|
193 |
* output stream. |
|
194 |
*/ |
|
195 |
protected abstract OutputStream getOutputStream() throws IOException; |
|
196 |
||
197 |
/** |
|
198 |
* Returns the number of bytes that can be read from this socket |
|
199 |
* without blocking. |
|
200 |
* |
|
201 |
* @return the number of bytes that can be read from this socket |
|
202 |
* without blocking. |
|
203 |
* @exception IOException if an I/O error occurs when determining the |
|
204 |
* number of bytes available. |
|
205 |
*/ |
|
206 |
protected abstract int available() throws IOException; |
|
207 |
||
208 |
/** |
|
209 |
* Closes this socket. |
|
210 |
* |
|
211 |
* @exception IOException if an I/O error occurs when closing this socket. |
|
212 |
*/ |
|
213 |
protected abstract void close() throws IOException; |
|
214 |
||
215 |
/** |
|
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
216 |
* Closes this socket, ignoring any IOException that is thrown by close. |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
217 |
*/ |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
218 |
void closeQuietly() { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
219 |
try { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
220 |
close(); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
221 |
} catch (IOException ignore) { } |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
222 |
} |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
223 |
|
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
224 |
/** |
2 | 225 |
* Places the input stream for this socket at "end of stream". |
226 |
* Any data sent to this socket is acknowledged and then |
|
227 |
* silently discarded. |
|
228 |
* |
|
10421
2ee16a0f6df5
7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
5506
diff
changeset
|
229 |
* If you read from a socket input stream after invoking this method on the |
2ee16a0f6df5
7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
5506
diff
changeset
|
230 |
* socket, the stream's {@code available} method will return 0, and its |
2ee16a0f6df5
7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
5506
diff
changeset
|
231 |
* {@code read} methods will return {@code -1} (end of stream). |
2 | 232 |
* |
233 |
* @exception IOException if an I/O error occurs when shutting down this |
|
234 |
* socket. |
|
235 |
* @see java.net.Socket#shutdownOutput() |
|
236 |
* @see java.net.Socket#close() |
|
237 |
* @see java.net.Socket#setSoLinger(boolean, int) |
|
238 |
* @since 1.3 |
|
239 |
*/ |
|
240 |
protected void shutdownInput() throws IOException { |
|
241 |
throw new IOException("Method not implemented!"); |
|
242 |
} |
|
243 |
||
244 |
/** |
|
245 |
* Disables the output stream for this socket. |
|
246 |
* For a TCP socket, any previously written data will be sent |
|
247 |
* followed by TCP's normal connection termination sequence. |
|
248 |
* |
|
249 |
* If you write to a socket output stream after invoking |
|
250 |
* shutdownOutput() on the socket, the stream will throw |
|
251 |
* an IOException. |
|
252 |
* |
|
253 |
* @exception IOException if an I/O error occurs when shutting down this |
|
254 |
* socket. |
|
255 |
* @see java.net.Socket#shutdownInput() |
|
256 |
* @see java.net.Socket#close() |
|
257 |
* @see java.net.Socket#setSoLinger(boolean, int) |
|
258 |
* @since 1.3 |
|
259 |
*/ |
|
260 |
protected void shutdownOutput() throws IOException { |
|
261 |
throw new IOException("Method not implemented!"); |
|
262 |
} |
|
263 |
||
264 |
/** |
|
19069 | 265 |
* Returns the value of this socket's {@code fd} field. |
2 | 266 |
* |
19069 | 267 |
* @return the value of this socket's {@code fd} field. |
2 | 268 |
* @see java.net.SocketImpl#fd |
269 |
*/ |
|
270 |
protected FileDescriptor getFileDescriptor() { |
|
271 |
return fd; |
|
272 |
} |
|
273 |
||
274 |
/** |
|
19069 | 275 |
* Returns the value of this socket's {@code address} field. |
2 | 276 |
* |
19069 | 277 |
* @return the value of this socket's {@code address} field. |
2 | 278 |
* @see java.net.SocketImpl#address |
279 |
*/ |
|
280 |
protected InetAddress getInetAddress() { |
|
281 |
return address; |
|
282 |
} |
|
283 |
||
284 |
/** |
|
19069 | 285 |
* Returns the value of this socket's {@code port} field. |
2 | 286 |
* |
19069 | 287 |
* @return the value of this socket's {@code port} field. |
2 | 288 |
* @see java.net.SocketImpl#port |
289 |
*/ |
|
290 |
protected int getPort() { |
|
291 |
return port; |
|
292 |
} |
|
293 |
||
294 |
/** |
|
295 |
* Returns whether or not this SocketImpl supports sending |
|
296 |
* urgent data. By default, false is returned |
|
297 |
* unless the method is overridden in a sub-class |
|
298 |
* |
|
299 |
* @return true if urgent data supported |
|
300 |
* @see java.net.SocketImpl#address |
|
301 |
* @since 1.4 |
|
302 |
*/ |
|
303 |
protected boolean supportsUrgentData () { |
|
304 |
return false; // must be overridden in sub-class |
|
305 |
} |
|
306 |
||
307 |
/** |
|
308 |
* Send one byte of urgent data on the socket. |
|
309 |
* The byte to be sent is the low eight bits of the parameter |
|
310 |
* @param data The byte of data to send |
|
311 |
* @exception IOException if there is an error |
|
312 |
* sending the data. |
|
313 |
* @since 1.4 |
|
314 |
*/ |
|
315 |
protected abstract void sendUrgentData (int data) throws IOException; |
|
316 |
||
317 |
/** |
|
19069 | 318 |
* Returns the value of this socket's {@code localport} field. |
2 | 319 |
* |
19069 | 320 |
* @return the value of this socket's {@code localport} field. |
2 | 321 |
* @see java.net.SocketImpl#localport |
322 |
*/ |
|
323 |
protected int getLocalPort() { |
|
324 |
return localport; |
|
325 |
} |
|
326 |
||
327 |
void setSocket(Socket soc) { |
|
328 |
this.socket = soc; |
|
329 |
} |
|
330 |
||
331 |
Socket getSocket() { |
|
332 |
return socket; |
|
333 |
} |
|
334 |
||
335 |
void setServerSocket(ServerSocket soc) { |
|
336 |
this.serverSocket = soc; |
|
337 |
} |
|
338 |
||
339 |
ServerSocket getServerSocket() { |
|
340 |
return serverSocket; |
|
341 |
} |
|
342 |
||
343 |
/** |
|
19069 | 344 |
* Returns the address and port of this socket as a {@code String}. |
2 | 345 |
* |
346 |
* @return a string representation of this socket. |
|
347 |
*/ |
|
348 |
public String toString() { |
|
349 |
return "Socket[addr=" + getInetAddress() + |
|
350 |
",port=" + getPort() + ",localport=" + getLocalPort() + "]"; |
|
351 |
} |
|
352 |
||
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
353 |
void reset() { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
354 |
fd = null; |
2 | 355 |
address = null; |
356 |
port = 0; |
|
357 |
localport = 0; |
|
358 |
} |
|
359 |
||
360 |
/** |
|
361 |
* Sets performance preferences for this socket. |
|
362 |
* |
|
363 |
* <p> Sockets use the TCP/IP protocol by default. Some implementations |
|
364 |
* may offer alternative protocols which have different performance |
|
365 |
* characteristics than TCP/IP. This method allows the application to |
|
366 |
* express its own preferences as to how these tradeoffs should be made |
|
367 |
* when the implementation chooses from the available protocols. |
|
368 |
* |
|
369 |
* <p> Performance preferences are described by three integers |
|
370 |
* whose values indicate the relative importance of short connection time, |
|
371 |
* low latency, and high bandwidth. The absolute values of the integers |
|
372 |
* are irrelevant; in order to choose a protocol the values are simply |
|
373 |
* compared, with larger values indicating stronger preferences. Negative |
|
374 |
* values represent a lower priority than positive values. If the |
|
375 |
* application prefers short connection time over both low latency and high |
|
376 |
* bandwidth, for example, then it could invoke this method with the values |
|
19069 | 377 |
* {@code (1, 0, 0)}. If the application prefers high bandwidth above low |
2 | 378 |
* latency, and low latency above short connection time, then it could |
19069 | 379 |
* invoke this method with the values {@code (0, 1, 2)}. |
2 | 380 |
* |
47478 | 381 |
* By default, this method does nothing, unless it is overridden in |
2 | 382 |
* a sub-class. |
383 |
* |
|
384 |
* @param connectionTime |
|
19069 | 385 |
* An {@code int} expressing the relative importance of a short |
2 | 386 |
* connection time |
387 |
* |
|
388 |
* @param latency |
|
19069 | 389 |
* An {@code int} expressing the relative importance of low |
2 | 390 |
* latency |
391 |
* |
|
392 |
* @param bandwidth |
|
19069 | 393 |
* An {@code int} expressing the relative importance of high |
2 | 394 |
* bandwidth |
395 |
* |
|
396 |
* @since 1.5 |
|
397 |
*/ |
|
398 |
protected void setPerformancePreferences(int connectionTime, |
|
399 |
int latency, |
|
400 |
int bandwidth) |
|
401 |
{ |
|
402 |
/* Not implemented yet */ |
|
403 |
} |
|
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
404 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
405 |
/** |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
406 |
* Called to set a socket option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
407 |
* |
28852
a581c7868768
8041395: Doclint regression in java.net.SocketOption
darcy
parents:
27341
diff
changeset
|
408 |
* @param <T> The type of the socket option value |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
409 |
* @param name The socket option |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
410 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
411 |
* @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
|
412 |
* may be valid for some options. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
413 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
414 |
* @throws UnsupportedOperationException if the SocketImpl does not |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
415 |
* support the option |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
416 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
417 |
* @throws IOException if an I/O error occurs, or if the socket is closed. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
418 |
* |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
34968
diff
changeset
|
419 |
* @since 9 |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
420 |
*/ |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
421 |
protected <T> void setOption(SocketOption<T> name, T value) throws IOException { |
34968
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
422 |
if (name == StandardSocketOptions.SO_KEEPALIVE && |
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
423 |
(getSocket() != null)) { |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
424 |
setOption(SocketOptions.SO_KEEPALIVE, value); |
34968
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
425 |
} else if (name == StandardSocketOptions.SO_SNDBUF && |
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
426 |
(getSocket() != null)) { |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
427 |
setOption(SocketOptions.SO_SNDBUF, value); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
428 |
} else if (name == StandardSocketOptions.SO_RCVBUF) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
429 |
setOption(SocketOptions.SO_RCVBUF, value); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
430 |
} else if (name == StandardSocketOptions.SO_REUSEADDR) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
431 |
setOption(SocketOptions.SO_REUSEADDR, value); |
36115 | 432 |
} else if (name == StandardSocketOptions.SO_REUSEPORT && |
433 |
supportedOptions().contains(name)) { |
|
434 |
setOption(SocketOptions.SO_REUSEPORT, value); |
|
34968
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
435 |
} else if (name == StandardSocketOptions.SO_LINGER && |
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
436 |
(getSocket() != null)) { |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
437 |
setOption(SocketOptions.SO_LINGER, value); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
438 |
} else if (name == StandardSocketOptions.IP_TOS) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
439 |
setOption(SocketOptions.IP_TOS, value); |
34968
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
440 |
} else if (name == StandardSocketOptions.TCP_NODELAY && |
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
441 |
(getSocket() != null)) { |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
442 |
setOption(SocketOptions.TCP_NODELAY, value); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
443 |
} else { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
444 |
throw new UnsupportedOperationException("unsupported option"); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
445 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
446 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
447 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
448 |
/** |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
449 |
* Called to get a socket option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
450 |
* |
28852
a581c7868768
8041395: Doclint regression in java.net.SocketOption
darcy
parents:
27341
diff
changeset
|
451 |
* @param <T> The type of the socket option value |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
452 |
* @param name The socket option |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
453 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
454 |
* @return the value of the named option |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
455 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
456 |
* @throws UnsupportedOperationException if the SocketImpl does not |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
457 |
* support the option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
458 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
459 |
* @throws IOException if an I/O error occurs, or if the socket is closed. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
460 |
* |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
34968
diff
changeset
|
461 |
* @since 9 |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
462 |
*/ |
24046
d1f6da65881a
8041397: Lint regression in java.net.SocketOption
michaelm
parents:
23879
diff
changeset
|
463 |
@SuppressWarnings("unchecked") |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
464 |
protected <T> T getOption(SocketOption<T> name) throws IOException { |
34968
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
465 |
if (name == StandardSocketOptions.SO_KEEPALIVE && |
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
466 |
(getSocket() != null)) { |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
467 |
return (T)getOption(SocketOptions.SO_KEEPALIVE); |
34968
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
468 |
} else if (name == StandardSocketOptions.SO_SNDBUF && |
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
469 |
(getSocket() != null)) { |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
470 |
return (T)getOption(SocketOptions.SO_SNDBUF); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
471 |
} else if (name == StandardSocketOptions.SO_RCVBUF) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
472 |
return (T)getOption(SocketOptions.SO_RCVBUF); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
473 |
} else if (name == StandardSocketOptions.SO_REUSEADDR) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
474 |
return (T)getOption(SocketOptions.SO_REUSEADDR); |
36115 | 475 |
} else if (name == StandardSocketOptions.SO_REUSEPORT && |
476 |
supportedOptions().contains(name)) { |
|
477 |
return (T)getOption(SocketOptions.SO_REUSEPORT); |
|
34968
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
478 |
} else if (name == StandardSocketOptions.SO_LINGER && |
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
479 |
(getSocket() != null)) { |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
480 |
return (T)getOption(SocketOptions.SO_LINGER); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
481 |
} else if (name == StandardSocketOptions.IP_TOS) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
482 |
return (T)getOption(SocketOptions.IP_TOS); |
34968
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
483 |
} else if (name == StandardSocketOptions.TCP_NODELAY && |
93b315c61ca3
8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents:
28852
diff
changeset
|
484 |
(getSocket() != null)) { |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
485 |
return (T)getOption(SocketOptions.TCP_NODELAY); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
486 |
} else { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
487 |
throw new UnsupportedOperationException("unsupported option"); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
488 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
489 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
490 |
|
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
491 |
/** |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
492 |
* Attempts to copy socket options from this SocketImpl to a target SocketImpl. |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
493 |
* At this time, only the SO_TIMEOUT make sense to copy. |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
494 |
*/ |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
495 |
void copyOptionsTo(SocketImpl target) { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
496 |
try { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
497 |
Object timeout = getOption(SocketOptions.SO_TIMEOUT); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
498 |
if (timeout instanceof Integer) { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
499 |
target.setOption(SocketOptions.SO_TIMEOUT, timeout); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
500 |
} |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
501 |
} catch (IOException ignore) { } |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
502 |
} |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
47478
diff
changeset
|
503 |
|
36228
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
504 |
private static final Set<SocketOption<?>> socketOptions; |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
505 |
|
36228
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
506 |
private static final Set<SocketOption<?>> serverSocketOptions; |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
507 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
508 |
static { |
36228
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
509 |
socketOptions = Set.of(StandardSocketOptions.SO_KEEPALIVE, |
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
510 |
StandardSocketOptions.SO_SNDBUF, |
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
511 |
StandardSocketOptions.SO_RCVBUF, |
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
512 |
StandardSocketOptions.SO_REUSEADDR, |
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
513 |
StandardSocketOptions.SO_LINGER, |
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
514 |
StandardSocketOptions.IP_TOS, |
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
515 |
StandardSocketOptions.TCP_NODELAY); |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
516 |
|
36228
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
517 |
serverSocketOptions = Set.of(StandardSocketOptions.SO_RCVBUF, |
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
518 |
StandardSocketOptions.SO_REUSEADDR, |
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
519 |
StandardSocketOptions.IP_TOS); |
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
520 |
} |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
521 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
522 |
/** |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
523 |
* 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
|
524 |
* and by this impl's socket (Socket or ServerSocket) |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
525 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
526 |
* @return a Set of SocketOptions |
36228
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
527 |
* |
a16a22218e23
8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents:
36115
diff
changeset
|
528 |
* @since 9 |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
529 |
*/ |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
530 |
protected Set<SocketOption<?>> supportedOptions() { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
531 |
if (getSocket() != null) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
532 |
return socketOptions; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
533 |
} else { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
534 |
return serverSocketOptions; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
535 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
19069
diff
changeset
|
536 |
} |
2 | 537 |
} |