Search in sources :

Example 21 with ByteBuf

use of org.apache.flink.shaded.netty4.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)

Example 22 with ByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project elasticsearch by elastic.

the class ByteBufBytesReferenceTests method testSliceOnAdvancedBuffer.

public void testSliceOnAdvancedBuffer() throws IOException {
    BytesReference bytesReference = newBytesReference(randomIntBetween(10, 3 * PAGE_SIZE));
    BytesRef bytesRef = bytesReference.toBytesRef();
    ByteBuf channelBuffer = Unpooled.wrappedBuffer(bytesRef.bytes, bytesRef.offset, bytesRef.length);
    int numBytesToRead = randomIntBetween(1, 5);
    for (int i = 0; i < numBytesToRead; i++) {
        channelBuffer.readByte();
    }
    BytesReference other = Netty4Utils.toBytesReference(channelBuffer);
    BytesReference slice = bytesReference.slice(numBytesToRead, bytesReference.length() - numBytesToRead);
    assertEquals(other, slice);
    assertEquals(other.slice(3, 1), slice.slice(3, 1));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) ByteBuf(io.netty.buffer.ByteBuf) BytesRef(org.apache.lucene.util.BytesRef)

Example 23 with ByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project vert.x by eclipse.

the class HttpClientRequestImpl method handleNextRequest.

private void handleNextRequest(HttpClientResponse resp, HttpClientRequestImpl next, long timeoutMs) {
    next.handler(respHandler);
    next.exceptionHandler(exceptionHandler());
    exceptionHandler(null);
    next.endHandler(endHandler);
    next.pushHandler = pushHandler;
    next.followRedirects = followRedirects - 1;
    next.written = written;
    if (next.hostHeader == null) {
        next.hostHeader = hostHeader;
    }
    if (headers != null && next.headers == null) {
        next.headers().addAll(headers);
    }
    ByteBuf body;
    switch(next.method) {
        case GET:
            body = null;
            break;
        case OTHER:
            next.rawMethod = rawMethod;
            body = null;
            break;
        default:
            if (cachedChunks != null) {
                body = cachedChunks;
            } else {
                body = null;
            }
            break;
    }
    cachedChunks = null;
    Future<Void> fut = Future.future();
    fut.setHandler(ar -> {
        if (ar.succeeded()) {
            if (timeoutMs > 0) {
                next.setTimeout(timeoutMs);
            }
            next.write(body, true);
        } else {
            next.handleException(ar.cause());
        }
    });
    if (exceptionOccurred != null) {
        fut.fail(exceptionOccurred);
    } else if (completed) {
        fut.complete();
    } else {
        exceptionHandler(err -> {
            if (!fut.isComplete()) {
                fut.fail(err);
            }
        });
        completionHandler = v -> {
            if (!fut.isComplete()) {
                fut.complete();
            }
        };
    }
}
Also used : VertxInternal(io.vertx.core.impl.VertxInternal) MultiMap(io.vertx.core.MultiMap) HttpHeaders(io.vertx.core.http.HttpHeaders) Future(io.vertx.core.Future) Unpooled(io.netty.buffer.Unpooled) Nullable(io.vertx.codegen.annotations.Nullable) Objects(java.util.Objects) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) Buffer(io.vertx.core.buffer.Buffer) HttpFrame(io.vertx.core.http.HttpFrame) HttpVersion(io.vertx.core.http.HttpVersion) HttpMethod(io.vertx.core.http.HttpMethod) HttpConnection(io.vertx.core.http.HttpConnection) Handler(io.vertx.core.Handler) CaseInsensitiveHeaders(io.vertx.core.http.CaseInsensitiveHeaders) NetSocket(io.vertx.core.net.NetSocket) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 24 with ByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project vert.x by eclipse.

the class HttpServerResponseImpl method end.

@Override
public void end(Buffer chunk) {
    synchronized (conn) {
        if (!chunked && !contentLengthSet()) {
            headers().set(HttpHeaders.CONTENT_LENGTH, String.valueOf(chunk.length()));
        }
        ByteBuf buf = chunk.getByteBuf();
        end0(buf);
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf)

Example 25 with ByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project vert.x by eclipse.

the class ServerConnection method createNetSocket.

NetSocket createNetSocket() {
    NetSocketImpl socket = new NetSocketImpl(vertx, channel, context, server.getSslHelper(), metrics);
    socket.metric(metric());
    Map<Channel, NetSocketImpl> connectionMap = new HashMap<>(1);
    connectionMap.put(channel, socket);
    // Flush out all pending data
    endReadAndFlush();
    // remove old http handlers and replace the old handler with one that handle plain sockets
    ChannelPipeline pipeline = channel.pipeline();
    ChannelHandler compressor = pipeline.get(HttpChunkContentCompressor.class);
    if (compressor != null) {
        pipeline.remove(compressor);
    }
    pipeline.remove("httpDecoder");
    if (pipeline.get("chunkedWriter") != null) {
        pipeline.remove("chunkedWriter");
    }
    channel.pipeline().replace("handler", "handler", new VertxNetHandler<NetSocketImpl>(channel, socket, connectionMap) {

        @Override
        public void exceptionCaught(ChannelHandlerContext chctx, Throwable t) throws Exception {
            // remove from the real mapping
            server.removeChannel(channel);
            super.exceptionCaught(chctx, t);
        }

        @Override
        public void channelInactive(ChannelHandlerContext chctx) throws Exception {
            // remove from the real mapping
            server.removeChannel(channel);
            super.channelInactive(chctx);
        }

        @Override
        public void channelRead(ChannelHandlerContext chctx, Object msg) throws Exception {
            if (msg instanceof HttpContent) {
                ReferenceCountUtil.release(msg);
                return;
            }
            super.channelRead(chctx, msg);
        }

        @Override
        protected void handleMsgReceived(Object msg) {
            ByteBuf buf = (ByteBuf) msg;
            conn.handleDataReceived(Buffer.buffer(buf));
        }
    });
    // check if the encoder can be removed yet or not.
    if (lastWriteFuture == null) {
        channel.pipeline().remove("httpEncoder");
    } else {
        lastWriteFuture.addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                channel.pipeline().remove("httpEncoder");
            }
        });
    }
    return socket;
}
Also used : HashMap(java.util.HashMap) ByteBuf(io.netty.buffer.ByteBuf) WebSocketHandshakeException(io.netty.handler.codec.http.websocketx.WebSocketHandshakeException) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) IOException(java.io.IOException) NetSocketImpl(io.vertx.core.net.impl.NetSocketImpl)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)5135 Test (org.junit.Test)1813 Test (org.junit.jupiter.api.Test)717 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)392 ArrayList (java.util.ArrayList)312 IOException (java.io.IOException)309 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)209 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)183 ByteBuffer (java.nio.ByteBuffer)174 InetSocketAddress (java.net.InetSocketAddress)156 List (java.util.List)146 ByteBuf (org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf)144 Channel (io.netty.channel.Channel)141 Test (org.testng.annotations.Test)140 ChannelFuture (io.netty.channel.ChannelFuture)132 Map (java.util.Map)118 CountDownLatch (java.util.concurrent.CountDownLatch)108 MatchEntryBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder)107 Position (org.traccar.model.Position)105 DeviceSession (org.traccar.DeviceSession)100