Azure / DotNetty

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

IByteBuffer.ToArray has removed in 0.4.7,need equivalence implement #307

Closed windsql closed 6 years ago

windsql commented 6 years ago

when update to DotNetty.Buffers from 0.4.6 to 0.4.7 The ToArray method has removed from IByteBuffer interface Is there a method could instead of it.

    protected override void ChannelRead0(IChannelHandlerContext ctx, DatagramPacket packet)
    {
        if (!packet.Content.IsReadable())
        {
            return;
        }

        var message = packet.Content.ToArray(); //It works in 0.4.6,but not work in 0.4.7
        //deal with bytes message
    }
StormHub commented 6 years ago

The buffer interface already supports it by default. For example:

var data = new byte[packet.Content.ReadableBytes]; packet.Content.ReadBytes(data);

gamemachine commented 6 years ago

Something about it is different. Using that to get the bytes I start seeing unconsumed data left in the buffer messages.

StormHub commented 6 years ago

@gamemachine What do you mean?

gamemachine commented 6 years ago

I mean upgrading and changing nothing but one line of code to use what you said to use above to get the byte array, and I get unconsumed data left in buffer errors. Whether it's directly or indirectly related I have no idea.

StormHub commented 6 years ago

This is the original implementation, it is not so different anyway. It was removed because everything can be done through public byte buffer interfaces. @gamemachine note that it does not advance the reader index In your case, it is probably equal to the following: var data = new byte[packet.Content.ReadableBytes]; packet.Content.GetBytes(packet.Content.ReaderIndex, data);

    public virtual byte[] ToArray()
    {
        int readableBytes = this.ReadableBytes;
        if (readableBytes == 0)
        {
            return ArrayExtensions.ZeroBytes;
        }

        if (this.HasArray)
        {
            return this.Array.Slice(this.ArrayOffset + this.ReaderIndex, readableBytes);
        }

        var bytes = new byte[readableBytes];
        this.GetBytes(this.ReaderIndex, bytes);
        return bytes;
    }