6
votes

What I am doing is attempting to send an IPEndpoint through protobuf-net and what I observed is that when deserializing the array of 4 bytes into the IP4 address, the set code recieves a value of 8 bytes. Four bytes containing the orignal address, and 4 more bytes containing the address that was serialized. By stepping through the code I have been able to confirm that when Deserialize is called, it first reads the bytes, and then sets they bytes.

After doing some reading I learned about OverwriteList, and as can been seen in the example below, I have set that to true. However the setter is still provided an 8 byte value.

Does anyone have a clue what I am doing wrong?

This sample code should throw an exception when used with protobuf-net r480, Visual Studio 2010 as a .Net 4.0 console application.

using ProtoBuf;
using System.Net;
using System.IO;

namespace ConsoleApplication1
{

    [ProtoContract]
    class AddressOWner
    {
        private IPEndPoint endpoint;

        public AddressOWner() 
        { endpoint = new IPEndPoint(new IPAddress(new byte[] {8,8,8,8}), 0); }

        public AddressOWner(IPEndPoint newendpoint)
        { this.endpoint = newendpoint; }

        [ProtoMember(1, OverwriteList=true)]
        public byte[] AddressBytes
        {
            get { return endpoint.Address.GetAddressBytes(); }
            set { endpoint.Address = new IPAddress(value); }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            AddressOWner ao = new AddressOWner(new IPEndPoint(new IPAddress(new byte[] { 192, 168, 1, 1 }), 80));

            MemoryStream ms = new MemoryStream();
            Serializer.Serialize(ms, ao);
            byte[] messageData = ms.GetBuffer();
            ms = new MemoryStream(messageData);
            AddressOWner aoCopy = Serializer.Deserialize<AddressOWner>(ms);
        }
    }
}
1

1 Answers

3
votes

It looks like this is actually a bug, specific to byte[], which is handled as a particular protobuf primitive. Other arrays/lists are mapped to repeated (in protobuf terms), and handle the OverwriteList option correctly. I will tweak the byte[] handling to support this option.

Edit: this is fixed in r484, with supporting integration test