#### Archived

This topic is now archived and is closed to further replies.

# Using structs/classes for packets...

This topic is 6010 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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 !! }

##### Share on other sites
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''.

##### Share on other sites
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?

Edited by - meh on September 6, 2001 3:10:46 PM

##### Share on other sites
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.

##### Share on other sites
Thanks Prefect, all these ends are confusing poor little meh.

##### Share on other sites
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;

##### Share on other sites
Good point, never thought about it that way... Thanks

##### Share on other sites
"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