byte[] to ByteArray

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

Moderators: Lapo, Bax

TomPo
Posts: 10
Joined: 08 Aug 2015, 21:57

byte[] to ByteArray

Postby TomPo » 15 May 2018, 16:54

Hello
Thought it will be simple but no.... :)

c#

Code: Select all

    public static byte[] CompressToBytes (string text) {
        byte[] raw = Encoding.UTF8.GetBytes(text);
        using (MemoryStream memory = new MemoryStream()) {
            using (GZipStream gzip = new GZipStream(memory, CompressionMode.Compress, true)) {
                gzip.Write(raw, 0, raw.Length);
            }
            return memory.ToArray();
        }
    }
   
...   
ob.PutByteArray("D", CompressToBytes(SomeLongString));
...


But it says that can't convert byte[] to ByteArray :?:
What kind of ByteArray sfs expects in that case ?
TomPo
Posts: 10
Joined: 08 Aug 2015, 21:57

Re: byte[] to ByteArray

Postby TomPo » 15 May 2018, 17:15

Code: Select all

ob.PutByteArray("D", new Sfs2X.Util.ByteArray(CompressToBytes(SomeLongText)));


did the job but is this a proper way ?
Question is: how to reverse it to c# byte[] ?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: byte[] to ByteArray

Postby Lapo » 16 May 2018, 08:03

Yes, that's correct.
One thing... I don't think it's necessary to compress the byte data yourself. The SFS2X protocol already does that for you and you would waste performance by compressing/decompressing twice.

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
v.potapov
Posts: 21
Joined: 12 Feb 2020, 06:29

Re: byte[] to ByteArray

Postby v.potapov » 09 Jun 2023, 12:09

Hi @Lapo,
Is there any size limitations for the byte[ ] ?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: byte[] to ByteArray

Postby Lapo » 09 Jun 2023, 12:52

Yes, 2^31 bytes, which is ~ 2GB
I would highly recommend not to go that high, though :wink: SmartFoxServer is not a file transfer server.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
v.potapov
Posts: 21
Joined: 12 Feb 2020, 06:29

Re: byte[] to ByteArray

Postby v.potapov » 09 Jun 2023, 13:14

Thanks a lot for response.

I'm asking because currently I'm trying to implement logic, that sends texture via Smartfox. Texture have size from ~500kb to ~4mb. It works well with small size textures, but when I send big ones I see that on receiving client Sfs2X.Entities.User doesn't have User variable, which I've putted before.
So my first guess was I'm off size limit. Apperantly that's not the case.

Can you please tell if there are cases when Sfs2X.Entities.User returns with missing variables?

Thanks,
Vas
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: byte[] to ByteArray

Postby Lapo » 09 Jun 2023, 14:05

Hi,
I am not sure what you mean. Are you sending textures via UserVariables?
Can you show me a code example, perhaps?

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
v.potapov
Posts: 21
Joined: 12 Feb 2020, 06:29

Re: byte[] to ByteArray

Postby v.potapov » 14 Jun 2023, 07:53

Hi,
Sorry for late response.
Sure, basically packing snippet looks like this

Code: Select all

public UserVariable ToSfsVariables()
{
   var sfsTextureArr = new Sfs2X.Util.ByteArray(TextureBin);
   sfsTextureArr.Compress();
   
   var userSettingsSFSObject = new SFSObject();
   userSettingsSFSObject.PutByteArray(PARAM_BODY_TEXTURE, sfsTextureArr);

   var settingsVar = new SFSUserVariable(PLAYER_SETTINGS_KEY, userSettingsSFSObject, (int)VariableType.OBJECT);
   settingsVar.IsPrivate = false;

   return settingsVar;
}


Then goes sending via Sfs.Send()

Parsing snippet in general look like this

Code: Select all

public void SetFromUser(Sfs2X.Entities.User user)
{
   var userVariable = user.GetVariable(PLAYER_SETTINGS_KEY);
   if (userVariable == null)
   {
      Log("Variable PLAYER_SETTINGS_KEY not found!");
      return;
   }

   var sfsArr = (SFSObject)userVariable.Value.GetByteArray(PARAM_BODY_TEXTURE);
   sfsArr.Uncompress();
   TextureBin = sfsArr.Bytes;
}


And everytime I try send Texture with size ~1mb it always logs "Variable PLAYER_SETTINGS_KEY not found!". Somehow Sfs2X.Entities.User lost its data or I don't know

Kind Regards
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: byte[] to ByteArray

Postby Lapo » 14 Jun 2023, 09:17

Thanks.
As a side note, calling Compress()/Uncompress() on the ByteArray object is redundant because compression is already performed on the whole packet before sending if it exceeds the configured size (1024 bytes by default).

I don't see how this could cause the problem you're reporting but it will certainly add latency due to the extra work.

The flow of communication is not entirely clear: you already have a number of User variables set and at a certain point the server sends and update containing the texture(s) and that's when a specific UserVar breaks? Correct?
What about the others though? (If there are other vars)

Is there a specific texture that causes the problem? Or anything will do?
Can you also double check with the AdminTool (Zone Monitor, select Zone, then Room, then User and finall check its variables) to see that the variable disappears after the "texture update"?

Finally: what SFS2X version and C# API version are you using?

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 28 guests