[#819] Allow for easy method-chaining in ChannelConfig and its sub-types

This commit also introduce a new interface which is called AioSocketChannelConfig to expose AIO only config options with the right visibility.
Also it change the ChannelConfig.setAllocator(..) to return the ChannelConfig to be more consistent with the other methods.
This commit is contained in:
Norman Maurer 2012-12-14 16:59:23 +01:00
parent 6eb7de04e7
commit caa698f235
17 changed files with 625 additions and 331 deletions

View File

@ -115,7 +115,7 @@ public interface ChannelConfig {
* @param connectTimeoutMillis the connect timeout in milliseconds.
* {@code 0} to disable.
*/
void setConnectTimeoutMillis(int connectTimeoutMillis);
ChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis);
/**
* Returns the maximum loop count for a write operation until
@ -136,8 +136,17 @@ public interface ChannelConfig {
* @throws IllegalArgumentException
* if the specified value is {@code 0} or less than {@code 0}
*/
void setWriteSpinCount(int writeSpinCount);
ChannelConfig setWriteSpinCount(int writeSpinCount);
/**
* Returns {@link ByteBufAllocator} which is used for the channel
* to allocate buffers.
*/
ByteBufAllocator getAllocator();
ByteBufAllocator setAllocator(ByteBufAllocator bufferPool);
/**
* Set the {@link ByteBufAllocator} which is used for the channel
* to allocate buffers.
*/
ChannelConfig setAllocator(ByteBufAllocator allocator);
}

View File

@ -120,12 +120,13 @@ public class DefaultChannelConfig implements ChannelConfig {
}
@Override
public void setConnectTimeoutMillis(int connectTimeoutMillis) {
public ChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
if (connectTimeoutMillis < 0) {
throw new IllegalArgumentException(String.format(
"connectTimeoutMillis: %d (expected: >= 0)", connectTimeoutMillis));
}
this.connectTimeoutMillis = connectTimeoutMillis;
return this;
}
@Override
@ -134,12 +135,13 @@ public class DefaultChannelConfig implements ChannelConfig {
}
@Override
public void setWriteSpinCount(int writeSpinCount) {
public ChannelConfig setWriteSpinCount(int writeSpinCount) {
if (writeSpinCount <= 0) {
throw new IllegalArgumentException(
"writeSpinCount must be a positive integer.");
}
this.writeSpinCount = writeSpinCount;
return this;
}
@Override
@ -148,12 +150,11 @@ public class DefaultChannelConfig implements ChannelConfig {
}
@Override
public ByteBufAllocator setAllocator(ByteBufAllocator allocator) {
public ChannelConfig setAllocator(ByteBufAllocator allocator) {
if (allocator == null) {
throw new NullPointerException("allocator");
}
ByteBufAllocator oldAllocator = this.allocator;
this.allocator = allocator;
return oldAllocator;
return this;
}
}

View File

@ -15,6 +15,7 @@
*/
package io.netty.channel.socket;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelConfig;
import java.net.InetAddress;
@ -63,7 +64,7 @@ public interface DatagramChannelConfig extends ChannelConfig {
/**
* Sets the {@link StandardSocketOptions#SO_SNDBUF} option.
*/
void setSendBufferSize(int sendBufferSize);
DatagramChannelConfig setSendBufferSize(int sendBufferSize);
/**
* Gets the {@link StandardSocketOptions#SO_RCVBUF} option.
@ -73,11 +74,11 @@ public interface DatagramChannelConfig extends ChannelConfig {
/**
* Sets the {@link StandardSocketOptions#SO_RCVBUF} option.
*/
void setReceiveBufferSize(int receiveBufferSize);
DatagramChannelConfig setReceiveBufferSize(int receiveBufferSize);
int getReceivePacketSize();
void setReceivePacketSize(int receivePacketSize);
DatagramChannelConfig setReceivePacketSize(int receivePacketSize);
/**
* Gets the {@link StandardSocketOptions#IP_TOS} option.
@ -87,7 +88,7 @@ public interface DatagramChannelConfig extends ChannelConfig {
/**
* Sets the {@link StandardSocketOptions#IP_TOS} option.
*/
void setTrafficClass(int trafficClass);
DatagramChannelConfig setTrafficClass(int trafficClass);
/**
* Gets the {@link StandardSocketOptions#SO_REUSEADDR} option.
@ -97,7 +98,7 @@ public interface DatagramChannelConfig extends ChannelConfig {
/**
* Gets the {@link StandardSocketOptions#SO_REUSEADDR} option.
*/
void setReuseAddress(boolean reuseAddress);
DatagramChannelConfig setReuseAddress(boolean reuseAddress);
/**
* Gets the {@link StandardSocketOptions#SO_BROADCAST} option.
@ -107,7 +108,7 @@ public interface DatagramChannelConfig extends ChannelConfig {
/**
* Sets the {@link StandardSocketOptions#SO_BROADCAST} option.
*/
void setBroadcast(boolean broadcast);
DatagramChannelConfig setBroadcast(boolean broadcast);
/**
* Gets the {@link StandardSocketOptions#IP_MULTICAST_LOOP} option.
@ -122,7 +123,7 @@ public interface DatagramChannelConfig extends ChannelConfig {
* @param loopbackModeDisabled
* {@code true} if and only if the loopback mode has been disabled
*/
void setLoopbackModeDisabled(boolean loopbackModeDisabled);
DatagramChannelConfig setLoopbackModeDisabled(boolean loopbackModeDisabled);
/**
* Gets the {@link StandardSocketOptions#IP_MULTICAST_TTL} option.
@ -132,7 +133,7 @@ public interface DatagramChannelConfig extends ChannelConfig {
/**
* Sets the {@link StandardSocketOptions#IP_MULTICAST_TTL} option.
*/
void setTimeToLive(int ttl);
DatagramChannelConfig setTimeToLive(int ttl);
/**
* Gets the address of the network interface used for multicast packets.
@ -142,7 +143,7 @@ public interface DatagramChannelConfig extends ChannelConfig {
/**
* Sets the address of the network interface used for multicast packets.
*/
void setInterface(InetAddress interfaceAddress);
DatagramChannelConfig setInterface(InetAddress interfaceAddress);
/**
* Gets the {@link StandardSocketOptions#IP_MULTICAST_IF} option.
@ -152,5 +153,14 @@ public interface DatagramChannelConfig extends ChannelConfig {
/**
* Sets the {@link StandardSocketOptions#IP_MULTICAST_IF} option.
*/
void setNetworkInterface(NetworkInterface networkInterface);
DatagramChannelConfig setNetworkInterface(NetworkInterface networkInterface);
@Override
DatagramChannelConfig setWriteSpinCount(int writeSpinCount);
@Override
DatagramChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis);
@Override
DatagramChannelConfig setAllocator(ByteBufAllocator allocator);
}

View File

@ -15,6 +15,7 @@
*/
package io.netty.channel.socket;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelException;
import io.netty.channel.ChannelOption;
import io.netty.channel.DefaultChannelConfig;
@ -140,7 +141,7 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
}
@Override
public void setBroadcast(boolean broadcast) {
public DatagramChannelConfig setBroadcast(boolean broadcast) {
try {
// See: https://github.com/netty/netty/issues/576
if (broadcast &&
@ -159,6 +160,7 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
} catch (SocketException e) {
throw new ChannelException(e);
}
return this;
}
@Override
@ -175,7 +177,7 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
}
@Override
public void setInterface(InetAddress interfaceAddress) {
public DatagramChannelConfig setInterface(InetAddress interfaceAddress) {
if (socket instanceof MulticastSocket) {
try {
((MulticastSocket) socket).setInterface(interfaceAddress);
@ -185,6 +187,7 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
} else {
throw new UnsupportedOperationException();
}
return this;
}
@Override
@ -201,7 +204,7 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
}
@Override
public void setLoopbackModeDisabled(boolean loopbackModeDisabled) {
public DatagramChannelConfig setLoopbackModeDisabled(boolean loopbackModeDisabled) {
if (socket instanceof MulticastSocket) {
try {
((MulticastSocket) socket).setLoopbackMode(loopbackModeDisabled);
@ -211,6 +214,7 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
} else {
throw new UnsupportedOperationException();
}
return this;
}
@Override
@ -227,7 +231,7 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
}
@Override
public void setNetworkInterface(NetworkInterface networkInterface) {
public DatagramChannelConfig setNetworkInterface(NetworkInterface networkInterface) {
if (socket instanceof MulticastSocket) {
try {
((MulticastSocket) socket).setNetworkInterface(networkInterface);
@ -237,6 +241,7 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
} else {
throw new UnsupportedOperationException();
}
return this;
}
@Override
@ -249,12 +254,13 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
}
@Override
public void setReuseAddress(boolean reuseAddress) {
public DatagramChannelConfig setReuseAddress(boolean reuseAddress) {
try {
socket.setReuseAddress(reuseAddress);
} catch (SocketException e) {
throw new ChannelException(e);
}
return this;
}
@Override
@ -267,12 +273,13 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
}
@Override
public void setReceiveBufferSize(int receiveBufferSize) {
public DatagramChannelConfig setReceiveBufferSize(int receiveBufferSize) {
try {
socket.setReceiveBufferSize(receiveBufferSize);
} catch (SocketException e) {
throw new ChannelException(e);
}
return this;
}
@Override
@ -285,12 +292,13 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
}
@Override
public void setSendBufferSize(int sendBufferSize) {
public DatagramChannelConfig setSendBufferSize(int sendBufferSize) {
try {
socket.setSendBufferSize(sendBufferSize);
} catch (SocketException e) {
throw new ChannelException(e);
}
return this;
}
@Override
@ -299,12 +307,13 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
}
@Override
public void setReceivePacketSize(int receivePacketSize) {
public DatagramChannelConfig setReceivePacketSize(int receivePacketSize) {
if (receivePacketSize <= 0) {
throw new IllegalArgumentException(
String.format("receivePacketSize: %d (expected: > 0)", receivePacketSize));
}
this.receivePacketSize = receivePacketSize;
return this;
}
@Override
@ -321,7 +330,7 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
}
@Override
public void setTimeToLive(int ttl) {
public DatagramChannelConfig setTimeToLive(int ttl) {
if (socket instanceof MulticastSocket) {
try {
((MulticastSocket) socket).setTimeToLive(ttl);
@ -331,6 +340,7 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
} else {
throw new UnsupportedOperationException();
}
return this;
}
@Override
@ -343,11 +353,27 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
}
@Override
public void setTrafficClass(int trafficClass) {
public DatagramChannelConfig setTrafficClass(int trafficClass) {
try {
socket.setTrafficClass(trafficClass);
} catch (SocketException e) {
throw new ChannelException(e);
}
return this;
}
@Override
public DatagramChannelConfig setWriteSpinCount(int writeSpinCount) {
return (DatagramChannelConfig) super.setWriteSpinCount(writeSpinCount);
}
@Override
public DatagramChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
return (DatagramChannelConfig) super.setConnectTimeoutMillis(connectTimeoutMillis);
}
@Override
public DatagramChannelConfig setAllocator(ByteBufAllocator allocator) {
return (DatagramChannelConfig) super.setAllocator(allocator);
}
}

View File

@ -17,6 +17,7 @@ package io.netty.channel.socket;
import com.sun.nio.sctp.SctpChannel;
import com.sun.nio.sctp.SctpStandardSocketOptions;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelException;
import io.netty.channel.ChannelOption;
import io.netty.channel.DefaultChannelConfig;
@ -91,12 +92,13 @@ public class DefaultSctpChannelConfig extends DefaultChannelConfig implements Sc
}
@Override
public void setSctpNoDelay(boolean sctpNoDelay) {
public SctpChannelConfig setSctpNoDelay(boolean sctpNoDelay) {
try {
channel.setOption(SctpStandardSocketOptions.SCTP_NODELAY, sctpNoDelay);
} catch (IOException e) {
throw new ChannelException(e);
}
return this;
}
@Override
@ -109,12 +111,13 @@ public class DefaultSctpChannelConfig extends DefaultChannelConfig implements Sc
}
@Override
public void setSendBufferSize(int sendBufferSize) {
public SctpChannelConfig setSendBufferSize(int sendBufferSize) {
try {
channel.setOption(SctpStandardSocketOptions.SO_SNDBUF, sendBufferSize);
} catch (IOException e) {
throw new ChannelException(e);
}
return this;
}
@Override
@ -127,12 +130,13 @@ public class DefaultSctpChannelConfig extends DefaultChannelConfig implements Sc
}
@Override
public void setReceiveBufferSize(int receiveBufferSize) {
public SctpChannelConfig setReceiveBufferSize(int receiveBufferSize) {
try {
channel.setOption(SctpStandardSocketOptions.SO_RCVBUF, receiveBufferSize);
} catch (IOException e) {
throw new ChannelException(e);
}
return this;
}
@Override
@ -145,11 +149,27 @@ public class DefaultSctpChannelConfig extends DefaultChannelConfig implements Sc
}
@Override
public void setInitMaxStreams(SctpStandardSocketOptions.InitMaxStreams initMaxStreams) {
public SctpChannelConfig setInitMaxStreams(SctpStandardSocketOptions.InitMaxStreams initMaxStreams) {
try {
channel.setOption(SctpStandardSocketOptions.SCTP_INIT_MAXSTREAMS, initMaxStreams);
} catch (IOException e) {
throw new ChannelException(e);
}
return this;
}
@Override
public SctpChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
return (SctpChannelConfig) super.setConnectTimeoutMillis(connectTimeoutMillis);
}
@Override
public SctpChannelConfig setWriteSpinCount(int writeSpinCount) {
return (SctpChannelConfig) super.setWriteSpinCount(writeSpinCount);
}
@Override
public SctpChannelConfig setAllocator(ByteBufAllocator allocator) {
return (SctpChannelConfig) super.setAllocator(allocator);
}
}

View File

@ -17,6 +17,7 @@ package io.netty.channel.socket;
import com.sun.nio.sctp.SctpServerChannel;
import com.sun.nio.sctp.SctpStandardSocketOptions;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelException;
import io.netty.channel.ChannelOption;
import io.netty.channel.DefaultChannelConfig;
@ -93,12 +94,13 @@ public class DefaultSctpServerChannelConfig extends DefaultChannelConfig impleme
}
@Override
public void setSendBufferSize(int sendBufferSize) {
public SctpServerChannelConfig setSendBufferSize(int sendBufferSize) {
try {
serverChannel.setOption(SO_SNDBUF, sendBufferSize);
} catch (IOException e) {
throw new ChannelException(e);
}
return this;
}
@Override
@ -111,12 +113,13 @@ public class DefaultSctpServerChannelConfig extends DefaultChannelConfig impleme
}
@Override
public void setReceiveBufferSize(int receiveBufferSize) {
public SctpServerChannelConfig setReceiveBufferSize(int receiveBufferSize) {
try {
serverChannel.setOption(SO_RCVBUF, receiveBufferSize);
} catch (IOException e) {
throw new ChannelException(e);
}
return this;
}
@Override
@ -129,12 +132,13 @@ public class DefaultSctpServerChannelConfig extends DefaultChannelConfig impleme
}
@Override
public void setInitMaxStreams(SctpStandardSocketOptions.InitMaxStreams initMaxStreams) {
public SctpServerChannelConfig setInitMaxStreams(SctpStandardSocketOptions.InitMaxStreams initMaxStreams) {
try {
serverChannel.setOption(SCTP_INIT_MAXSTREAMS, initMaxStreams);
} catch (IOException e) {
throw new ChannelException(e);
}
return this;
}
@Override
@ -143,10 +147,26 @@ public class DefaultSctpServerChannelConfig extends DefaultChannelConfig impleme
}
@Override
public void setBacklog(int backlog) {
public SctpServerChannelConfig setBacklog(int backlog) {
if (backlog < 0) {
throw new IllegalArgumentException("backlog: " + backlog);
}
this.backlog = backlog;
return this;
}
@Override
public SctpServerChannelConfig setWriteSpinCount(int writeSpinCount) {
return (SctpServerChannelConfig) super.setWriteSpinCount(writeSpinCount);
}
@Override
public SctpServerChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
return (SctpServerChannelConfig) super.setConnectTimeoutMillis(connectTimeoutMillis);
}
@Override
public SctpServerChannelConfig setAllocator(ByteBufAllocator allocator) {
return (SctpServerChannelConfig) super.setAllocator(allocator);
}
}

View File

@ -16,6 +16,8 @@
package io.netty.channel.socket;
import static io.netty.channel.ChannelOption.*;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelException;
import io.netty.channel.ChannelOption;
import io.netty.channel.DefaultChannelConfig;
@ -92,12 +94,13 @@ public class DefaultServerSocketChannelConfig extends DefaultChannelConfig
}
@Override
public void setReuseAddress(boolean reuseAddress) {
public ServerSocketChannelConfig setReuseAddress(boolean reuseAddress) {
try {
socket.setReuseAddress(reuseAddress);
} catch (SocketException e) {
throw new ChannelException(e);
}
return this;
}
@Override
@ -110,17 +113,19 @@ public class DefaultServerSocketChannelConfig extends DefaultChannelConfig
}
@Override
public void setReceiveBufferSize(int receiveBufferSize) {
public ServerSocketChannelConfig setReceiveBufferSize(int receiveBufferSize) {
try {
socket.setReceiveBufferSize(receiveBufferSize);
} catch (SocketException e) {
throw new ChannelException(e);
}
return this;
}
@Override
public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
public ServerSocketChannelConfig setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
socket.setPerformancePreferences(connectionTime, latency, bandwidth);
return this;
}
@Override
@ -129,10 +134,26 @@ public class DefaultServerSocketChannelConfig extends DefaultChannelConfig
}
@Override
public void setBacklog(int backlog) {
public ServerSocketChannelConfig setBacklog(int backlog) {
if (backlog < 0) {
throw new IllegalArgumentException("backlog: " + backlog);
}
this.backlog = backlog;
return this;
}
@Override
public ServerSocketChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
return (ServerSocketChannelConfig) super.setConnectTimeoutMillis(connectTimeoutMillis);
}
@Override
public ServerSocketChannelConfig setWriteSpinCount(int writeSpinCount) {
return (ServerSocketChannelConfig) super.setWriteSpinCount(writeSpinCount);
}
@Override
public ServerSocketChannelConfig setAllocator(ByteBufAllocator allocator) {
return (ServerSocketChannelConfig) super.setAllocator(allocator);
}
}

View File

@ -16,6 +16,8 @@
package io.netty.channel.socket;
import static io.netty.channel.ChannelOption.*;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelException;
import io.netty.channel.ChannelOption;
import io.netty.channel.DefaultChannelConfig;
@ -173,49 +175,54 @@ public class DefaultSocketChannelConfig extends DefaultChannelConfig
}
@Override
public void setKeepAlive(boolean keepAlive) {
public SocketChannelConfig setKeepAlive(boolean keepAlive) {
try {
socket.setKeepAlive(keepAlive);
} catch (SocketException e) {
throw new ChannelException(e);
}
return this;
}
@Override
public void setPerformancePreferences(
public SocketChannelConfig setPerformancePreferences(
int connectionTime, int latency, int bandwidth) {
socket.setPerformancePreferences(connectionTime, latency, bandwidth);
return this;
}
@Override
public void setReceiveBufferSize(int receiveBufferSize) {
public SocketChannelConfig setReceiveBufferSize(int receiveBufferSize) {
try {
socket.setReceiveBufferSize(receiveBufferSize);
} catch (SocketException e) {
throw new ChannelException(e);
}
return this;
}
@Override
public void setReuseAddress(boolean reuseAddress) {
public SocketChannelConfig setReuseAddress(boolean reuseAddress) {
try {
socket.setReuseAddress(reuseAddress);
} catch (SocketException e) {
throw new ChannelException(e);
}
return this;
}
@Override
public void setSendBufferSize(int sendBufferSize) {
public SocketChannelConfig setSendBufferSize(int sendBufferSize) {
try {
socket.setSendBufferSize(sendBufferSize);
} catch (SocketException e) {
throw new ChannelException(e);
}
return this;
}
@Override
public void setSoLinger(int soLinger) {
public SocketChannelConfig setSoLinger(int soLinger) {
try {
if (soLinger < 0) {
socket.setSoLinger(false, 0);
@ -225,24 +232,27 @@ public class DefaultSocketChannelConfig extends DefaultChannelConfig
} catch (SocketException e) {
throw new ChannelException(e);
}
return this;
}
@Override
public void setTcpNoDelay(boolean tcpNoDelay) {
public SocketChannelConfig setTcpNoDelay(boolean tcpNoDelay) {
try {
socket.setTcpNoDelay(tcpNoDelay);
} catch (SocketException e) {
throw new ChannelException(e);
}
return this;
}
@Override
public void setTrafficClass(int trafficClass) {
public SocketChannelConfig setTrafficClass(int trafficClass) {
try {
socket.setTrafficClass(trafficClass);
} catch (SocketException e) {
throw new ChannelException(e);
}
return this;
}
@Override
@ -251,7 +261,23 @@ public class DefaultSocketChannelConfig extends DefaultChannelConfig
}
@Override
public void setAllowHalfClosure(boolean allowHalfClosure) {
public SocketChannelConfig setAllowHalfClosure(boolean allowHalfClosure) {
this.allowHalfClosure = allowHalfClosure;
return this;
}
@Override
public SocketChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
return (SocketChannelConfig) super.setConnectTimeoutMillis(connectTimeoutMillis);
}
@Override
public SocketChannelConfig setWriteSpinCount(int writeSpinCount) {
return (SocketChannelConfig) super.setWriteSpinCount(writeSpinCount);
}
@Override
public SocketChannelConfig setAllocator(ByteBufAllocator allocator) {
return (SocketChannelConfig) super.setAllocator(allocator);
}
}

View File

@ -15,6 +15,7 @@
*/
package io.netty.channel.socket;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelConfig;
import static com.sun.nio.sctp.SctpStandardSocketOptions.InitMaxStreams;
@ -54,7 +55,7 @@ public interface SctpChannelConfig extends ChannelConfig {
* Sets the <a href="http://openjdk.java.net/projects/sctp/javadoc/com/sun/nio/sctp/SctpStandardSocketOption.html">
* {@code SCTP_NODELAY}</a> option.
*/
void setSctpNoDelay(boolean sctpNoDelay);
SctpChannelConfig setSctpNoDelay(boolean sctpNoDelay);
/**
* Gets the <a href="http://openjdk.java.net/projects/sctp/javadoc/com/sun/nio/sctp/SctpStandardSocketOption.html">
@ -66,7 +67,7 @@ public interface SctpChannelConfig extends ChannelConfig {
* Sets the <a href="http://openjdk.java.net/projects/sctp/javadoc/com/sun/nio/sctp/SctpStandardSocketOption.html">
* {@code SO_SNDBUF}</a> option.
*/
void setSendBufferSize(int sendBufferSize);
SctpChannelConfig setSendBufferSize(int sendBufferSize);
/**
* Gets the <a href="http://openjdk.java.net/projects/sctp/javadoc/com/sun/nio/sctp/SctpStandardSocketOption.html">
@ -78,7 +79,7 @@ public interface SctpChannelConfig extends ChannelConfig {
* Gets the <a href="http://openjdk.java.net/projects/sctp/javadoc/com/sun/nio/sctp/SctpStandardSocketOption.html">
* {@code SO_RCVBUF}</a> option.
*/
void setReceiveBufferSize(int receiveBufferSize);
SctpChannelConfig setReceiveBufferSize(int receiveBufferSize);
/**
* Gets the <a href="http://openjdk.java.net/projects/sctp/javadoc/com/sun/nio/sctp/SctpStandardSocketOption.html">
@ -90,5 +91,14 @@ public interface SctpChannelConfig extends ChannelConfig {
* Gets the <a href="http://openjdk.java.net/projects/sctp/javadoc/com/sun/nio/sctp/SctpStandardSocketOption.html">
* {@code SCTP_INIT_MAXSTREAMS}</a> option.
*/
void setInitMaxStreams(InitMaxStreams initMaxStreams);
SctpChannelConfig setInitMaxStreams(InitMaxStreams initMaxStreams);
@Override
SctpChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis);
@Override
SctpChannelConfig setWriteSpinCount(int writeSpinCount);
@Override
SctpChannelConfig setAllocator(ByteBufAllocator allocator);
}

View File

@ -15,6 +15,7 @@
*/
package io.netty.channel.socket;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelConfig;
import static com.sun.nio.sctp.SctpStandardSocketOptions.InitMaxStreams;
@ -53,7 +54,7 @@ public interface SctpServerChannelConfig extends ChannelConfig {
/**
* Sets the backlog value to specify when the channel binds to a local address.
*/
void setBacklog(int backlog);
SctpServerChannelConfig setBacklog(int backlog);
/**
* Gets the <a href="http://openjdk.java.net/projects/sctp/javadoc/com/sun/nio/sctp/SctpStandardSocketOption.html">
@ -65,7 +66,7 @@ public interface SctpServerChannelConfig extends ChannelConfig {
* Sets the <a href="http://openjdk.java.net/projects/sctp/javadoc/com/sun/nio/sctp/SctpStandardSocketOption.html">
* {@code SO_SNDBUF}</a> option.
*/
void setSendBufferSize(int sendBufferSize);
SctpServerChannelConfig setSendBufferSize(int sendBufferSize);
/**
* Gets the <a href="http://openjdk.java.net/projects/sctp/javadoc/com/sun/nio/sctp/SctpStandardSocketOption.html">
@ -77,7 +78,7 @@ public interface SctpServerChannelConfig extends ChannelConfig {
* Gets the <a href="http://openjdk.java.net/projects/sctp/javadoc/com/sun/nio/sctp/SctpStandardSocketOption.html">
* {@code SO_RCVBUF}</a> option.
*/
void setReceiveBufferSize(int receiveBufferSize);
SctpServerChannelConfig setReceiveBufferSize(int receiveBufferSize);
/**
* Gets the <a href="http://openjdk.java.net/projects/sctp/javadoc/com/sun/nio/sctp/SctpStandardSocketOption.html">
@ -89,5 +90,14 @@ public interface SctpServerChannelConfig extends ChannelConfig {
* Gets the <a href="http://openjdk.java.net/projects/sctp/javadoc/com/sun/nio/sctp/SctpStandardSocketOption.html">
* {@code SCTP_INIT_MAXSTREAMS}</a> option.
*/
void setInitMaxStreams(InitMaxStreams initMaxStreams);
SctpServerChannelConfig setInitMaxStreams(InitMaxStreams initMaxStreams);
@Override
SctpServerChannelConfig setWriteSpinCount(int writeSpinCount);
@Override
SctpServerChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis);
@Override
SctpServerChannelConfig setAllocator(ByteBufAllocator allocator);
}

View File

@ -15,6 +15,7 @@
*/
package io.netty.channel.socket;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelConfig;
import java.net.ServerSocket;
@ -53,7 +54,7 @@ public interface ServerSocketChannelConfig extends ChannelConfig {
* Sets the backlog value to specify when the channel binds to a local
* address.
*/
void setBacklog(int backlog);
ServerSocketChannelConfig setBacklog(int backlog);
/**
* Gets the {@link StandardSocketOptions#SO_REUSEADDR} option.
@ -63,7 +64,7 @@ public interface ServerSocketChannelConfig extends ChannelConfig {
/**
* Sets the {@link StandardSocketOptions#SO_REUSEADDR} option.
*/
void setReuseAddress(boolean reuseAddress);
ServerSocketChannelConfig setReuseAddress(boolean reuseAddress);
/**
* Gets the {@link StandardSocketOptions#SO_RCVBUF} option.
@ -73,11 +74,20 @@ public interface ServerSocketChannelConfig extends ChannelConfig {
/**
* Gets the {@link StandardSocketOptions#SO_SNDBUF} option.
*/
void setReceiveBufferSize(int receiveBufferSize);
ServerSocketChannelConfig setReceiveBufferSize(int receiveBufferSize);
/**
* Sets the performance preferences as specified in
* {@link ServerSocket#setPerformancePreferences(int, int, int)}.
*/
void setPerformancePreferences(int connectionTime, int latency, int bandwidth);
ServerSocketChannelConfig setPerformancePreferences(int connectionTime, int latency, int bandwidth);
@Override
ServerSocketChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis);
@Override
ServerSocketChannelConfig setWriteSpinCount(int writeSpinCount);
@Override
ServerSocketChannelConfig setAllocator(ByteBufAllocator allocator);
}

View File

@ -15,6 +15,7 @@
*/
package io.netty.channel.socket;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelConfig;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
@ -61,7 +62,7 @@ public interface SocketChannelConfig extends ChannelConfig {
/**
* Sets the {@link StandardSocketOptions#TCP_NODELAY} option.
*/
void setTcpNoDelay(boolean tcpNoDelay);
SocketChannelConfig setTcpNoDelay(boolean tcpNoDelay);
/**
* Gets the {@link StandardSocketOptions#SO_LINGER} option.
@ -71,7 +72,7 @@ public interface SocketChannelConfig extends ChannelConfig {
/**
* Sets the {@link StandardSocketOptions#SO_LINGER} option.
*/
void setSoLinger(int soLinger);
SocketChannelConfig setSoLinger(int soLinger);
/**
* Gets the {@link StandardSocketOptions#SO_SNDBUF} option.
@ -81,7 +82,7 @@ public interface SocketChannelConfig extends ChannelConfig {
/**
* Sets the {@link StandardSocketOptions#SO_SNDBUF} option.
*/
void setSendBufferSize(int sendBufferSize);
SocketChannelConfig setSendBufferSize(int sendBufferSize);
/**
* Gets the {@link StandardSocketOptions#SO_RCVBUF} option.
@ -91,7 +92,7 @@ public interface SocketChannelConfig extends ChannelConfig {
/**
* Sets the {@link StandardSocketOptions#SO_RCVBUF} option.
*/
void setReceiveBufferSize(int receiveBufferSize);
SocketChannelConfig setReceiveBufferSize(int receiveBufferSize);
/**
* Gets the {@link StandardSocketOptions#SO_KEEPALIVE} option.
@ -101,7 +102,7 @@ public interface SocketChannelConfig extends ChannelConfig {
/**
* Sets the {@link StandardSocketOptions#SO_KEEPALIVE} option.
*/
void setKeepAlive(boolean keepAlive);
SocketChannelConfig setKeepAlive(boolean keepAlive);
/**
* Gets the {@link StandardSocketOptions#IP_TOS} option.
@ -111,7 +112,7 @@ public interface SocketChannelConfig extends ChannelConfig {
/**
* Sets the {@link StandardSocketOptions#IP_TOS} option.
*/
void setTrafficClass(int trafficClass);
SocketChannelConfig setTrafficClass(int trafficClass);
/**
* Gets the {@link StandardSocketOptions#SO_REUSEADDR} option.
@ -121,13 +122,13 @@ public interface SocketChannelConfig extends ChannelConfig {
/**
* Sets the {@link StandardSocketOptions#SO_REUSEADDR} option.
*/
void setReuseAddress(boolean reuseAddress);
SocketChannelConfig setReuseAddress(boolean reuseAddress);
/**
* Sets the performance preferences as specified in
* {@link Socket#setPerformancePreferences(int, int, int)}.
*/
void setPerformancePreferences(int connectionTime, int latency, int bandwidth);
SocketChannelConfig setPerformancePreferences(int connectionTime, int latency, int bandwidth);
/**
* Returns {@code true} if and only if the channel should not close itself when its remote
@ -143,5 +144,14 @@ public interface SocketChannelConfig extends ChannelConfig {
* is invoked with a {@link ChannelInputShutdownEvent} object. If {@code false}, the connection
* is closed automatically.
*/
void setAllowHalfClosure(boolean allowHalfClosure);
SocketChannelConfig setAllowHalfClosure(boolean allowHalfClosure);
@Override
SocketChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis);
@Override
SocketChannelConfig setWriteSpinCount(int writeSpinCount);
@Override
SocketChannelConfig setAllocator(ByteBufAllocator allocator);
}

View File

@ -16,6 +16,8 @@
package io.netty.channel.socket.aio;
import static io.netty.channel.ChannelOption.*;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelException;
import io.netty.channel.ChannelOption;
import io.netty.channel.DefaultChannelConfig;
@ -94,8 +96,9 @@ final class AioServerSocketChannelConfig extends DefaultChannelConfig
}
@Override
public void setReuseAddress(boolean reuseAddress) {
public ServerSocketChannelConfig setReuseAddress(boolean reuseAddress) {
setOption(StandardSocketOptions.SO_REUSEADDR, reuseAddress);
return this;
}
@Override
@ -104,12 +107,13 @@ final class AioServerSocketChannelConfig extends DefaultChannelConfig
}
@Override
public void setReceiveBufferSize(int receiveBufferSize) {
public ServerSocketChannelConfig setReceiveBufferSize(int receiveBufferSize) {
setOption(StandardSocketOptions.SO_RCVBUF, receiveBufferSize);
return this;
}
@Override
public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
public ServerSocketChannelConfig setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
throw new UnsupportedOperationException();
}
@ -119,11 +123,12 @@ final class AioServerSocketChannelConfig extends DefaultChannelConfig
}
@Override
public void setBacklog(int backlog) {
public ServerSocketChannelConfig setBacklog(int backlog) {
if (backlog < 0) {
throw new IllegalArgumentException("backlog: " + backlog);
}
this.backlog = backlog;
return this;
}
private Object getOption(SocketOption option, Object defaultValue) {
@ -178,4 +183,19 @@ final class AioServerSocketChannelConfig extends DefaultChannelConfig
// not needed anymore
options = null;
}
@Override
public ServerSocketChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
return (ServerSocketChannelConfig) super.setConnectTimeoutMillis(connectTimeoutMillis);
}
@Override
public ServerSocketChannelConfig setWriteSpinCount(int writeSpinCount) {
return (ServerSocketChannelConfig) super.setWriteSpinCount(writeSpinCount);
}
@Override
public ServerSocketChannelConfig setAllocator(ByteBufAllocator allocator) {
return (ServerSocketChannelConfig) super.setAllocator(allocator);
}
}

View File

@ -26,7 +26,6 @@ import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoop;
import io.netty.channel.FileRegion;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.SocketChannelConfig;
import java.io.IOException;
import java.net.InetSocketAddress;
@ -60,7 +59,7 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne
}
}
private AioSocketChannelConfig config;
private DefaultAioSocketChannelConfig config;
private volatile boolean inputShutdown;
private volatile boolean outputShutdown;
@ -85,7 +84,7 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne
AioSocketChannel(
AioServerSocketChannel parent, Integer id, AsynchronousSocketChannel ch) {
super(parent, id, ch);
config = new AioSocketChannelConfig(ch);
config = new DefaultAioSocketChannelConfig(ch);
}
@Override
@ -523,7 +522,7 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne
}
@Override
public SocketChannelConfig config() {
public AioSocketChannelConfig config() {
return config;
}

View File

@ -15,228 +15,46 @@
*/
package io.netty.channel.socket.aio;
import io.netty.channel.ChannelException;
import io.netty.channel.ChannelOption;
import io.netty.channel.DefaultChannelConfig;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.socket.SocketChannelConfig;
import java.io.IOException;
import java.net.SocketOption;
import java.net.StandardSocketOptions;
import java.nio.channels.InterruptedByTimeoutException;
import java.nio.channels.NetworkChannel;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import static io.netty.channel.ChannelOption.*;
/**
* The default {@link SocketChannelConfig} implementation.
*/
final class AioSocketChannelConfig extends DefaultChannelConfig
implements SocketChannelConfig {
private final AtomicReference<NetworkChannel> channel = new AtomicReference<NetworkChannel>();
private volatile boolean allowHalfClosure;
private volatile long readTimeoutInMillis;
private volatile long writeTimeoutInMillis;
private Map<SocketOption<?>, Object> options = new ConcurrentHashMap<SocketOption<?>, Object>();
private static final int DEFAULT_RCV_BUF_SIZE = 32 * 1024;
private static final int DEFAULT_SND_BUF_SIZE = 32 * 1024;
private static final int DEFAULT_SO_LINGER = -1;
private static final boolean DEFAULT_SO_KEEP_ALIVE = false;
private static final int DEFAULT_IP_TOS = 0;
private static final boolean DEFAULT_SO_REUSEADDR = false;
private static final boolean DEFAULT_TCP_NODELAY = false;
AioSocketChannelConfig() {
}
AioSocketChannelConfig(NetworkChannel channel) {
this.channel.set(channel);
}
public interface AioSocketChannelConfig extends SocketChannelConfig {
@Override
AioSocketChannelConfig setTcpNoDelay(boolean tcpNoDelay);
@Override
public Map<ChannelOption<?>, Object> getOptions() {
return getOptions(
super.getOptions(),
SO_RCVBUF, SO_SNDBUF, TCP_NODELAY, SO_KEEPALIVE, SO_REUSEADDR, SO_LINGER, IP_TOS,
AIO_READ_TIMEOUT, AIO_WRITE_TIMEOUT, ALLOW_HALF_CLOSURE);
}
@SuppressWarnings("unchecked")
@Override
public <T> T getOption(ChannelOption<T> option) {
if (option == SO_RCVBUF) {
return (T) Integer.valueOf(getReceiveBufferSize());
}
if (option == SO_SNDBUF) {
return (T) Integer.valueOf(getSendBufferSize());
}
if (option == TCP_NODELAY) {
return (T) Boolean.valueOf(isTcpNoDelay());
}
if (option == SO_KEEPALIVE) {
return (T) Boolean.valueOf(isKeepAlive());
}
if (option == SO_REUSEADDR) {
return (T) Boolean.valueOf(isReuseAddress());
}
if (option == SO_LINGER) {
return (T) Integer.valueOf(getSoLinger());
}
if (option == IP_TOS) {
return (T) Integer.valueOf(getTrafficClass());
}
if (option == AIO_READ_TIMEOUT) {
return (T) Long.valueOf(getReadTimeout());
}
if (option == AIO_WRITE_TIMEOUT) {
return (T) Long.valueOf(getWriteTimeout());
}
if (option == ALLOW_HALF_CLOSURE) {
return (T) Boolean.valueOf(isAllowHalfClosure());
}
return super.getOption(option);
}
AioSocketChannelConfig setSoLinger(int soLinger);
@Override
public <T> boolean setOption(ChannelOption<T> option, T value) {
validate(option, value);
if (option == SO_RCVBUF) {
setReceiveBufferSize((Integer) value);
} else if (option == SO_SNDBUF) {
setSendBufferSize((Integer) value);
} else if (option == TCP_NODELAY) {
setTcpNoDelay((Boolean) value);
} else if (option == SO_KEEPALIVE) {
setKeepAlive((Boolean) value);
} else if (option == SO_REUSEADDR) {
setReuseAddress((Boolean) value);
} else if (option == SO_LINGER) {
setSoLinger((Integer) value);
} else if (option == IP_TOS) {
setTrafficClass((Integer) value);
} else if (option == AIO_READ_TIMEOUT) {
setReadTimeout((Long) value);
} else if (option == AIO_WRITE_TIMEOUT) {
setWriteTimeout((Long) value);
} else if (option == ALLOW_HALF_CLOSURE) {
setAllowHalfClosure((Boolean) value);
} else {
return super.setOption(option, value);
}
return true;
}
AioSocketChannelConfig setSendBufferSize(int sendBufferSize);
@Override
public int getReceiveBufferSize() {
return (Integer) getOption(StandardSocketOptions.SO_RCVBUF, DEFAULT_RCV_BUF_SIZE);
}
AioSocketChannelConfig setReceiveBufferSize(int receiveBufferSize);
@Override
public int getSendBufferSize() {
return (Integer) getOption(StandardSocketOptions.SO_SNDBUF, DEFAULT_SND_BUF_SIZE);
}
AioSocketChannelConfig setKeepAlive(boolean keepAlive);
@Override
public int getSoLinger() {
return (Integer) getOption(StandardSocketOptions.SO_LINGER, DEFAULT_SO_LINGER);
}
AioSocketChannelConfig setTrafficClass(int trafficClass);
@Override
public int getTrafficClass() {
return (Integer) getOption(StandardSocketOptions.IP_TOS, DEFAULT_IP_TOS);
}
AioSocketChannelConfig setReuseAddress(boolean reuseAddress);
@Override
public boolean isKeepAlive() {
return (Boolean) getOption(StandardSocketOptions.SO_KEEPALIVE, DEFAULT_SO_KEEP_ALIVE);
}
AioSocketChannelConfig setPerformancePreferences(int connectionTime, int latency, int bandwidth);
@Override
public boolean isReuseAddress() {
return (Boolean) getOption(StandardSocketOptions.SO_REUSEADDR, DEFAULT_SO_REUSEADDR);
}
AioSocketChannelConfig setAllowHalfClosure(boolean allowHalfClosure);
@Override
public boolean isTcpNoDelay() {
return (Boolean) getOption(StandardSocketOptions.TCP_NODELAY, DEFAULT_TCP_NODELAY);
}
AioSocketChannelConfig setWriteSpinCount(int writeSpinCount);
@Override
public void setKeepAlive(boolean keepAlive) {
setOption(StandardSocketOptions.SO_KEEPALIVE, keepAlive);
}
AioSocketChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis);
@Override
public void setPerformancePreferences(
int connectionTime, int latency, int bandwidth) {
throw new UnsupportedOperationException();
}
@Override
public void setReceiveBufferSize(int receiveBufferSize) {
setOption(StandardSocketOptions.SO_RCVBUF, receiveBufferSize);
}
@Override
public void setReuseAddress(boolean reuseAddress) {
setOption(StandardSocketOptions.SO_REUSEADDR, reuseAddress);
}
@Override
public void setSendBufferSize(int sendBufferSize) {
setOption(StandardSocketOptions.SO_SNDBUF, sendBufferSize);
}
@Override
public void setSoLinger(int soLinger) {
setOption(StandardSocketOptions.SO_LINGER, soLinger);
}
@Override
public void setTcpNoDelay(boolean tcpNoDelay) {
setOption(StandardSocketOptions.TCP_NODELAY, tcpNoDelay);
}
@Override
public void setTrafficClass(int trafficClass) {
setOption(StandardSocketOptions.IP_TOS, trafficClass);
}
private Object getOption(SocketOption option, Object defaultValue) {
if (channel.get() == null) {
Object value = options.get(option);
if (value == null) {
return defaultValue;
} else {
return value;
}
}
try {
return channel.get().getOption(option);
} catch (IOException e) {
throw new ChannelException(e);
}
}
private void setOption(SocketOption option, Object defaultValue) {
if (channel.get() == null) {
options.put(option, defaultValue);
return;
}
try {
channel.get().setOption(option, defaultValue);
} catch (IOException e) {
throw new ChannelException(e);
}
}
AioSocketChannelConfig setAllocator(ByteBufAllocator allocator);
/**
* Return the read timeout in milliseconds after which a {@link InterruptedByTimeoutException} will get thrown.
@ -245,12 +63,7 @@ final class AioSocketChannelConfig extends DefaultChannelConfig
*
* To disable it just use {@code 0}.
*/
public void setReadTimeout(long readTimeoutInMillis) {
if (readTimeoutInMillis < 0) {
throw new IllegalArgumentException("readTimeoutInMillis: " + readTimeoutInMillis);
}
this.readTimeoutInMillis = readTimeoutInMillis;
}
AioSocketChannelConfig setReadTimeout(long readTimeoutInMillis);
/**
* Return the write timeout in milliseconds after which a {@link InterruptedByTimeoutException} will get thrown.
@ -259,62 +72,20 @@ final class AioSocketChannelConfig extends DefaultChannelConfig
*
* To disable it just use {@code 0}.
*/
public void setWriteTimeout(long writeTimeoutInMillis) {
if (writeTimeoutInMillis < 0) {
throw new IllegalArgumentException("writeTimeoutInMillis: " + writeTimeoutInMillis);
}
this.writeTimeoutInMillis = writeTimeoutInMillis;
}
AioSocketChannelConfig setWriteTimeout(long writeTimeoutInMillis);
/**
* Return the read timeout in milliseconds after which a {@link InterruptedByTimeoutException} will get thrown.
*
* The default is {@code 0}
*/
public long getReadTimeout() {
return readTimeoutInMillis;
}
long getReadTimeout();
/**
* Return the write timeout in milliseconds after which a {@link InterruptedByTimeoutException} will get thrown.
*
* The default is {@code 0}
*/
public long getWriteTimeout() {
return writeTimeoutInMillis;
}
long getWriteTimeout();
@Override
public boolean isAllowHalfClosure() {
return allowHalfClosure;
}
@Override
public void setAllowHalfClosure(boolean allowHalfClosure) {
this.allowHalfClosure = allowHalfClosure;
}
void active(NetworkChannel channel) {
if (channel == null) {
throw new NullPointerException("channel");
}
if (this.channel.compareAndSet(null, channel)) {
propagateOptions();
}
}
private void propagateOptions() {
for (SocketOption option: options.keySet()) {
Object value = options.remove(option);
if (value != null) {
try {
channel.get().setOption(option, value);
} catch (IOException e) {
throw new ChannelException(e);
}
}
}
// not needed anymore
options = null;
}
}

View File

@ -0,0 +1,324 @@
/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.channel.socket.aio;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelException;
import io.netty.channel.ChannelOption;
import io.netty.channel.DefaultChannelConfig;
import java.io.IOException;
import java.net.SocketOption;
import java.net.StandardSocketOptions;
import java.nio.channels.NetworkChannel;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import static io.netty.channel.ChannelOption.*;
/**
* The default {@link AioSocketChannelConfig} implementation.
*/
final class DefaultAioSocketChannelConfig extends DefaultChannelConfig
implements AioSocketChannelConfig {
private final AtomicReference<NetworkChannel> channel = new AtomicReference<NetworkChannel>();
private volatile boolean allowHalfClosure;
private volatile long readTimeoutInMillis;
private volatile long writeTimeoutInMillis;
private Map<SocketOption<?>, Object> options = new ConcurrentHashMap<SocketOption<?>, Object>();
private static final int DEFAULT_RCV_BUF_SIZE = 32 * 1024;
private static final int DEFAULT_SND_BUF_SIZE = 32 * 1024;
private static final int DEFAULT_SO_LINGER = -1;
private static final boolean DEFAULT_SO_KEEP_ALIVE = false;
private static final int DEFAULT_IP_TOS = 0;
private static final boolean DEFAULT_SO_REUSEADDR = false;
private static final boolean DEFAULT_TCP_NODELAY = false;
DefaultAioSocketChannelConfig() {
}
DefaultAioSocketChannelConfig(NetworkChannel channel) {
this.channel.set(channel);
}
@Override
public Map<ChannelOption<?>, Object> getOptions() {
return getOptions(
super.getOptions(),
SO_RCVBUF, SO_SNDBUF, TCP_NODELAY, SO_KEEPALIVE, SO_REUSEADDR, SO_LINGER, IP_TOS,
AIO_READ_TIMEOUT, AIO_WRITE_TIMEOUT, ALLOW_HALF_CLOSURE);
}
@SuppressWarnings("unchecked")
@Override
public <T> T getOption(ChannelOption<T> option) {
if (option == SO_RCVBUF) {
return (T) Integer.valueOf(getReceiveBufferSize());
}
if (option == SO_SNDBUF) {
return (T) Integer.valueOf(getSendBufferSize());
}
if (option == TCP_NODELAY) {
return (T) Boolean.valueOf(isTcpNoDelay());
}
if (option == SO_KEEPALIVE) {
return (T) Boolean.valueOf(isKeepAlive());
}
if (option == SO_REUSEADDR) {
return (T) Boolean.valueOf(isReuseAddress());
}
if (option == SO_LINGER) {
return (T) Integer.valueOf(getSoLinger());
}
if (option == IP_TOS) {
return (T) Integer.valueOf(getTrafficClass());
}
if (option == AIO_READ_TIMEOUT) {
return (T) Long.valueOf(getReadTimeout());
}
if (option == AIO_WRITE_TIMEOUT) {
return (T) Long.valueOf(getWriteTimeout());
}
if (option == ALLOW_HALF_CLOSURE) {
return (T) Boolean.valueOf(isAllowHalfClosure());
}
return super.getOption(option);
}
@Override
public <T> boolean setOption(ChannelOption<T> option, T value) {
validate(option, value);
if (option == SO_RCVBUF) {
setReceiveBufferSize((Integer) value);
} else if (option == SO_SNDBUF) {
setSendBufferSize((Integer) value);
} else if (option == TCP_NODELAY) {
setTcpNoDelay((Boolean) value);
} else if (option == SO_KEEPALIVE) {
setKeepAlive((Boolean) value);
} else if (option == SO_REUSEADDR) {
setReuseAddress((Boolean) value);
} else if (option == SO_LINGER) {
setSoLinger((Integer) value);
} else if (option == IP_TOS) {
setTrafficClass((Integer) value);
} else if (option == AIO_READ_TIMEOUT) {
setReadTimeout((Long) value);
} else if (option == AIO_WRITE_TIMEOUT) {
setWriteTimeout((Long) value);
} else if (option == ALLOW_HALF_CLOSURE) {
setAllowHalfClosure((Boolean) value);
} else {
return super.setOption(option, value);
}
return true;
}
@Override
public int getReceiveBufferSize() {
return (Integer) getOption(StandardSocketOptions.SO_RCVBUF, DEFAULT_RCV_BUF_SIZE);
}
@Override
public int getSendBufferSize() {
return (Integer) getOption(StandardSocketOptions.SO_SNDBUF, DEFAULT_SND_BUF_SIZE);
}
@Override
public int getSoLinger() {
return (Integer) getOption(StandardSocketOptions.SO_LINGER, DEFAULT_SO_LINGER);
}
@Override
public int getTrafficClass() {
return (Integer) getOption(StandardSocketOptions.IP_TOS, DEFAULT_IP_TOS);
}
@Override
public boolean isKeepAlive() {
return (Boolean) getOption(StandardSocketOptions.SO_KEEPALIVE, DEFAULT_SO_KEEP_ALIVE);
}
@Override
public boolean isReuseAddress() {
return (Boolean) getOption(StandardSocketOptions.SO_REUSEADDR, DEFAULT_SO_REUSEADDR);
}
@Override
public boolean isTcpNoDelay() {
return (Boolean) getOption(StandardSocketOptions.TCP_NODELAY, DEFAULT_TCP_NODELAY);
}
@Override
public AioSocketChannelConfig setKeepAlive(boolean keepAlive) {
setOption(StandardSocketOptions.SO_KEEPALIVE, keepAlive);
return this;
}
@Override
public AioSocketChannelConfig setPerformancePreferences(
int connectionTime, int latency, int bandwidth) {
throw new UnsupportedOperationException();
}
@Override
public AioSocketChannelConfig setReceiveBufferSize(int receiveBufferSize) {
setOption(StandardSocketOptions.SO_RCVBUF, receiveBufferSize);
return this;
}
@Override
public AioSocketChannelConfig setReuseAddress(boolean reuseAddress) {
setOption(StandardSocketOptions.SO_REUSEADDR, reuseAddress);
return this;
}
@Override
public AioSocketChannelConfig setSendBufferSize(int sendBufferSize) {
setOption(StandardSocketOptions.SO_SNDBUF, sendBufferSize);
return this;
}
@Override
public AioSocketChannelConfig setSoLinger(int soLinger) {
setOption(StandardSocketOptions.SO_LINGER, soLinger);
return this;
}
@Override
public AioSocketChannelConfig setTcpNoDelay(boolean tcpNoDelay) {
setOption(StandardSocketOptions.TCP_NODELAY, tcpNoDelay);
return this;
}
@Override
public AioSocketChannelConfig setTrafficClass(int trafficClass) {
setOption(StandardSocketOptions.IP_TOS, trafficClass);
return this;
}
private Object getOption(SocketOption option, Object defaultValue) {
if (channel.get() == null) {
Object value = options.get(option);
if (value == null) {
return defaultValue;
} else {
return value;
}
}
try {
return channel.get().getOption(option);
} catch (IOException e) {
throw new ChannelException(e);
}
}
private void setOption(SocketOption option, Object defaultValue) {
if (channel.get() == null) {
options.put(option, defaultValue);
return;
}
try {
channel.get().setOption(option, defaultValue);
} catch (IOException e) {
throw new ChannelException(e);
}
}
@Override
public AioSocketChannelConfig setReadTimeout(long readTimeoutInMillis) {
if (readTimeoutInMillis < 0) {
throw new IllegalArgumentException("readTimeoutInMillis: " + readTimeoutInMillis);
}
this.readTimeoutInMillis = readTimeoutInMillis;
return this;
}
@Override
public AioSocketChannelConfig setWriteTimeout(long writeTimeoutInMillis) {
if (writeTimeoutInMillis < 0) {
throw new IllegalArgumentException("writeTimeoutInMillis: " + writeTimeoutInMillis);
}
this.writeTimeoutInMillis = writeTimeoutInMillis;
return this;
}
@Override
public long getReadTimeout() {
return readTimeoutInMillis;
}
@Override
public long getWriteTimeout() {
return writeTimeoutInMillis;
}
@Override
public boolean isAllowHalfClosure() {
return allowHalfClosure;
}
@Override
public AioSocketChannelConfig setAllowHalfClosure(boolean allowHalfClosure) {
this.allowHalfClosure = allowHalfClosure;
return this;
}
void active(NetworkChannel channel) {
if (channel == null) {
throw new NullPointerException("channel");
}
if (this.channel.compareAndSet(null, channel)) {
propagateOptions();
}
}
private void propagateOptions() {
for (SocketOption option: options.keySet()) {
Object value = options.remove(option);
if (value != null) {
try {
channel.get().setOption(option, value);
} catch (IOException e) {
throw new ChannelException(e);
}
}
}
// not needed anymore
options = null;
}
@Override
public AioSocketChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
return (AioSocketChannelConfig) setConnectTimeoutMillis(connectTimeoutMillis);
}
@Override
public AioSocketChannelConfig setWriteSpinCount(int writeSpinCount) {
return (AioSocketChannelConfig) setWriteSpinCount(writeSpinCount);
}
@Override
public AioSocketChannelConfig setAllocator(ByteBufAllocator allocator) {
return (AioSocketChannelConfig) setAllocator(allocator);
}
}

View File

@ -15,7 +15,9 @@
*/
package io.netty.channel.socket.nio;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelException;
import io.netty.channel.socket.DatagramChannelConfig;
import io.netty.channel.socket.DefaultDatagramChannelConfig;
import io.netty.util.internal.DetectionUtil;
@ -28,7 +30,7 @@ import java.nio.channels.NetworkChannel;
import java.util.Enumeration;
/**
* The default {@link NioSocketChannelConfig} implementation.
* The default {@link NioDatagramChannelConfig} implementation.
*/
class NioDatagramChannelConfig extends DefaultDatagramChannelConfig {
@ -109,8 +111,9 @@ class NioDatagramChannelConfig extends DefaultDatagramChannelConfig {
}
@Override
public void setTimeToLive(int ttl) {
public DatagramChannelConfig setTimeToLive(int ttl) {
setOption0(IP_MULTICAST_TTL, ttl);
return this;
}
@Override
@ -128,12 +131,13 @@ class NioDatagramChannelConfig extends DefaultDatagramChannelConfig {
}
@Override
public void setInterface(InetAddress interfaceAddress) {
public DatagramChannelConfig setInterface(InetAddress interfaceAddress) {
try {
setNetworkInterface(NetworkInterface.getByInetAddress(interfaceAddress));
} catch (SocketException e) {
throw new ChannelException(e);
}
return this;
}
@Override
@ -142,8 +146,9 @@ class NioDatagramChannelConfig extends DefaultDatagramChannelConfig {
}
@Override
public void setNetworkInterface(NetworkInterface networkInterface) {
public DatagramChannelConfig setNetworkInterface(NetworkInterface networkInterface) {
setOption0(IP_MULTICAST_IF, networkInterface);
return this;
}
@Override
@ -152,8 +157,9 @@ class NioDatagramChannelConfig extends DefaultDatagramChannelConfig {
}
@Override
public void setLoopbackModeDisabled(boolean loopbackModeDisabled) {
public DatagramChannelConfig setLoopbackModeDisabled(boolean loopbackModeDisabled) {
setOption0(IP_MULTICAST_LOOP, loopbackModeDisabled);
return this;
}
private Object getOption0(Object option) {
@ -179,4 +185,5 @@ class NioDatagramChannelConfig extends DefaultDatagramChannelConfig {
}
}
}
}