Search in sources :

Example 61 with ByteBuf

use of io.netty.buffer.ByteBuf in project netty by netty.

the class GlobalChannelTrafficShapingHandler method handlerRemoved.

@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
    trafficCounter.resetCumulativeTime();
    Channel channel = ctx.channel();
    Integer key = channel.hashCode();
    PerChannel perChannel = channelQueues.remove(key);
    if (perChannel != null) {
        // write operations need synchronization
        synchronized (perChannel) {
            if (channel.isActive()) {
                for (ToSend toSend : perChannel.messagesQueue) {
                    long size = calculateSize(toSend.toSend);
                    trafficCounter.bytesRealWriteFlowControl(size);
                    perChannel.channelTrafficCounter.bytesRealWriteFlowControl(size);
                    perChannel.queueSize -= size;
                    queuesSize.addAndGet(-size);
                    ctx.write(toSend.toSend, toSend.promise);
                }
            } else {
                queuesSize.addAndGet(-perChannel.queueSize);
                for (ToSend toSend : perChannel.messagesQueue) {
                    if (toSend.toSend instanceof ByteBuf) {
                        ((ByteBuf) toSend.toSend).release();
                    }
                }
            }
            perChannel.messagesQueue.clear();
        }
    }
    releaseWriteSuspended(ctx);
    releaseReadSuspended(ctx);
    super.handlerRemoved(ctx);
}
Also used : Channel(io.netty.channel.Channel) ByteBuf(io.netty.buffer.ByteBuf)

Example 62 with ByteBuf

use of io.netty.buffer.ByteBuf in project netty by netty.

the class GlobalTrafficShapingHandler method handlerRemoved.

@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
    Channel channel = ctx.channel();
    Integer key = channel.hashCode();
    PerChannel perChannel = channelQueues.remove(key);
    if (perChannel != null) {
        // write operations need synchronization
        synchronized (perChannel) {
            if (channel.isActive()) {
                for (ToSend toSend : perChannel.messagesQueue) {
                    long size = calculateSize(toSend.toSend);
                    trafficCounter.bytesRealWriteFlowControl(size);
                    perChannel.queueSize -= size;
                    queuesSize.addAndGet(-size);
                    ctx.write(toSend.toSend, toSend.promise);
                }
            } else {
                queuesSize.addAndGet(-perChannel.queueSize);
                for (ToSend toSend : perChannel.messagesQueue) {
                    if (toSend.toSend instanceof ByteBuf) {
                        ((ByteBuf) toSend.toSend).release();
                    }
                }
            }
            perChannel.messagesQueue.clear();
        }
    }
    releaseWriteSuspended(ctx);
    releaseReadSuspended(ctx);
    super.handlerRemoved(ctx);
}
Also used : Channel(io.netty.channel.Channel) ByteBuf(io.netty.buffer.ByteBuf)

Example 63 with ByteBuf

use of io.netty.buffer.ByteBuf in project netty by netty.

the class OptionalSslHandlerTest method handlerReplaced.

@Test
public void handlerReplaced() throws Exception {
    final ChannelHandler nonSslHandler = Mockito.mock(ChannelHandler.class);
    OptionalSslHandler handler = new OptionalSslHandler(sslContext) {

        @Override
        protected ChannelHandler newNonSslHandler(ChannelHandlerContext context) {
            return nonSslHandler;
        }

        @Override
        protected String newNonSslHandlerName() {
            return HANDLER_NAME;
        }
    };
    final ByteBuf payload = Unpooled.copiedBuffer("plaintext".getBytes());
    try {
        handler.decode(context, payload, null);
        verify(pipeline).replace(handler, HANDLER_NAME, nonSslHandler);
    } finally {
        payload.release();
    }
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 64 with ByteBuf

use of io.netty.buffer.ByteBuf in project netty by netty.

the class OptionalSslHandlerTest method sslHandlerReplaced.

@Test
public void sslHandlerReplaced() throws Exception {
    final SslHandler sslHandler = Mockito.mock(SslHandler.class);
    OptionalSslHandler handler = new OptionalSslHandler(sslContext) {

        @Override
        protected SslHandler newSslHandler(ChannelHandlerContext context, SslContext sslContext) {
            return sslHandler;
        }

        @Override
        protected String newSslHandlerName() {
            return SSL_HANDLER_NAME;
        }
    };
    final ByteBuf payload = Unpooled.wrappedBuffer(new byte[] { 22, 3, 1, 0, 5 });
    try {
        handler.decode(context, payload, null);
        verify(pipeline).replace(handler, SSL_HANDLER_NAME, sslHandler);
    } finally {
        payload.release();
    }
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 65 with ByteBuf

use of io.netty.buffer.ByteBuf in project netty by netty.

the class OptionalSslHandlerTest method decodeBuffered.

@Test
public void decodeBuffered() throws Exception {
    OptionalSslHandler handler = new OptionalSslHandler(sslContext);
    final ByteBuf payload = Unpooled.wrappedBuffer(new byte[] { 22, 3 });
    try {
        handler.decode(context, payload, null);
        verifyZeroInteractions(pipeline);
    } finally {
        payload.release();
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)1557 Test (org.junit.Test)668 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)162 IOException (java.io.IOException)99 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)89 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)81 Test (org.testng.annotations.Test)68 InetSocketAddress (java.net.InetSocketAddress)60 Channel (io.netty.channel.Channel)57 ChannelFuture (io.netty.channel.ChannelFuture)56 ArrayList (java.util.ArrayList)55 Map (java.util.Map)45 ChannelPromise (io.netty.channel.ChannelPromise)41 AtomicReference (java.util.concurrent.atomic.AtomicReference)36 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)35 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)34 HashMap (java.util.HashMap)34 CountDownLatch (java.util.concurrent.CountDownLatch)34 RecyclableDuplicateByteBuf (io.netty.buffer.RecyclableDuplicateByteBuf)32 EventLoopGroup (io.netty.channel.EventLoopGroup)32