Search in sources :

Example 1 with RequestDataComposite

use of org.apache.ratis.experiments.nettyzerocopy.objects.RequestDataComposite 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;
    }
}
Also used : RequestDataComposite(org.apache.ratis.experiments.nettyzerocopy.objects.RequestDataComposite) ByteBuf(org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf)

Example 2 with RequestDataComposite

use of org.apache.ratis.experiments.nettyzerocopy.objects.RequestDataComposite in project incubator-ratis by apache.

the class NettyServer method getServerHandler.

/**
 * Casts inbound message as {@link RequestDataComposite}
 * because a NIO CompositeByteBuf interface is needed to allow for zero-copy.
 * @return ChannelInboundHandler
 */
private ChannelInboundHandler getServerHandler() {
    return new ChannelInboundHandlerAdapter() {

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            final ResponseData reply = new ResponseData();
            RequestDataComposite req = (RequestDataComposite) msg;
            req.getBuff().release();
            reply.setId(req.getDataId());
            ctx.writeAndFlush(reply);
        }
    };
}
Also used : RequestDataComposite(org.apache.ratis.experiments.nettyzerocopy.objects.RequestDataComposite) ResponseData(org.apache.ratis.experiments.nettyzerocopy.objects.ResponseData)

Aggregations

RequestDataComposite (org.apache.ratis.experiments.nettyzerocopy.objects.RequestDataComposite)2 ResponseData (org.apache.ratis.experiments.nettyzerocopy.objects.ResponseData)1 ByteBuf (org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf)1