Simple Class Serialization

Options
ddks
edited July 2012 in Photon Server
Hi,

I was reading the forum posts on serialization. If I define a class like:

public MyClass
{
public int positionX {get;set;}
public int positionY {get;set;}
public int positionZ {get;set;}
}

Can I send this class back-n-forth across Photon? E.g. will it handle the serialization for me?

And if not, is the recommended approach to just stick with defining a parameterCodes e.g. positionX, positionY, positionZ and store the int values and send these inside the event/request/response parameters?

Thanks,
Dan

Comments

  • [Deleted User]
    Options
    Hi ddks,

    In the client you will have to register the types you want to be (de-)serialized, which is pretty straight forward:

    Client-code:

    // Register your type for serialization
    if (PhotonPeer.RegisterType(typeof(<YourTypeToBeRegistered>), (byte)'<a Byte code>', Serialize, Deserialize))
    {
    throw new Exception("not working...");
    }

    // Additionally you will have to write your De- serialization methods
    public static byte[] Serialize(object customobject)
    {
    // your serialization code here
    }

    public static object Deserialize(byte[] bytes)
    {
    // your deserialization code here
    }

    For the Server side a colleague will give you additional info here.


    thx.


    Tim
  • ddks
    Options
    Thanks that helps.

    dan
  • mcodes
    Options
    Can you also post what to do on the server here? Is it the same thing but just on the server side?

    Can you also give an example of the Serialize and Deserialize methods for the class above?

    Thanks
  • Tobias
    Options
    mcodes: We are talking about the server.
    The serialize/deserialize methods just require you to convert your class/Type into whatever byte-array that might represent it.

    A very simple example:
        public class MyType
        {
            public byte a;
            public byte b;
    
            public static byte&#91;&#93; Serialize(object arg)
            {
                MyType m = arg as MyType;
                if (m == null)
                {
                    return new byte&#91;&#93; { (byte)0 };
                }
    
                byte&#91;&#93; result = new byte&#91;2&#93;;
                result&#91;0&#93; = m.a;
                result&#91;1&#93; = m.b;
                return result;
            }
    
            public static object Deserialize(byte&#91;&#93; arg)
            {
                if (arg.Length &lt;= 1)
                {
                    return null;
                }
    
                return new MyType(arg&#91;0&#93;, arg&#91;1&#93;);
            }
        }
    
  • Nadegem
    Options
    Hi,

    I'm using serialization like this in my project and it worked fine. But rencently I added some fields in one of the Class I serialize and the objects are so big now that the byte-array resulting from their serialization has a length superior to 65532 (around 70000).
    I get no exception server side, but the client try to deserialize a byte array of length "lengthOfMyByteArray - 65532" (so, around 5000), which obsiously can't work.

    The documentation states that the length limit for byte-array and int-array is int.MaxValue ( and short.MaxValue for Array of <type> ).
    Is there a bug in photon or is the documentation wrong ? (Or did I misunderstood something ?)
  • Tobias
    Options
    That will be a bug, yes.
    I'm not sure which side it's on yet. Maybe the server is not writing the byte-array as byte-array but instead of array-of-byte (which is a small difference only but results in length having a max of short.MaxValue).

    Aside from that:
    Photon is built primarily to send a lot of small data chunks very fast. It's not good for downloading such amounts of bytes. It requires ALL the data in memory until it will process it and provides NO hint of the progress of downloads. You might want to use TCP for streaming something like this in parallel and use Photon for fast communication of smaller data chunks.
  • Nadegem
    Options
    Thanks for the quick reply.
    I think I will split the big data in smaller parts.