|
23 | 23 | import java.nio.ByteOrder;
|
24 | 24 |
|
25 | 25 | /**
|
26 |
| - * Per connection frame handling. |
27 |
| - * Holds codecs and DirectBuffers for wrapping |
| 26 | + * Per connection frame flyweight. |
| 27 | + * Holds codecs and DirectBuffer for wrapping |
28 | 28 | */
|
29 | 29 | public class FrameFlyweight
|
30 | 30 | {
|
31 | 31 | /**
|
32 |
| - * Not the real frame layout, just an iteration on the ASCII version |
| 32 | + * Not the latest frame layout, but close |
| 33 | + * Does not include |
| 34 | + * - (initial) request N for REQUEST_STREAM and REQUEST_SUB and REQUEST |
| 35 | + * - fragmentation / reassembly |
| 36 | + * - encode should remove Type param and have it as part of method name (1 encode per type) |
33 | 37 | */
|
34 |
| - private static final int VERSION_FIELD_OFFSET = 0; |
35 |
| - private static final int STREAM_ID_FIELD_OFFSET = VERSION_FIELD_OFFSET + BitUtil.SIZE_OF_INT; |
36 |
| - private static final int TYPE_FIELD_OFFSET = STREAM_ID_FIELD_OFFSET + BitUtil.SIZE_OF_LONG; |
37 |
| - private static final int DATA_LENGTH_OFFSET = TYPE_FIELD_OFFSET + BitUtil.SIZE_OF_INT; |
38 |
| - private static final int DATA_OFFSET = DATA_LENGTH_OFFSET + BitUtil.SIZE_OF_INT; |
| 38 | + private static final boolean INCLUDE_FRAME_LENGTH = true; |
| 39 | + |
| 40 | + private static final int FRAME_LENGTH_FIELD_OFFSET; |
| 41 | + private static final int VERSION_FIELD_OFFSET; |
| 42 | + private static final int FLAGS_FIELD_OFFSET; |
| 43 | + private static final int TYPE_FIELD_OFFSET; |
| 44 | + private static final int STREAM_ID_FIELD_OFFSET; |
| 45 | + private static final int DATA_OFFSET; |
39 | 46 |
|
40 | 47 | private static final byte CURRENT_VERSION = 0;
|
41 | 48 |
|
| 49 | + private static final int FLAGS_I = 0b1000_000; |
| 50 | + private static final int FLAGS_B = 0b0100_000; |
| 51 | + private static final int FLAGS_E = 0b0010_000; |
| 52 | + private static final int FLAGS_C = 0b0001_000; |
| 53 | + |
| 54 | + static |
| 55 | + { |
| 56 | + if (INCLUDE_FRAME_LENGTH) |
| 57 | + { |
| 58 | + FRAME_LENGTH_FIELD_OFFSET = 0; |
| 59 | + } |
| 60 | + else |
| 61 | + { |
| 62 | + FRAME_LENGTH_FIELD_OFFSET = - BitUtil.SIZE_OF_INT; |
| 63 | + } |
| 64 | + |
| 65 | + VERSION_FIELD_OFFSET = FRAME_LENGTH_FIELD_OFFSET + BitUtil.SIZE_OF_INT; |
| 66 | + FLAGS_FIELD_OFFSET = VERSION_FIELD_OFFSET + BitUtil.SIZE_OF_BYTE; |
| 67 | + TYPE_FIELD_OFFSET = FLAGS_FIELD_OFFSET + BitUtil.SIZE_OF_BYTE; |
| 68 | + STREAM_ID_FIELD_OFFSET = TYPE_FIELD_OFFSET + BitUtil.SIZE_OF_SHORT; |
| 69 | + DATA_OFFSET = STREAM_ID_FIELD_OFFSET + BitUtil.SIZE_OF_LONG; |
| 70 | + } |
| 71 | + |
42 | 72 | private static final ByteBuffer EMPTY_BUFFER = ByteBuffer.allocate(0);
|
43 | 73 |
|
44 | 74 | // single threaded assumed
|
45 | 75 | private final MutableDirectBuffer frameBuffer = new UnsafeBuffer(EMPTY_BUFFER);
|
46 | 76 |
|
47 |
| - public static int frameLength(final int dataLength) |
| 77 | + public static int computeFrameLength(final int dataLength) |
48 | 78 | {
|
49 | 79 | return DATA_OFFSET + dataLength;
|
50 | 80 | }
|
51 | 81 |
|
52 |
| - public void encode(final ByteBuffer byteBuffer, final long streamId, final FrameType type, final byte[] data) |
| 82 | + public int encode(final ByteBuffer byteBuffer, final long streamId, final FrameType type, final byte[] data) |
53 | 83 | {
|
| 84 | + final int frameLength = computeFrameLength(data.length); |
| 85 | + |
54 | 86 | frameBuffer.wrap(byteBuffer);
|
| 87 | + |
| 88 | + if (INCLUDE_FRAME_LENGTH) |
| 89 | + { |
| 90 | + frameBuffer.putInt(FRAME_LENGTH_FIELD_OFFSET, frameLength, ByteOrder.BIG_ENDIAN); |
| 91 | + } |
| 92 | + |
| 93 | + final FrameType outFrameType; |
| 94 | + int flags = 0; |
| 95 | + |
| 96 | + switch (type) |
| 97 | + { |
| 98 | + case COMPLETE: |
| 99 | + outFrameType = FrameType.RESPONSE; |
| 100 | + flags |= FLAGS_C; |
| 101 | + break; |
| 102 | + case NEXT: |
| 103 | + outFrameType = FrameType.RESPONSE; |
| 104 | + break; |
| 105 | + default: |
| 106 | + outFrameType = type; |
| 107 | + break; |
| 108 | + } |
| 109 | + |
55 | 110 | frameBuffer.putByte(VERSION_FIELD_OFFSET, CURRENT_VERSION);
|
| 111 | + frameBuffer.putByte(FLAGS_FIELD_OFFSET, (byte) flags); |
| 112 | + frameBuffer.putShort(TYPE_FIELD_OFFSET, (short)outFrameType.getEncodedType(), ByteOrder.BIG_ENDIAN); |
56 | 113 | frameBuffer.putLong(STREAM_ID_FIELD_OFFSET, streamId, ByteOrder.BIG_ENDIAN);
|
57 |
| - frameBuffer.putInt(TYPE_FIELD_OFFSET, type.getMessageId(), ByteOrder.BIG_ENDIAN); |
58 |
| - frameBuffer.putInt(DATA_LENGTH_OFFSET, data.length, ByteOrder.BIG_ENDIAN); |
59 | 114 | frameBuffer.putBytes(DATA_OFFSET, data);
|
| 115 | + |
| 116 | + return frameLength; |
60 | 117 | }
|
61 | 118 |
|
62 |
| - public void decode(Frame frame, final ByteBuffer byteBuffer) |
| 119 | + public void decode(Frame frame, final ByteBuffer byteBuffer, final int length) |
63 | 120 | {
|
64 | 121 | frameBuffer.wrap(byteBuffer);
|
65 | 122 |
|
| 123 | + int frameLength = length; |
| 124 | + |
| 125 | + if (INCLUDE_FRAME_LENGTH) |
| 126 | + { |
| 127 | + frameLength = frameBuffer.getInt(FRAME_LENGTH_FIELD_OFFSET, ByteOrder.BIG_ENDIAN); |
| 128 | + } |
| 129 | + |
66 | 130 | final int version = frameBuffer.getByte(VERSION_FIELD_OFFSET);
|
| 131 | + final int flags = frameBuffer.getByte(FLAGS_FIELD_OFFSET); |
| 132 | + FrameType frameType = FrameType.from(frameBuffer.getShort(TYPE_FIELD_OFFSET, ByteOrder.BIG_ENDIAN)); |
| 133 | + |
67 | 134 | final long streamId = frameBuffer.getLong(STREAM_ID_FIELD_OFFSET, ByteOrder.BIG_ENDIAN);
|
68 |
| - final FrameType frameType = FrameType.from(frameBuffer.getInt(TYPE_FIELD_OFFSET, ByteOrder.BIG_ENDIAN)); |
69 |
| - final int dataLength = frameBuffer.getInt(DATA_LENGTH_OFFSET, ByteOrder.BIG_ENDIAN); |
| 135 | + |
| 136 | + final int dataLength = frameLength - DATA_OFFSET; |
| 137 | + int dataOffset = DATA_OFFSET; |
| 138 | + |
| 139 | + switch (frameType) |
| 140 | + { |
| 141 | + case RESPONSE: |
| 142 | + if (FLAGS_C == (flags & FLAGS_C)) |
| 143 | + { |
| 144 | + frameType = FrameType.COMPLETE; |
| 145 | + } |
| 146 | + else |
| 147 | + { |
| 148 | + frameType = FrameType.NEXT; |
| 149 | + } |
| 150 | + break; |
| 151 | + |
| 152 | + case REQUEST_N: |
| 153 | + // TODO: grab N value |
| 154 | + break; |
| 155 | + case REQUEST_STREAM: |
| 156 | + // TODO: grab N value, and move DATA_OFFSET value |
| 157 | + break; |
| 158 | + case REQUEST_SUBSCRIPTION: |
| 159 | + // TODO: grab N value, and move DATA_OFFSET value |
| 160 | + break; |
| 161 | + } |
70 | 162 |
|
71 | 163 | // fill in Frame fields
|
72 |
| - frame.setFromDecode(version, streamId, frameType); |
73 |
| - frame.setFromDecode(frameBuffer, DATA_OFFSET, dataLength); |
| 164 | + frame.setFromDecode(version, streamId, frameType, flags); |
| 165 | + frame.setFromDecode(frameBuffer, dataOffset, dataLength); |
74 | 166 | }
|
75 | 167 | }
|
0 commit comments