Search in sources :

Example 1 with RequestData

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

the class RequestDecoder 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) {
            RequestData req = new RequestData();
            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);
                req.setBuff(bf.nioBuffer());
            } 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 : RequestData(org.apache.ratis.experiments.nettyzerocopy.objects.RequestData) ByteBuf(org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf)

Example 2 with RequestData

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

the class NettyClient method startTransfer.

public void startTransfer() throws InterruptedException {
    int cur = 1;
    ByteBuffer bf = ByteBuffer.allocateDirect(1024 * 1024);
    for (int i = 0; i < bf.capacity(); i++) {
        bf.put((byte) 'a');
    }
    bf.flip();
    startTime = System.nanoTime();
    while (cur <= times) {
        RequestData msg = new RequestData();
        msg.setDataId(cur);
        bf.position(0).limit(bf.capacity());
        msg.setBuff(bf.slice());
        transferMessage(msg);
        cur++;
    }
    System.out.println("Sent all messages.");
}
Also used : RequestData(org.apache.ratis.experiments.nettyzerocopy.objects.RequestData) ByteBuffer(java.nio.ByteBuffer)

Aggregations

RequestData (org.apache.ratis.experiments.nettyzerocopy.objects.RequestData)2 ByteBuffer (java.nio.ByteBuffer)1 ByteBuf (org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf)1