Search in sources :

Example 26 with ByteBuf

use of 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 27 with ByteBuf

use of 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 28 with ByteBuf

use of 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 29 with ByteBuf

use of 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)

Example 30 with ByteBuf

use of io.netty.buffer.ByteBuf in project vert.x by eclipse.

the class FileStreamChannel method doWrite.

@Override
protected void doWrite(ChannelOutboundBuffer in) throws Exception {
    ByteBuf chunk;
    while (!stream.isNotWritable() && (chunk = (ByteBuf) in.current()) != null) {
        bytesWritten += chunk.readableBytes();
        stream.writeData(chunk.retain(), false);
        stream.handlerContext.flush();
        in.remove();
    }
}
Also used : 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