open-dis-cpp icon indicating copy to clipboard operation
open-dis-cpp copied to clipboard

example usage of std::vector<OneByteChunk>

Open kurtsansom opened this issue 5 years ago • 7 comments

I have what feels like a dumb question.

I want to populate a variable length PDU SignalPdu with "fake" data and test it out.

e.g. I want to send an array of Ints, or 1 int for now.

I know that 1 int should get packed into a char array of size 4, int32 would be four bytes where each byte is an instance of OneByteChunk in the std::vector which should then be of size 4.

I am confused how to do this, and any help would be appreciated.

kurtsansom avatar Feb 17 '21 22:02 kurtsansom

I had a fiddle, an was able to come up with the below example - using the DataStream DIS util:

#include "DataStream.h"
#include "OneByteChunk.h"
using namespace DIS;

#include <iostream>
#include <vector>
using namespace std;

int main () {
  DataStream ds(Endian::BIG);
  vector<OneByteChunk> chunks;

  int int32 = 0x48692100; // 0x48 0x69 0x21 0x00 ("Hi!") 
  
  ds << int32;
  while (ds.GetReadPos() < ds.size()) {
    OneByteChunk tmp;
    tmp.unmarshal(ds);
    chunks.push_back(tmp);
  } 
  
  cout << "chunks (4 bytes): ";
  for (OneByteChunk chunk : chunks) {
    // Note '*' to dereference from char*
    cout << *(chunk.getOtherParameters());
  }
  cout << endl;
  
  return 0;
}
// outputs: "chunks (4 bytes): Hi!"

Does that help with your question?

Basically, push your integer(s) into a data stream. Then unmarshal into single OneByteChunk, and store in your vector. Keep unmarshalling until you've read the whole data stream.

Note: Includes are local folder because I copied to another folder to test.

rodneyp290 avatar Feb 18 '21 05:02 rodneyp290

This is exactly what I was struggling with. Thank you very much.

Every time I was reading about a "stream", it was in the context of sending it over a network. But this example describes what I was looking for. Using a datastream to pack (serialize?) c++ data into another data structure chunks. Where the chunks is the one that gets sent not a raw stream.

I am going to add this example into my branch.

kurtsansom avatar Feb 19 '21 15:02 kurtsansom

What would be the extension to this for a struct of data? I think it would be to create a new class like OneByteChunk or Vector3Double e.g. somedatatype

and replace this:

int int32 = 0x48692100;
ds << int32;

with:

DIS::somedatatype new_data;
... fill in new_data
new_data.marshal(ds);

Is this the right direction?

kurtsansom avatar Feb 19 '21 15:02 kurtsansom

are Marshalling/Unmarshalling synonyms with serialization and de-serialization?

kurtsansom avatar Feb 19 '21 16:02 kurtsansom

Yeah, you're change for a new data type looks like it should work. Yeah, they seem pretty interchangeable, although skim reading this Wikipedia article here suggests there might be subtle differences.

rodneyp290 avatar Feb 20 '21 05:02 rodneyp290

In open-dis-java we removed the OneByteChunk class and swapped it for a raw byte array. Probably worth doing same here.

Ref https://github.com/open-dis/open-dis-java/pull/44

leif81 avatar Feb 27 '21 04:02 leif81

If someone wants to open a PR for replacing OneByteChunk I'll gladly merge!

leif81 avatar Feb 27 '21 04:02 leif81

Closing. PR #81 replaced OneByteChunk class with uint8_t.

leif81 avatar Apr 26 '23 01:04 leif81