Multiple Variables with one UDP message

BlitzMax Forums/BlitzMax Beginners Area/Multiple Variables with one UDP message

Say I'm sending data about a spaceship in an asteroids-like game. I want to send from the host to the client the X, Y, Speed, and Direction of the ship. This seems like only a little bit of data for a UDP packet. So, how would I go about sending it all packed together neatly?

I'm having a suspicion that I might need to put all the data into a string, and then have the client convert it back into its proper form. Is there a better method then this?

I have just made myself some thought about this for further time and not tested yet.
What about serializing an object with the needed information
(with persistence.mod) to a string, sending it and then deserializing it?

Don't you think that would cause a lot of extra overhead? If all 4 fields are, for example, 4 integers. That that is all you would have to send. A package with the size of 4 integers that contains these values. Would be a hell of a lot faster than putting it in a string and decode it. Serializing and reversing it later on would give big performance problems I think.

Don't you think that would cause a lot of extra overhead?
I made a little test.
On my system (Core2Duo 2.6 Ghz) it does around 5.000 serializings/deserializings per second.
The string of this small object gets a lenght of 245 bytes (can be packed within in the mod) which have to be sent. Yes, this can be called overhead :-)
But I like the idea to transfer complete objects.
For a game with a limited amount of data and players it could work nice.

SuperStrict
Import BaH.persistence ' the mod

Local t:TTimer = CreateTimer(100)

Local counter:Int
Local tosend:String
Repeat
	counter:+1
	Local pl:TPlayerData = New TPlayerData
	pl.x = 99'  set the values x,y etc.
	pl.y = 98
	pl.speed = 330000
	pl.direction = 1
	
	 tosend = TPersist.Serialize(pl) ' serialize playerdata to a string
	
	' now send string with UDP
	' .....
	' and at the client side deserialize it
	
	Local clientpd:TPlayerData = TPlayerData(TPersist.DeSerialize(tosend))
	PollEvent() ' for the timer
Until t.Ticks() = 100

Print "serializing/deserializing per sec.:" + counter
Print "length of string:" + tosend.Length
Print tosend

Type TPlayerData
	Field x:Int, y:Int
	Field speed:Int
	Field direction:Int
End Type



So, would it actually be more ideal to send one UDP message for each variable? You see, I though that merging data into one message would actually reduce bandwidth usage. Is this true?

Is this true?

Marginally. Each packet has a slight overhead ( packet size, confirmations, etc ) so sending one larger packet is slightly less data than sending three smaller ones. It's not just the total bandwidth you should be accounting for though. Sending one larger packet is more efficient simply because you don't have to send three times, wait three times, potentially confirm receipt three times, potentially reorder packets three times. Yes, it's definitely better to send your data in moderately sized packets. I'm not sure what would be optimal, but I certainly wouldn't want to be sending four bytes at a time.

I've never done the serializing/deserializing thing, but I did pack my data into a string in KickShot Pool and the networking on that seemed reasonably efficient, although it was not a particularly stressful application in the first place.

The way I have been doing it is with bank streams. For example:
local bank:TBank=new TBank,message:TBankStream=CreateBankStream(bank)
message.WriteInt(x)
message.WriteInt(y)
message.WriteFloat(speed)
message.writeFloat(direction)
socket.Send(Message._bank._buf, Message._bank._size) 


Also if I recall, packets have a minimum number of bytes which it will be padded with if you don't use them, so you might as well at least use the minimum.