Protobuf-net OverwriteList in byte array

I am trying to send an IPEndpoint via protobuf-net, and I noticed that when deserializing an array of 4 bytes into an IP4 address, the installed code gets the value 8 bytes. Four bytes containing the orignal address, and another 4 bytes containing the address that was serialized. After going through the code, I was able to confirm that when I called Deserialize, it first reads the bytes, and then sets their bytes.

After some reading, I found out about OverwriteList, and as you can see from the example below, I set this to true. However, the installer still has an 8-byte value.

Does anyone know what I'm doing wrong?

This code sample 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);
         }
     }
 }
+6
source share
1 answer

This seems to be actually a bug specific to byte[] , which is handled as a concrete protobuf primitive. Other arrays / lists are mapped to repeated (in protobuf terms) and correctly handle the OverwriteList parameter. I will configure byte[] processing to support this option.

Edit: this is fixed in r484 with integration test support

+3
source

Source: https://habr.com/ru/post/907653/


All Articles