Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Netty giving me only 768 Bytes from UDP messages

I have set the "receiveBufferSize" option to 1024, but for some reason I'm still getting only 768 bytes in messageReceived. The header of the data indicates that size of the data being sent is 1004.

Below is the initialization code for the server:

public static void main(String[] args) throws Exception {
    ConnectionlessBootstrap b = new ConnectionlessBootstrap(new NioDatagramChannelFactory());

    // Options for a new channel        
    b.setOption("receiveBufferSize", 1024);
    System.out.println(b.getOptions());

    b.setPipelineFactory(new ChannelPipelineFactory() {

        @Override
        public ChannelPipeline getPipeline() throws Exception {
            return Channels.pipeline(
                    new MyUDPPacketDecoder(),
                    new StdOutPrintHandler());
        }
    });

    b.bind(new InetSocketAddress(myPort));
}
like image 876
Eero Aaltonen Avatar asked Jul 17 '12 15:07

Eero Aaltonen


1 Answers

You need to set an additional option - receiveBufferSizePredictorFactory.

in order to predict how much space it needs to allocate in order to hold the incoming message, netty uses a predictor that predicts the amount of byte to allocate.

there are two type of receive buffer size predictors, adaptive and fixed-size. the predictors are created by a predictor factory, which creates one for each channel created by the bootstrap.

if no predictor factory is set for the bootstrap (or no predictor is set manually for the channel), the channel uses the default 768 byte fixed-size predictor. all messages bigger then 768 bytes are cut down to that size.

you can add:

b.setOption("receiveBufferSizePredictorFactory", new FixedReceiveBufferSizePredictorFactory(1024));

you can read about the predictors and their factories in netty documentation

ReceiveBufferSizePredictor Inteface

ReceiveBufferSizePredictorFactory Inteface

like image 161
Yoav Slomka Avatar answered Oct 12 '22 21:10

Yoav Slomka