From 4a3c54b0fa8c3122b1005452b2593b71360ed6f2 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Thu, 24 May 2012 10:37:51 -0700 Subject: [PATCH] Add UDP_RECEIVE_PACKET_SIZE option for datagram transport --- .../java/io/netty/channel/ChannelOption.java | 3 +++ .../channel/socket/DatagramChannelConfig.java | 4 ++++ .../socket/DefaultDatagramChannelConfig.java | 22 ++++++++++++++++++- .../socket/nio/NioDatagramChannel.java | 3 +-- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/transport/src/main/java/io/netty/channel/ChannelOption.java b/transport/src/main/java/io/netty/channel/ChannelOption.java index 65362cc5b2..3d89b934ec 100644 --- a/transport/src/main/java/io/netty/channel/ChannelOption.java +++ b/transport/src/main/java/io/netty/channel/ChannelOption.java @@ -44,6 +44,9 @@ public class ChannelOption extends UniqueKey { public static final ChannelOption IP_MULTICAST_LOOP_DISABLED = new ChannelOption("IP_MULTICAST_LOOP_DISABLED", Boolean.class); + public static final ChannelOption UDP_RECEIVE_PACKET_SIZE = + new ChannelOption("UDP_RECEIVE_PACKET_SIZE", Integer.class); + public static final ChannelOption TCP_NODELAY = new ChannelOption("TCP_NODELAY", Boolean.class); diff --git a/transport/src/main/java/io/netty/channel/socket/DatagramChannelConfig.java b/transport/src/main/java/io/netty/channel/socket/DatagramChannelConfig.java index f379799728..3f3f28d899 100644 --- a/transport/src/main/java/io/netty/channel/socket/DatagramChannelConfig.java +++ b/transport/src/main/java/io/netty/channel/socket/DatagramChannelConfig.java @@ -75,6 +75,10 @@ public interface DatagramChannelConfig extends ChannelConfig { */ void setReceiveBufferSize(int receiveBufferSize); + int getReceivePacketSize(); + + void setReceivePacketSize(int receivePacketSize); + /** * Gets the traffic class. */ diff --git a/transport/src/main/java/io/netty/channel/socket/DefaultDatagramChannelConfig.java b/transport/src/main/java/io/netty/channel/socket/DefaultDatagramChannelConfig.java index c217980ba6..89aba8988b 100644 --- a/transport/src/main/java/io/netty/channel/socket/DefaultDatagramChannelConfig.java +++ b/transport/src/main/java/io/netty/channel/socket/DefaultDatagramChannelConfig.java @@ -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) { diff --git a/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java b/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java index d8504eb088..65154f37ba 100644 --- a/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java @@ -174,8 +174,7 @@ public final class NioDatagramChannel extends AbstractNioChannel implements io.n @Override protected int doRead(ChannelBufferHolder 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;