public static Frame from(ByteBuf data, boolean respond) { final Frame frame = RECYCLER.get(); frame.setRefCnt(1); frame.content = ByteBufAllocator.DEFAULT.buffer( KeepaliveFrameFlyweight.computeFrameLength(data.readableBytes())); final int flags = respond ? KeepaliveFrameFlyweight.FLAGS_KEEPALIVE_R : 0; frame.content.writerIndex(KeepaliveFrameFlyweight.encode(frame.content, flags, data)); return frame; }
@Test public void testEncoding() { int encoded = KeepaliveFrameFlyweight.encode( byteBuf, KeepaliveFrameFlyweight.FLAGS_KEEPALIVE_R, Unpooled.copiedBuffer("d", StandardCharsets.UTF_8)); assertEquals("00000f000000000c80000000000000000064", ByteBufUtil.hexDump(byteBuf, 0, encoded)); } }
public static int encode(final ByteBuf byteBuf, int flags, final ByteBuf data) { final int frameLength = computeFrameLength(data.readableBytes()); int length = FrameHeaderFlyweight.encodeFrameHeader(byteBuf, frameLength, flags, FrameType.KEEPALIVE, 0); // We don't support resumability, last position is always zero byteBuf.setLong(length, 0); length += Long.BYTES; length += FrameHeaderFlyweight.encodeData(byteBuf, length, data); return length; }
private static int payloadOffset(final ByteBuf byteBuf) { int typeAndFlags = byteBuf.getShort(FRAME_TYPE_AND_FLAGS_FIELD_OFFSET); FrameType frameType = FrameType.fromEncodedType(typeAndFlags >> FRAME_TYPE_SHIFT); int result = PAYLOAD_OFFSET; switch (frameType) { case SETUP: result = SetupFrameFlyweight.payloadOffset(byteBuf); break; case ERROR: result = ErrorFrameFlyweight.payloadOffset(byteBuf); break; case LEASE: result = LeaseFrameFlyweight.payloadOffset(byteBuf); break; case KEEPALIVE: result = KeepaliveFrameFlyweight.payloadOffset(byteBuf); break; case REQUEST_RESPONSE: case REQUEST_FNF: case REQUEST_STREAM: case REQUEST_CHANNEL: result = RequestFrameFlyweight.payloadOffset(frameType, byteBuf); break; case REQUEST_N: result = RequestNFrameFlyweight.payloadOffset(byteBuf); break; } return result; }
@Test public void canReadData() { ByteBuf data = Unpooled.wrappedBuffer(new byte[] {5, 4, 3}); int length = KeepaliveFrameFlyweight.encode(byteBuf, KeepaliveFrameFlyweight.FLAGS_KEEPALIVE_R, data); data.resetReaderIndex(); assertEquals( KeepaliveFrameFlyweight.FLAGS_KEEPALIVE_R, FrameHeaderFlyweight.flags(byteBuf) & KeepaliveFrameFlyweight.FLAGS_KEEPALIVE_R); assertEquals(data, FrameHeaderFlyweight.sliceFrameData(byteBuf)); }
public static int encode(final ByteBuf byteBuf, int flags, final ByteBuf data) { final int frameLength = computeFrameLength(data.readableBytes()); int length = FrameHeaderFlyweight.encodeFrameHeader(byteBuf, frameLength, flags, FrameType.KEEPALIVE, 0); // We don't support resumability, last position is always zero byteBuf.setLong(length, 0); length += Long.BYTES; length += FrameHeaderFlyweight.encodeData(byteBuf, length, data); return length; }
private static int payloadOffset(final ByteBuf byteBuf) { int typeAndFlags = byteBuf.getShort(FRAME_TYPE_AND_FLAGS_FIELD_OFFSET); FrameType frameType = FrameType.fromEncodedType(typeAndFlags >> FRAME_TYPE_SHIFT); int result = PAYLOAD_OFFSET; switch (frameType) { case SETUP: result = SetupFrameFlyweight.payloadOffset(byteBuf); break; case ERROR: result = ErrorFrameFlyweight.payloadOffset(byteBuf); break; case LEASE: result = LeaseFrameFlyweight.payloadOffset(byteBuf); break; case KEEPALIVE: result = KeepaliveFrameFlyweight.payloadOffset(byteBuf); break; case REQUEST_RESPONSE: case REQUEST_FNF: case REQUEST_STREAM: case REQUEST_CHANNEL: result = RequestFrameFlyweight.payloadOffset(frameType, byteBuf); break; case REQUEST_N: result = RequestNFrameFlyweight.payloadOffset(byteBuf); break; } return result; }
public static Frame from(ByteBuf data, boolean respond) { final Frame frame = RECYCLER.get(); frame.setRefCnt(1); frame.content = ByteBufAllocator.DEFAULT.buffer( KeepaliveFrameFlyweight.computeFrameLength(data.readableBytes())); final int flags = respond ? KeepaliveFrameFlyweight.FLAGS_KEEPALIVE_R : 0; frame.content.writerIndex(KeepaliveFrameFlyweight.encode(frame.content, flags, data)); return frame; }