How many times is the messageReceived() Method invoked when I send large data?

brunodecarvalho kindernade at gmail.com
Wed Aug 25 21:29:24 EDT 2010


Daniel,


I'm assuming you're using stream based transport (TCP) so there's really no
telling whether 1 write at the client side will result in 1 read at the
server side.

I'd be surprised if your message would arrive at the server in "one piece"
(resulting in a single read).

What you probably want to do is have a message header with the size of the
message, so you know how many bytes you should read to complete a message.
In your case, I'd allocate a buffer with the size of the bytes you're trying
to transfer + 4 bytes (an integer, but usually a short or a byte are
enough... depends on your requirements and the size of the stuff you expect
to transfer).

Assume your pipeline looks like this: Decoder -> Handler

So the first 4 bytes of your message would be the size, which would tell
your Decoder (a ChannelHandler) that it would have to read X more bytes to
get a complete message.
When this read was complete, the Decoder would send the message (just the
actual data bytes, without the 4 bytes for the size) upstream, to the
Handler. To the Handler, it would appear that only one read took place.
In other words, the Handler only gets its messageReceived() called once the
Decoder lets data pass through

You could also implement an Encoder so that those 4 bytes with the size
would be added automatically when you call write() on a channel. This way
you wouldn't have to worry about preparing data to be read at the other
side.

Did this clear things up or made them even more confusing? :)


Bruno
-- 
View this message in context: http://netty-forums-and-mailing-lists.685743.n2.nabble.com/How-many-times-is-the-messageReceived-Method-invoked-when-I-send-large-data-tp5463771p5463817.html
Sent from the Netty User Group mailing list archive at Nabble.com.


More information about the netty-users mailing list