Advertisement

A different 'what kind of package is incoming' question

Started by August 06, 2006 07:01 PM
1 comment, last by Dookie 18 years, 6 months ago
Hey guys! It's me again. I'm working on some UDP multiplayer code of my own and have this line in my 'receive' code... int error = recvfrom(srvr_socket, reinterpret_cast<char*>(TempData), datSize, 0, (struct sockaddr *)&client_in, &addr_size); The thing is, 'TempData' can be one of three different structs: 'iDat', 'cDat', or 'conDat'. In order to receive the data into 'TempData' regardless of what struct is being received, what data type should 'TempData' be? For example, I can't define TempData like this: iDat TempData; because I don't know if TempData is going to receive an iDat struct. Should it be something like 'void TempData'? If so (or, if not), how should I define it? After receiving data into 'TempData', I can tell what struct type it is by comparing the value in 'error' with each struct size (assuming the entire packet was sent intact). After I figure out with which struct 'TempData' belongs, how do I convert 'TempData' into the appropriate struct type (iDat, cDat or conDat)? Hopefully you can help me with my two questions. I've been scratching my head on this all night! [sad]
"The crows seemed to be calling his name, thought Caw"
You have a few options:

1) Declare a union:

union {  ADat a;  BDat b;  CDat c;} UDat;UDat toReceive;


2) Receive into a plain array of bytes, and then memcpy() to an instance of the correct datatype.

3) Some blend of the above -- very popular with having a shared header, but variant payload data.

struct Message {  unsigned char type;  unsigned char flags;  union {    AData a;    BData b;  } payload;};
enum Bool { True, False, FileNotFound };
Advertisement
Whew, finally got around to playing around with my network code again! Been REALLY busy with work, school, married life, etc... Makes me want to say "C'mon people, I want to CODE!" [grin]

Anyways, I tried your idea of simply memcpy()'ing the data from a string array into the appropriate struct. And it worked great! I now have a hastily-coded network Pong game working, and it looks pretty friggin' awful, but the net play works. Yippie! Now let's see if I can make a cool multiplayer adventure game using this new-found knowledge. Wish me luck, because I'm gonna need it.

Thanks for the help, hplus0603. I really appreciate it!
"The crows seemed to be calling his name, thought Caw"

This topic is closed to new replies.

Advertisement