Search in sources :

Example 1 with DataStreamRequestHeader

use of org.apache.ratis.protocol.DataStreamRequestHeader in project incubator-ratis by apache.

the class NettyDataStreamUtils method decodeDataStreamRequestHeader.

static DataStreamRequestHeader decodeDataStreamRequestHeader(ByteBuf buf) {
    if (DataStreamPacketHeader.getSizeOfHeaderBodyLen() > buf.readableBytes()) {
        return null;
    }
    long headerBodyBufLen = buf.readLong();
    if (headerBodyBufLen > buf.readableBytes()) {
        buf.resetReaderIndex();
        return null;
    }
    int headerBufLen = buf.readInt();
    if (headerBufLen > buf.readableBytes()) {
        buf.resetReaderIndex();
        return null;
    }
    try {
        ByteBuf headerBuf = buf.slice(buf.readerIndex(), headerBufLen);
        DataStreamRequestHeaderProto header = DataStreamRequestHeaderProto.parseFrom(headerBuf.nioBuffer());
        final DataStreamPacketHeaderProto h = header.getPacketHeader();
        if (h.getDataLength() + headerBufLen <= buf.readableBytes()) {
            buf.readerIndex(buf.readerIndex() + headerBufLen);
            WriteOption[] options = new WriteOption[h.getOptionsCount()];
            for (int i = 0; i < options.length; i++) {
                options[i] = StandardWriteOption.values()[h.getOptions(i).ordinal()];
            }
            return new DataStreamRequestHeader(ClientId.valueOf(h.getClientId()), h.getType(), h.getStreamId(), h.getStreamOffset(), h.getDataLength(), options);
        } else {
            buf.resetReaderIndex();
            return null;
        }
    } catch (InvalidProtocolBufferException e) {
        LOG.error("Fail to decode request header:", e);
        buf.resetReaderIndex();
        return null;
    }
}
Also used : DataStreamPacketHeaderProto(org.apache.ratis.proto.RaftProtos.DataStreamPacketHeaderProto) DataStreamRequestHeaderProto(org.apache.ratis.proto.RaftProtos.DataStreamRequestHeaderProto) InvalidProtocolBufferException(org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException) DataStreamRequestHeader(org.apache.ratis.protocol.DataStreamRequestHeader) DataStreamRequestByteBuf(org.apache.ratis.netty.server.DataStreamRequestByteBuf) ByteBuf(org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf) StandardWriteOption(org.apache.ratis.io.StandardWriteOption) WriteOption(org.apache.ratis.io.WriteOption)

Aggregations

StandardWriteOption (org.apache.ratis.io.StandardWriteOption)1 WriteOption (org.apache.ratis.io.WriteOption)1 DataStreamRequestByteBuf (org.apache.ratis.netty.server.DataStreamRequestByteBuf)1 DataStreamPacketHeaderProto (org.apache.ratis.proto.RaftProtos.DataStreamPacketHeaderProto)1 DataStreamRequestHeaderProto (org.apache.ratis.proto.RaftProtos.DataStreamRequestHeaderProto)1 DataStreamRequestHeader (org.apache.ratis.protocol.DataStreamRequestHeader)1 InvalidProtocolBufferException (org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException)1 ByteBuf (org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf)1