List of usage examples for io.netty.buffer ByteBuf retain
@Override public abstract ByteBuf retain(int increment);
From source file:com.couchbase.client.core.cluster.SubdocumentMessageTest.java
License:Apache License
@Test public void shouldApplyAllMultiMutationsAndReleaseCommandFragments() { ByteBuf counterFragment = Unpooled.copiedBuffer("-404", CharsetUtil.UTF_8); counterFragment.retain(2); ByteBuf stringFragment = Unpooled.copiedBuffer("\"mutated\"", CharsetUtil.UTF_8); stringFragment.retain(2);//w w w .j a va2 s. co m ByteBuf arrayInsertedFragment = Unpooled.copiedBuffer("\"inserted\"", CharsetUtil.UTF_8); ByteBuf arrayFirstFragment = Unpooled.copiedBuffer("\"first\"", CharsetUtil.UTF_8); ByteBuf arrayLastFragment = Unpooled.copiedBuffer("\"last\"", CharsetUtil.UTF_8); ByteBuf uniqueFragment = Unpooled.copiedBuffer("\"unique\"", CharsetUtil.UTF_8); MutationCommand[] commands = new MutationCommand[] { new MutationCommand(Mutation.COUNTER, "counter", counterFragment, false), new MutationCommand(Mutation.COUNTER, "another.counter", counterFragment, true), new MutationCommand(Mutation.COUNTER, "another.counter", counterFragment, false), new MutationCommand(Mutation.DICT_ADD, "sub.value2", stringFragment), new MutationCommand(Mutation.DICT_UPSERT, "sub.value3", stringFragment), new MutationCommand(Mutation.REPLACE, "value", stringFragment), new MutationCommand(Mutation.ARRAY_INSERT, "sub.array[1]", arrayInsertedFragment), new MutationCommand(Mutation.ARRAY_PUSH_FIRST, "sub.array", arrayFirstFragment), new MutationCommand(Mutation.ARRAY_PUSH_LAST, "sub.array", arrayLastFragment), new MutationCommand(Mutation.ARRAY_ADD_UNIQUE, "sub.array", uniqueFragment), new MutationCommand(Mutation.DELETE, "sub.value") }; SubMultiMutationRequest request = new SubMultiMutationRequest(testSubKey, bucket(), commands); MultiMutationResponse response = cluster().<MultiMutationResponse>send(request).toBlocking().single(); assertEquals(ResponseStatus.SUCCESS, response.status()); assertEquals(Unpooled.EMPTY_BUFFER, response.content()); assertEquals(-1, response.firstErrorIndex()); assertEquals(ResponseStatus.SUCCESS, response.firstErrorStatus()); assertEquals(0, stringFragment.refCnt()); assertEquals(0, counterFragment.refCnt()); assertEquals(0, arrayInsertedFragment.refCnt()); assertEquals(0, arrayFirstFragment.refCnt()); assertEquals(0, arrayLastFragment.refCnt()); assertEquals(0, uniqueFragment.refCnt()); StringBuilder sb = new StringBuilder(); for (int i = 0; i < commands.length; i++) { MutationCommand command = commands[i]; MultiResult result = response.responses().get(i); assertEquals(command.path(), result.path()); assertEquals(command.mutation(), result.operation()); sb.append('\n').append(result); ReferenceCountUtil.releaseLater(result.value()); } if (sb.length() > 1) sb.deleteCharAt(0); String expectedResponse = "COUNTER(counter): SUCCESS = -404" + "\nCOUNTER(another.counter): SUCCESS = -404" + "\nCOUNTER(another.counter): SUCCESS = -808" + //values below have no content "\nDICT_ADD(sub.value2): SUCCESS" + "\nDICT_UPSERT(sub.value3): SUCCESS" + "\nREPLACE(value): SUCCESS" + "\nARRAY_INSERT(sub.array[1]): SUCCESS" + "\nARRAY_PUSH_FIRST(sub.array): SUCCESS" + "\nARRAY_PUSH_LAST(sub.array): SUCCESS" + "\nARRAY_ADD_UNIQUE(sub.array): SUCCESS" + "\nDELETE(sub.value): SUCCESS"; assertEquals(expectedResponse, sb.toString()); String expected = "{\"value\":\"mutated\"," + "\"sub\":{" + // "\"value\":\"subStringValue\"," + //DELETED "\"array\":[\"first\",\"array1\",\"inserted\",2,true,\"last\",\"unique\"]" + ",\"value2\":\"mutated\"" + ",\"value3\":\"mutated\"}," + "\"counter\":-404," + "\"another\":{\"counter\":-808}}"; assertMutation(testSubKey, expected); }
From source file:org.apache.drill.exec.rpc.RpcDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception { if (!ctx.channel().isOpen()) { return;//from ww w. jav a 2 s . co m } if (RpcConstants.EXTRA_DEBUGGING) { logger.debug("Inbound rpc message received."); } // now, we know the entire message is in the buffer and the buffer is constrained to this message. Additionally, // this process should avoid reading beyond the end of this buffer so we inform the ByteBufInputStream to throw an // exception if be go beyond readable bytes (as opposed to blocking). final ByteBufInputStream is = new ByteBufInputStream(buffer, buffer.readableBytes()); // read the rpc header, saved in delimited format. checkTag(is, RpcEncoder.HEADER_TAG); final RpcHeader header = RpcHeader.parseDelimitedFrom(is); if (RpcConstants.EXTRA_DEBUGGING) { logger.debug(" post header read index {}", buffer.readerIndex()); } // read the protobuf body into a buffer. checkTag(is, RpcEncoder.PROTOBUF_BODY_TAG); final int pBodyLength = readRawVarint32(is); final ByteBuf pBody = buffer.slice(buffer.readerIndex(), pBodyLength); buffer.skipBytes(pBodyLength); pBody.retain(1); if (RpcConstants.EXTRA_DEBUGGING) { logger.debug("Read protobuf body of length {} into buffer {}.", pBodyLength, pBody); } if (RpcConstants.EXTRA_DEBUGGING) { logger.debug("post protobufbody read index {}", buffer.readerIndex()); } ByteBuf dBody = null; int dBodyLength = 0; // read the data body. if (buffer.readableBytes() > 0) { if (RpcConstants.EXTRA_DEBUGGING) { logger.debug("Reading raw body, buffer has {} bytes available, is available {}.", buffer.readableBytes(), is.available()); } checkTag(is, RpcEncoder.RAW_BODY_TAG); dBodyLength = readRawVarint32(is); if (buffer.readableBytes() != dBodyLength) { throw new CorruptedFrameException(String.format( "Expected to receive a raw body of %d bytes but received a buffer with %d bytes.", dBodyLength, buffer.readableBytes())); } dBody = buffer.slice(); dBody.retain(1); if (RpcConstants.EXTRA_DEBUGGING) { logger.debug("Read raw body of {}", dBody); } } else { if (RpcConstants.EXTRA_DEBUGGING) { logger.debug("No need to read raw body, no readable bytes left."); } } // return the rpc message. InboundRpcMessage m = new InboundRpcMessage(header.getMode(), header.getRpcType(), header.getCoordinationId(), pBody, dBody); // move the reader index forward so the next rpc call won't try to work with it. buffer.skipBytes(dBodyLength); messageCounter.incrementAndGet(); if (RpcConstants.SOME_DEBUGGING) { logger.debug("Inbound Rpc Message Decoded {}.", m); } out.add(m); }
From source file:org.starnub.starnubserver.connections.player.session.PlayerSession.java
License:Open Source License
private static void sendPacketToGroup(Packet packet, Predicate<PlayerSession> filter, boolean flush) { Players connectedPlayers = Connections.getInstance().getCONNECTED_PLAYERS(); int onlinePlayers = connectedPlayers.size(); if (onlinePlayers > 0) { ByteBuf byteBufPacket = packet.packetToMessageEncoder(); if (onlinePlayers > 1) { int refCountChange = onlinePlayers - 1; byteBufPacket.retain(refCountChange); }/*from w ww . j a v a2 s . c o m*/ int remotePlayers = 0; if (filter != null) { for (PlayerSession playerSession : connectedPlayers.values()) { boolean notFilter = filter.test(playerSession); if (notFilter) { if (playerSession.getCONNECTION_TYPE() == ConnectionType.PROXY_IN_GAME) { ChannelHandlerContext ctx = playerSession.getCONNECTION().getCLIENT_CTX(); if (flush) { ctx.writeAndFlush(byteBufPacket, ctx.voidPromise()); } else { ctx.write(byteBufPacket, ctx.voidPromise()); } } else { sendRemote(playerSession, packet); remotePlayers++; } } } } else { for (PlayerSession playerSession : connectedPlayers.values()) { if (playerSession.getCONNECTION_TYPE() == ConnectionType.PROXY_IN_GAME) { ChannelHandlerContext ctx = playerSession.getCONNECTION().getCLIENT_CTX(); if (flush) { ctx.writeAndFlush(byteBufPacket, ctx.voidPromise()); } else { ctx.write(byteBufPacket, ctx.voidPromise()); } } else { sendRemote(playerSession, packet); remotePlayers++; } } } if (remotePlayers > 0) { byteBufPacket.release(remotePlayers); } } }