Search in sources :

Example 21 with ByteBuf

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

the class Netty4MessageChannelHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    Transports.assertTransportThread();
    if (!(msg instanceof ByteBuf)) {
        ctx.fireChannelRead(msg);
        return;
    }
    final ByteBuf buffer = (ByteBuf) msg;
    final int remainingMessageSize = buffer.getInt(buffer.readerIndex() - TcpHeader.MESSAGE_LENGTH_SIZE);
    final int expectedReaderIndex = buffer.readerIndex() + remainingMessageSize;
    try {
        InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
        // netty always copies a buffer, either in NioWorker in its read handler, where it copies to a fresh
        // buffer, or in the cumulative buffer, which is cleaned each time so it could be bigger than the actual size
        BytesReference reference = Netty4Utils.toBytesReference(buffer, remainingMessageSize);
        transport.messageReceived(reference, ctx.channel(), profileName, remoteAddress, remainingMessageSize);
    } finally {
        // Set the expected position of the buffer, no matter what happened
        buffer.readerIndex(expectedReaderIndex);
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) InetSocketAddress(java.net.InetSocketAddress) ByteBuf(io.netty.buffer.ByteBuf)

Example 22 with ByteBuf

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

the class Netty4SizeHeaderFrameDecoder method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    try {
        boolean continueProcessing = TcpTransport.validateMessageHeader(Netty4Utils.toBytesReference(in));
        final ByteBuf message = in.skipBytes(TcpHeader.MARKER_BYTES_SIZE + TcpHeader.MESSAGE_LENGTH_SIZE);
        if (!continueProcessing)
            return;
        out.add(message);
    } catch (IllegalArgumentException ex) {
        throw new TooLongFrameException(ex);
    } catch (IllegalStateException ex) {
    /* decode will be called until the ByteBuf is fully consumed; when it is fully
             * consumed, transport#validateMessageHeader will throw an IllegalStateException which
             * is okay, it means we have finished consuming the ByteBuf and we can get out
             */
    }
}
Also used : TooLongFrameException(io.netty.handler.codec.TooLongFrameException) ByteBuf(io.netty.buffer.ByteBuf)

Example 23 with ByteBuf

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

the class Netty4HttpClient method processRequestsWithBody.

private Collection<FullHttpResponse> processRequestsWithBody(HttpMethod method, SocketAddress remoteAddress, Tuple<String, CharSequence>... urisAndBodies) throws InterruptedException {
    Collection<HttpRequest> requests = new ArrayList<>(urisAndBodies.length);
    for (Tuple<String, CharSequence> uriAndBody : urisAndBodies) {
        ByteBuf content = Unpooled.copiedBuffer(uriAndBody.v2(), StandardCharsets.UTF_8);
        HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, uriAndBody.v1(), content);
        request.headers().add(HttpHeaderNames.HOST, "localhost");
        request.headers().add(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
        request.headers().add(HttpHeaderNames.CONTENT_TYPE, "application/json");
        requests.add(request);
    }
    return sendRequests(remoteAddress, requests);
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf)

Example 24 with ByteBuf

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

the class Netty4UtilsTests method testToChannelBuffer.

public void testToChannelBuffer() throws IOException {
    BytesReference ref = getRandomizedBytesReference(randomIntBetween(1, 3 * PAGE_SIZE));
    ByteBuf buffer = Netty4Utils.toByteBuf(ref);
    BytesReference bytesReference = Netty4Utils.toBytesReference(buffer);
    if (ref instanceof ByteBufBytesReference) {
        assertEquals(buffer, ((ByteBufBytesReference) ref).toByteBuf());
    } else if (AbstractBytesReferenceTestCase.getNumPages(ref) > 1) {
        // we gather the buffers into a channel buffer
        assertTrue(buffer instanceof CompositeByteBuf);
    }
    assertArrayEquals(BytesReference.toBytes(ref), BytesReference.toBytes(bytesReference));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 25 with ByteBuf

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

the class Netty4UtilsTests method testToChannelBufferWithSliceAfter.

public void testToChannelBufferWithSliceAfter() throws IOException {
    BytesReference ref = getRandomizedBytesReference(randomIntBetween(1, 3 * PAGE_SIZE));
    int sliceOffset = randomIntBetween(0, ref.length());
    int sliceLength = randomIntBetween(ref.length() - sliceOffset, ref.length() - sliceOffset);
    ByteBuf buffer = Netty4Utils.toByteBuf(ref);
    BytesReference bytesReference = Netty4Utils.toBytesReference(buffer);
    assertArrayEquals(BytesReference.toBytes(ref.slice(sliceOffset, sliceLength)), BytesReference.toBytes(bytesReference.slice(sliceOffset, sliceLength)));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)1517 Test (org.junit.Test)663 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)160 IOException (java.io.IOException)97 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)81 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)80 Test (org.testng.annotations.Test)68 InetSocketAddress (java.net.InetSocketAddress)59 Channel (io.netty.channel.Channel)57 ChannelFuture (io.netty.channel.ChannelFuture)56 ArrayList (java.util.ArrayList)53 Map (java.util.Map)44 ChannelPromise (io.netty.channel.ChannelPromise)41 AtomicReference (java.util.concurrent.atomic.AtomicReference)36 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)34 CountDownLatch (java.util.concurrent.CountDownLatch)34 HashMap (java.util.HashMap)33 RecyclableDuplicateByteBuf (io.netty.buffer.RecyclableDuplicateByteBuf)32 EventLoopGroup (io.netty.channel.EventLoopGroup)32 List (java.util.List)32