Forum Multiplayer Sending Array with message

Discussion and help relating to the PlayerIO Multiplayer API.

Sending Array with message

Postby GBurg » March 2nd, 2011, 6:00 pm

Hi everyone,

I want to send an 3 arrays from the server to the client, each consisting of 6 elements. 1 array consists of booleans, the other of ints and the 3rd of Strings. I want to use as little bandwidth as possible, so how to do this? I could Implode the arrays to strings, with a comma as separater, or I could add a counter at the start, and then add the different array elements, or maybe a third method :). What do you think is best?

regards
GBurg
Paid Member
 
Posts: 78
Joined: February 9th, 2011, 10:27 am

Re: Sending Array with message

Postby SelfPossessed » March 3rd, 2011, 5:32 am

I can't say for certain since I'm a newbie, but couldn't you just use PlayerIO's built in message class? I would imagine that PlayerIO's built in message class is already efficient with bandwidth.

To clarify…


Server Pseudocode:

Code: Select all
//create message
Message m = Message.Create("data");

//add booleans
m.add(boolean1);
m.add(boolean2);

m.add(boolean6);

//add integers
m.add(integer1);

m.add(integer6);

//add strings
m.add(string1);

m.add(string6);

Client Pseudocode:

Code: Select all
//parse message booleans
m.getBoolean(0);
m.getBoolean(1);

m.getBoolean(5);

//parse message integers
m.getInt(6);
m.getInt(7);

m.getInt(11);

//parse message strings
m.getString(12);

m.getString(17);
SelfPossessed
 
Posts: 33
Joined: June 29th, 2010, 4:22 am

Re: Sending Array with message

Postby phil.peron » March 3rd, 2011, 5:08 pm

Player.IO does a great job at serializing messages. You don't have to worry about it too much. As far as doing this responsibly, I suggest the following...

When a message contains one or more complex data structures, simply use a bitfield followed by the message content. In your situation you would flag the existence of three arrays in the bitfield and precede each one with it's length. Something like:
Code: Select all
bitField.Flag(HasBooleans)
bitField.Flag(HasInts)
bitField.Flag(HasStrings)

message.Add(bitField.Bits)
message.Add(boolCollection.Count)
// iterate over bool collection and add value...
message.Add(intCollection.Count)
// iterate over int collection and add value...
// etc.


Now it's dead simple on the client side to inspect the bitfield and know exactly what you have and how to deserialize it.

Good luck.
phil.peron
 
Posts: 35
Joined: September 24th, 2010, 7:50 pm

Re: Sending Array with message

Postby vinceas3 » March 15th, 2011, 10:17 pm

I would love to use this method of serializing and sending arrays, but I don't see "bitField" anywhere. What namespace does it use? I can't find it in the playerIO docs.
vinceas3
 
Posts: 77
Joined: January 16th, 2011, 11:25 pm

Re: Sending Array with message

Postby phil.peron » March 16th, 2011, 1:19 am

A bitfield is a common way to set flags for a variety of uses.

You can read more about them here: http://en.wikipedia.org/wiki/Bitfield
phil.peron
 
Posts: 35
Joined: September 24th, 2010, 7:50 pm

Re: Sending Array with message

Postby vinceas3 » March 16th, 2011, 1:34 am

"C# does not support bit fields."

http://msdn.microsoft.com/en-us/library/yyaad03b(v=vs.90).aspx


Too bad, that would have been great for me to send from the C# based, PlayerIO server :) If you have any ideas for a C# environment, please let us know.

Thanks!
vinceas3
 
Posts: 77
Joined: January 16th, 2011, 11:25 pm

Re: Sending Array with message

Postby phil.peron » March 16th, 2011, 1:51 am

vinceas3 wrote:"C# does not support bit fields."

http://msdn.microsoft.com/en-us/library/yyaad03b(v=vs.90).aspx


Too bad, that would have been great for me to send from the C# based, PlayerIO server :) If you have any ideas for a C# environment, please let us know.

Thanks!


Correct. C# doesn't support C++ bitfields. This doesn't mean you can't create your own. I did. And it works perfectly.

If you understand what a bitfield is, you'll find nothing is preventing you from using it in the C# based, PlayerIO server. :)

You're welcome.
phil.peron
 
Posts: 35
Joined: September 24th, 2010, 7:50 pm

Re: Sending Array with message

Postby vinceas3 » March 16th, 2011, 2:11 am

I see. Guess I gotta do more research. Thanks!
vinceas3
 
Posts: 77
Joined: January 16th, 2011, 11:25 pm


Return to Multiplayer