Note that there are some explanatory texts on larger screens.

plurals
  1. POprotobuf-net fails deserializing my class
    primarykey
    data
    text
    <p>I want to serialize a 'Player' class and send it over my network stream to a client.</p> <p>Player Class</p> <pre><code> [ProtoMember(1)] public int flag; [ProtoMember(2)] public Int16 id; [ProtoMember(3)] public MyVector3 CharPos; [ProtoMember(7)] public bool spawned; </code></pre> <p><code>MyVector3</code> (due to the fact that protobuf does not support serialization of <code>Vector3</code>)</p> <pre><code>[ProtoContract] public class MyVector3 { [ProtoMember(4)] public float X { get; set; } [ProtoMember(5)] public float Y { get; set; } [ProtoMember(6)] public float Z { get; set; } public MyVector3() { this.X = 0.0f; this.Y = 0.0f; this.Z = 0.0f; } public MyVector3(float x, float y, float z) { this.X = x; this.Y = y; this.Z = z; } public static implicit operator Vector3(MyVector3 v) { return new Vector3(v.X, v.Y, v.Z); } public static implicit operator MyVector3(Vector3 v) { return new MyVector3(v.X, v.Y, v.Z); } } </code></pre> <p>Serialize and Deserialize Function</p> <pre><code>public byte[] serialize(Object obj) { if (obj == null) { return null; } MemoryStream ms = new MemoryStream(); Serializer.SerializeWithLengthPrefix(ms,obj,PrefixStyle.Base128); return ms.ToArray(); } public Player deserialize(NetworkStream inc) { Player obj = Serializer.DeserializeWithLengthPrefix&lt;Player&gt;(inc,PrefixStyle.Base128); return obj; } </code></pre> <p>Test Function (which in fact does not work!)</p> <pre><code> static void Main(string[] args) { TcpListener serverSocket = new TcpListener(8888); TcpClient clientSocket = default(TcpClient); Serialize ser = new Serialize(); Player temp = new Player(); serverSocket.Start(); while (true) { clientSocket = serverSocket.AcceptTcpClient(); using (NetworkStream networkStream = clientSocket.GetStream()) { DeserializeTest(ser,networkStream); SerializeTest(ser, networkStream); } } } static void SerializeTest(Serialize ser,NetworkStream networkStream) { Player temp = new Player(); BinaryWriter writer = new BinaryWriter(networkStream); writer.Write(ser.serialize(temp)); networkStream.Flush(); } static void DeserializeTest(Serialize ser, NetworkStream networkStream) { Player temp = new Player(); temp = (Player)ser.deserialize(networkStream); Console.WriteLine(temp.flag.ToString() + temp.CharPos.ToString()); networkStream.Flush(); } } </code></pre> <p>What now happens:</p> <p>When the <code>DeserializeTest</code> is launched, and protobuf tries to deserialize the data from the <code>networkStream</code> the whole function kind of "Freezes" and starts to loop. When I'm <strong>only</strong> serializing the data (in the client) and send it to the server (this code) where its deserialized, everything works like a charm.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload