use of org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf 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;
}
}
use of org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf in project incubator-ratis by apache.
the class NettyDataStreamUtils method encodeDataStreamReplyByteBuffer.
static void encodeDataStreamReplyByteBuffer(DataStreamReplyByteBuffer reply, Consumer<ByteBuf> out, ByteBufAllocator allocator) {
ByteBuffer headerBuf = getDataStreamReplyHeaderProtoByteBuf(reply);
final ByteBuf headerLenBuf = allocator.directBuffer(DataStreamPacketHeader.getSizeOfHeaderLen());
headerLenBuf.writeInt(headerBuf.remaining());
out.accept(headerLenBuf);
out.accept(Unpooled.wrappedBuffer(headerBuf));
out.accept(Unpooled.wrappedBuffer(reply.slice()));
}
use of org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf in project incubator-ratis by apache.
the class NettyDataStreamUtils method encodeDataStreamRequestHeader.
static void encodeDataStreamRequestHeader(DataStreamRequest request, Consumer<Object> out, ByteBufAllocator allocator) {
final ByteBuffer headerBuf = getDataStreamRequestHeaderProtoByteBuffer(request);
final ByteBuf headerBodyLenBuf = allocator.directBuffer(DataStreamPacketHeader.getSizeOfHeaderBodyLen());
headerBodyLenBuf.writeLong(headerBuf.remaining() + request.getDataLength());
out.accept(headerBodyLenBuf);
final ByteBuf headerLenBuf = allocator.directBuffer(DataStreamPacketHeader.getSizeOfHeaderLen());
headerLenBuf.writeInt(headerBuf.remaining());
out.accept(headerLenBuf);
out.accept(Unpooled.wrappedBuffer(headerBuf));
}
use of org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf in project incubator-ratis by apache.
the class RequestDecoderComposite method decode.
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
if (msg.readableBytes() >= 8) {
int id = msg.readInt();
int buflen = msg.readInt();
if (msg.readableBytes() >= buflen) {
RequestDataComposite req = new RequestDataComposite();
req.setDataId(id);
// System.out.printf("msg id and buflen %d and %d bytes\n", id, buflen, msg.readableBytes());
try {
ByteBuf bf = msg.slice(msg.readerIndex(), buflen);
bf.retain();
req.setBuff(bf);
} catch (Exception e) {
System.out.println(e);
}
msg.readerIndex(msg.readerIndex() + buflen);
msg.markReaderIndex();
out.add(req);
} else {
msg.resetReaderIndex();
return;
}
} else {
return;
}
}
Aggregations