![]() |
VR-Link API Documentation for DIS
|
This file contains the TestPdu class, which extends DtPdu and holds the Number and Vector data.
To derive a class from DtPdu:
By using our "Net" types, such as DtNetInt32 (defined in NetTypes.h), you ensure platform independence. When you are on little endian machines, byte swapping is performed when assigning to a Net type, and when a Net type is implicitly cast to a native type.
The first field in all network representations should be a DtNetPduHeader.
If you are creating a fixed-length PDU, the structure should describe the entire layout of the PDU. Since this PDU is variable length, it is not possible to fully describe the PDU layout.
Define as much of the network representation structure as is possible with a C-style structure. The cardinality of "b" in the structure does not matter, since we will only be casting buffers to pointers to this structure, and not creating instances of the structure or relying on its size. We will not be able to access "c" at all through the structure, since it comes after the variable length array "b". We will have to use byte-arithmetic to reach "c".
The value you choose for PDU kind should not be one that is used by any other PDU. We recommend a value between 220 and 255.
In testPdu.cxx, we do the following:
The size passed to initPdu() is the size of the smallest legal TestPdu; that is, one with zero elements in the array b. The size of the PDU is therefore the size of the header plus the size of the DtNetInt32 field "a" plus the size of the DtNetFloat32 field "c".
initPdu() is a member of the base class DtPdu, and it handles allocating memory for the network representation of the PDU, and other related items.
For a fixed-length PDU, writing mutator and inspector functions is fairly straightforward. A pointer to the PDU's network representation is stored in a DtPdu member and can be obtained using DtPdu::packet(). The void* returned must be cast to a pointer to your network representation structure before fields are examined and set within your accessors. We define the function netTestPdu() to perform this cast. In our test PDU example, we can then use this function to obtain a pointer to the PDU's network representation structure.
When you write mutators for variable length PDUs, like the testPdu example, you need to call one of the following DtPdu member functions every time you change the size or layout of the PDU:
In addition, you may need to do some pointer arithmetic to access certain fields of the PDU that cannot be accessed through members of the static structure.
The insertBytes() function inserts the indicated number of blank (zeroed) bytes at the desired offset into the PDU's network representation.
The deleteBytes() function deletes the desired number of bytes, starting with the indicated offset. These functions handle reallocating the memory for the network representation if necessary, and will also make sure the size field in the header is updated to reflect the new size.
Since a represents the number of elements in the array b, we must insert or delete bytes from the PDU layout whenever we change a through its mutator.
To implement the mutator for A, the code does the following:
To implement the inspector and mutator for B:
The inspector and mutator for the c field must do some pointer arithmetic to find where the data is stored, since it cannot be accessed through the structure.