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:
parent
5aa2b7e9f7
commit
cd5f9a2212
@ -19,6 +19,7 @@ import io.netty.bootstrap.Bootstrap;
|
|||||||
import io.netty.buffer.Unpooled;
|
import io.netty.buffer.Unpooled;
|
||||||
import io.netty.channel.Channel;
|
import io.netty.channel.Channel;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.channel.ChannelOption;
|
||||||
import io.netty.channel.SimpleChannelInboundHandler;
|
import io.netty.channel.SimpleChannelInboundHandler;
|
||||||
import io.netty.channel.socket.DatagramPacket;
|
import io.netty.channel.socket.DatagramPacket;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -65,7 +66,7 @@ public class DatagramUnicastTest extends AbstractDatagramTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSimpleSendWithoutBind() throws Throwable {
|
public void testSimpleSendWithoutBind() throws Throwable {
|
||||||
//run();
|
run();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testSimpleSendWithoutBind(Bootstrap sb, Bootstrap cb) throws Throwable {
|
public void testSimpleSendWithoutBind(Bootstrap sb, Bootstrap cb) throws Throwable {
|
||||||
@ -85,6 +86,7 @@ public class DatagramUnicastTest extends AbstractDatagramTest {
|
|||||||
// Nothing will be sent.
|
// Nothing will be sent.
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
cb.option(ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION, true);
|
||||||
|
|
||||||
Channel sc = sb.bind().sync().channel();
|
Channel sc = sb.bind().sync().channel();
|
||||||
Channel cc = cb.register().sync().channel();
|
Channel cc = cb.register().sync().channel();
|
||||||
|
@ -89,6 +89,9 @@ public class ChannelOption<T> extends UniqueName {
|
|||||||
public static final ChannelOption<Boolean> TCP_NODELAY =
|
public static final ChannelOption<Boolean> TCP_NODELAY =
|
||||||
new ChannelOption<Boolean>("TCP_NODELAY");
|
new ChannelOption<Boolean>("TCP_NODELAY");
|
||||||
|
|
||||||
|
@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
|
* Create a new {@link ChannelOption} with the given name. The name needs to be
|
||||||
* unique.
|
* unique.
|
||||||
|
@ -46,6 +46,7 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
|
|||||||
private static final RecvByteBufAllocator DEFAULT_RCVBUF_ALLOCATOR = new FixedRecvByteBufAllocator(2048);
|
private static final RecvByteBufAllocator DEFAULT_RCVBUF_ALLOCATOR = new FixedRecvByteBufAllocator(2048);
|
||||||
|
|
||||||
private final DatagramSocket javaSocket;
|
private final DatagramSocket javaSocket;
|
||||||
|
private volatile boolean activeOnOpen;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new instance.
|
* Creates a new instance.
|
||||||
@ -64,7 +65,7 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
|
|||||||
return getOptions(
|
return getOptions(
|
||||||
super.getOptions(),
|
super.getOptions(),
|
||||||
SO_BROADCAST, SO_RCVBUF, SO_SNDBUF, SO_REUSEADDR, IP_MULTICAST_LOOP_DISABLED,
|
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")
|
@SuppressWarnings("unchecked")
|
||||||
@ -99,7 +100,9 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
|
|||||||
if (option == IP_TOS) {
|
if (option == IP_TOS) {
|
||||||
return (T) Integer.valueOf(getTrafficClass());
|
return (T) Integer.valueOf(getTrafficClass());
|
||||||
}
|
}
|
||||||
|
if (option == DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION) {
|
||||||
|
return (T) Boolean.valueOf(activeOnOpen);
|
||||||
|
}
|
||||||
return super.getOption(option);
|
return super.getOption(option);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,6 +128,8 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
|
|||||||
setTimeToLive((Integer) value);
|
setTimeToLive((Integer) value);
|
||||||
} else if (option == IP_TOS) {
|
} else if (option == IP_TOS) {
|
||||||
setTrafficClass((Integer) value);
|
setTrafficClass((Integer) value);
|
||||||
|
} else if (option == DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION) {
|
||||||
|
setActiveOnOpen((Boolean) value);
|
||||||
} else {
|
} else {
|
||||||
return super.setOption(option, value);
|
return super.setOption(option, value);
|
||||||
}
|
}
|
||||||
@ -132,6 +137,12 @@ public class DefaultDatagramChannelConfig extends DefaultChannelConfig implement
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setActiveOnOpen(boolean activeOnOpen) {
|
||||||
|
if (channel.isRegistered()) {
|
||||||
|
throw new IllegalStateException("Can only changed before channel was registered");
|
||||||
|
}
|
||||||
|
this.activeOnOpen = activeOnOpen;
|
||||||
|
}
|
||||||
@Override
|
@Override
|
||||||
public boolean isBroadcast() {
|
public boolean isBroadcast() {
|
||||||
try {
|
try {
|
||||||
|
@ -23,6 +23,7 @@ import io.netty.channel.Channel;
|
|||||||
import io.netty.channel.ChannelException;
|
import io.netty.channel.ChannelException;
|
||||||
import io.netty.channel.ChannelFuture;
|
import io.netty.channel.ChannelFuture;
|
||||||
import io.netty.channel.ChannelMetadata;
|
import io.netty.channel.ChannelMetadata;
|
||||||
|
import io.netty.channel.ChannelOption;
|
||||||
import io.netty.channel.ChannelOutboundBuffer;
|
import io.netty.channel.ChannelOutboundBuffer;
|
||||||
import io.netty.channel.ChannelPromise;
|
import io.netty.channel.ChannelPromise;
|
||||||
import io.netty.channel.DefaultAddressedEnvelope;
|
import io.netty.channel.DefaultAddressedEnvelope;
|
||||||
@ -128,7 +129,9 @@ public final class NioDatagramChannel
|
|||||||
@Override
|
@Override
|
||||||
public boolean isActive() {
|
public boolean isActive() {
|
||||||
DatagramChannel ch = javaChannel();
|
DatagramChannel ch = javaChannel();
|
||||||
return ch.isOpen();
|
return ch.isOpen() && (
|
||||||
|
(config.getOption(ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION) && isRegistered())
|
||||||
|
|| ch.socket().isBound());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -22,6 +22,7 @@ import io.netty.channel.Channel;
|
|||||||
import io.netty.channel.ChannelException;
|
import io.netty.channel.ChannelException;
|
||||||
import io.netty.channel.ChannelFuture;
|
import io.netty.channel.ChannelFuture;
|
||||||
import io.netty.channel.ChannelMetadata;
|
import io.netty.channel.ChannelMetadata;
|
||||||
|
import io.netty.channel.ChannelOption;
|
||||||
import io.netty.channel.ChannelOutboundBuffer;
|
import io.netty.channel.ChannelOutboundBuffer;
|
||||||
import io.netty.channel.ChannelPromise;
|
import io.netty.channel.ChannelPromise;
|
||||||
import io.netty.channel.RecvByteBufAllocator;
|
import io.netty.channel.RecvByteBufAllocator;
|
||||||
@ -125,7 +126,8 @@ public class OioDatagramChannel extends AbstractOioMessageChannel
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isActive() {
|
public boolean isActive() {
|
||||||
return isOpen() && socket.isBound();
|
return isOpen() && (config.getOption(ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION) && isRegistered())
|
||||||
|
|| socket.isBound();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user