Vanetza
 
Loading...
Searching...
No Matches
cohesive_packet.hpp
1#ifndef COHESIVE_PACKET_HPP_VG2XKSCV
2#define COHESIVE_PACKET_HPP_VG2XKSCV
3
4#include <vanetza/common/byte_buffer.hpp>
5#include <vanetza/net/osi_layer.hpp>
6#include <boost/range/iterator_range.hpp>
7#include <array>
8
9namespace vanetza
10{
11
12/**
13 * A cohesive packet is stored in contiguous memory
14 */
16{
17public:
18 typedef boost::iterator_range<ByteBuffer::iterator> buffer_range;
19 typedef boost::iterator_range<ByteBuffer::const_iterator> buffer_const_range;
20
21 /**
22 * Create packet from buffer and assign all bytes to given layer
23 * \param buffer copy data from this buffer
24 * \param layer all bytes belong to this layer (at least at first)
25 */
26 CohesivePacket(const ByteBuffer& buffer, OsiLayer layer);
27 CohesivePacket(ByteBuffer&& buffer, OsiLayer layer);
28
30 CohesivePacket& operator=(const CohesivePacket&);
31
32 CohesivePacket(CohesivePacket&&) = default;
33 CohesivePacket& operator=(CohesivePacket&&) = default;
34
35 /**
36 * Access a certain sub-range of packet data belonging to a specific layer
37 * \param layer requested layer data
38 * \return buffer range with data, might be empty
39 */
40 buffer_const_range operator[](OsiLayer layer) const;
41 buffer_range operator[](OsiLayer layer);
42
43 /**
44 * Set boundary of layer data.
45 * Data beyond boundary belongs to next upper layer afterwards.
46 * \param layer set boundary of this layer
47 * \param bytes length of layer
48 * \note Never set boundary larger than previous layer length!
49 */
50 void set_boundary(OsiLayer, unsigned bytes);
51
52 /**
53 * Trim size of packet, i.e. cut bytes at the end if too long.
54 * \param from start counting with this layer
55 * \param bytes target length in bytes
56 */
57 void trim(OsiLayer from, unsigned bytes);
58
59 /**
60 * Get size of whole packet
61 * \return length in bytes
62 */
63 std::size_t size() const;
64
65 /**
66 * Get size of a single layer in packet
67 * \param single_layer which layer has to be considered
68 * \return length in bytes
69 */
70 std::size_t size(OsiLayer single_layer) const;
71
72 /**
73 * Get size of several layers
74 * \param from start counting with this layer
75 * \param to stop counting after this layer
76 * \return length in bytes
77 */
78 std::size_t size(OsiLayer from, OsiLayer to) const;
79
80 /**
81 * Non-mutable access to internal byte buffer
82 * \return const byte buffer reference
83 */
84 const ByteBuffer& buffer() const { return m_buffer; }
85
86private:
87 void reset_iterators(OsiLayer layer);
88 void rebuild_iterators(const CohesivePacket&);
89 buffer_const_range get(unsigned idx) const;
90 buffer_range get(unsigned idx);
91
92 ByteBuffer m_buffer;
93 std::array<ByteBuffer::iterator, osi_layers.size() + 1> m_iterators;
94};
95
96} // namespace vanetza
97
98#endif /* COHESIVE_PACKET_HPP_VG2XKSCV */
99
buffer_const_range operator[](OsiLayer layer) const
std::size_t size() const
void set_boundary(OsiLayer, unsigned bytes)
const ByteBuffer & buffer() const
void trim(OsiLayer from, unsigned bytes)