diff --git a/transport-rxtx/src/main/java/io/netty/channel/rxtx/RxtxChannel.java b/transport-rxtx/src/main/java/io/netty/channel/rxtx/RxtxChannel.java index 386580f152..e80fe104f0 100644 --- a/transport-rxtx/src/main/java/io/netty/channel/rxtx/RxtxChannel.java +++ b/transport-rxtx/src/main/java/io/netty/channel/rxtx/RxtxChannel.java @@ -40,7 +40,7 @@ public class RxtxChannel extends OioByteStreamChannel { private SerialPort serialPort; public RxtxChannel() { - super(null, null); + super(null); config = new DefaultRxtxChannelConfig(this); } diff --git a/transport-sctp/src/main/java/io/netty/channel/sctp/nio/NioSctpChannel.java b/transport-sctp/src/main/java/io/netty/channel/sctp/nio/NioSctpChannel.java index 098508480d..d3b5bcbae3 100644 --- a/transport-sctp/src/main/java/io/netty/channel/sctp/nio/NioSctpChannel.java +++ b/transport-sctp/src/main/java/io/netty/channel/sctp/nio/NioSctpChannel.java @@ -86,7 +86,7 @@ public class NioSctpChannel extends AbstractNioMessageChannel implements io.nett * Create a new instance using {@link SctpChannel} */ public NioSctpChannel(SctpChannel sctpChannel) { - this(null, null, sctpChannel); + this(null, sctpChannel); } /** @@ -94,12 +94,10 @@ public class NioSctpChannel extends AbstractNioMessageChannel implements io.nett * * @param parent the {@link Channel} which is the parent of this {@link NioSctpChannel} * or {@code null}. - * @param id the id to use for this instance or {@code null} if a new once should be - * generated * @param sctpChannel the underlying {@link SctpChannel} */ - public NioSctpChannel(Channel parent, Integer id, SctpChannel sctpChannel) { - super(parent, id, sctpChannel, SelectionKey.OP_READ); + public NioSctpChannel(Channel parent, SctpChannel sctpChannel) { + super(parent, sctpChannel, SelectionKey.OP_READ); try { sctpChannel.configureBlocking(false); config = new DefaultSctpChannelConfig(this, sctpChannel); diff --git a/transport-sctp/src/main/java/io/netty/channel/sctp/nio/NioSctpServerChannel.java b/transport-sctp/src/main/java/io/netty/channel/sctp/nio/NioSctpServerChannel.java index c2e52bdc90..7d7ae91a12 100644 --- a/transport-sctp/src/main/java/io/netty/channel/sctp/nio/NioSctpServerChannel.java +++ b/transport-sctp/src/main/java/io/netty/channel/sctp/nio/NioSctpServerChannel.java @@ -62,7 +62,7 @@ public class NioSctpServerChannel extends AbstractNioMessageChannel * Create a new instance */ public NioSctpServerChannel() { - super(null, null, newSocket(), SelectionKey.OP_ACCEPT); + super(null, newSocket(), SelectionKey.OP_ACCEPT); config = new DefaultSctpServerChannelConfig(this, javaChannel()); } @@ -139,7 +139,7 @@ public class NioSctpServerChannel extends AbstractNioMessageChannel if (ch == null) { return 0; } - buf.add(new NioSctpChannel(this, null, ch)); + buf.add(new NioSctpChannel(this, ch)); return 1; } diff --git a/transport-sctp/src/main/java/io/netty/channel/sctp/oio/OioSctpChannel.java b/transport-sctp/src/main/java/io/netty/channel/sctp/oio/OioSctpChannel.java index 09ac4d09d4..de0bfd4e82 100755 --- a/transport-sctp/src/main/java/io/netty/channel/sctp/oio/OioSctpChannel.java +++ b/transport-sctp/src/main/java/io/netty/channel/sctp/oio/OioSctpChannel.java @@ -96,7 +96,7 @@ public class OioSctpChannel extends AbstractOioMessageChannel * @param ch the {@link SctpChannel} which is used by this instance */ public OioSctpChannel(SctpChannel ch) { - this(null, null, ch); + this(null, ch); } /** @@ -104,11 +104,10 @@ public class OioSctpChannel extends AbstractOioMessageChannel * * @param parent the parent {@link Channel} which was used to create this instance. This can be null if the * {@link} has no parent as it was created by your self. - * @param id the id which should be used for this instance or {@code null} if a new one should be generated * @param ch the {@link SctpChannel} which is used by this instance */ - public OioSctpChannel(Channel parent, Integer id, SctpChannel ch) { - super(parent, id); + public OioSctpChannel(Channel parent, SctpChannel ch) { + super(parent); this.ch = ch; boolean success = false; try { diff --git a/transport-sctp/src/main/java/io/netty/channel/sctp/oio/OioSctpServerChannel.java b/transport-sctp/src/main/java/io/netty/channel/sctp/oio/OioSctpServerChannel.java index f3673f424f..f8f876fdfa 100755 --- a/transport-sctp/src/main/java/io/netty/channel/sctp/oio/OioSctpServerChannel.java +++ b/transport-sctp/src/main/java/io/netty/channel/sctp/oio/OioSctpServerChannel.java @@ -80,17 +80,7 @@ public class OioSctpServerChannel extends AbstractOioMessageChannel * @param sch the {@link SctpServerChannel} which is used by this instance */ public OioSctpServerChannel(SctpServerChannel sch) { - this(null, sch); - } - - /** - * Create a new instance from the given {@link SctpServerChannel} - * - * @param id the id which should be used for this instance or {@code null} if a new one should be generated - * @param sch the {@link SctpServerChannel} which is used by this instance - */ - public OioSctpServerChannel(Integer id, SctpServerChannel sch) { - super(null, id); + super(null); if (sch == null) { throw new NullPointerException("sctp server channel"); } @@ -206,7 +196,7 @@ public class OioSctpServerChannel extends AbstractOioMessageChannel if (key.isAcceptable()) { s = sch.accept(); if (s != null) { - buf.add(new OioSctpChannel(this, null, s)); + buf.add(new OioSctpChannel(this, s)); acceptedChannels ++; } } diff --git a/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtAcceptorChannel.java b/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtAcceptorChannel.java index 95ddff4339..bf83a93db5 100644 --- a/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtAcceptorChannel.java +++ b/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtAcceptorChannel.java @@ -34,16 +34,15 @@ import static java.nio.channels.SelectionKey.*; /** * Common base for Netty Byte/Message UDT Stream/Datagram acceptors. */ -public abstract class NioUdtAcceptorChannel extends AbstractNioMessageChannel - implements UdtServerChannel { +public abstract class NioUdtAcceptorChannel extends AbstractNioMessageChannel implements UdtServerChannel { - protected static final InternalLogger logger = InternalLoggerFactory - .getInstance(NioUdtAcceptorChannel.class); + protected static final InternalLogger logger = + InternalLoggerFactory.getInstance(NioUdtAcceptorChannel.class); private final UdtServerChannelConfig config; protected NioUdtAcceptorChannel(final ServerSocketChannelUDT channelUDT) { - super(null, channelUDT.socketUDT().id(), channelUDT, OP_ACCEPT); + super(null, channelUDT, OP_ACCEPT); try { channelUDT.configureBlocking(false); config = new DefaultUdtServerChannelConfig(this, channelUDT, true); diff --git a/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtByteAcceptorChannel.java b/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtByteAcceptorChannel.java index 09fe795441..7ffb3273bb 100644 --- a/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtByteAcceptorChannel.java +++ b/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtByteAcceptorChannel.java @@ -37,7 +37,7 @@ public class NioUdtByteAcceptorChannel extends NioUdtAcceptorChannel { if (channelUDT == null) { return 0; } else { - buf.add(new NioUdtByteConnectorChannel(this, channelUDT.socketUDT().id(), channelUDT)); + buf.add(new NioUdtByteConnectorChannel(this, channelUDT)); return 1; } } diff --git a/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtByteConnectorChannel.java b/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtByteConnectorChannel.java index 8ce766a540..cdb0f04487 100644 --- a/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtByteConnectorChannel.java +++ b/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtByteConnectorChannel.java @@ -37,11 +37,10 @@ import static java.nio.channels.SelectionKey.*; /** * Byte Channel Connector for UDT Streams. */ -public class NioUdtByteConnectorChannel extends AbstractNioByteChannel - implements UdtChannel { +public class NioUdtByteConnectorChannel extends AbstractNioByteChannel implements UdtChannel { - private static final InternalLogger logger = InternalLoggerFactory - .getInstance(NioUdtByteConnectorChannel.class); + private static final InternalLogger logger = + InternalLoggerFactory.getInstance(NioUdtByteConnectorChannel.class); private static final ChannelMetadata METADATA = new ChannelMetadata(false); @@ -51,9 +50,8 @@ public class NioUdtByteConnectorChannel extends AbstractNioByteChannel this(TypeUDT.STREAM); } - public NioUdtByteConnectorChannel(final Channel parent, final Integer id, - final SocketChannelUDT channelUDT) { - super(parent, id, channelUDT); + public NioUdtByteConnectorChannel(final Channel parent, final SocketChannelUDT channelUDT) { + super(parent, channelUDT); try { channelUDT.configureBlocking(false); switch (channelUDT.socketUDT().status()) { @@ -78,7 +76,7 @@ public class NioUdtByteConnectorChannel extends AbstractNioByteChannel } public NioUdtByteConnectorChannel(final SocketChannelUDT channelUDT) { - this(null, channelUDT.socketUDT().id(), channelUDT); + this(null, channelUDT); } public NioUdtByteConnectorChannel(final TypeUDT type) { diff --git a/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtMessageAcceptorChannel.java b/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtMessageAcceptorChannel.java index 0ccb785924..9977d6ff10 100644 --- a/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtMessageAcceptorChannel.java +++ b/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtMessageAcceptorChannel.java @@ -37,7 +37,7 @@ public class NioUdtMessageAcceptorChannel extends NioUdtAcceptorChannel { if (channelUDT == null) { return 0; } else { - buf.add(new NioUdtMessageConnectorChannel(this, channelUDT.socketUDT().id(), channelUDT)); + buf.add(new NioUdtMessageConnectorChannel(this, channelUDT)); return 1; } } diff --git a/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtMessageConnectorChannel.java b/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtMessageConnectorChannel.java index c1fb1d39f5..745e029573 100644 --- a/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtMessageConnectorChannel.java +++ b/transport-udt/src/main/java/io/netty/channel/udt/nio/NioUdtMessageConnectorChannel.java @@ -41,11 +41,10 @@ import static java.nio.channels.SelectionKey.*; *
* Note: send/receive must use {@link UdtMessage} in the pipeline */ -public class NioUdtMessageConnectorChannel extends AbstractNioMessageChannel - implements UdtChannel { +public class NioUdtMessageConnectorChannel extends AbstractNioMessageChannel implements UdtChannel { - private static final InternalLogger logger = InternalLoggerFactory - .getInstance(NioUdtMessageConnectorChannel.class); + private static final InternalLogger logger = + InternalLoggerFactory.getInstance(NioUdtMessageConnectorChannel.class); private static final ChannelMetadata METADATA = new ChannelMetadata(false); @@ -55,9 +54,8 @@ public class NioUdtMessageConnectorChannel extends AbstractNioMessageChannel this(TypeUDT.DATAGRAM); } - public NioUdtMessageConnectorChannel(final Channel parent, - final Integer id, final SocketChannelUDT channelUDT) { - super(parent, id, channelUDT, OP_READ); + public NioUdtMessageConnectorChannel(final Channel parent, final SocketChannelUDT channelUDT) { + super(parent, channelUDT, OP_READ); try { channelUDT.configureBlocking(false); switch (channelUDT.socketUDT().status()) { @@ -82,7 +80,7 @@ public class NioUdtMessageConnectorChannel extends AbstractNioMessageChannel } public NioUdtMessageConnectorChannel(final SocketChannelUDT channelUDT) { - this(null, channelUDT.socketUDT().id(), channelUDT); + this(null, channelUDT); } public NioUdtMessageConnectorChannel(final TypeUDT type) { diff --git a/transport/src/main/java/io/netty/channel/AbstractChannel.java b/transport/src/main/java/io/netty/channel/AbstractChannel.java index dc96709884..db2e6326a6 100644 --- a/transport/src/main/java/io/netty/channel/AbstractChannel.java +++ b/transport/src/main/java/io/netty/channel/AbstractChannel.java @@ -39,23 +39,8 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha private static final InternalLogger logger = InternalLoggerFactory.getInstance(AbstractChannel.class); - /** - * Generates a negative unique integer ID. This method generates only - * negative integers to avoid conflicts with user-specified IDs where only - * non-negative integers are allowed. - */ - private static Integer allocateId() { - int idVal = ThreadLocalRandom.current().nextInt(); - if (idVal > 0) { - idVal = -idVal; - } else if (idVal == 0) { - idVal = -1; - } - return idVal; - } - private final Channel parent; - private final Integer id; + private final long hashCode = ThreadLocalRandom.current().nextLong(); private final Unsafe unsafe; private final DefaultChannelPipeline pipeline; private final ChannelOutboundBuffer outboundBuffer = new ChannelOutboundBuffer(this); @@ -80,24 +65,11 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha /** * Creates a new instance. * - * @param id - * the unique non-negative integer ID of this channel. - * Specify {@code null} to auto-generate a unique negative integer - * ID. * @param parent * the parent of this channel. {@code null} if there's no parent. */ - protected AbstractChannel(Channel parent, Integer id) { - if (id == null) { - id = allocateId(); - } else { - if (id.intValue() < 0) { - throw new IllegalArgumentException("id: " + id + " (expected: >= 0)"); - } - } - + protected AbstractChannel(Channel parent) { this.parent = parent; - this.id = id; unsafe = newUnsafe(); pipeline = new DefaultChannelPipeline(this); } @@ -300,7 +272,7 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha */ @Override public final int hashCode() { - return id; + return (int) hashCode; } /** @@ -314,10 +286,25 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha @Override public final int compareTo(Channel o) { - if (o instanceof AbstractChannel) { - return id.compareTo(((AbstractChannel) o).id); + if (this == o) { + return 0; } - return id.compareTo(Integer.valueOf(o.hashCode())); + + long ret = hashCode - o.hashCode(); + if (ret > 0) { + return 1; + } + if (ret < 0) { + return -1; + } + + ret = System.identityHashCode(this) - System.identityHashCode(o); + if (ret != 0) { + return (int) ret; + } + + // Jackpot! - different objects with same hashes + throw new Error(); } /** @@ -345,11 +332,11 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha srcAddr = remoteAddr; dstAddr = localAddr; } - strVal = String.format("[id: 0x%08x, %s %s %s]", id, srcAddr, active? "=>" : ":>", dstAddr); + strVal = String.format("[id: 0x%08x, %s %s %s]", (int) hashCode, srcAddr, active? "=>" : ":>", dstAddr); } else if (localAddr != null) { - strVal = String.format("[id: 0x%08x, %s]", id, localAddr); + strVal = String.format("[id: 0x%08x, %s]", (int) hashCode, localAddr); } else { - strVal = String.format("[id: 0x%08x]", id); + strVal = String.format("[id: 0x%08x]", (int) hashCode); } strValActive = active; diff --git a/transport/src/main/java/io/netty/channel/AbstractServerChannel.java b/transport/src/main/java/io/netty/channel/AbstractServerChannel.java index 7d135c638d..946fdc4f27 100755 --- a/transport/src/main/java/io/netty/channel/AbstractServerChannel.java +++ b/transport/src/main/java/io/netty/channel/AbstractServerChannel.java @@ -36,8 +36,8 @@ public abstract class AbstractServerChannel extends AbstractChannel implements S /** * Creates a new instance. */ - protected AbstractServerChannel(Integer id) { - super(null, id); + protected AbstractServerChannel() { + super(null); } @Override diff --git a/transport/src/main/java/io/netty/channel/MessageList.java b/transport/src/main/java/io/netty/channel/MessageList.java index 3a3d5b0c49..0091a504a1 100644 --- a/transport/src/main/java/io/netty/channel/MessageList.java +++ b/transport/src/main/java/io/netty/channel/MessageList.java @@ -47,6 +47,42 @@ import java.util.NoSuchElementException; * *
* + *+ * The following example shows how to iterate over the list which contains {@code ReferenceCounted} messages safely + * (i.e. without leaking them) while consuming the messages. + *
+ *+ * public void messageReceived({@link ChannelHandlerContext} ctx, {@link MessageList}<Object> msgs) { + * final int size = msgs.size(); + * final Object[] in = msgs.array(); + * boolean success = false; + * try { + * for (int i = 0; i < size; i ++) { + * Object m = in[i]; + * + * // Handle the message. + * doSomethingWithMessage(m); + * + * // Release the handled message. + * {@link ReferenceCountUtil#release(Object) ReferenceCountUtil.release(m)}; + * + * // To prevent {@link #releaseAllAndRecycle()} from releasing the message again, + * // replace the message with a dummy object. + * in[i] = MessageList.NONE; + * } + * + * success = true; + * } finally { + * if (success) { + * {@link #recycle() msgs.recycle()}; + * } else { + * {@link #releaseAllAndRecycle() msgs.releaseAllAndRecycle()}; + * } + * } + * } + *+ * * @param