Jump to content
  • Advertisement
Sign in to follow this  
Kada2k6

Non-static sized TCP-packets?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
You could give your packet structures a size() function that would take the place of sizeof.

struct packet_character_say {
unsigned short character_number;
char *message;
unsigned int size() { return sizeof(character_number) + strlen(message); }
};

struct packet_character_move {
int dx;
int dy;
unsigned int size() { return sizeof(dx) + sizeof(dy); }
};


packet_data.character_number = 123; /* or whatever */
sprintf(packet_data.message, "hello"); /* this isn't done manually, but just to illustrate... */
packet_header.packet_type = PACKET_CHARACTER_SAY;
packet_header.packet_size = packet_data.size();



Keep in mind, though, since you're dealing with a pointer, you'll have to worry about how you're getting that data into and out of the network buffer. Cant just cast raw memory into a struct like you might have been able to do otherwise.

Share this post


Link to post
Share on other sites
The Etwork library lets you do just that. You can download the source and look at it for inspiration, if you don't want to use it as-is.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!