Multiple Variables with one UDP message

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

lukehedman(Posted 2008) [#1]
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?


Volker(Posted 2008) [#2]
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?


Htbaa(Posted 2008) [#3]
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.


Volker(Posted 2008) [#4]
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.




lukehedman(Posted 2008) [#5]
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?


Gabriel(Posted 2008) [#6]
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.


spacerat(Posted 2008) [#7]
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) 



ImaginaryHuman(Posted 2008) [#8]
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.