Serializing Dictionary

Options
stucko
edited November 2013 in Photon Server
Hi,

Im currently in the process of finding a way to send data between server-client,

In the past , ive used hashtables to pass data between server-client but I find that it is difficult to manage and after reading about custom datatype serialization, decided to try it since it sems like it will make the codebase easier to read/understand/manage.

so I went ahead to try the
"Photon.SocketServer.Protocol.TryRegisterCustomType" and "Protocol.GpBinaryV162.Serialize" methods.
it works nicely, ive even tried to have custom objects in a custom object. That too works nicely.

However now I face a problem. Im trying to serialize an object which has a property that is a dictionary that contains another custom object as it's value. and the result, I encountered this error :
2013-11-13 12:12:44,489 [9] ERROR Lite.Room [(null)] - System.IO.InvalidDataException: Unexpected - cannot serialize Dictionary with value type: GameTitle.OverlordModule.OverlordSimpleChild
   at Photon.SocketServer.Rpc.Protocols.GpBinaryByte.GpBinaryByteWriter.WriteDictionary(IBinaryWriter writer, Object dict) in c:\svncontent\photon-socketserver-sdk_3.2\src\Photon.SocketServer\Rpc\Protocols\GpBinaryByte\GpBinaryByteWriter.cs:line 577
   at Photon.SocketServer.Rpc.Protocols.GpBinaryByte.GpBinaryByteWriter.Write(IBinaryWriter writer, Object value, Boolean setType) in c:\svncontent\photon-socketserver-sdk_3.2\src\Photon.SocketServer\Rpc\Protocols\GpBinaryByte\GpBinaryByteWriter.cs:line 364
   at Photon.SocketServer.Rpc.Protocols.GpBinaryByte.GpBinaryByteProtocolV16.Serialize(Stream stream, Object obj) in c:\svncontent\photon-socketserver-sdk_3.2\src\Photon.SocketServer\Rpc\Protocols\GpBinaryByte\GpBinaryByteProtocolV16.cs:line 120
   at GameTitle.OverlordModule.OverlordSimple.SerializeOverlordSimple(Object customObject_) in C:\Documents and Settings\Administrator\Desktop\00 Company\01 GameTitle\02 Source\01 ServerSide\PhotonServer\src-server\01 GameServer 0.2\GameTitle\OverlordModule\Overlord.cs:line 226
   at Photon.SocketServer.Rpc.Protocols.GpBinaryByte.GpBinaryByteWriter.WriteCustomType(IBinaryWriter writer, CustomTypeInfo customTypeInfo, Object value) in c:\svncontent\photon-socketserver-sdk_3.2\src\Photon.SocketServer\Rpc\Protocols\GpBinaryByte\GpBinaryByteWriter.cs:line 472
   at Photon.SocketServer.Rpc.Protocols.GpBinaryByte.GpBinaryByteWriter.Write(IBinaryWriter writer, Object value, Boolean setType) in c:\svncontent\photon-socketserver-sdk_3.2\src\Photon.SocketServer\Rpc\Protocols\GpBinaryByte\GpBinaryByteWriter.cs:line 245
   at Photon.SocketServer.Rpc.Protocols.GpBinaryByte.GpBinaryByteWriter.WriteHashTable(IBinaryWriter writer, Hashtable serObject) in c:\svncontent\photon-socketserver-sdk_3.2\src\Photon.SocketServer\Rpc\Protocols\GpBinaryByte\GpBinaryByteWriter.cs:line 719
   at Photon.SocketServer.Rpc.Protocols.GpBinaryByte.GpBinaryByteWriter.Write(IBinaryWriter writer, Object value, Boolean setType) in c:\svncontent\photon-socketserver-sdk_3.2\src\Photon.SocketServer\Rpc\Protocols\GpBinaryByte\GpBinaryByteWriter.cs:line 335
   at Photon.SocketServer.Rpc.Protocols.GpBinaryByte.GpBinaryByteWriter.WriteEventData(IBinaryWriter binaryWriter, IEventData eventData) in c:\svncontent\photon-socketserver-sdk_3.2\src\Photon.SocketServer\Rpc\Protocols\GpBinaryByte\GpBinaryByteWriter.cs:line 72
   at Photon.SocketServer.Rpc.Protocols.GpBinaryByte.GpBinaryByteProtocolV16.SerializeEventData(EventData eventData) in c:\svncontent\photon-socketserver-sdk_3.2\src\Photon.SocketServer\Rpc\Protocols\GpBinaryByte\GpBinaryByteProtocolV16.cs:line 144
   at Photon.SocketServer.EventData.Serialize(IRpcProtocol protocol) in c:\svncontent\photon-socketserver-sdk_3.2\src\Photon.SocketServer\EventData.cs:line 203
   at Photon.SocketServer.PeerBase.SendEvent(IEventData eventData, SendParameters sendParameters) in c:\svncontent\photon-socketserver-sdk_3.2\src\Photon.SocketServer\PeerBase.cs:line 516
   at Lite.Room.PublishEvent(LiteEventBase e, Overlord actor, SendParameters sendParameters) in C:\Documents and Settings\Administrator\Desktop\00 Company\01 GameTitle\02 Source\01 ServerSide\PhotonServer\src-server\01 GameServer 0.2\GameTitle\LiteLobby\Room.cs:line 286
   at GameTitle.DungeonLobby.EvaluateDungeonCommand(Byte command, Object commandValue, Overlord overlord) in C:\Documents and Settings\Administrator\Desktop\00 Company\01 GameTitle\02 Source\01 ServerSide\PhotonServer\src-server\01 GameServer 0.2\GameTitle\Game\DungeonLobby.cs:line 163
   at GameTitle.DungeonLobby.HandleRaiseEventOperation(LitePeer peer, RaiseEventRequest raiseEventRequest, SendParameters sendParameters) in C:\Documents and Settings\Administrator\Desktop\00 Company\01 Vyrium\02 Source\01 ServerSide\PhotonServer\src-server\01 GameServer 0.2\GameTitle\Game\DungeonLobby.cs:line 129
   at Lite.LiteGame.ExecuteOperation(LitePeer peer, OperationRequest operationRequest, SendParameters sendParameters) in C:\Documents and Settings\Administrator\Desktop\00 Company\01 GameTitle\02 Source\01 ServerSide\PhotonServer\src-server\01 GameServer 0.2\GameTitle\Lite\LiteGame.cs:line 207

can photon do this out of the box? am I missing a step? or is this not possible out of the box ? if it is not what is the recommended way to do this ?

thank you in advance :)

Comments

  • Sorry to not exactly answer your question- I dont know how to use that particular photon feature. However I highly recommend protobuf-net. There are other lightweight/fast serialization libraries too, but this is my favorite. Just serialize to a byte[] and send/receive that with Photon. Looks like this in my code

    [code2=csharp]public static GameStateModel DeserializeGame( byte[] buffer )
    {
    using( var stream = new MemoryStream( buffer ))
    {
    return Serializer.Deserialize<GameStateModel>(stream);
    }
    }
    public static byte[] SerializeGame( GameStateModel state)
    {
    using (var stream = new MemoryStream ())
    {
    Serializer.Serialize<GameStateModel>( stream, state );
    return stream.ToArray();
    }
    }[/code2]

    https://code.google.com/p/protobuf-net/
  • Kaiserludi
    Options
    Hi stucko.

    This is a bug in the current Photon Server release.

    It will get fixed with the next release.

    Until then as a workaround it works fine with the current release if you set the value type of the Dictionary to "Object".
  • Hi mindlube, thanks for the recommendation, will definitely check that out after our initial code release.

    Hi Kasierludi,

    Thanks for conforming that its a bug, I used the workaround for now, it worked fine on test objects.

    I noticed that list are not in the release notes
    4. Extended Data Type Support Photon now supports more data-types than ever: Null values, dictionaries, arrays of objects (each with its own type) and even custom classes can be integrated and used in events.

    I tried to serialize a list and during serialization, there was no errors, but during deserialization I got an error. I tried this last week, so dont remember the exact error, let me know if Lists suppose to work.
    At the moment im boxing and unboxing lists to array before serializing/deserializing.
  • Kaiserludi
    Options
    Hi stucko.

    Lists are not supported as a built-in type. You would have to either add them as a custom type yourself or, like you currently do, convert them to arrays.

    Supported types are:
    byte
    short
    int
    long
    float
    double
    bool
    string
    Hashtable
    Dictionary
    arrays of all of the types listed above (max size is SHRT_MAX for all except byte arrays, which are supported up to a size of INT_MAX), including jagged arrays
    Object arrays
    CustomType