Go to the documentation of this file.00001
00006
00007
00008
00009
00010 #include "../../include/mm_messages/headers.hpp"
00011 #include <ecl/containers/stencil.hpp>
00012 #include <ecl/converters.hpp>
00013
00014
00015
00016
00017
00018
00019 namespace mm_messages {
00020
00021
00022
00023
00024
00025 const unsigned int PacketHeader::size = sizeof(unsigned int);
00026 const unsigned int SubPacketHeader::size = 2*sizeof(unsigned int);
00027
00028 SubPacketHeader::SubPacketHeader(const int& id, const unsigned int& length)
00029 : id(id)
00030 , length(length)
00031 {}
00032
00033
00034
00035
00036
00037 void Message<PacketHeader>::encode(const PacketHeader& header, ByteArray& buffer) {
00038
00039 buffer.resize(PacketHeader::size);
00040 ecl::Converter<ByteArray, unsigned int> to_byte_array;
00041 to_byte_array(buffer, header.signature);
00042 }
00043
00044 PacketHeader Message<PacketHeader>::decode(const unsigned char* buffer, const unsigned int& size) {
00045
00046 unsigned int signature;
00047 ecl::Converter<unsigned int, ConstRawByteStencil> from_byte_array;
00048 unsigned int window_size = sizeof(unsigned int);
00049 ConstRawByteStencil stencil(buffer, size, buffer, buffer+window_size);
00050 signature = from_byte_array(stencil);
00051 return PacketHeader(signature);
00052 }
00053
00054 void Message<SubPacketHeader>::encode(const SubPacketHeader& header, ByteArray& buffer) {
00055 ByteArray sub_packet_header_buffer(SubPacketHeader::size);
00056 unsigned int window_size = sizeof(unsigned int);
00057 ecl::Converter<ByteStencil, unsigned int> to_byte_array;
00058 ByteStencil stencil(sub_packet_header_buffer, sub_packet_header_buffer.begin(), buffer.end());
00059 stencil.resettle(0, window_size); to_byte_array(stencil, header.id);
00060 stencil.resettle(window_size, window_size); to_byte_array(stencil, header.length);
00061 buffer.insert(buffer.end(), sub_packet_header_buffer.begin(), sub_packet_header_buffer.end());
00062 }
00063
00064 SubPacketHeader Message<SubPacketHeader>::decode(const unsigned char* buffer, const unsigned int& size) {
00065
00066 unsigned int signature;
00067 ecl::Converter<unsigned int, ConstRawByteStencil> from_byte_array;
00068 unsigned int window_size = sizeof(unsigned int);
00069 unsigned int id, length;
00070 ConstRawByteStencil stencil(buffer, size, buffer, buffer+size);
00071 stencil.resettle(0, window_size); id = from_byte_array(stencil);
00072 stencil.resettle(window_size, window_size); length = from_byte_array(stencil);
00073 return SubPacketHeader(id, length);
00074 }
00075
00076 }