io.netty.channel.RecvByteBufAllocator Java Examples

The following examples show how to use io.netty.channel.RecvByteBufAllocator. 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: Http2MultiplexCodec.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
void doRead0(Http2Frame frame, RecvByteBufAllocator.Handle allocHandle) {
    int numBytesToBeConsumed = 0;
    if (frame instanceof Http2DataFrame) {
        numBytesToBeConsumed = ((Http2DataFrame) frame).initialFlowControlledBytes();
        allocHandle.lastBytesRead(numBytesToBeConsumed);
    } else {
        allocHandle.lastBytesRead(MIN_HTTP2_FRAME_SIZE);
    }
    allocHandle.incMessagesRead(1);
    pipeline().fireChannelRead(frame);

    if (numBytesToBeConsumed != 0) {
        try {
            writeDoneAndNoFlush |= onBytesConsumed(ctx, stream, numBytesToBeConsumed);
        } catch (Http2Exception e) {
            pipeline().fireExceptionCaught(e);
        }
    }
}
 
Example #2
Source File: AbstractEpollStreamChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
protected final int spliceIn(FileDescriptor pipeOut, RecvByteBufAllocator.Handle handle) throws IOException {
    // calculate the maximum amount of data we are allowed to splice
    int length = Math.min(handle.guess(), len);
    int splicedIn = 0;
    for (;;) {
        // Splicing until there is nothing left to splice.
        int localSplicedIn = Native.splice(socket.intValue(), -1, pipeOut.intValue(), -1, length);
        if (localSplicedIn == 0) {
            break;
        }
        splicedIn += localSplicedIn;
        length -= localSplicedIn;
    }

    return splicedIn;
}
 
Example #3
Source File: AbstractNioByteChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
private void handleReadException(ChannelPipeline pipeline, ByteBuf byteBuf, Throwable cause, boolean close,
        RecvByteBufAllocator.Handle allocHandle) {
    if (byteBuf != null) {
        if (byteBuf.isReadable()) {
            readPending = false;
            pipeline.fireChannelRead(byteBuf);
        } else {
            byteBuf.release();
        }
    }
    allocHandle.readComplete();
    pipeline.fireChannelReadComplete();
    pipeline.fireExceptionCaught(cause);
    if (close || cause instanceof IOException) {
        closeOnRead(pipeline);
    }
}
 
Example #4
Source File: AbstractOioByteChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
private void handleReadException(ChannelPipeline pipeline, ByteBuf byteBuf, Throwable cause, boolean close,
        RecvByteBufAllocator.Handle allocHandle) {
    if (byteBuf != null) {
        if (byteBuf.isReadable()) {
            readPending = false;
            pipeline.fireChannelRead(byteBuf);
        } else {
            byteBuf.release();
        }
    }
    allocHandle.readComplete();
    pipeline.fireChannelReadComplete();
    pipeline.fireExceptionCaught(cause);
    if (close || cause instanceof IOException) {
        closeOnRead(pipeline);
    }
}
 
Example #5
Source File: VirtualChannel.java    From quarkus with Apache License 2.0 5 votes vote down vote up
protected void readInbound() {
    RecvByteBufAllocator.Handle handle = unsafe().recvBufAllocHandle();
    handle.reset(config());
    ChannelPipeline pipeline = pipeline();
    do {
        Object received = inboundBuffer.poll();
        if (received == null) {
            break;
        }
        pipeline.fireChannelRead(received);
    } while (handle.continueReading());

    pipeline.fireChannelReadComplete();
}
 
Example #6
Source File: NioSctpChannel.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
protected int doReadMessages(List<Object> buf) throws Exception {
    SctpChannel ch = javaChannel();

    RecvByteBufAllocator.Handle allocHandle = this.allocHandle;
    if (allocHandle == null) {
        this.allocHandle = allocHandle = config().getRecvByteBufAllocator().newHandle();
    }
    ByteBuf buffer = allocHandle.allocate(config().getAllocator());
    boolean free = true;
    try {
        ByteBuffer data = buffer.internalNioBuffer(buffer.writerIndex(), buffer.writableBytes());
        int pos = data.position();

        MessageInfo messageInfo = ch.receive(data, null, notificationHandler);
        if (messageInfo == null) {
            return 0;
        }
        buf.add(new SctpMessage(messageInfo, buffer.writerIndex(buffer.writerIndex() + data.position() - pos)));
        free = false;
        return 1;
    } catch (Throwable cause) {
        PlatformDependent.throwException(cause);
        return -1;
    }  finally {
        int bytesRead = buffer.readableBytes();
        allocHandle.record(bytesRead);
        if (free) {
            buffer.release();
        }
    }
}
 
Example #7
Source File: Http2MultiplexCodec.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public ChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
    if (!(allocator.newHandle() instanceof RecvByteBufAllocator.ExtendedHandle)) {
        throw new IllegalArgumentException("allocator.newHandle() must return an object of type: " +
                RecvByteBufAllocator.ExtendedHandle.class);
    }
    super.setRecvByteBufAllocator(allocator);
    return this;
}
 
Example #8
Source File: Http2MultiplexCodec.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public RecvByteBufAllocator.ExtendedHandle recvBufAllocHandle() {
    if (recvHandle == null) {
        recvHandle = (RecvByteBufAllocator.ExtendedHandle) config().getRecvByteBufAllocator().newHandle();
    }
    return recvHandle;
}
 
Example #9
Source File: UkcpServerChannel.java    From kcp-netty with MIT License 5 votes vote down vote up
@Override
protected int doReadMessages(List<Object> buf) throws Exception {
    DatagramChannel ch = javaChannel();
    UkcpServerChannelConfig config = config();
    RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();

    ByteBuf data = allocHandle.allocate(config.getAllocator());
    allocHandle.attemptedBytesRead(data.writableBytes());
    boolean free = true;
    try {
        ByteBuffer nioData = data.internalNioBuffer(data.writerIndex(), data.writableBytes());
        int pos = nioData.position();
        InetSocketAddress remoteAddress = (InetSocketAddress) ch.receive(nioData);
        if (remoteAddress == null) {
            return 0;
        }

        allocHandle.lastBytesRead(nioData.position() - pos);
        buf.add(UkcpPacket.newInstance(data.writerIndex(data.writerIndex() + allocHandle.lastBytesRead()),
                remoteAddress));
        free = false;
        return 1;
    } catch (Throwable cause) {
        PlatformDependent.throwException(cause);
        return -1;
    } finally {
        if (free) {
            data.release();
        }
    }
}
 
Example #10
Source File: VirtualServerChannel.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private void readInbound() {
    RecvByteBufAllocator.Handle handle = unsafe().recvBufAllocHandle();
    handle.reset(config());
    ChannelPipeline pipeline = pipeline();
    do {
        Object m = inboundBuffer.poll();
        if (m == null) {
            break;
        }
        pipeline.fireChannelRead(m);
    } while (handle.continueReading());

    pipeline.fireChannelReadComplete();
}
 
Example #11
Source File: NioDatagramChannel.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
protected int doReadMessages(List<Object> buf) throws Exception {
    DatagramChannel ch = javaChannel();
    DatagramChannelConfig config = config();
    RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();

    ByteBuf data = allocHandle.allocate(config.getAllocator());
    allocHandle.attemptedBytesRead(data.writableBytes());
    boolean free = true;
    try {
        ByteBuffer nioData = data.internalNioBuffer(data.writerIndex(), data.writableBytes());
        int pos = nioData.position();
        InetSocketAddress remoteAddress = (InetSocketAddress) ch.receive(nioData);
        if (remoteAddress == null) {
            return 0;
        }

        allocHandle.lastBytesRead(nioData.position() - pos);
        buf.add(new DatagramPacket(data.writerIndex(data.writerIndex() + allocHandle.lastBytesRead()),
                localAddress(), remoteAddress));
        free = false;
        return 1;
    } catch (Throwable cause) {
        PlatformDependent.throwException(cause);
        return -1;
    }  finally {
        if (free) {
            data.release();
        }
    }
}
 
Example #12
Source File: AbstractEpollChannel.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public EpollRecvByteAllocatorHandle recvBufAllocHandle() {
    if (allocHandle == null) {
        allocHandle = newEpollHandle((RecvByteBufAllocator.ExtendedHandle) super.recvBufAllocHandle());
    }
    return allocHandle;
}
 
Example #13
Source File: AbstractEpollStreamChannel.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public boolean spliceIn(RecvByteBufAllocator.Handle handle) {
    assert eventLoop().inEventLoop();
    if (len == 0) {
        promise.setSuccess();
        return true;
    }

    try {
        FileDescriptor[] pipe = pipe();
        FileDescriptor pipeIn = pipe[0];
        FileDescriptor pipeOut = pipe[1];
        try {
            int splicedIn = spliceIn(pipeOut, handle);
            if (splicedIn > 0) {
                // Integer.MAX_VALUE is a special value which will result in splice forever.
                if (len != Integer.MAX_VALUE) {
                    len -= splicedIn;
                }
                do {
                    int splicedOut = Native.splice(pipeIn.intValue(), -1, fd.intValue(), offset, splicedIn);
                    splicedIn -= splicedOut;
                } while (splicedIn > 0);
                if (len == 0) {
                    promise.setSuccess();
                    return true;
                }
            }
            return false;
        } finally {
            safeClosePipe(pipeIn);
            safeClosePipe(pipeOut);
        }
    } catch (Throwable cause) {
        promise.setFailure(cause);
        return true;
    }
}
 
Example #14
Source File: NioSctpChannel.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
protected int doReadMessages(List<Object> buf) throws Exception {
    SctpChannel ch = javaChannel();

    RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
    ByteBuf buffer = allocHandle.allocate(config().getAllocator());
    boolean free = true;
    try {
        ByteBuffer data = buffer.internalNioBuffer(buffer.writerIndex(), buffer.writableBytes());
        int pos = data.position();

        MessageInfo messageInfo = ch.receive(data, null, notificationHandler);
        if (messageInfo == null) {
            return 0;
        }

        allocHandle.lastBytesRead(data.position() - pos);
        buf.add(new SctpMessage(messageInfo,
                buffer.writerIndex(buffer.writerIndex() + allocHandle.lastBytesRead())));
        free = false;
        return 1;
    } catch (Throwable cause) {
        PlatformDependent.throwException(cause);
        return -1;
    }  finally {
        if (free) {
            buffer.release();
        }
    }
}
 
Example #15
Source File: KQueueChannelConfig.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public KQueueChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
    if (!(allocator.newHandle() instanceof RecvByteBufAllocator.ExtendedHandle)) {
        throw new IllegalArgumentException("allocator.newHandle() must return an object of type: " +
                RecvByteBufAllocator.ExtendedHandle.class);
    }
    super.setRecvByteBufAllocator(allocator);
    return this;
}
 
Example #16
Source File: AbstractKQueueChannel.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public KQueueRecvByteAllocatorHandle recvBufAllocHandle() {
    if (allocHandle == null) {
        allocHandle = new KQueueRecvByteAllocatorHandle(
                (RecvByteBufAllocator.ExtendedHandle) super.recvBufAllocHandle());
    }
    return allocHandle;
}
 
Example #17
Source File: EpollChannelConfig.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public EpollChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
    if (!(allocator.newHandle() instanceof RecvByteBufAllocator.ExtendedHandle)) {
        throw new IllegalArgumentException("allocator.newHandle() must return an object of type: " +
                RecvByteBufAllocator.ExtendedHandle.class);
    }
    super.setRecvByteBufAllocator(allocator);
    return this;
}
 
Example #18
Source File: EpollRecvByteAllocatorHandle.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
EpollRecvByteAllocatorHandle(RecvByteBufAllocator.ExtendedHandle handle) {
    this.delegate = ObjectUtil.checkNotNull(handle, "handle");
}
 
Example #19
Source File: EpollServerChannelConfig.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
public EpollServerChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
    super.setRecvByteBufAllocator(allocator);
    return this;
}
 
Example #20
Source File: DefaultUdtChannelConfig.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
public UdtChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
    super.setRecvByteBufAllocator(allocator);
    return this;
}
 
Example #21
Source File: EpollRecvByteAllocatorStreamingHandle.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
public EpollRecvByteAllocatorStreamingHandle(RecvByteBufAllocator.ExtendedHandle handle) {
    super(handle);
}
 
Example #22
Source File: DefaultSctpChannelConfig.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
public SctpChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
    super.setRecvByteBufAllocator(allocator);
    return this;
}
 
Example #23
Source File: OioSocketChannelConfig.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
OioSocketChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator);
 
Example #24
Source File: AbstractXnioSocketChannel.java    From netty-xnio-transport with Apache License 2.0 4 votes vote down vote up
@Override
public void handleEvent(ConduitStreamSourceChannel channel) {
    final ChannelConfig config = config();
    final ChannelPipeline pipeline = pipeline();
    final ByteBufAllocator allocator = config.getAllocator();
    final int maxMessagesPerRead = config.getMaxMessagesPerRead();
    RecvByteBufAllocator.Handle allocHandle = this.allocHandle;
    if (allocHandle == null) {
        this.allocHandle = allocHandle = config.getRecvByteBufAllocator().newHandle();
    }

    ByteBuf byteBuf = null;
    int messages = 0;
    boolean close = false;
    try {
        int byteBufCapacity = allocHandle.guess();
        int totalReadAmount = 0;
        do {
            byteBuf = allocator.ioBuffer(byteBufCapacity);
            int writable = byteBuf.writableBytes();
            int localReadAmount = byteBuf.writeBytes(channel, byteBuf.writableBytes());
            if (localReadAmount <= 0) {
                // not was read release the buffer
                byteBuf.release();
                close = localReadAmount < 0;
                break;
            }
            ((AbstractXnioUnsafe) unsafe()).readPending = false;
            pipeline.fireChannelRead(byteBuf);
            byteBuf = null;

            if (totalReadAmount >= Integer.MAX_VALUE - localReadAmount) {
                // Avoid overflow.
                totalReadAmount = Integer.MAX_VALUE;
                break;
            }

            totalReadAmount += localReadAmount;

            // stop reading
            if (!config.isAutoRead()) {
                break;
            }

            if (localReadAmount < writable) {
                // Read less than what the buffer can hold,
                // which might mean we drained the recv buffer completely.
                break;
            }
        } while (++ messages < maxMessagesPerRead);

        pipeline.fireChannelReadComplete();
        allocHandle.record(totalReadAmount);

        if (close) {
            closeOnRead();
            close = false;
        }
    } catch (Throwable t) {
        handleReadException(pipeline, byteBuf, t, close);
    } finally {
        // Check if there is a readPending which was not processed yet.
        // This could be for two reasons:
        // * The user called Channel.read() or ChannelHandlerContext.read() in channelRead(...) method
        // * The user called Channel.read() or ChannelHandlerContext.read() in channelReadComplete(...) method
        //
        // See https://github.com/netty/netty/issues/2254
        if (!config.isAutoRead() && !((AbstractXnioUnsafe) unsafe()).readPending) {
            removeReadOp(channel);
        }
    }
}
 
Example #25
Source File: DefaultOioSocketChannelConfig.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
public OioSocketChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
    super.setRecvByteBufAllocator(allocator);
    return this;
}
 
Example #26
Source File: OioServerSocketChannelConfig.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
OioServerSocketChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator);
 
Example #27
Source File: AbstractEpollStreamChannel.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public boolean spliceIn(RecvByteBufAllocator.Handle handle) {
    assert ch.eventLoop().inEventLoop();
    if (len == 0) {
        promise.setSuccess();
        return true;
    }
    try {
        // We create the pipe on the target channel as this will allow us to just handle pending writes
        // later in a correct fashion without get into any ordering issues when spliceTo(...) is called
        // on multiple Channels pointing to one target Channel.
        FileDescriptor pipeOut = ch.pipeOut;
        if (pipeOut == null) {
            // Create a new pipe as non was created before.
            FileDescriptor[] pipe = pipe();
            ch.pipeIn = pipe[0];
            pipeOut = ch.pipeOut = pipe[1];
        }

        int splicedIn = spliceIn(pipeOut, handle);
        if (splicedIn > 0) {
            // Integer.MAX_VALUE is a special value which will result in splice forever.
            if (len != Integer.MAX_VALUE) {
                len -= splicedIn;
            }

            // Depending on if we are done with splicing inbound data we set the right promise for the
            // outbound splicing.
            final ChannelPromise splicePromise;
            if (len == 0) {
                splicePromise = promise;
            } else {
                splicePromise = ch.newPromise().addListener(this);
            }

            boolean autoRead = config().isAutoRead();

            // Just call unsafe().write(...) and flush() as we not want to traverse the whole pipeline for this
            // case.
            ch.unsafe().write(new SpliceOutTask(ch, splicedIn, autoRead), splicePromise);
            ch.unsafe().flush();
            if (autoRead && !splicePromise.isDone()) {
                // Write was not done which means the target channel was not writable. In this case we need to
                // disable reading until we are done with splicing to the target channel because:
                //
                // - The user may want to to trigger another splice operation once the splicing was complete.
                config().setAutoRead(false);
            }
        }

        return len == 0;
    } catch (Throwable cause) {
        promise.setFailure(cause);
        return true;
    }
}
 
Example #28
Source File: DefaultRxtxChannelConfig.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
public RxtxChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
    super.setRecvByteBufAllocator(allocator);
    return this;
}
 
Example #29
Source File: EpollDomainSocketChannelConfig.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public EpollDomainSocketChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
    super.setRecvByteBufAllocator(allocator);
    return this;
}
 
Example #30
Source File: EpollServerChannelConfig.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public EpollServerChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
    super.setRecvByteBufAllocator(allocator);
    return this;
}