Unexpected Data Received - Photon Server and XmlSerializer

Options
bradleyfshepherd
edited July 2014 in Photon Server
Hello,

I am working on a game in Unity using the Photon Server. I have been following along with CJR Gaming's excellent tutorials and have now been adding onto their framework to suit my own game. My problem occurs with the serialization of an object, a deck of cards to be more specific. See, when I start the game, the player can create a deck of cards and edit the deck of cards by adding and removing cards from it. When the player has finished editing his deck he then presses a "Save" button that sends a request to my Photon server to save the deck into the database. The way I send the deck is serialize it and put it in the parameters of the request. Here is the code I am using to serialize my deck:

[code2=csharp]XmlSerializer serializer = new XmlSerializer(typeof(List<CardListItem>));
StringWriter writer = new StringWriter();
serializer.Serialize(writer, this.DeckList);

Dictionary<byte, object> parameters = new Dictionary<byte, object>()
{
{(byte)ClientParameterCode.SubOperationCode, MessageSubCode.SaveDeck},
{(byte)ClientParameterCode.DeckId, deckId},
{(byte)ClientParameterCode.DeckList, writer.ToString()}
};

OperationRequest request = new OperationRequest { OperationCode = (byte)ClientOperationCode.Login, Parameters = parameters };[/code2]

If the deck saves successfully, it saves into my database as a (very long) string since it turns all of the objects in the deck into strings beforehand. I did some stress tests with how many cards I could serialize, and, when I serialize 30 of the same card, it serializes and saves correctly. However, when I serialize 31 of the same card, the server gives me an "Unexpected data received" at the PeerBase. I have a feeling that I should be serializing the information a different way than a string but I don't know a good way to go about it. I assume that there is some kind of throttle on either the serializer or message limit or elsewhere. I researched some XmlSerializer limits and it seems unlikely that the serializer is being overloaded. I changed the message limit on my server config to double the size and that didn't help either. I am kind of stuck with this and I don't really know a better way go about fixing it since the error message is quite vague. FYI, "this.DeckList" is a list of "CardListItem"s.


Thanks in advance!

Comments

  • chvetsov
    Options
    Hi, breadley.
    Could you say which length has string when you serialize 31 cards?
    I assume that you faced with limitations of message size
    there is a set of paramters which control how many data transfred, description you may find in photon-configuration.pdf
    here is some of them:
    MaxMessageSize="512000"
    MaxQueuedDataPerPeer="1000"
    PerPeerMaxReliableDataInTransit="51200"
    PerPeerTransmitRateLimitKBSec="256"

    Try to change them and see what happens
  • chvetsov wrote:
    Hi, breadley.
    Could you say which length has string when you serialize 31 cards?

    Are you asking for the length of the string I am serializing?

    chvetsov wrote:
    I assume that you faced with limitations of message size
    there is a set of paramters which control how many data transfred, description you may find in photon-configuration.pdf
    here is some of them:
    MaxMessageSize="512000"
    MaxQueuedDataPerPeer="1000"
    PerPeerMaxReliableDataInTransit="51200"
    PerPeerTransmitRateLimitKBSec="256"

    Try to change them and see what happens

    I multiplied each of those 4 values by 32 and still I am being disconnected for an "Unexpected Data Received" error from the PeerBase. These values were changed in the PhotonServer.config file under my server name.
  • Hi,

    first, I would recommend that you change the settings back to the default - they are working good in most cases and should only be changed for *very* special requirements.

    Ilya was on a good track: I think the string is too long. You are allowed to send strings with a max. size of 32767 bytes - see the details here: http://doc.exitgames.com/en/onpremise/c ... y-protocol

    This is a protocol limitation and can not be changed by any config.

    I think there are two issues:
    1. the client library should raise an error when you try to serialize that data - and not send the corrupt data to the server.
    Which client lib are you using? Is that PUN?

    2. your approach of sending data back and forth is... horrible. Sorry. ;)
    Don't serialize whole objects and "dump" them anywhere (neither in your database, nor to the network between client and server). Especially, don't use XML Serialization, by no means - the overhead is terrible.

    We have implemented a very efficient protocol for sending data to and from Photon. Put your information into arrays and dictionaries, work with codes / ids instead of full data serialization.
    Try to send information like this: cardsInDeck = {1,2, 5, 8} - the server should "know" what these cards are.

    Always send as little data as possible.
  • Nicole wrote:

    1. the client library should raise an error when you try to serialize that data - and not send the corrupt data to the server.
    Which client lib are you using? Is that PUN?
    I am using the self hosted Photon Server.


    Nicole wrote:
    2. your approach of sending data back and forth is... horrible. Sorry. ;)
    Don't serialize whole objects and "dump" them anywhere (neither in your database, nor to the network between client and server). Especially, don't use XML Serialization, by no means - the overhead is terrible.

    We have implemented a very efficient protocol for sending data to and from Photon. Put your information into arrays and dictionaries, work with codes / ids instead of full data serialization.
    Try to send information like this: cardsInDeck = {1,2, 5, 8} - the server should "know" what these cards are.

    Always send as little data as possible.

    Thank you Nicole. I should say that my game is a turn based game and sending more data is preferable to sending data fast. Additionally, if I have thousands of different cards, is it wise to have the server be in charge of holding information about each of the cards? My approach comes from CJRGaming's tutorials which serialized and stored character "Stats" data the same way I am storing my cards.

    Thank you for your response!
  • Nicole wrote:

    1. the client library should raise an error when you try to serialize that data - and not send the corrupt data to the server.
    Which client lib are you using? Is that PUN?
    I am using the self hosted Photon Server.

    The code snippet above (with the XML serialization) was from the client side, right? Or is that a Server-To-Server operation?

    My advice for data sizes applies for all kind of network applications - no matter if turnbased, realtime, websites etc. - the less you send, the better.
  • Nicole wrote:
    Nicole wrote:

    1. the client library should raise an error when you try to serialize that data - and not send the corrupt data to the server.
    Which client lib are you using? Is that PUN?
    I am using the self hosted Photon Server.

    The code snippet above (with the XML serialization) was from the client side, right? Or is that a Server-To-Server operation?

    The code was from the client side. Thank you very much for helping me through this.
  • Ok, thanks. We'll look into a fix / improvement for the client-side as well, so that you will receive a proper warning on the client if the serialization fails.