I've got a complicated data structure that I wanted to send out as a UDP packet using the C++ class. Is this possible?
My issue is that the C++ constructors seem to be limited to a BYTE a WORD or a null terminated string.
Here's just part of my struct below I'm trying to send. As you can see there are different BYTE arrays encompassed within the struct, so even doing pkt.AddData(NodeReport); doesn't work since I need it to send the full padded size and not null terminate.
Is it possible to do this? Or am I going to be stuck re-writing to use sockets instead?
Not pretty but:
// Define a template for a data structure that will be the same on both
// platforms. We'll use this template to create specific instances of
// structures with this layout.
struct ATemplateStructForDataToTransferViaUDP {
int intVar1; //4 bytes
int intVar2; //4 + 4 = 8 bytes
WORD wordVar3; //8 + 2 = 10 bytes
BYTE byteVar4; //10 + 1 = 11 bytes
One other thing: You must typically force the alignment of these structs so they are consistent and so space between struct members, or at the end is NOT padded. Both structs need to be the same on the Tx and Rx ends if you plan to Tx out, then Rx into similar objects. Depending on the platform, you MAY be OK with 4 or 8 bit entities but with bytes or chars, alignment demands more attention. Use sizeof() to checkout the difference w/ and w/o the packed attribute. Note that aligning objects off of 4 bit boundaries can slow things down which is why, unless you align, char and byte members usually get padded. You can also use offsetof(struct_type, member) to see the offset in bytes from the beginning of the object.