20 BEGIN_NAMESPACE_MEMORY
22 template <
typename T, std::
size_t BufferSize, std::
size_t HeaderLength>
37 const std::vector<T>&
filter(
const uint8_t* data, std::size_t size);
44 virtual bool isHeader(
const uint8_t* data, std::size_t size) = 0;
50 virtual std::size_t
findEnd(
const uint8_t* start, std::size_t size) = 0;
60 std::array<uint8_t, BufferSize> buffer;
62 std::vector<T> packets;
65 std::pair<std::size_t, std::size_t> findPacket(
const uint8_t* data, std::size_t size);
66 void removeFromBeginning(std::size_t size);
71 template <
typename T, std::
size_t BufferSize, std::
size_t HeaderLength>
75 static_assert(BufferSize > HeaderLength,
"BufferSize must be greater than HeaderLength");
78 template <
typename T, std::
size_t BufferSize, std::
size_t HeaderLength>
84 template <
typename T, std::
size_t BufferSize, std::
size_t HeaderLength>
89 std::size_t copySize = 0;
90 while (copySize != size)
92 std::size_t remaining = BufferSize - fillSize;
93 if (size - copySize <= remaining)
95 std::memcpy(buffer.data() + fillSize, data + copySize, size - copySize);
96 fillSize += size - copySize;
101 std::memcpy(buffer.data() + fillSize, data + copySize, remaining);
102 fillSize = BufferSize;
103 copySize += remaining;
109 if (fillSize == BufferSize)
110 removeFromBeginning(HeaderLength);
115 template <
typename T, std::
size_t BufferSize, std::
size_t HeaderLength>
118 std::pair<std::size_t, std::size_t> result(std::string::npos, std::string::npos);
120 if (size < HeaderLength)
123 for (std::size_t i = 0; i < size - HeaderLength; i++)
125 if (isHeader(data + i, size - i))
132 if (result.first != std::string::npos)
134 std::size_t end = findEnd(data + result.first, size - result.first);
135 if (end != std::string::npos)
136 result.second = result.first + end;
141 template <
typename T, std::
size_t BufferSize, std::
size_t HeaderLength>
144 if (size >= fillSize)
150 std::memmove(buffer.data(), buffer.data() + size, fillSize - size);
155 template <
typename T, std::
size_t BufferSize, std::
size_t HeaderLength>
158 std::pair<std::size_t, std::size_t> packetInds = findPacket(buffer.data(), fillSize);
159 if (packetInds.first != std::string::npos && packetInds.second != std::string::npos)
161 packets.emplace_back(convertToPacket(buffer.data() + packetInds.first, packetInds.second - packetInds.first + 1));
162 removeFromBeginning(packetInds.second + 1);
165 else if (packetInds.first != std::string::npos)
167 removeFromBeginning(packetInds.first);
171 removeFromBeginning(fillSize - HeaderLength);
Definition: packet_filter.h:23
const std::vector< T > & filter(const uint8_t *data, std::size_t size)
Adds data to the internal buffer and filters out packets.
Definition: packet_filter.h:85
virtual T convertToPacket(const uint8_t *start, std::size_t size)=0
Function that converts complete packet bytes to a packet.
virtual void clear()
Clears the internal buffer.
Definition: packet_filter.h:79
virtual bool isHeader(const uint8_t *data, std::size_t size)=0
Pure virtual function that checks if the data is a header.
virtual std::size_t findEnd(const uint8_t *start, std::size_t size)=0
Function that checks if the packet is complete from the start of the header to the end of available d...