Writing data to mysql

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Writing data to mysql

Postby appels » 12 Jan 2011, 03:04

Hi,

I'm sending arrays to SFS to store in mysql.
I noticed when i send large amounts of data, SFS stops responding.
Is there anything special i need to consider ?
When i send 10 objects, all seems to work ok but it goes wrong on larger amounts.
I'm sending transforms of gameobjects with some other vars to store and retrieve later.
I store all properties of my object sepperately in mysql.
Thanks :)
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 12 Jan 2011, 06:28

Yeah - there is definitely something to consider.

(I'm in the exact same situation right now, and adding some failsafe mechanisms to the API for the next release)

Right now the API writes something like "wrong sized array or negative size array -1231" and then disconnects. Which is bad. I would rather personally that it throws away the incoming data (with a proper warning in the logfiles) and then simply continues.

A SFSArray can "only" contain 32k objects. A string can only be 32k bytes long.

Sending things as binary data and send via ByteArray. That bastard according to the SFS guys can handle 2 gb of content theoretically.

/Thomas
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 12 Jan 2011, 06:36

Ok, time to change tactics then, i'll look into the bytearrays.
Thanks Thomas.
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 12 Jan 2011, 14:16

Are there any docs available on this, i can't find anything related in the SFS2x docs.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 12 Jan 2011, 14:22

I can ship you some code as an example. See below. xmlDoc is a XmlDocument BTW

Code: Select all

      // Convert to string
      StringWriter sw = new StringWriter();
      XmlTextWriter xw = new XmlTextWriter(sw);
      xmlDoc.WriteTo(xw);
            
      // Lets put level xml into sfsobject
      requestObject = new SFSObject();
      requestObject.PutUtfString("sceneName", sceneName);

      // SFS2X can only handle <32kb strings - lets send as byte array instead
      System.Text.UTF8Encoding  encoding = new System.Text.UTF8Encoding();
      byte[] levelXmlBytes = encoding.GetBytes(sw.ToString());
      requestObject.PutByteArray("levelXml", new Sfs2X.Util.ByteArray(levelXmlBytes));

      byte[] sceneXmlBytes = encoding.GetBytes(sceneXmlFile.text);
      requestObject.PutByteArray("sceneXml", new Sfs2X.Util.ByteArray(sceneXmlBytes));
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 12 Jan 2011, 17:31

Thanks Thomas, i'll give this a spin later tonight. I suppose i just reverse this using decoding serverside ?
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 13 Jan 2011, 06:38

Corresponding server side (slightly cut'n'pasted from different places, as its not a single block):

Basically I take the byte array and assemlble it back to XML - and write the xml file to the harddrive

Code: Select all

String levelXml = new String(params.getByteArray("levelXml"));
Document doc = new SAXBuilder().build(new StringReader(levelXml));

// Write file to folder
      try {
         File file = new File(BASEPATH + sceneName + "/" + filename);
         BufferedWriter writer = new BufferedWriter(new FileWriter(file));
         XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
         outputter.output(doc, writer);

         Log.info(LogSystem.SERVERSYSTEM, "Wrote new xml file to " + file.getAbsolutePath());
      }
      catch (IOException e) {
         e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
      }
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 14 Jan 2011, 06:36

ok, i made some progress :)
I created a byte array from a serialized class and sent it to SFS.
I'm receiving it fine.
I also created the class on SFS.
I can reach the byte array server side but i'm not sure how to deserialize them to the class so i can write to mysql.

2 questions about this approach :
- is this a valid approach
- how can i deserialize ?

Any hints ?
Thanks.
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 15 Jan 2011, 02:55

ah never mind, all sorted now.
mysql was choking.
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 03 Mar 2011, 12:47

byte[] ba = (byte[]) dt.GetByteArray("levelXmlBytes");

does anyone know how i can convert my received sfs byte array back to the original xml ?
tchen
Posts: 191
Joined: 11 Dec 2010, 14:14

Postby tchen » 07 Mar 2011, 21:18

appels wrote:byte[] ba = (byte[]) dt.GetByteArray("levelXmlBytes");

does anyone know how i can convert my received sfs byte array back to the original xml ?


It should be one of the String constructors. (Java)

or

System.Text.Encoding.UTF8.GetString (C#)

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 46 guests