PutLong / PutShort Appears largely meaningless sent from WebGL

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

Moderators: Lapo, Bax

andrew2110
Posts: 90
Joined: 28 May 2008, 13:36

PutLong / PutShort Appears largely meaningless sent from WebGL

Postby andrew2110 » 25 Mar 2017, 00:09

As the title suggests really, I've started to notice an error when a WebGL client is sending others numbers of different sizes inside of an SFSObject.

For example Horse ID is stored as a Long, however when sent from a WebGL client it can only be read as an int otherwise will cause a classcastexception

It's the same when I send a Short, it will only allow me to read it if I read it as an int. To keep data transfer to a minimum it is very useful for me to be able to use Short variable types, and as Horse ID's are likely to exceed 2.14billion, I need to use the Long data type. What's going on here?
andrew2110
Posts: 90
Joined: 28 May 2008, 13:36

Re: PutLong / PutShort Appears largely meaningless sent from WebGL

Postby andrew2110 » 25 Mar 2017, 08:00

Just an update to this, it appears that so long as the SFSObject is not created by the WebGL user it works ok and WebGL can read Long and Short values from an SFSObject, they just can't write them without them then all having to be read as int values.
User avatar
Lapo
Site Admin
Posts: 23007
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: PutLong / PutShort Appears largely meaningless sent from WebGL

Postby Lapo » 25 Mar 2017, 09:03

Hi,
are you using the latest API available (1.7.2)?
If not please update from here:
http://www.smartfoxserver.com/download/sfs2x#p=client

thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
andrew2110
Posts: 90
Joined: 28 May 2008, 13:36

Re: PutLong / PutShort Appears largely meaningless sent from WebGL

Postby andrew2110 » 25 Mar 2017, 09:51

This was my first thought too and went and updated straight away and am getting the same result. If this was a previously known issue and is patched I will double check it all when home and test it again
User avatar
Lapo
Site Admin
Posts: 23007
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: PutLong / PutShort Appears largely meaningless sent from WebGL

Postby Lapo » 25 Mar 2017, 12:16

Yes please, check again with the latest API and let us know.
Lapo

--

gotoAndPlay()

...addicted to flash games
andrew2110
Posts: 90
Joined: 28 May 2008, 13:36

Re: PutLong / PutShort Appears largely meaningless sent from WebGL

Postby andrew2110 » 25 Mar 2017, 12:55

It's definitely running 1.7.2 at the moment (added a command to the games chat to output the current version of the SFS Client and it reports 1.7.2)

I've noticed that if it's only WebGL users running together, it runs fine. The problem comes when a WebGL client is sending information to a non-webgl user (iOS, Android, PC, Mac)

An example of code thats causing the offending behaviour. This is called when the host wants to tell all clients the AI Updates that will come into effect in 30 frames time. Again, this works fine when it's two WebGL clients talking to one another, but does not work when a WebGL client is sending this to a non-WebGL client.

Code: Select all

            SFSObject obj = new SFSObject ();
            obj.PutSFSArray (MiscFieldNames.HORSES, horseUpdates);
            obj.PutShort ("f", Convert.ToInt16 ((short)this.framesPassed + (short)30));
            base.sendHorseUpdatePackage (ServerRequests.RACE_HANDLE_MESSAGE_BOUNCE_INFO, obj);


Looking at the release notes on the patches on the server side, seems relevant to mention that I'm using 2.12.3
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: PutLong / PutShort Appears largely meaningless sent from WebGL

Postby Bax » 27 Mar 2017, 09:04

The issue you are experiencing is due to the fact that WebGL (and also our native JavaScript API) uses a text protocol based on JSON, while all other clients use a binary protocol. This will change in the future (all clients will use the binary protocol), but at the moment you have to deal with it.

The issue with the JSON protocol is that there's no way to differentiate numbers (because in JavaScript you just have Number, not Byte, Short, Int, etc), so all those up to 32 bits are transported as integers. The API makes auto-conversions when you do obj.getShort (both on the client and the server when it receives an object from a WebGL client), but at the core of the SFSObject the number is still represented as an Int.

Everything works fine until your Extension forwards an object received from the WebGL client to other clients directly. In fact, the other clients will decode the object as it is, so containing an integer and not a short.
The only workaround to this issue is to rebuild the object like this before forwarding it to the other clients (whether they are WebGL or not):

Code: Select all

ISFSObject outParams = new SFSObject();
outParams.putShort("f", params.getShort("f"));
send(cmd, outParams, recipients);
Paolo Bax
The SmartFoxServer Team
andrew2110
Posts: 90
Joined: 28 May 2008, 13:36

Re: PutLong / PutShort Appears largely meaningless sent from WebGL

Postby andrew2110 » 27 Mar 2017, 11:05

Have got this updated now and seems to work ok so far! Thank you for your help!
andrew2110
Posts: 90
Joined: 28 May 2008, 13:36

Re: PutLong / PutShort Appears largely meaningless sent from WebGL

Postby andrew2110 » 28 Mar 2017, 09:16

Is there a way to make this apply do SFSObjects that go with PrivateMessageRequest?

This is my attempt so far which doesn't seem to work:

Code: Select all

public class HandlePrivateMessage extends BaseServerEventHandler {

   @Override
   public void handleServerEvent(ISFSEvent aEvent) throws SFSException {
      // TODO Auto-generated method stub

      SFSObject customMessage = (SFSObject) aEvent.getParameter(SFSEventParam.OBJECT);
      trace("Handling private trade message");
      if(customMessage.containsKey(MiscFieldNames.PRIVATE_TRADE_MY_HORSE)) {
         trace("Checking to see if I have a horse");
         SFSObject obj = (SFSObject) customMessage.getSFSObject(MiscFieldNames.PRIVATE_TRADE_MY_HORSE);
         trace("Checking if I have ID");
         if(obj.containsKey(HorseFields.FIELD_ID)) {
            trace("I have ID");
            obj.putLong(HorseFields.FIELD_ID, obj.getLong(HorseFields.FIELD_ID));
         } else {
         
            trace("I don't have ID");
         }
         if(obj.containsKey(HorseFields.FIELD_XP)) {
            obj.putLong(HorseFields.FIELD_XP, obj.getLong(HorseFields.FIELD_XP));
         }
         if(obj.containsKey("Mother")) {
            obj.putLong("Mother", obj.getLong("Mother"));
         }
         if(obj.containsKey("Father")) {
            obj.putLong("Father", obj.getLong("Father"));
         }
         
         customMessage.putSFSObject(MiscFieldNames.PRIVATE_TRADE_MY_HORSE, obj);
      }
   }

}


Looking at the server logs, I can see that this code is being ran when users send PrivateMessageRequests to one another Image


However as soon as we hit this bit of code:

Code: Select all

               
               SFSObject obj = (SFSObject)aPrivateTradeObject.GetSFSObject (MiscFieldNames.PRIVATE_TRADE_MY_HORSE);
               try {
                  if (obj != null && obj.GetLong(HorseFields.FIELD_ID)>0) {
               


It breaks complaining about it not like the Long variable type.
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: PutLong / PutShort Appears largely meaningless sent from WebGL

Postby Bax » 28 Mar 2017, 12:39

andrew2110 wrote:However as soon as we hit this bit of code:

Code: Select all

               
               SFSObject obj = (SFSObject)aPrivateTradeObject.GetSFSObject (MiscFieldNames.PRIVATE_TRADE_MY_HORSE);
               try {
                  if (obj != null && obj.GetLong(HorseFields.FIELD_ID)>0) {
               


It breaks complaining about it not like the Long variable type.


Can you please show the the dump of that object? -- obj.GetDump()
Paolo Bax
The SmartFoxServer Team
andrew2110
Posts: 90
Joined: 28 May 2008, 13:36

Re: PutLong / PutShort Appears largely meaningless sent from WebGL

Postby andrew2110 » 28 Mar 2017, 13:23

Image

No problem, I've included it on both the server side to show on the server side at least it thinks everything that needs to be of type "long" is of type "long", but when it gets back to the client side (this is going WebGL -> Server -> Windows Client), it's as if the packet is untouched by the server. I guess the next thing I could test is to change a variable in the SFSObject and see if changing that variable on the server side is possible at all?
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: PutLong / PutShort Appears largely meaningless sent from WebGL

Postby Bax » 29 Mar 2017, 07:03

There's a chance you can't modify the custom data object in messages like that.
We suggest to go a different way: instead of sending a private message directly, call the Extension, change data the way you need and send that private message from the Extension.
Paolo Bax
The SmartFoxServer Team
andrew2110
Posts: 90
Joined: 28 May 2008, 13:36

Re: PutLong / PutShort Appears largely meaningless sent from WebGL

Postby andrew2110 » 29 Mar 2017, 11:54

Thank you for getting back to me, the PrivateMessageRequest stuff is just so ideal for this scenario I've just gone and made it so that the data can be converted to bytes, then a string, then converted back the other way on the other side without having to worry about it.

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 24 guests