🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Messaging Schemes

Started by
5 comments, last by a2k 22 years, 8 months ago
what''s a good scheme for packing/unpacking messages? The code that came with Adrian Perez''s book "Advanced 3D Game Programming" uses a class for each message, and dynamically allocates and deallocates the message everytime a packet is received. Is this a good way of doing it or are there better ways? a2k
------------------General Equation, this is Private Function reporting for duty, sir!a2k
Advertisement
Hiya,

quote:
dynamically allocates and deallocates the message everytime a packet is received


Bad bad bad allocating and deallocating always takes time.. personally i dont use a class to encapsulate a message? but if you like doing it that way then have a "cash" of allocated message classes

ie have a linked list pointing to allocated but un used message classes. When you would delete one just throw it onto the front of the linked list, and when you want to allocate one, first check to see if there is one available on the list and use that.. if not allocate a new one ;-)

hmm.. thinking about it a linked list is wrong hehe because you will need to allocate/deallocate the linked list nodes lol.. best to decided on how many messages you will ever have at the same time (this may only be one.. in which case you need no list just a single instance of the class which you always use and dont deallocate) then make something like a circular array and put old ones on the end of the array and take from the front ;-)

good luck
~tim
~ Tim
quote: Original post by wrathgame
hmm.. thinking about it a linked list is wrong hehe because you will need to allocate/deallocate the linked list nodes lol.. best to decided on how many messages you will ever have at the same time (this may only be one.. in which case you need no list just a single instance of the class which you always use and dont deallocate) then make something like a circular array and put old ones on the end of the array and take from the front ;-)


Yup, that''s called resource pooling.

There''s nothing wrong with implementing a message protocol as a class. Consider the class essentially to be a data filter that takes your message (delivered by TCP or UDP or whatever) and loads it into itself and renders the contents of the message as properties of the class (msg.loadFromString()). The reverse should also be true in that the class should be able to help you create messages (msg.saveToString()). This saves a LOT of parsing time because all that work is done only once per message, instead of every time you need to get just a field from the message.

I write these all day...

-------------
-WarMage
...mbgmbgbgmmbgmbmgbbmgmb...
yes, i''d have to agree that it''s a really sleek design, but for the allocation and deallocation, would this be inefficient? if not, i''ll stick with this, because I like this design a lot.

a2k
------------------General Equation, this is Private Function reporting for duty, sir!a2k
The use of a resource pool mitigates the innefficiency inherent in dynamic alloc/dealloc, because although you are doing X allocations, you are doing them all at startup, when the application doesn''t have any messages to deal with. Once they''ve been allocated, they just take up space, not time.

Just be sure that whatever vector or circular queue you use for managing the pool doesn''t have any problems pushing the empty objects around (shouldn''t be a problem because they''re non-null) or have wasteful code in its routines that would waste ticks. Common sense, that.

----------
-WarMage
...i fart burning sodium....
oh, okay... well, the code that i''ve been looking at Adrian Perez''s actually dynamically allocates on the fly when a message is received, and deleted once the message has been parsed. I suppose i can just allocate all the different messages on initialization, but why didn''t Adrian Perez do that?

a2k
quote:
why didn''t Adrian Perez do that

Have to ask him that

Check out the "Why Pluggable Factories Rock My Multiplayer World" article for an excellent method to setup message handlers.

Now, what to do once you have the byte stream?

From your winsock/directplay receive function, you should just flat copy the packet into an interlocked circular packet queue and bail. In a seperate worker thread rip through the circular packet queue and build actual game messages from the packets. Allocate the message from another circular message queue.

So for each message you need two things, a message struct and a message maker class.

I''ll have to work on this.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement