Using structs/classes for packets...

Started by
7 comments, last by meh 22 years, 7 months ago
This may be a rather stupid/simple question but I haven''t been able to find the answer anywhere. I know you can use structs as packets but what about using class''s. I''m only interested in the inheritance aspect. e.g.
  
class CGenericPacket
{
   int m_ID;
   int m_size;
};

class CVelocity : public CGenericPacket
{
   CVector m_velocity;
};
  
Then sending an instance of the class accross and grabbing the info out in a similar method to a struct. Thus you can construct packets with as much/as little info in as you want through inheritance. Also are there any advantages to not converting the struct/class to Big-Endian before sending it through (assuming both machines are win32 based) or will it screw things up.
Advertisement
i am not sure also, but i think it will be ok..
just guessing..

i also want to know whether it will work .. .



{ Stating the obvious never helped any situation !! }
Yes, it will work. The only thing that won''t work is having virtual methods in the classes, because the v-table would be screwed up if you transfer an object with virtual methods. In your simple case with only data (or data and non-virtual methods) then it''s perfectly alright. Structs and classes are implemented in the same way by the compiler (i.e. You can have structs inherit each other and structs can have methods, etc).

The only real difference between structs and classes is that the default access modifier for structs is ''public'' and the default access modifier for classes is ''private''.
Woo! I had a sneaking suspicion it would work, I don't want to transfer across methods (a bit of a waste of bandwidth :D). But its good to know that I can use inheritance to build up more complex packets.

As for the big-endian stuff, anyone have any answers for that?

[edit] Added last line. [/edit]

Edited by - meh on September 6, 2001 3:10:46 PM
You can just ignore any endianness-issues as long as you don''t port to different hardware platforms.

And even if you will port to a different platform some time in the future, you don''t need to change the network protocol - you just make x86 ordering the default and only convert it on the other platform(s).

Really, all that the networking does is transferring a stream of bits without even touching it (well, it calculates a checksum, but don''t worry about that). What you do with these bits is completely up to you.

cu,
Prefect

One line of sourcecode says more than a thousand words.
Widelands - laid back, free software strategy
Thanks Prefect, all these ends are confusing poor little meh.
quote:Original post by meh
I don''t want to transfer across methods (a bit of a waste of bandwidth :D).


You can add as many methods as you want and it won''t affect the packet size a bit. The only thing transferred over the wire is the actual data contained in the class. If the client has the same definition of the class as you have(which by definition he must have if he''s going to extract the data), the code for those methods will be linked into the client executable when the client is built. No methods are ever transferred over the network.



  if ( value == 0 )   return value;else    return 0;     
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Good point, never thought about it that way... Thanks
"You can add as many methods as you want and it won''t affect the packet size a bit [so long as no method is virtual and no base classes are virtually inherited]."

Magmai Kai Holmlor
- Not For Rent
- 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