@Override public final <T> AsynchronousServerSocketChannel setOption(SocketOption<T> name, T value) throws IOException { if (name == null) throw new NullPointerException(); if (!supportedOptions().contains(name)) throw new UnsupportedOperationException("'" + name + "' not supported"); try { begin(); if (name == StandardSocketOptions.SO_REUSEADDR && Net.useExclusiveBind()) { // SO_REUSEADDR emulated when using exclusive bind isReuseAddress = (Boolean)value; } else { Net.setSocketOption(fd, Net.UNSPEC, name, value); } return this; } finally { end(); } }
@Override @SuppressWarnings("unchecked") public final <T> T getOption(SocketOption<T> name) throws IOException { if (name == null) throw new NullPointerException(); if (!supportedOptions().contains(name)) throw new UnsupportedOperationException("'" + name + "' not supported"); try { begin(); if (name == StandardSocketOptions.SO_REUSEADDR && Net.useExclusiveBind()) { // SO_REUSEADDR emulated when using exclusive bind return (T)Boolean.valueOf(isReuseAddress); } return (T) Net.getSocketOption(fd, Net.UNSPEC, name); } finally { end(); } }
@Override public final <T> AsynchronousSocketChannel setOption(SocketOption<T> name, T value) throws IOException { if (name == null) throw new NullPointerException(); if (!supportedOptions().contains(name)) throw new UnsupportedOperationException("'" + name + "' not supported"); try { begin(); if (writeShutdown) throw new IOException("Connection has been shutdown for writing"); if (name == StandardSocketOptions.SO_REUSEADDR && Net.useExclusiveBind()) { // SO_REUSEADDR emulated when using exclusive bind isReuseAddress = (Boolean)value; } else { Net.setSocketOption(fd, Net.UNSPEC, name, value); } return this; } finally { end(); } }
@Override public void setOption(FileDescriptor fd, SocketOption<?> option, Object value) throws SocketException { SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkPermission(new NetworkPermission("setOption." + option.name())); if (fd == null || !fd.valid()) throw new SocketException("socket closed"); if (option == SO_FLOW_SLA) { assert flowSupported; SocketFlow flow = checkValueType(value, option.type()); setFlowOption(fd, flow); } else { throw new InternalError("Unexpected option " + option); } }
@Override public Object getOption(FileDescriptor fd, SocketOption<?> option) throws SocketException { SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkPermission(new NetworkPermission("getOption." + option.name())); if (fd == null || !fd.valid()) throw new SocketException("socket closed"); if (option == SO_FLOW_SLA) { assert flowSupported; SocketFlow flow = SocketFlow.create(); getFlowOption(fd, flow); return flow; } else { throw new InternalError("Unexpected option " + option); } }
/** * Sets the supplied option on the channel to have the value if option is a member of * allowedOptions. * * @throws IOException * if the value could not be set due to IO errors. * @throws IllegalArgumentException * if the socket option or the value is invalid. * @throws UnsupportedOperationException * if the option is not a member of allowedOptions. * @throws ClosedChannelException * if the channel is closed */ public static <T> void setSocketOption( FileDescriptorChannel channel, Set<SocketOption<?>> allowedOptions, SocketOption<T> option, T value) throws IOException { if (!(option instanceof StandardSocketOptions.SocketOptionImpl)) { throw new IllegalArgumentException("SocketOption must come from StandardSocketOptions"); } if (!allowedOptions.contains(option)) { throw new UnsupportedOperationException( option + " is not supported for this type of socket"); } if (!channel.getFD().valid()) { throw new ClosedChannelException(); } ((StandardSocketOptions.SocketOptionImpl<T>) option).setValue(channel.getFD(), value); }
/** * Gets the supplied option from the channel if option is a member of allowedOptions. * * @throws IOException * if the value could not be read due to IO errors. * @throws IllegalArgumentException * if the socket option is invalid. * @throws UnsupportedOperationException * if the option is not a member of allowedOptions. * @throws ClosedChannelException * if the channel is closed */ public static <T> T getSocketOption( FileDescriptorChannel channel, Set<SocketOption<?>> allowedOptions, SocketOption<T> option) throws IOException { if (!(option instanceof StandardSocketOptions.SocketOptionImpl)) { throw new IllegalArgumentException("SocketOption must come from StandardSocketOptions"); } if (!allowedOptions.contains(option)) { throw new UnsupportedOperationException( option + " is not supported for this type of socket"); } if (!channel.getFD().valid()) { throw new ClosedChannelException(); } return ((StandardSocketOptions.SocketOptionImpl<T>) option).getValue(channel.getFD()); }
private static SocketOption<?> findOption(int optID) throws SocketException { switch (optID) { case TCP_NODELAY: return StandardSocketOptions.TCP_NODELAY; case SO_REUSEADDR: return StandardSocketOptions.SO_REUSEADDR; case SO_BROADCAST: return StandardSocketOptions.SO_BROADCAST; case IP_MULTICAST_IF: return StandardSocketOptions.IP_MULTICAST_IF; case IP_MULTICAST_LOOP: return StandardSocketOptions.IP_MULTICAST_LOOP; case IP_TOS: return StandardSocketOptions.IP_TOS; case SO_LINGER: return StandardSocketOptions.SO_LINGER; case SO_SNDBUF: return StandardSocketOptions.SO_SNDBUF; case SO_RCVBUF: return StandardSocketOptions.SO_RCVBUF; case SO_KEEPALIVE: return StandardSocketOptions.SO_KEEPALIVE; default: throw new SocketException("unrecognized TCP option: " + optID); } }
public <T> Socket setOption(SocketOption<T> name, T value) throws IOException { try { SSLSocket.class.getMethod("setOption", SocketOption.class, Object.class).invoke(delegate, name, value); return this; } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { throw new AssertionError(); } }