How to registers new types/classes for de/serialization

Options
xzlxt720
xzlxt720 ✭✭
edited February 2012 in Photon Server
public class MailInfo
{
     public int Id {get;set;}
     public string Sender {get;set;}
     public string Receiver {get;set;}
     public DataTime SendTime {get;set;}
     public bool IsRead {get;set;}
}
Now, I have a list of MailInfo want to send to server from client, and want to get a list of MailInfo from server.

So, in client side, how to register the custom type for de/serialization
?And how to do it in server side?

Could anyone give me some example code?

Comments

  • Tobias
    Options
    Which client(s) are you going to use? Not all support this feature yet.

    Some platforms don't support custom types: Flash and JS.
    Define methods to de/serialize your types. Example: Vector3:
        private static byte[] SerializeVector3(object customobject)
        {
            Vector3 vo = (Vector3)customobject;
            MemoryStream ms = new MemoryStream(3 * 4);
    
            ms.Write(BitConverter.GetBytes(vo.x), 0, 4);
            ms.Write(BitConverter.GetBytes(vo.y), 0, 4);
            ms.Write(BitConverter.GetBytes(vo.z), 0, 4);
            return ms.ToArray();
        }
    
        private static object DeserializeVector3(byte[] bytes)
        {
            Vector3 vo = new Vector3();
            vo.x = BitConverter.ToSingle(bytes, 0);
            vo.y = BitConverter.ToSingle(bytes, 4);
            vo.z = BitConverter.ToSingle(bytes, 8);
    
            return vo;
        }
    
    

    Then register this new type:
    PhotonPeer.RegisterType(typeof(Vector3), (byte)'V', SerializeVector3, DeserializeVector3);
    

    The server sample will follow in a post by my colleage.
  • BenStahl
    Options
    At the server side you can override the setup method of ApplicationBase to register youre own custom types.

    public class LiteApplication : ApplicationBase
    {
      ....
       protected override void Setup()
       {
          base.Setup();
          Protocol.TryRegisterCustomType(typeof(Vector3), (byte)'V', Vector3.SerializeVector3, Vector3.DeserializeVector3);
        }
    

    public class Vector3
    {
       public float x;
       public float y;
       public float z;
    
        public static byte[] SerializeVector3(object customobject)
        {
                Vector3 vo = (Vector3)customobject;
                MemoryStream ms = new MemoryStream(3 * 4);
    
                ms.Write(BitConverter.GetBytes(vo.x), 0, 4);
                ms.Write(BitConverter.GetBytes(vo.y), 0, 4);
                ms.Write(BitConverter.GetBytes(vo.z), 0, 4);
                return ms.ToArray();
        }
    
        public static object DeserializeVector3(byte[] bytes)
        {
                Vector3 vo = new Vector3();
                vo.x = BitConverter.ToSingle(bytes, 0);
                vo.y = BitConverter.ToSingle(bytes, 4);
                vo.z = BitConverter.ToSingle(bytes, 8);
    
                return vo;
        }
    }