2

So I'm trying to use ns-3 to do some network simulations. I want to transfer a file (which can be quite big, some video file for example), so I have to "pack" the file in the data structure provided by the framework

ns3::Packet::Packet ( uint8_t const * buffer, uint32_t size )

So I need to have my file serialized to a uint8_t buffer - I've tried this so far:

std::ifstream input_sample_file("sample_input_file.mp4",std::ifstream::out | std::ifstream::binary);
std::ofstream output_sample_file("sample_output_file.mp4",std::ifstream::binary | std::ifstream::in );
long sample_file_size;
sample_file_size = getFileSize(input_sample_file);
char * sample_buffer = new char [sample_file_size];
input_sample_file.read(sample_buffer, sample_file_size);
uint8_t * sample_buffer_adjusted = new uint8_t [sample_file_size];
std::memcpy(&sample_buffer_adjusted, (const char *) sample_buffer, sample_file_size);

Which of course is very inefficient in the first place, and of course the memcpy fails, which shouldn't be much of a surprise, but I haven't tried to fix this, because there has to be some other way to directly serialize the input file to a uint8_t buffer, and then hand it over to ns3::Packet::Packet.

Biruk Abebe
  • 2,235
  • 1
  • 13
  • 24
harrisonfooord
  • 308
  • 3
  • 19
  • Allocate just the `uint8_t` buffer, and use it directly with `read()`, via `reinterpret_cast`. Even better: use `std::vector` instead of `new`. P.S.: the bug with memcpy is because it's first parameter should be `&sample_buffer_adjusted[0]`. – Sam Varshavchik Apr 23 '16 at 17:28
  • Here you find a previous discussion about your question (http://stackoverflow.com/questions/25360893/convert-char-to-uint8-t). In the end you need to convert char* to uint8_t* – madduci Apr 23 '16 at 19:21
  • fwiw: if this is under linux and doesn't need to be portable, consider `mmap`. If it's under Windows and doesn't need to be portable, consider `MapViewOfFile`. – kfsone Apr 23 '16 at 19:21

0 Answers0