List of usage examples for io.netty.buffer ByteBuf refCnt
int refCnt();
From source file:io.lettuce.core.protocol.CommandHandler.java
License:Apache License
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer) throws InterruptedException { if (pristine && stack.isEmpty() && buffer.isReadable()) { if (debugEnabled) { logger.debug("{} Received response without a command context (empty stack)", logPrefix()); }// w ww . j ava 2 s . c o m if (consumeResponse(buffer)) { pristine = false; } return; } while (canDecode(buffer)) { RedisCommand<?, ?, ?> command = stack.peek(); if (debugEnabled) { logger.debug("{} Stack contains: {} commands", logPrefix(), stack.size()); } pristine = false; try { if (!decode(ctx, buffer, command)) { return; } } catch (Exception e) { ctx.close(); throw e; } if (isProtectedMode(command)) { onProtectedMode(command.getOutput().getError()); } else { if (canComplete(command)) { stack.poll(); try { complete(command); } catch (Exception e) { logger.warn("{} Unexpected exception during request: {}", logPrefix, e.toString(), e); } } } afterDecode(ctx, command); } if (buffer.refCnt() != 0) { buffer.discardReadBytes(); } }
From source file:io.reactivex.netty.protocol.http.UnicastContentSubjectTest.java
License:Apache License
@Test public void testByteBufRelease() throws Exception { UnicastContentSubject<ByteBuf> subject = UnicastContentSubject.createWithoutNoSubscriptionTimeout(); ByteBuf buffer = Unpooled.buffer(); Assert.assertEquals("Created byte buffer not retained.", 1, buffer.refCnt()); subject.onNext(buffer);/*ww w . j a va 2s .co m*/ subject.onCompleted(); final AtomicInteger byteBufRefCnt = new AtomicInteger(-1); ByteBuf last = subject.doOnNext(new Action1<ByteBuf>() { @Override public void call(ByteBuf byteBuf) { byteBufRefCnt.set(byteBuf.refCnt()); byteBuf.release();// Simulate consumption as ByteBuf refcount is 1 when created. } }).toBlocking().last(); Assert.assertEquals("Unexpected ByteBuf ref count when received.", 2, byteBufRefCnt.get()); Assert.assertSame("Unexpected byte buffer received.", buffer, last); Assert.assertEquals("Byte buffer not released.", 0, last.refCnt()); }
From source file:io.reactivex.netty.samples.SimplePostServer.java
License:Apache License
public HttpServer<ByteBuf, ByteBuf> createServer() { HttpServer<ByteBuf, ByteBuf> server = RxNetty .newHttpServerBuilder(port, new RequestHandler<ByteBuf, ByteBuf>() { @Override//from w ww.j a v a 2s.c om public Observable<Void> handle(HttpServerRequest<ByteBuf> request, final HttpServerResponse<ByteBuf> response) { return request.getContent().map(new Func1<ByteBuf, ByteBuf>() { @Override public ByteBuf call(ByteBuf buf) { ByteBuf buf0 = Unpooled.copiedBuffer(buf); logger.info("buf0 {} refCnt() : {}", buf0.toString(), buf0.refCnt()); return buf0; } }).reduce(new Func2<ByteBuf, ByteBuf, ByteBuf>() { @Override public ByteBuf call(ByteBuf buf1, ByteBuf buf2) { logger.info("reduce"); logger.info("buf1 {} refCnt() : {}", buf1.toString(), buf1.refCnt()); logger.info("buf2 {} refCnt() : {}", buf2.toString(), buf2.refCnt()); ByteBuf buf3 = Unpooled.copiedBuffer(buf1, buf2); buf1.release(); logger.info("buf1 release"); logger.info("buf1 {} refCnt() : {}", buf1.toString(), buf1.refCnt()); buf2.release(); logger.info("buf2 release"); logger.info("buf2 {} refCnt() : {}", buf2.toString(), buf2.refCnt()); logger.info("buf3 {} refCnt() : {}", buf3.toString(), buf3.refCnt()); return buf3; } }).map(new Func1<ByteBuf, Void>() { @Override public Void call(ByteBuf buf4) { String str = buf4.toString(Charset.defaultCharset()); QueryStringDecoder decoder = new QueryStringDecoder(str, false); Map<String, List<String>> map = decoder.parameters(); for (String key : map.keySet()) { System.out.println(key + " : " + map.get(key).get(0)); } // response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR); response.setStatus(HttpResponseStatus.OK); response.writeStringAndFlush("1"); logger.info("buf4 {} refCnt() : {}", buf4.toString(), buf4.refCnt()); buf4.release(); logger.info("buf4 release"); logger.info("buf4 {} refCnt() : {}", buf4.toString(), buf4.refCnt()); return null; } }) // .collect(new Func0<List<String>>() { // // @Override public List<String> call() { // return new ArrayList<>(); // } // }, new Action2<List<String>, ByteBuf>() { // // @Override public void call(List<String> list, ByteBuf buf) { // // System.out.println(list.size() + " , " + buf.toString(Charset.defaultCharset())); // list.add(buf.toString(Charset.defaultCharset())); // } // }) // .map(new Func1<List<String>, Void>() { // // @Override public Void call(List<String> list) { // String str = ""; // for (String s : list) { // str += s; // } // // System.out.println(str); // QueryStringDecoder decoder = new QueryStringDecoder(str, false); // Map<String, List<String>> map = decoder.parameters(); // for (String key : map.keySet()) { // System.out.println(key + " : " + map.get(key).get(0)); // } // response.writeStringAndFlush("1"); // return null; // } // }) .ignoreElements(); } }).pipelineConfigurator(new HttpServerPipelineConfigurator<ByteBuf, ByteBuf>()) // .enableWireLogging(LogLevel.ERROR) .build(); logger.info("Simple POST server started..."); return server; }
From source file:io.vertx.core.http.Http2ServerTest.java
License:Open Source License
@Test public void testHttp1xOrH2CHandlerHttp1xRequest() throws Exception { EmbeddedChannel ch = new EmbeddedChannel(new TestHttp1xOrH2CHandler()); ByteBuf buff = HTTP_1_1_POST.copy(0, HTTP_1_1_POST.readableBytes()); ch.writeInbound(buff);//from w w w.j a v a 2s . c o m assertEquals(0, buff.refCnt()); assertEquals(1, ch.outboundMessages().size()); HttpRequest req = (HttpRequest) ch.outboundMessages().poll(); assertEquals("POST", req.method().name()); assertNull(ch.pipeline().get(TestHttp1xOrH2CHandler.class)); }
From source file:io.vertx.core.http.Http2ServerTest.java
License:Open Source License
@Test public void testHttp1xOrH2CHandlerFragmentedHttp1xRequest() throws Exception { EmbeddedChannel ch = new EmbeddedChannel(new TestHttp1xOrH2CHandler()); ByteBuf buff = HTTP_1_1_POST.copy(0, 1); ch.writeInbound(buff);//w ww .j av a2 s.c o m assertEquals(0, buff.refCnt()); assertEquals(0, ch.outboundMessages().size()); buff = HTTP_1_1_POST.copy(1, HTTP_1_1_POST.readableBytes() - 1); ch.writeInbound(buff); assertEquals(0, buff.refCnt()); assertEquals(1, ch.outboundMessages().size()); HttpRequest req = (HttpRequest) ch.outboundMessages().poll(); assertEquals("POST", req.method().name()); assertNull(ch.pipeline().get(TestHttp1xOrH2CHandler.class)); }
From source file:io.vertx.core.http.Http2ServerTest.java
License:Open Source License
@Test public void testHttp1xOrH2CHandlerHttp2Request() throws Exception { EmbeddedChannel ch = new EmbeddedChannel(new TestHttp1xOrH2CHandler()); ByteBuf expected = Unpooled.copiedBuffer(Http1xOrH2CHandler.HTTP_2_PREFACE, StandardCharsets.UTF_8); ch.writeInbound(expected);/*from w w w .j av a2 s . c o m*/ assertEquals(1, expected.refCnt()); assertEquals(1, ch.outboundMessages().size()); ByteBuf res = (ByteBuf) ch.outboundMessages().poll(); assertEquals(Http1xOrH2CHandler.HTTP_2_PREFACE, res.toString(StandardCharsets.UTF_8)); assertNull(ch.pipeline().get(TestHttp1xOrH2CHandler.class)); }
From source file:io.vertx.core.http.Http2ServerTest.java
License:Open Source License
@Test public void testHttp1xOrH2CHandlerFragmentedHttp2Request() throws Exception { EmbeddedChannel ch = new EmbeddedChannel(new TestHttp1xOrH2CHandler()); ByteBuf expected = Unpooled.copiedBuffer(Http1xOrH2CHandler.HTTP_2_PREFACE, StandardCharsets.UTF_8); ByteBuf buff = expected.copy(0, 1); ch.writeInbound(buff);/* w w w .j a v a2s. co m*/ assertEquals(0, buff.refCnt()); assertEquals(0, ch.outboundMessages().size()); buff = expected.copy(1, expected.readableBytes() - 1); ch.writeInbound(buff); assertEquals(0, buff.refCnt()); assertEquals(1, ch.outboundMessages().size()); ByteBuf res = (ByteBuf) ch.outboundMessages().poll(); assertEquals(1, res.refCnt()); assertEquals(Http1xOrH2CHandler.HTTP_2_PREFACE, res.toString(StandardCharsets.UTF_8)); assertNull(ch.pipeline().get(TestHttp1xOrH2CHandler.class)); }
From source file:io.vertx.core.net.NetTest.java
License:Open Source License
private void testNetClientInternal_(HttpServerOptions options, boolean expectSSL) throws Exception { waitFor(2);/* ww w . ja va 2 s .c o m*/ HttpServer server = vertx.createHttpServer(options); server.requestHandler(req -> { req.response().end("Hello World"); }); CountDownLatch latch = new CountDownLatch(1); server.listen(onSuccess(v -> { latch.countDown(); })); awaitLatch(latch); client.connect(1234, "localhost", onSuccess(so -> { NetSocketInternal soInt = (NetSocketInternal) so; assertEquals(expectSSL, soInt.isSsl()); ChannelHandlerContext chctx = soInt.channelHandlerContext(); ChannelPipeline pipeline = chctx.pipeline(); pipeline.addBefore("handler", "http", new HttpClientCodec()); AtomicInteger status = new AtomicInteger(); soInt.handler(buff -> fail()); soInt.messageHandler(obj -> { switch (status.getAndIncrement()) { case 0: assertTrue(obj instanceof HttpResponse); HttpResponse resp = (HttpResponse) obj; assertEquals(200, resp.status().code()); break; case 1: assertTrue(obj instanceof LastHttpContent); ByteBuf content = ((LastHttpContent) obj).content(); assertEquals(!expectSSL, content.isDirect()); assertEquals(1, content.refCnt()); String val = content.toString(StandardCharsets.UTF_8); assertTrue(content.release()); assertEquals("Hello World", val); complete(); break; default: fail(); } }); soInt.writeMessage(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/somepath"), onSuccess(v -> complete())); })); await(); }
From source file:io.vertx.core.net.NetTest.java
License:Open Source License
@Test public void testNetSocketInternalBuffer() throws Exception { server.connectHandler(so -> {/*from w w w. j a v a2 s. com*/ NetSocketInternal soi = (NetSocketInternal) so; soi.messageHandler(msg -> fail("Unexpected")); soi.handler(msg -> { ByteBuf byteBuf = msg.getByteBuf(); assertFalse(byteBuf.isDirect()); assertEquals(1, byteBuf.refCnt()); assertFalse(byteBuf.release()); assertEquals(1, byteBuf.refCnt()); soi.write(msg); }); }); startServer(); client.connect(testAddress, onSuccess(so -> { NetSocketInternal soi = (NetSocketInternal) so; soi.write(Buffer.buffer("Hello World")); soi.messageHandler(msg -> fail("Unexpected")); soi.handler(msg -> { ByteBuf byteBuf = msg.getByteBuf(); assertFalse(byteBuf.isDirect()); assertEquals(1, byteBuf.refCnt()); assertFalse(byteBuf.release()); assertEquals(1, byteBuf.refCnt()); assertEquals("Hello World", msg.toString()); testComplete(); }); })); await(); }
From source file:net.epsilony.utils.codec.modbus.handler.ModbusMasterResponseDecoderTest.java
License:Open Source License
@Test public void testDecoding() { EmbeddedChannel channel = new EmbeddedChannel(new ModbusMasterResponseDecoder(requests::remove)); TestData[] datas = initTestData();//from ww w.j a v a 2 s .c o m ByteBuf buf = createBuf(); for (TestData data : datas) { data.writeBuffer(buf); } channel.writeInbound(buf); for (TestData data : datas) { Object decoded = channel.readInbound(); assertEquals(data.response, decoded); } assertEquals(null, channel.readInbound()); assertTrue(buf.refCnt() <= 0); assertTrue(requests.isEmpty()); }