-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplitter.h
43 lines (33 loc) · 1.35 KB
/
splitter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#pragma once
#include <optional>
#include <vector>
#include <span>
#include <iostream>
class LengthExtractor {
public:
virtual ~LengthExtractor() = default;
virtual std::optional<size_t> GetDatagramLength(const std::span<char>& buffer) = 0;
};
class Splitter {
public:
Splitter(std::shared_ptr<LengthExtractor> datagram_length_extractor)
: datagram_length_extractor_(std::move(datagram_length_extractor)) {}
std::vector<std::vector<char>> SplitDatagrams(std::vector<char>& buffer) {
std::vector<std::vector<char>> datagrams;
size_t offset = 0;
while (offset < buffer.size()) {
std::span<char> buffer_span(buffer.data() + offset, buffer.size() - offset);
const auto datagram_length = datagram_length_extractor_->GetDatagramLength(buffer_span);
if (!datagram_length.has_value() || offset + datagram_length.value() > buffer.size()) {
break;
}
datagrams.emplace_back(std::make_move_iterator(buffer.begin() + offset),
std::make_move_iterator(buffer.begin() + offset + datagram_length.value()));
offset += datagram_length.value();
}
buffer.erase(buffer.begin(), buffer.begin() + offset);
return datagrams;
}
private:
std::shared_ptr<LengthExtractor> datagram_length_extractor_;
};