public static Frame from(int streamId, FrameType type, int flags) { final Frame frame = RECYCLER.get(); frame.setRefCnt(1); frame.content = ByteBufAllocator.DEFAULT.buffer(RequestFrameFlyweight.computeFrameLength(type, null, 0)); frame.content.writerIndex( RequestFrameFlyweight.encode( frame.content, streamId, flags, type, Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER)); return frame; }
public static int initialRequestN(final Frame frame) { final FrameType type = frame.getType(); int result; if (!type.isRequestType()) { throw new AssertionError("expected request type, but saw " + type.name()); } switch (frame.getType()) { case REQUEST_RESPONSE: result = 1; break; case REQUEST_FNF: case METADATA_PUSH: result = 0; break; default: result = RequestFrameFlyweight.initialRequestN(frame.content); break; } return result; }
@Test public void testEncoding() { int encoded = RequestFrameFlyweight.encode( byteBuf, 1, FrameHeaderFlyweight.FLAGS_M, FrameType.REQUEST_STREAM, 1, Unpooled.copiedBuffer("md", StandardCharsets.UTF_8), Unpooled.copiedBuffer("d", StandardCharsets.UTF_8)); assertEquals( "000010000000011900000000010000026d6464", ByteBufUtil.hexDump(byteBuf, 0, encoded)); Payload payload = DefaultPayload.create(Frame.from(stringToBuf("000010000000011900000000010000026d6464"))); assertEquals("md", StandardCharsets.UTF_8.decode(payload.getMetadata()).toString()); }
public static int encode( final ByteBuf byteBuf, final int streamId, int flags, final FrameType type, final int initialRequestN, final @Nullable ByteBuf metadata, final ByteBuf data) { if (Frame.isFlagSet(flags, FrameHeaderFlyweight.FLAGS_M) != (metadata != null)) { throw new IllegalArgumentException("metadata flag set incorrectly"); } final int frameLength = computeFrameLength( type, metadata != null ? metadata.readableBytes() : null, data.readableBytes()); int length = FrameHeaderFlyweight.encodeFrameHeader(byteBuf, frameLength, flags, type, streamId); byteBuf.setInt(INITIAL_REQUEST_N_FIELD_OFFSET, initialRequestN); length += Integer.BYTES; length += FrameHeaderFlyweight.encodeMetadata(byteBuf, type, length, metadata); 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 testEncodingWithEmptyMetadata() { int encoded = RequestFrameFlyweight.encode( byteBuf, 1, FrameHeaderFlyweight.FLAGS_M, FrameType.REQUEST_STREAM, 1, Unpooled.copiedBuffer("", StandardCharsets.UTF_8), Unpooled.copiedBuffer("d", StandardCharsets.UTF_8)); assertEquals("00000e0000000119000000000100000064", ByteBufUtil.hexDump(byteBuf, 0, encoded)); Payload payload = DefaultPayload.create(Frame.from(stringToBuf("00000e0000000119000000000100000064"))); assertEquals("", StandardCharsets.UTF_8.decode(payload.getMetadata()).toString()); }
public static int encode( final ByteBuf byteBuf, final int streamId, final int flags, final FrameType type, final @Nullable ByteBuf metadata, final ByteBuf data) { if (Frame.isFlagSet(flags, FrameHeaderFlyweight.FLAGS_M) != (metadata != null)) { throw new IllegalArgumentException("metadata flag set incorrectly"); } if (type.hasInitialRequestN()) { throw new AssertionError(type + " must not be encoded without initial request N"); } final int frameLength = computeFrameLength( type, metadata != null ? metadata.readableBytes() : null, data.readableBytes()); int length = FrameHeaderFlyweight.encodeFrameHeader(byteBuf, frameLength, flags, type, streamId); length += FrameHeaderFlyweight.encodeMetadata(byteBuf, type, length, metadata); 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( int streamId, FrameType type, ByteBuf metadata, ByteBuf data, int initialRequestN, int flags) { final Frame frame = RECYCLER.get(); frame.setRefCnt(1); frame.content = ByteBufAllocator.DEFAULT.buffer( RequestFrameFlyweight.computeFrameLength( type, metadata.readableBytes(), data.readableBytes())); frame.content.writerIndex( RequestFrameFlyweight.encode( frame.content, streamId, flags, type, initialRequestN, metadata, data)); return frame; }
@Test public void testEncodingWithNullMetadata() { int encoded = RequestFrameFlyweight.encode( byteBuf, 1, 0, FrameType.REQUEST_STREAM, 1, null, Unpooled.copiedBuffer("d", StandardCharsets.UTF_8)); assertEquals("00000b0000000118000000000164", ByteBufUtil.hexDump(byteBuf, 0, encoded)); Payload payload = DefaultPayload.create(Frame.from(stringToBuf("00000b0000000118000000000164"))); assertFalse(payload.hasMetadata()); }
@Test(timeout = 2_000) public void testStreamInitialN() { Flux<Payload> stream = rule.socket.requestStream(EmptyPayload.INSTANCE); BaseSubscriber<Payload> subscriber = new BaseSubscriber<Payload>() { @Override protected void hookOnSubscribe(Subscription subscription) { // don't request here // subscription.request(3); } }; stream.subscribe(subscriber); subscriber.request(5); List<Frame> sent = rule.connection .getSent() .stream() .filter(f -> f.getType() != KEEPALIVE) .collect(Collectors.toList()); assertThat("sent frame count", sent.size(), is(1)); Frame f = sent.get(0); assertThat("initial frame", f.getType(), is(REQUEST_STREAM)); assertThat("initial request n", RequestFrameFlyweight.initialRequestN(f.content()), is(5)); }
public static int encode( final ByteBuf byteBuf, final int streamId, final int flags, final FrameType type, final @Nullable ByteBuf metadata, final ByteBuf data) { if (Frame.isFlagSet(flags, FrameHeaderFlyweight.FLAGS_M) != (metadata != null)) { throw new IllegalArgumentException("metadata flag set incorrectly"); } if (type.hasInitialRequestN()) { throw new AssertionError(type + " must not be encoded without initial request N"); } final int frameLength = computeFrameLength( type, metadata != null ? metadata.readableBytes() : null, data.readableBytes()); int length = FrameHeaderFlyweight.encodeFrameHeader(byteBuf, frameLength, flags, type, streamId); length += FrameHeaderFlyweight.encodeMetadata(byteBuf, type, length, metadata); length += FrameHeaderFlyweight.encodeData(byteBuf, length, data); return length; }
public static Frame from(int streamId, FrameType type, Payload payload, int initialRequestN) { if (initialRequestN < 1) { throw new IllegalStateException("initial request n must be greater than 0"); } final @Nullable ByteBuf metadata = payload.hasMetadata() ? payload.sliceMetadata() : null; final ByteBuf data = payload.sliceData(); final Frame frame = RECYCLER.get(); frame.setRefCnt(1); frame.content = ByteBufAllocator.DEFAULT.buffer( RequestFrameFlyweight.computeFrameLength( type, metadata != null ? metadata.readableBytes() : null, data.readableBytes())); if (type.hasInitialRequestN()) { frame.content.writerIndex( RequestFrameFlyweight.encode( frame.content, streamId, metadata != null ? FLAGS_M : 0, type, initialRequestN, metadata, data)); } else { frame.content.writerIndex( RequestFrameFlyweight.encode( frame.content, streamId, metadata != null ? FLAGS_M : 0, type, metadata, data)); } return frame; }
public static int initialRequestN(final Frame frame) { final FrameType type = frame.getType(); int result; if (!type.isRequestType()) { throw new AssertionError("expected request type, but saw " + type.name()); } switch (frame.getType()) { case REQUEST_RESPONSE: result = 1; break; case REQUEST_FNF: case METADATA_PUSH: result = 0; break; default: result = RequestFrameFlyweight.initialRequestN(frame.content); break; } return result; }
public static int encode( final ByteBuf byteBuf, final int streamId, int flags, final FrameType type, final int initialRequestN, final @Nullable ByteBuf metadata, final ByteBuf data) { if (Frame.isFlagSet(flags, FrameHeaderFlyweight.FLAGS_M) != (metadata != null)) { throw new IllegalArgumentException("metadata flag set incorrectly"); } final int frameLength = computeFrameLength( type, metadata != null ? metadata.readableBytes() : null, data.readableBytes()); int length = FrameHeaderFlyweight.encodeFrameHeader(byteBuf, frameLength, flags, type, streamId); byteBuf.setInt(INITIAL_REQUEST_N_FIELD_OFFSET, initialRequestN); length += Integer.BYTES; length += FrameHeaderFlyweight.encodeMetadata(byteBuf, type, length, metadata); length += FrameHeaderFlyweight.encodeData(byteBuf, length, data); return length; }
public static Frame from(int streamId, FrameType type, int flags) { final Frame frame = RECYCLER.get(); frame.setRefCnt(1); frame.content = ByteBufAllocator.DEFAULT.buffer(RequestFrameFlyweight.computeFrameLength(type, null, 0)); frame.content.writerIndex( RequestFrameFlyweight.encode( frame.content, streamId, flags, type, Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER)); return frame; }
public static Frame from( int streamId, FrameType type, ByteBuf metadata, ByteBuf data, int initialRequestN, int flags) { final Frame frame = RECYCLER.get(); frame.setRefCnt(1); frame.content = ByteBufAllocator.DEFAULT.buffer( RequestFrameFlyweight.computeFrameLength( type, metadata.readableBytes(), data.readableBytes())); frame.content.writerIndex( RequestFrameFlyweight.encode( frame.content, streamId, flags, type, initialRequestN, metadata, data)); return frame; }
public static Frame from(int streamId, FrameType type, Payload payload, int initialRequestN) { if (initialRequestN < 1) { throw new IllegalStateException("initial request n must be greater than 0"); } final @Nullable ByteBuf metadata = payload.hasMetadata() ? payload.sliceMetadata() : null; final ByteBuf data = payload.sliceData(); final Frame frame = RECYCLER.get(); frame.setRefCnt(1); frame.content = ByteBufAllocator.DEFAULT.buffer( RequestFrameFlyweight.computeFrameLength( type, metadata != null ? metadata.readableBytes() : null, data.readableBytes())); if (type.hasInitialRequestN()) { frame.content.writerIndex( RequestFrameFlyweight.encode( frame.content, streamId, metadata != null ? FLAGS_M : 0, type, initialRequestN, metadata, data)); } else { frame.content.writerIndex( RequestFrameFlyweight.encode( frame.content, streamId, metadata != null ? FLAGS_M : 0, type, metadata, data)); } return frame; }