[6132+] 10005 Walk Packet Protobuff Serialization/Deserialization (the PROPER way)

10/25/2015 01:12 CrystalCastle#1
Requirements
  • Download and include in project [Only registered and activated users can see links. Click Here To Register...]
  • Make sure you strip the 4 bytes at the beginning of the packet (Size and Type) and ensure that you remove the packet footer (8 bytes containing the TQServer/ TQClient) before deserializing your packet. And obviously ensure you add the correct packet headers and footers when sending the serialized packets back to the client


[6132] Walk Packet Class Structure
Code:
    [ProtoContract]
    public class Packet_Movement
    {
        [ProtoMember(1)]
        public uint Direction { get; set; }

        [ProtoMember(2)]
        public uint UID { get; set; }

        [ProtoMember(3)]
        public uint Running { get; set; }

        [ProtoMember(4)]
        public uint TimeStamp { get; set; }

        [ProtoMember(5)]
        public uint MapID { get; set; }
    }

Protobuff .Net Deserialization Example
Code:
Serializer.Deserialize<Packet_Movement>(new MemoryStream(Packet));

Protobuff .Net Serialization Example
This also converts it from a MemoryStream into a Byte array
Code:
Packet_Movement PM_Class = new Packet_Movement();
byte[] Packet_Data;
using (var ms = new MemoryStream())
{
    Serializer.Serialize(ms, PM_Class);
    Packet_Data = ms.ToArray();
}
10/25/2015 03:40 Spirited#2
Damn you! I was going to write this up before leaving this morning and put it on my wiki. I had an example project and everything. You win this round! *Shakes fist in air* Edit: Put up my version on the wiki anyways. Best to have more than one example / implementation, I suppose.
10/25/2015 13:56 CrystalCastle#3
Quote:
Originally Posted by Spirited View Post
Damn you! I was going to write this up before leaving this morning and put it on my wiki. I had an example project and everything. You win this round! *Shakes fist in air* Edit: Put up my version on the wiki anyways. Best to have more than one example / implementation, I suppose.
Haha. Yeah dude, do as you please :)
10/26/2015 10:15 KraHen#4
The new intern at TQ HQ must have had fun implementing this in CO.
11/01/2015 21:48 -impulse-#5
You might want to mark Direction and Running as Required fields in their proto attributes. In case they are 0 the serializer will not write them and the client will not use the packet, because the client defined the proto fields are required most likely (not writing them in the packet would result in failure in deserialization).
01/03/2016 07:18 modyali2#6
it need array.copy
the final step
01/03/2016 17:23 Xio.#7
Quote:
Originally Posted by modyali2 View Post
it need array.copy
the final step
Why?
01/21/2016 07:49 MCJackson#8
CrystalCastle,I really appreciate your help very much, You really are a genius
01/23/2016 20:20 tkblackbelt#9
I thought protobuf also include the proto member index in the encoded data? Wouldn't that cause the client to not be able to read the packet?
01/23/2016 20:36 Spirited#10
Quote:
Originally Posted by tkblackbelt View Post
I thought protobuf also include the proto member index in the encoded data? Wouldn't that cause the client to not be able to read the packet?
The header of the packet, which includes the packet length and identifier, doesn't go through Google Protocol Buffers. If you take a look at [Only registered and activated users can see links. Click Here To Register...] from my wiki website, the first int of data isn't included as a protomember. Each field is also required, so if the direction is zero or movement type is zero (for example), it won't crash the client or cause mishandling on the server.