Introduce a new ChannelOption called DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION. Related to [#1830]

This ChannelOption allows to tell the DatagramChannel implementation to be active as soon as they are registrated to their EventLoop. This can be used to make it possible to write to a not bound DatagramChannel.
The ChannelOption is marked as @deprecated as I'm looking for a better solution in master which breaks default behaviour with 4.0 branch.
This commit is contained in:
Norman Maurer 2013-09-24 11:42:58 +02:00
parent 29d34c672c
commit ae904a1f56
5 changed files with 26 additions and 5 deletions

View File

@ -19,6 +19,7 @@ import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOption;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import org.junit.Test;
@ -65,7 +66,7 @@ public class DatagramUnicastTest extends AbstractDatagramTest {
@Test
public void testSimpleSendWithoutBind() throws Throwable {
//run();
run();
}
public void testSimpleSendWithoutBind(Bootstrap sb, Bootstrap cb) throws Throwable {
@ -85,6 +86,7 @@ public class DatagramUnicastTest extends AbstractDatagramTest {
// Nothing will be sent.
}
});
cb.option(ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION, true);
Channel sc = sb.bind().sync().channel();
Channel cc = cb.register().sync().channel();

View File

@ -96,6 +96,9 @@ public class ChannelOption<T> extends UniqueName {
public static final ChannelOption<Long> AIO_WRITE_TIMEOUT =
new ChannelOption<Long>("AIO_WRITE_TIMEOUT");
@Deprecated
public static final ChannelOption<Boolean> DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION =
new ChannelOption<Boolean>("DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION");
/**
* Create a new {@link ChannelOption} with the given name. The name needs to be
* unique.

View File

@ -46,6 +46,7 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
private static final RecvByteBufAllocator DEFAULT_RCVBUF_ALLOCATOR = new FixedRecvByteBufAllocator(2048);
private final DatagramSocket javaSocket;
private volatile boolean activeOnOpen;
/**
* Creates a new instance.
@ -64,7 +65,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, DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION);
}
@SuppressWarnings("unchecked")
@ -99,7 +100,9 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
if (option == IP_TOS) {
return (T) Integer.valueOf(getTrafficClass());
}
if (option == DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION) {
return (T) Boolean.valueOf(activeOnOpen);
}
return super.getOption(option);
}
@ -125,6 +128,8 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
setTimeToLive((Integer) value);
} else if (option == IP_TOS) {
setTrafficClass((Integer) value);
} else if (option == DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION) {
setActiveOnOpen((Boolean) value);
} else {
return super.setOption(option, value);
}
@ -132,6 +137,12 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
return true;
}
private void setActiveOnOpen(boolean activeOnOpen) {
if (channel.isRegistered()) {
throw new IllegalStateException("Can only changed before channel was registered");
}
this.activeOnOpen = activeOnOpen;
}
@Override
public boolean isBroadcast() {
try {

View File

@ -23,6 +23,7 @@ import io.netty.channel.Channel;
import io.netty.channel.ChannelException;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelMetadata;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelOutboundBuffer;
import io.netty.channel.ChannelPromise;
import io.netty.channel.DefaultAddressedEnvelope;
@ -128,7 +129,9 @@ public final class NioDatagramChannel
@Override
public boolean isActive() {
DatagramChannel ch = javaChannel();
return ch.isOpen();
return ch.isOpen() && (
(config.getOption(ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION) && isRegistered())
|| ch.socket().isBound());
}
@Override

View File

@ -22,6 +22,7 @@ import io.netty.channel.Channel;
import io.netty.channel.ChannelException;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelMetadata;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelOutboundBuffer;
import io.netty.channel.ChannelPromise;
import io.netty.channel.RecvByteBufAllocator;
@ -125,7 +126,8 @@ public class OioDatagramChannel extends AbstractOioMessageChannel
@Override
public boolean isActive() {
return isOpen() && socket.isBound();
return isOpen() && (config.getOption(ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION) && isRegistered())
|| socket.isBound();
}
@Override