Azure / DotNetty

DotNetty project – a port of netty, event-driven asynchronous network application framework
Other
4.09k stars 977 forks source link

how to manage IByteBuffer lifetime properly? #139

Closed egmkang closed 8 years ago

egmkang commented 8 years ago

I'm using DotNetty writing codes to communicate with my c++ server. I read some blogs and realized IByteBuffer must be release. to confirm this and usage DotNetty in the right way, i have these questions:

public override void ChannelRead(IChannelHandlerContext context, object message)
{
    IByteBuffer buffer = message as IByteBuffer;
    //dost i need release this message after my useage
    buffer.release();
}
IByteBuffer buffer1 = Unpooled.WrappedBuffer(array);
IByteBuffer buffer2 = UnpooledHeapByteBuffer(PooledByteBufferAllocator.Default, array, array.Length);
IByteBuffer buff = (message as IByteBuffer).ReadBytes(128);
IByteBuffer slice = (message as IByteBuffer).ReadSlice(128);
nayato commented 8 years ago

@egmkang, please refer to this first. Extra comments on your questions (in order):

egmkang commented 8 years ago

@nayato thank you very much. i have read the article carefully. it have solved my No.2 and No.3 problems. about the first problem. the article said i have release the buffer. my question is Netty always Allocate A New Buffer, it won't reuse the buffer object? for example, when the buffer contains part of the whole message, i must cache the bytes of buffer netty allocated, and release the buffer, until i can decode one whole message.

https://github.com/Azure/DotNetty/blob/86749e908c1be0800ca43374102b28980519b7ba/src/DotNetty.Transport/Channels/Sockets/AbstractSocketByteChannel.cs#L101

egmkang commented 8 years ago

@nayato i found out ByteToMessageDecoder has finished the feature. thank you again.