Why is Netty giving me only 768 Bytes from UDP messages
Asked Answered
F

1

6

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));
}
Federica answered 17/7, 2012 at 15:23 Comment(0)
M
10

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

Mopey answered 23/7, 2012 at 13:42 Comment(2)
Could you add a citation for the default predictor being 768 byte fixed-size predictor?Kilar
DatagramChannelFactory. look at setReceiveBufferSizePredictor(ReceiveBufferSizePredictor) 'The default predictor is FixedReceiveBufferSizePredictor(768)Mopey

© 2022 - 2024 — McMap. All rights reserved.