Search in sources :

Example 16 with ByteBufAllocator

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

the class AbstractOioByteChannel method doRead.

@Override
protected void doRead() {
    final ChannelConfig config = config();
    if (isInputShutdown() || !readPending) {
        // during the same read loop readPending was set to false.
        return;
    }
    // In OIO we should set readPending to false even if the read was not successful so we can schedule
    // another read on the event loop if no reads are done.
    readPending = false;
    final ChannelPipeline pipeline = pipeline();
    final ByteBufAllocator allocator = config.getAllocator();
    final RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
    allocHandle.reset(config);
    ByteBuf byteBuf = null;
    boolean close = false;
    boolean readData = false;
    try {
        byteBuf = allocHandle.allocate(allocator);
        do {
            allocHandle.lastBytesRead(doReadBytes(byteBuf));
            if (allocHandle.lastBytesRead() <= 0) {
                if (!byteBuf.isReadable()) {
                    // nothing was read. release the buffer.
                    byteBuf.release();
                    byteBuf = null;
                    close = allocHandle.lastBytesRead() < 0;
                }
                break;
            } else {
                readData = true;
            }
            final int available = available();
            if (available <= 0) {
                break;
            }
            // Oio collects consecutive read operations into 1 ByteBuf before propagating up the pipeline.
            if (!byteBuf.isWritable()) {
                final int capacity = byteBuf.capacity();
                final int maxCapacity = byteBuf.maxCapacity();
                if (capacity == maxCapacity) {
                    allocHandle.incMessagesRead(1);
                    readPending = false;
                    pipeline.fireChannelRead(byteBuf);
                    byteBuf = allocHandle.allocate(allocator);
                } else {
                    final int writerIndex = byteBuf.writerIndex();
                    if (writerIndex + available > maxCapacity) {
                        byteBuf.capacity(maxCapacity);
                    } else {
                        byteBuf.ensureWritable(available);
                    }
                }
            }
        } while (allocHandle.continueReading());
        if (byteBuf != null) {
            // it because allocHandle.continueReading() returned false.
            if (byteBuf.isReadable()) {
                readPending = false;
                pipeline.fireChannelRead(byteBuf);
            } else {
                byteBuf.release();
            }
            byteBuf = null;
        }
        if (readData) {
            allocHandle.readComplete();
            pipeline.fireChannelReadComplete();
        }
        if (close) {
            closeOnRead(pipeline);
        }
    } catch (Throwable t) {
        handleReadException(pipeline, byteBuf, t, close, allocHandle);
    } finally {
        if (readPending || config.isAutoRead() || !readData && isActive()) {
            // Reading 0 bytes could mean there is a SocketTimeout and no data was actually read, so we
            // should execute read() again because no data may have been read.
            read();
        }
    }
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) RecvByteBufAllocator(io.netty.channel.RecvByteBufAllocator) RecvByteBufAllocator(io.netty.channel.RecvByteBufAllocator) ChannelConfig(io.netty.channel.ChannelConfig) ByteBuf(io.netty.buffer.ByteBuf) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 17 with ByteBufAllocator

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

the class NioSctpChannel method doWriteMessage.

@Override
protected boolean doWriteMessage(Object msg, ChannelOutboundBuffer in) throws Exception {
    SctpMessage packet = (SctpMessage) msg;
    ByteBuf data = packet.content();
    int dataLen = data.readableBytes();
    if (dataLen == 0) {
        return true;
    }
    ByteBufAllocator alloc = alloc();
    boolean needsCopy = data.nioBufferCount() != 1;
    if (!needsCopy) {
        if (!data.isDirect() && alloc.isDirectBufferPooled()) {
            needsCopy = true;
        }
    }
    ByteBuffer nioData;
    if (!needsCopy) {
        nioData = data.nioBuffer();
    } else {
        data = alloc.directBuffer(dataLen).writeBytes(data);
        nioData = data.nioBuffer();
    }
    final MessageInfo mi = MessageInfo.createOutgoing(association(), null, packet.streamIdentifier());
    mi.payloadProtocolID(packet.protocolIdentifier());
    mi.streamNumber(packet.streamIdentifier());
    mi.unordered(packet.isUnordered());
    final int writtenBytes = javaChannel().send(nioData, mi);
    return writtenBytes > 0;
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) RecvByteBufAllocator(io.netty.channel.RecvByteBufAllocator) ByteBuf(io.netty.buffer.ByteBuf) SctpMessage(io.netty.channel.sctp.SctpMessage) ByteBuffer(java.nio.ByteBuffer) MessageInfo(com.sun.nio.sctp.MessageInfo)

Example 18 with ByteBufAllocator

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

the class ChunkedWriteHandlerTest method testChunkedMessageInput.

@Test
public void testChunkedMessageInput() {
    ChunkedInput<Object> input = new ChunkedInput<Object>() {

        private boolean done;

        @Override
        public boolean isEndOfInput() throws Exception {
            return done;
        }

        @Override
        public void close() throws Exception {
        // NOOP
        }

        @Deprecated
        @Override
        public Object readChunk(ChannelHandlerContext ctx) throws Exception {
            return readChunk(ctx.alloc());
        }

        @Override
        public Object readChunk(ByteBufAllocator ctx) throws Exception {
            if (done) {
                return false;
            }
            done = true;
            return 0;
        }

        @Override
        public long length() {
            return -1;
        }

        @Override
        public long progress() {
            return 1;
        }
    };
    EmbeddedChannel ch = new EmbeddedChannel(new ChunkedWriteHandler());
    ch.writeAndFlush(input).syncUninterruptibly();
    ch.checkException();
    assertTrue(ch.finish());
    assertEquals(0, ch.readOutbound());
    assertNull(ch.readOutbound());
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Test(org.junit.Test)

Example 19 with ByteBufAllocator

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

the class ChunkedWriteHandlerTest method testListenerNotifiedWhenIsEnd.

// Test case which shows that there is not a bug like stated here:
// http://stackoverflow.com/a/10426305
@Test
public void testListenerNotifiedWhenIsEnd() {
    ByteBuf buffer = Unpooled.copiedBuffer("Test", CharsetUtil.ISO_8859_1);
    ChunkedInput<ByteBuf> input = new ChunkedInput<ByteBuf>() {

        private boolean done;

        private final ByteBuf buffer = Unpooled.copiedBuffer("Test", CharsetUtil.ISO_8859_1);

        @Override
        public boolean isEndOfInput() throws Exception {
            return done;
        }

        @Override
        public void close() throws Exception {
            buffer.release();
        }

        @Deprecated
        @Override
        public ByteBuf readChunk(ChannelHandlerContext ctx) throws Exception {
            return readChunk(ctx.alloc());
        }

        @Override
        public ByteBuf readChunk(ByteBufAllocator allocator) throws Exception {
            if (done) {
                return null;
            }
            done = true;
            return buffer.retainedDuplicate();
        }

        @Override
        public long length() {
            return -1;
        }

        @Override
        public long progress() {
            return 1;
        }
    };
    final AtomicBoolean listenerNotified = new AtomicBoolean(false);
    final ChannelFutureListener listener = new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            listenerNotified.set(true);
        }
    };
    EmbeddedChannel ch = new EmbeddedChannel(new ChunkedWriteHandler());
    ch.writeAndFlush(input).addListener(listener).syncUninterruptibly();
    ch.checkException();
    ch.finish();
    // the listener should have been notified
    assertTrue(listenerNotified.get());
    ByteBuf buffer2 = ch.readOutbound();
    assertEquals(buffer, buffer2);
    assertNull(ch.readOutbound());
    buffer.release();
    buffer2.release();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Test(org.junit.Test)

Example 20 with ByteBufAllocator

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

the class AbstractEpollChannel method newDirectBuffer.

/**
     * Returns an off-heap copy of the specified {@link ByteBuf}, and releases the specified holder.
     * The caller must ensure that the holder releases the original {@link ByteBuf} when the holder is released by
     * this method.
     */
protected final ByteBuf newDirectBuffer(Object holder, ByteBuf buf) {
    final int readableBytes = buf.readableBytes();
    if (readableBytes == 0) {
        ReferenceCountUtil.safeRelease(holder);
        return Unpooled.EMPTY_BUFFER;
    }
    final ByteBufAllocator alloc = alloc();
    if (alloc.isDirectBufferPooled()) {
        return newDirectBuffer0(holder, buf, alloc, readableBytes);
    }
    final ByteBuf directBuf = ByteBufUtil.threadLocalDirectBuffer();
    if (directBuf == null) {
        return newDirectBuffer0(holder, buf, alloc, readableBytes);
    }
    directBuf.writeBytes(buf, buf.readerIndex(), readableBytes);
    ReferenceCountUtil.safeRelease(holder);
    return directBuf;
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) RecvByteBufAllocator(io.netty.channel.RecvByteBufAllocator) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

ByteBufAllocator (io.netty.buffer.ByteBufAllocator)27 ByteBuf (io.netty.buffer.ByteBuf)12 Channel (io.netty.channel.Channel)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 RecvByteBufAllocator (io.netty.channel.RecvByteBufAllocator)3 URI (java.net.URI)3 Test (org.junit.Test)3 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)2 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)2 UnpooledByteBufAllocator (io.netty.buffer.UnpooledByteBufAllocator)2 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelFutureListener (io.netty.channel.ChannelFutureListener)2 ChannelPipeline (io.netty.channel.ChannelPipeline)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 Publisher (org.reactivestreams.Publisher)2 Context (ratpack.handling.Context)2 Streams (ratpack.stream.Streams)2 MetricFilter (com.codahale.metrics.MetricFilter)1