|
| 1 | +/** |
| 2 | + * Copyright 2015 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.reactivesocket; |
| 17 | + |
| 18 | +import uk.co.real_logic.agrona.BitUtil; |
| 19 | +import uk.co.real_logic.agrona.MutableDirectBuffer; |
| 20 | +import uk.co.real_logic.agrona.concurrent.UnsafeBuffer; |
| 21 | + |
| 22 | +import java.nio.ByteBuffer; |
| 23 | +import java.nio.ByteOrder; |
| 24 | + |
| 25 | +/** |
| 26 | + * Per connection frame handling. |
| 27 | + * Holds codecs and DirectBuffers for wrapping |
| 28 | + */ |
| 29 | +public class FrameHandler |
| 30 | +{ |
| 31 | + /** |
| 32 | + * Not the real frame layout, just an iteration on the ASCII version |
| 33 | + */ |
| 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; |
| 39 | + |
| 40 | + private static final byte CURRENT_VERSION = 0; |
| 41 | + |
| 42 | + private static final ByteBuffer EMPTY_BUFFER = ByteBuffer.allocate(0); |
| 43 | + |
| 44 | + // single threaded assumed |
| 45 | + private final MutableDirectBuffer frameBuffer = new UnsafeBuffer(EMPTY_BUFFER); |
| 46 | + |
| 47 | + // set by decode |
| 48 | + private long streamId; |
| 49 | + private MessageType messageType; |
| 50 | + private byte version; |
| 51 | + private int dataLength; |
| 52 | + |
| 53 | + public static int frameLength(final int dataLength) |
| 54 | + { |
| 55 | + return DATA_OFFSET + dataLength; |
| 56 | + } |
| 57 | + |
| 58 | + public void encode(final ByteBuffer byteBuffer, final long streamId, final MessageType type, final byte[] data) |
| 59 | + { |
| 60 | + frameBuffer.wrap(byteBuffer); |
| 61 | + frameBuffer.putByte(VERSION_FIELD_OFFSET, CURRENT_VERSION); |
| 62 | + frameBuffer.putLong(STREAM_ID_FIELD_OFFSET, streamId, ByteOrder.BIG_ENDIAN); |
| 63 | + frameBuffer.putInt(TYPE_FIELD_OFFSET, type.getMessageId(), ByteOrder.BIG_ENDIAN); |
| 64 | + frameBuffer.putInt(DATA_LENGTH_OFFSET, data.length, ByteOrder.BIG_ENDIAN); |
| 65 | + frameBuffer.putBytes(DATA_OFFSET, data); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * populate streamId, type, dataLength, etc. |
| 70 | + */ |
| 71 | + public void decode(final ByteBuffer byteBuffer) |
| 72 | + { |
| 73 | + frameBuffer.wrap(byteBuffer); |
| 74 | + |
| 75 | + version = frameBuffer.getByte(VERSION_FIELD_OFFSET); |
| 76 | + streamId = frameBuffer.getLong(STREAM_ID_FIELD_OFFSET, ByteOrder.BIG_ENDIAN); |
| 77 | + messageType = MessageType.from(frameBuffer.getInt(TYPE_FIELD_OFFSET, ByteOrder.BIG_ENDIAN)); |
| 78 | + dataLength = frameBuffer.getInt(DATA_LENGTH_OFFSET, ByteOrder.BIG_ENDIAN); |
| 79 | + } |
| 80 | + |
| 81 | + public ByteBuffer buffer() |
| 82 | + { |
| 83 | + return frameBuffer.byteBuffer(); |
| 84 | + } |
| 85 | + |
| 86 | + public byte version() |
| 87 | + { |
| 88 | + return version; |
| 89 | + } |
| 90 | + |
| 91 | + public MessageType messageType() |
| 92 | + { |
| 93 | + return messageType; |
| 94 | + } |
| 95 | + |
| 96 | + public long streamId() |
| 97 | + { |
| 98 | + return streamId; |
| 99 | + } |
| 100 | + |
| 101 | + public int dataLength() |
| 102 | + { |
| 103 | + return dataLength; |
| 104 | + } |
| 105 | + |
| 106 | + public void getDataBytes(final byte[] array) |
| 107 | + { |
| 108 | + frameBuffer.getBytes(DATA_OFFSET, array); |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments