PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/overlays/nio2/openjdk/jdk/src/share/classes/org/classpath/icedtea/java/net/StandardSocketOption.java

https://bitbucket.org/xiaoqiangnk/icedtea6
Java | 370 lines | 38 code | 23 blank | 309 comment | 0 complexity | a8d485768042856596a00ec7799d7184 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved.
  3. * Copyright 2009 Red Hat, Inc.
  4. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5. *
  6. * This code is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 only, as
  8. * published by the Free Software Foundation. Sun designates this
  9. * particular file as subject to the "Classpath" exception as provided
  10. * by Sun in the LICENSE file that accompanied this code.
  11. *
  12. * This code is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  15. * version 2 for more details (a copy is included in the LICENSE file that
  16. * accompanied this code).
  17. *
  18. * You should have received a copy of the GNU General Public License version
  19. * 2 along with this work; if not, write to the Free Software Foundation,
  20. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23. * CA 95054 USA or visit www.sun.com if you need additional information or
  24. * have any questions.
  25. */
  26. package org.classpath.icedtea.java.net;
  27. import java.net.NetworkInterface;
  28. /**
  29. * Defines the <em>standard</em> socket options.
  30. *
  31. * <p> The {@link SocketOption#name name} of each socket option defined by this
  32. * class is its field name.
  33. *
  34. * <p> In this release, the socket options defined here are used by {@link
  35. * java.nio.channels.NetworkChannel network} channels in the {@link
  36. * java.nio.channels channels} package.
  37. *
  38. * @since 1.7
  39. */
  40. public final class StandardSocketOption {
  41. private StandardSocketOption() { }
  42. // -- SOL_SOCKET --
  43. /**
  44. * Allow transmission of broadcast datagrams.
  45. *
  46. * <p> The value of this socket option is a {@code Boolean} that represents
  47. * whether the option is enabled or disabled. The option is specific to
  48. * datagram-oriented sockets sending to {@link java.net.Inet4Address IPv4}
  49. * broadcast addresses. When the socket option is enabled then the socket
  50. * can be used to send <em>broadcast datagrams</em>.
  51. *
  52. * <p> The initial value of this socket option is {@code FALSE}. The socket
  53. * option may be enabled or disabled at any time. Some operating systems may
  54. * require that the Java virtual machine be started with implementation
  55. * specific privileges to enable this option or send broadcast datagrams.
  56. *
  57. * @see <a href="http://www.ietf.org/rfc/rfc919.txt">RFC&nbsp;929:
  58. * Broadcasting Internet Datagrams</a>
  59. * @see DatagramSocket#setBroadcast
  60. */
  61. public static final SocketOption<Boolean> SO_BROADCAST =
  62. new StdSocketOption<Boolean>("SO_BROADCAST", Boolean.class);
  63. /**
  64. * Keep connection alive.
  65. *
  66. * <p> The value of this socket option is a {@code Boolean} that represents
  67. * whether the option is enabled or disabled. When the {@code SO_KEEPALIVE}
  68. * option is enabled the operating system may use a <em>keep-alive</em>
  69. * mechanism to periodically probe the other end of a connection when the
  70. * connection is otherwise idle. The exact semantics of the keep alive
  71. * mechanism is system dependent and therefore unspecified.
  72. *
  73. * <p> The initial value of this socket option is {@code FALSE}. The socket
  74. * option may be enabled or disabled at any time.
  75. *
  76. * @see <a href="http://www.ietf.org/rfc/rfc1122.txt">RFC&nbsp;1122
  77. * Requirements for Internet Hosts -- Communication Layers</a>
  78. * @see Socket#setKeepAlive
  79. */
  80. public static final SocketOption<Boolean> SO_KEEPALIVE =
  81. new StdSocketOption<Boolean>("SO_KEEPALIVE", Boolean.class);
  82. /**
  83. * The size of the socket send buffer.
  84. *
  85. * <p> The value of this socket option is an {@code Integer} that is the
  86. * size of the socket send buffer in bytes. The socket send buffer is an
  87. * output buffer used by the networking implementation. It may need to be
  88. * increased for high-volume connections. The value of the socket option is
  89. * a <em>hint</em> to the implementation to size the buffer and the actual
  90. * size may differ. The socket option can be queried to retrieve the actual
  91. * size.
  92. *
  93. * <p> For datagram-oriented sockets, the size of the send buffer may limit
  94. * the size of the datagrams that may be sent by the socket. Whether
  95. * datagrams larger than the buffer size are sent or discarded is system
  96. * dependent.
  97. *
  98. * <p> The initial/default size of the socket send buffer and the range of
  99. * allowable values is system dependent although a negative size is not
  100. * allowed. An attempt to set the socket send buffer to larger than its
  101. * maximum size causes it to be set to its maximum size.
  102. *
  103. * <p> An implementation allows this socket option to be set before the
  104. * socket is bound or connected. Whether an implementation allows the
  105. * socket send buffer to be changed after the socket is bound is system
  106. * dependent.
  107. *
  108. * @see Socket#setSendBufferSize
  109. */
  110. public static final SocketOption<Integer> SO_SNDBUF =
  111. new StdSocketOption<Integer>("SO_SNDBUF", Integer.class);
  112. /**
  113. * The size of the socket receive buffer.
  114. *
  115. * <p> The value of this socket option is an {@code Integer} that is the
  116. * size of the socket receive buffer in bytes. The socket receive buffer is
  117. * an input buffer used by the networking implementation. It may need to be
  118. * increased for high-volume connections or decreased to limit the possible
  119. * backlog of incoming data. The value of the socket option is a
  120. * <em>hint</em> to the implementation to size the buffer and the actual
  121. * size may differ.
  122. *
  123. * <p> For datagram-oriented sockets, the size of the receive buffer may
  124. * limit the size of the datagrams that can be received. Whether datagrams
  125. * larger than the buffer size can be received is system dependent.
  126. * Increasing the socket receive buffer may be important for cases where
  127. * datagrams arrive in bursts faster than they can be processed.
  128. *
  129. * <p> In the case of stream-oriented sockets and the TCP/IP protocol, the
  130. * size of the socket receive buffer may be used when advertising the size
  131. * of the TCP receive window to the remote peer.
  132. *
  133. * <p> The initial/default size of the socket receive buffer and the range
  134. * of allowable values is system dependent although a negative size is not
  135. * allowed. An attempt to set the socket receive buffer to larger than its
  136. * maximum size causes it to be set to its maximum size.
  137. *
  138. * <p> An implementation allows this socket option to be set before the
  139. * socket is bound or connected. Whether an implementation allows the
  140. * socket receive buffer to be changed after the socket is bound is system
  141. * dependent.
  142. *
  143. * @see <a href="http://www.ietf.org/rfc/rfc1323.txt">RFC&nbsp;1323: TCP
  144. * Extensions for High Performance</a>
  145. * @see Socket#setReceiveBufferSize
  146. * @see ServerSocket#setReceiveBufferSize
  147. */
  148. public static final SocketOption<Integer> SO_RCVBUF =
  149. new StdSocketOption<Integer>("SO_RCVBUF", Integer.class);
  150. /**
  151. * Re-use address.
  152. *
  153. * <p> The value of this socket option is a {@code Boolean} that represents
  154. * whether the option is enabled or disabled. The exact semantics of this
  155. * socket option are socket type and system dependent.
  156. *
  157. * <p> In the case of stream-oriented sockets, this socket option will
  158. * usually determine whether the socket can be bound to a socket address
  159. * when a previous connection involving that socket address is in the
  160. * <em>TIME_WAIT</em> state. On implementations where the semantics differ,
  161. * and the socket option is not required to be enabled in order to bind the
  162. * socket when a previous connection is in this state, then the
  163. * implementation may choose to ignore this option.
  164. *
  165. * <p> For datagram-oriented sockets the socket option is used to allow
  166. * multiple programs bind to the same address. This option should be enabled
  167. * when the socket is to be used for Internet Protocol (IP) multicasting.
  168. *
  169. * <p> An implementation allows this socket option to be set before the
  170. * socket is bound or connected. Changing the value of this socket option
  171. * after the socket is bound has no effect. The default value of this
  172. * socket option is system dependent.
  173. *
  174. * @see <a href="http://www.ietf.org/rfc/rfc793.txt">RFC&nbsp;793: Transmission
  175. * Control Protocol</a>
  176. * @see ServerSocket#setReuseAddress
  177. */
  178. public static final SocketOption<Boolean> SO_REUSEADDR =
  179. new StdSocketOption<Boolean>("SO_REUSEADDR", Boolean.class);
  180. /**
  181. * Linger on close if data is present.
  182. *
  183. * <p> The value of this socket option is an {@code Integer} that controls
  184. * the action taken when unsent data is queued on the socket and a method
  185. * to close the socket is invoked. If the value of the socket option is zero
  186. * or greater, then it represents a timeout value, in seconds, known as the
  187. * <em>linger interval</em>. The linger interval is the timeout for the
  188. * {@code close} method to block while the operating system attempts to
  189. * transmit the unsent data or it decides that it is unable to transmit the
  190. * data. If the value of the socket option is less than zero then the option
  191. * is disabled. In that case the {@code close} method does not wait until
  192. * unsent data is transmitted; if possible the operating system will transmit
  193. * any unsent data before the connection is closed.
  194. *
  195. * <p> This socket option is intended for use with sockets that are configured
  196. * in {@link java.nio.channels.SelectableChannel#isBlocking() blocking} mode
  197. * only. The behavior of the {@code close} method when this option is
  198. * enabled on a non-blocking socket is not defined.
  199. *
  200. * <p> The initial value of this socket option is a negative value, meaning
  201. * that the option is disabled. The option may be enabled, or the linger
  202. * interval changed, at any time. The maximum value of the linger interval
  203. * is system dependent. Setting the linger interval to a value that is
  204. * greater than its maximum value causes the linger interval to be set to
  205. * its maximum value.
  206. *
  207. * @see Socket#setSoLinger
  208. */
  209. public static final SocketOption<Integer> SO_LINGER =
  210. new StdSocketOption<Integer>("SO_LINGER", Integer.class);
  211. // -- IPPROTO_IP --
  212. /**
  213. * The Type of Service (ToS) octet in the Internet Protocol (IP) header.
  214. *
  215. * <p> The value of this socket option is an {@code Integer} representing
  216. * the value of the ToS octet in IP packets sent by sockets to an {@link
  217. * StandardProtocolFamily#INET IPv4} socket. The interpretation of the ToS
  218. * octet is network specific and is not defined by this class. Further
  219. * information on the ToS octet can be found in <a
  220. * href="http://www.ietf.org/rfc/rfc1349.txt">RFC&nbsp;1349</a> and <a
  221. * href="http://www.ietf.org/rfc/rfc2474.txt">RFC&nbsp;2474</a>. The value
  222. * of the socket option is a <em>hint</em>. An implementation may ignore the
  223. * value, or ignore specific values.
  224. *
  225. * <p> The initial/default value of the TOS field in the ToS octet is
  226. * implementation specific but will typically be {@code 0}. For
  227. * datagram-oriented sockets the option may be configured at any time after
  228. * the socket has been bound. The new value of the octet is used when sending
  229. * subsequent datagrams. It is system dependent whether this option can be
  230. * queried or changed prior to binding the socket.
  231. *
  232. * <p> The behavior of this socket option on a stream-oriented socket, or an
  233. * {@link StandardProtocolFamily#INET6 IPv6} socket, is not defined in this
  234. * release.
  235. *
  236. * @see DatagramSocket#setTrafficClass
  237. */
  238. public static final SocketOption<Integer> IP_TOS =
  239. new StdSocketOption<Integer>("IP_TOS", Integer.class);
  240. /**
  241. * The network interface for Internet Protocol (IP) multicast datagrams.
  242. *
  243. * <p> The value of this socket option is a {@link NetworkInterface} that
  244. * represents the outgoing interface for multicast datagrams sent by the
  245. * datagram-oriented socket. For {@link StandardProtocolFamily#INET6 IPv6}
  246. * sockets then it is system dependent whether setting this option also
  247. * sets the outgoing interface for multlicast datagrams sent to IPv4
  248. * addresses.
  249. *
  250. * <p> The initial/default value of this socket option may be {@code null}
  251. * to indicate that outgoing interface will be selected by the operating
  252. * system, typically based on the network routing tables. An implementation
  253. * allows this socket option to be set after the socket is bound. Whether
  254. * the socket option can be queried or changed prior to binding the socket
  255. * is system dependent.
  256. *
  257. * @see java.nio.channels.MulticastChannel
  258. * @see MulticastSocket#setInterface
  259. */
  260. public static final SocketOption<NetworkInterface> IP_MULTICAST_IF =
  261. new StdSocketOption<NetworkInterface>("IP_MULTICAST_IF", NetworkInterface.class);
  262. /**
  263. * The <em>time-to-live</em> for Internet Protocol (IP) multicast datagrams.
  264. *
  265. * <p> The value of this socket option is an {@code Integer} in the range
  266. * <tt>0&nbsp;<=&nbsp;value&nbsp;<=&nbsp;255</tt>. It is used to control
  267. * the scope of multicast datagrams sent by the datagram-oriented socket.
  268. * In the case of an {@link StandardProtocolFamily#INET IPv4} socket
  269. * the option is the time-to-live (TTL) on multicast datagrams sent by the
  270. * socket. Datagrams with a TTL of zero are not transmitted on the network
  271. * but may be delivered locally. In the case of an {@link
  272. * StandardProtocolFamily#INET6 IPv6} socket the option is the
  273. * <em>hop limit</em> which is number of <em>hops</em> that the datagram can
  274. * pass through before expiring on the network. For IPv6 sockets it is
  275. * system dependent whether the option also sets the <em>time-to-live</em>
  276. * on multicast datagrams sent to IPv4 addresses.
  277. *
  278. * <p> The initial/default value of the time-to-live setting is typically
  279. * {@code 1}. An implementation allows this socket option to be set after
  280. * the socket is bound. Whether the socket option can be queried or changed
  281. * prior to binding the socket is system dependent.
  282. *
  283. * @see java.nio.channels.MulticastChannel
  284. * @see MulticastSocket#setTimeToLive
  285. */
  286. public static final SocketOption<Integer> IP_MULTICAST_TTL =
  287. new StdSocketOption<Integer>("IP_MULTICAST_TTL", Integer.class);
  288. /**
  289. * Loopback for Internet Protocol (IP) multicast datagrams.
  290. *
  291. * <p> The value of this socket option is a {@code Boolean} that controls
  292. * the <em>loopback</em> of multicast datagrams. The value of the socket
  293. * option represents if the option is enabled or disabled.
  294. *
  295. * <p> The exact semantics of this socket options are system dependent.
  296. * In particular, it is system dependent whether the loopback applies to
  297. * multicast datagrams sent from the socket or received by the socket.
  298. * For {@link StandardProtocolFamily#INET6 IPv6} sockets then it is
  299. * system dependent whether the option also applies to multicast datagrams
  300. * sent to IPv4 addresses.
  301. *
  302. * <p> The initial/default value of this socket option is {@code TRUE}. An
  303. * implementation allows this socket option to be set after the socket is
  304. * bound. Whether the socket option can be queried or changed prior to
  305. * binding the socket is system dependent.
  306. *
  307. * @see java.nio.channels.MulticastChannel
  308. * @see MulticastSocket#setLoopbackMode
  309. */
  310. public static final SocketOption<Boolean> IP_MULTICAST_LOOP =
  311. new StdSocketOption<Boolean>("IP_MULTICAST_LOOP", Boolean.class);
  312. // -- IPPROTO_TCP --
  313. /**
  314. * Disable the Nagle algorithm.
  315. *
  316. * <p> The value of this socket option is a {@code Boolean} that represents
  317. * whether the option is enabled or disabled. The socket option is specific to
  318. * stream-oriented sockets using the TCP/IP protocol. TCP/IP uses an algorithm
  319. * known as <em>The Nagle Algorithm</em> to coalesce short segments and
  320. * improve network efficiency.
  321. *
  322. * <p> The default value of this socket option is {@code FALSE}. The
  323. * socket option should only be enabled in cases where it is known that the
  324. * coalescing impacts performance. The socket option may be enabled at any
  325. * time. In other words, the Nagle Algorithm can be disabled. Once the option
  326. * is enabled, it is system dependent whether it can be subsequently
  327. * disabled. If it cannot, then invoking the {@code setOption} method to
  328. * disable the option has no effect.
  329. *
  330. * @see <a href="http://www.ietf.org/rfc/rfc1122.txt">RFC&nbsp;1122:
  331. * Requirements for Internet Hosts -- Communication Layers</a>
  332. * @see Socket#setTcpNoDelay
  333. */
  334. public static final SocketOption<Boolean> TCP_NODELAY =
  335. new StdSocketOption<Boolean>("TCP_NODELAY", Boolean.class);
  336. private static class StdSocketOption<T> implements SocketOption<T> {
  337. private final String name;
  338. private final Class<T> type;
  339. StdSocketOption(String name, Class<T> type) {
  340. this.name = name;
  341. this.type = type;
  342. }
  343. public String name() { return name; }
  344. public Class<T> type() { return type; }
  345. public String toString() { return name; }
  346. }
  347. }