Sign in to follow this  

Non-static sized TCP-packets?

Recommended Posts

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this