ChannelBuffer and messageReceived
manish_iitg
excellencetechnologies08 at gmail.com
Fri Dec 26 21:35:39 EST 2008
Hello,
I have faced the same problem. I think there are two solutions for this.
1. If the message that you are sending is of fixed size then, you can use
framedecoder as given in the UserGuide.
2. IF the message that you are sending is of variable size then you need to
send the length of message first and the entire message.
ChannelBuffer buffer = (ChannelBuffer) e.getMessage();
dataBuffer.writeBytes(buffer); // data buffer, class level variable
if (dataBuffer.readableBytes() < length) //the length you sent
return;
//go ahead
nixter wrote:
>
> Hello,
>
> I have a writed a client/server program.
>
> The client send data assembled in ChannelBuffer, the data look like this :
>
> Sender, receiver, tag, DATA
>
> Where sender, receiver, tag are integer and datas are a set of bytes.
>
> the client send data like this :
>
> channel.write(ChannelBuffers.wrappedBuffer(buffer)); where buffer is a
> BufferByte.
>
> In server side, to gather datas I do like this :
>
> public synchronized void messageReceived(ChannelHandlerContext ctx,
> MessageEvent e) {
>
> ByteBuffer buff = null;
>
> ChannelBuffer chanbuf = (ChannelBuffer) e.getMessage();
>
> buff=chanbuf.toByteBuffer();
>
> int src=buff.getInt();
> int dest=buff.getInt();
> int tag=buff.getInt();
>
> ...
> }
>
> The problème is for small buffer i got the correct data, but for some
> buffer i receive wrong data!! I know because communication are
> non-bloquant.
>
> My question is : when netty call messageReceived () is the ChannelBuffer
> contains the whole data?
> is there a mecansime like ByteBuffer.hasremaining to read the whole data
> sent?
>
> Please help if you have an idea or an example.
>
> P.S sorry for my English
>
> ---- Here some log :
>
> the client send one message (header+data), and i noticed that
> messageReceived () is invoked 5 time!!
>
>
>
> (message: BigEndianHeapChannelBuffer(ridx=0, widx=1536, cap=1536))
> (message: BigEndianHeapChannelBuffer(ridx=0, widx=2048, cap=2048))
> (message: BigEndianHeapChannelBuffer(ridx=0, widx=3072, cap=3072))
> (message: BigEndianHeapChannelBuffer(ridx=0, widx=4096, cap=4096))
> (message: BigEndianHeapChannelBuffer(ridx=0, widx=4657, cap=6144)
>
>
>
--
View this message in context: http://n2.nabble.com/ChannelBuffer-and---messageReceived-tp1943393p1967638.html
Sent from the Netty User Group mailing list archive at Nabble.com.
More information about the netty-users
mailing list