io.netty.channel.Channel.Unsafe Java Examples

The following examples show how to use io.netty.channel.Channel.Unsafe. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IdleStateHandler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * @see #hasOutputChanged(ChannelHandlerContext, boolean)
 */
private void initOutputChanged(ChannelHandlerContext ctx) {
    if (observeOutput) {
        Channel channel = ctx.channel();
        Unsafe unsafe = channel.unsafe();
        ChannelOutboundBuffer buf = unsafe.outboundBuffer();

        if (buf != null) {
            lastMessageHashCode = System.identityHashCode(buf.current());
            lastPendingWriteBytes = buf.totalPendingWriteBytes();
        }
    }
}
 
Example #2
Source File: IdleStateHandler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * Returns {@code true} if and only if the {@link IdleStateHandler} was constructed
 * with {@link #observeOutput} enabled and there has been an observed change in the
 * {@link ChannelOutboundBuffer} between two consecutive calls of this method.
 * 如果且仅当IdleStateHandler被构造为启用了observeOutput并且该方法的两个连续调用之间的ChannelOutboundBuffer中出现了观察到的更改时,返回true。
 *
 * https://github.com/netty/netty/issues/6150
 */
private boolean hasOutputChanged(ChannelHandlerContext ctx, boolean first) {
    if (observeOutput) {

        // We can take this shortcut if the ChannelPromises that got passed into write()
        // appear to complete. It indicates "change" on message level and we simply assume
        // that there's change happening on byte level. If the user doesn't observe channel
        // writability events then they'll eventually OOME and there's clearly a different
        // problem and idleness is least of their concerns.
        if (lastChangeCheckTimeStamp != lastWriteTime) {
            lastChangeCheckTimeStamp = lastWriteTime;

            // But this applies only if it's the non-first call.
            if (!first) {
                return true;
            }
        }

        Channel channel = ctx.channel();
        Unsafe unsafe = channel.unsafe();
        ChannelOutboundBuffer buf = unsafe.outboundBuffer();

        if (buf != null) {
            int messageHashCode = System.identityHashCode(buf.current());
            long pendingWriteBytes = buf.totalPendingWriteBytes();

            if (messageHashCode != lastMessageHashCode || pendingWriteBytes != lastPendingWriteBytes) {
                lastMessageHashCode = messageHashCode;
                lastPendingWriteBytes = pendingWriteBytes;

                if (!first) {
                    return true;
                }
            }
        }
    }

    return false;
}