Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netty Increase ChannelBuffer Size

Tags:

java

netty

Hello I have a Netty Server with a handler that should accept strings. It seems to only receive content up to 1024 bytes. How can i increase Buffer size. I have already tried

bootstrap.setOption("child.sendBufferSize", 1048576); 
bootstrap.setOption("child.receiveBufferSize", 1048576);

without success.

The handler is as below

public class TestHandler extends SimpleChannelHandler {


@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {

    ChannelBuffer buf = (ChannelBuffer) e.getMessage();

    String response = "";

    if (buf.readable()) {

        response = buf.toString(CharsetUtil.UTF_8);
        System.out.println("CONTENT: " + response);
    }

    System.out.println(response);


}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
    e.getCause().printStackTrace();

    Channel ch = e.getChannel();
    ch.close();
}

}

like image 869
ukuta Avatar asked Oct 26 '12 12:10

ukuta


2 Answers

Are you using UDP ? If so, packets will max out at 1024 bytes. This code comment is in the QOTM code sample:

Allow packets as large as up to 1024 bytes (default is 768). You could increase or decrease this value to avoid truncated packets or to improve memory footprint respectively.

Please also note that a large UDP packet might be truncated or dropped by your router no matter how you configured this option. In UDP, a packet is truncated or dropped if it is larger than a certain size, depending on router configuration. IPv4 routers truncate and IPv6 routers drop a large packet. That's why it is safe to send small packets in UDP.

If you are using TCP, you should add a frame decoder and a string decoder into your pipeline before yout handler; Something like this:

pipeline.addLast("frameDecoder", new DelimiterBasedFrameDecoder(80960, Delimiters.lineDelimiter()));
pipeline.addLast("stringDecoder", new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast("myHandler", new TestHandler());

Mind you, you will need to modify your test handler because the MessageEvent will actually contain your string.

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
    String response = (String) e.getMessage();
    System.out.println(response);
}

Make sense ?

like image 72
Nicholas Avatar answered Nov 15 '22 10:11

Nicholas


In version 4.0.10.Final for UDP Buffer size is set to 2048 bytes.

If You want to increase it set ChannelOptions as follows:

option(ChannelOption.SO_RCVBUF, int bytes)

and also

option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(int Bytes))
like image 33
Gaurav Avatar answered Nov 15 '22 10:11

Gaurav