Add UDP_RECEIVE_PACKET_SIZE option for datagram transport

This commit is contained in:
Trustin Lee 2012-05-24 10:37:51 -07:00
parent c7c923cab3
commit 4a3c54b0fa
4 changed files with 29 additions and 3 deletions

View File

@ -44,6 +44,9 @@ public class ChannelOption<T> extends UniqueKey<T> {
public static final ChannelOption<Boolean> IP_MULTICAST_LOOP_DISABLED =
new ChannelOption<Boolean>("IP_MULTICAST_LOOP_DISABLED", Boolean.class);
public static final ChannelOption<Integer> UDP_RECEIVE_PACKET_SIZE =
new ChannelOption<Integer>("UDP_RECEIVE_PACKET_SIZE", Integer.class);
public static final ChannelOption<Boolean> TCP_NODELAY =
new ChannelOption<Boolean>("TCP_NODELAY", Boolean.class);

View File

@ -75,6 +75,10 @@ public interface DatagramChannelConfig extends ChannelConfig {
*/
void setReceiveBufferSize(int receiveBufferSize);
int getReceivePacketSize();
void setReceivePacketSize(int receivePacketSize);
/**
* Gets the traffic class.
*/

View File

@ -33,7 +33,10 @@ import java.util.Map;
*/
public class DefaultDatagramChannelConfig extends DefaultChannelConfig implements DatagramChannelConfig {
private static int DEFAULT_RECEIVE_PACKET_SIZE = 2048;
private final DatagramSocket socket;
private volatile int receivePacketSize = DEFAULT_RECEIVE_PACKET_SIZE;
/**
* Creates a new instance.
@ -50,7 +53,7 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
return getOptions(
super.getOptions(),
SO_BROADCAST, SO_RCVBUF, SO_SNDBUF, SO_REUSEADDR, IP_MULTICAST_LOOP_DISABLED,
IP_MULTICAST_ADDR, IP_MULTICAST_IF, IP_MULTICAST_TTL, IP_TOS);
IP_MULTICAST_ADDR, IP_MULTICAST_IF, IP_MULTICAST_TTL, IP_TOS, UDP_RECEIVE_PACKET_SIZE);
}
@Override
@ -64,6 +67,9 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
if (option == SO_SNDBUF) {
return (T) Integer.valueOf(getSendBufferSize());
}
if (option == UDP_RECEIVE_PACKET_SIZE) {
return (T) Integer.valueOf(getReceivePacketSize());
}
if (option == SO_REUSEADDR) {
return (T) Boolean.valueOf(isReuseAddress());
}
@ -269,6 +275,20 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
}
}
@Override
public int getReceivePacketSize() {
return receivePacketSize;
}
@Override
public void setReceivePacketSize(int receivePacketSize) {
if (receivePacketSize <= 0) {
throw new IllegalArgumentException(
String.format("receivePacketSize: %d (expected: > 0)", receivePacketSize));
}
this.receivePacketSize = receivePacketSize;
}
@Override
public int getTimeToLive() {
if (socket instanceof MulticastSocket) {

View File

@ -174,8 +174,7 @@ public final class NioDatagramChannel extends AbstractNioChannel implements io.n
@Override
protected int doRead(ChannelBufferHolder<Object> buf) throws Exception {
DatagramChannel ch = javaChannel();
// FIXME: Make this configurable.
ByteBuffer data = ByteBuffer.allocate(1024);
ByteBuffer data = ByteBuffer.allocate(config().getReceivePacketSize());
InetSocketAddress remoteAddress = (InetSocketAddress) ch.receive(data);
if (remoteAddress == null) {
return 0;