headers.cpp
Go to the documentation of this file.
1 
6 /*****************************************************************************
7 ** Includes
8 *****************************************************************************/
9 
10 #include "../../include/mm_messages/headers.hpp"
12 #include <ecl/converters.hpp>
13 //#include "../../../include/mm_messages/registry.hpp"
14 
15 /*****************************************************************************
16 ** Namespaces
17 *****************************************************************************/
18 
19 namespace mm_messages {
20 
21 /*****************************************************************************
22 ** Definitions
23 *****************************************************************************/
24 
25 const unsigned int PacketHeader::size = sizeof(unsigned int);
26 const unsigned int SubPacketHeader::size = 2*sizeof(unsigned int);
27 
28 SubPacketHeader::SubPacketHeader(const int& id, const unsigned int& length)
29  : id(id)
30  , length(length)
31 {}
32 
33 /*****************************************************************************
34 ** Serialisations
35 *****************************************************************************/
36 
38  // This is the header...the buffer should be clear anyway.
39  buffer.resize(PacketHeader::size);
41  to_byte_array(buffer, header.signature);
42 }
43 
44 PacketHeader Message<PacketHeader>::decode(const unsigned char* buffer, const unsigned int& size) {
45  // TODO : check PacketHeader::size == size, throw exception
46  unsigned int signature;
48  unsigned int window_size = sizeof(unsigned int);
49  ConstRawByteStencil stencil(buffer, size, buffer, buffer+window_size);
50  signature = from_byte_array(stencil);
51  return PacketHeader(signature);
52 }
53 
55  ByteArray sub_packet_header_buffer(SubPacketHeader::size);
56  unsigned int window_size = sizeof(unsigned int);
58  ByteStencil stencil(sub_packet_header_buffer, sub_packet_header_buffer.begin(), buffer.end());
59  stencil.resettle(0, window_size); to_byte_array(stencil, header.id);
60  stencil.resettle(window_size, window_size); to_byte_array(stencil, header.length);
61  buffer.insert(buffer.end(), sub_packet_header_buffer.begin(), sub_packet_header_buffer.end());
62 }
63 
64 SubPacketHeader Message<SubPacketHeader>::decode(const unsigned char* buffer, const unsigned int& size) {
65  // TODO : check PacketHeader::size == size, throw exception
66  unsigned int signature;
68  unsigned int window_size = sizeof(unsigned int);
69  unsigned int id, length;
70  ConstRawByteStencil stencil(buffer, size, buffer, buffer+size);
71  stencil.resettle(0, window_size); id = from_byte_array(stencil);
72  stencil.resettle(window_size, window_size); length = from_byte_array(stencil);
73  return SubPacketHeader(id, length);
74 }
75 
76 } // mm_messages
static void encode(const T &msg, ByteArray &buffer)
Definition: message.hpp:47
static const unsigned int size
Definition: headers.hpp:31
void resettle(const unsigned int &start_index, const unsigned int &n) ecl_assert_throw_decl(StandardException)
unsigned int signature
Definition: headers.hpp:33
void resettle(const unsigned int &start_index, const unsigned int &n) ecl_assert_throw_decl(StandardException)
static const unsigned int size
Definition: headers.hpp:37
std::vector< unsigned char > ByteArray
Definition: message.hpp:29
static T decode(const unsigned char *buffer, const unsigned int &size)
Definition: message.hpp:50
SubPacketHeader(const int &id=-1, const unsigned int &length=0)
Definition: headers.cpp:28


mm_messages
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:52:10