Page 1 of 1

UDP send data struct using AddData()

Posted: Tue Apr 17, 2012 6:25 pm
by sblair
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?

Thanks
Scott

Code: Select all

   BYTE ID[8];		// 'Art-Net' 0x00  null terminated.
	WORD OpCode;	// low byte first.
	BYTE IPAddress[4];
	WORD Port;		// always 0x1936 low byte first.
	BYTE VersInfoH;
	BYTE VersInfo;
	BYTE NetSwitch;
	BYTE SubSwitch;
	BYTE OemHi;
	BYTE Oem;
	BYTE UbeaVersion;
	BYTE Status1;
	WORD ESTAMan;
	BYTE ShortName[18];
	BYTE LongName[64];
	BYTE NodeReport[64];
	BYTE NumPortsHi;
	BYTE NumPortsLo;
	BYTE PortTypes[4];
	BYTE GoodInput[4];
	BYTE GoodOutput[4];
	BYTE Swin[4];
   BYTE Filler[26];

Re: UDP send data struct using AddData()

Posted: Tue Apr 17, 2012 9:29 pm
by Ridgeglider
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

#define BUFLEN (80) //11 + 80 = 91 bytes
char Mybuf[BUFLEN];
};

Then:
//Use the struct defined above to create an intance of an object to Tx from...
ATemplateStructForDataToTransferViaUDP MyTxStruct;

and later...

UDPPacket UdpTxPkt;
UdpTxPkt.SetSourcePort(SrcPortnum);
UdpTxPkt.SetDestinationPort(DestPortnum);
UdpTxPkt.ResetData();

//Starting at the beginning byte of MyTxStruct (BYTE *)&MyTxStruct
// send sizeof(MyTxStruct) bytes.
UdpTxPkt.AddData((BYTE *)&MyTxStruct, sizeof(MyTxStruct));
UdpTxPkt.Send(ipaddr);

Re: UDP send data struct using AddData()

Posted: Tue Apr 17, 2012 9:51 pm
by sblair
Clever! I like it! I didn't realize the constructor would allow you to pass a sizeof parameter with a pointer for AddData();

Thanks!!
Scott

Re: UDP send data struct using AddData()

Posted: Wed Apr 18, 2012 6:36 am
by Ridgeglider
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.

struct __attribute__ ((__packed__)) ByteIntDbl_t {
BYTE aByte ; //a Byte
int AnInt ; //an integer
double aDbl; // a double
};


If you are sending to a non NB platform, endianness will be a consideration too...