@@ -13,119 +13,6 @@ const std::uint8_t StartByte = 0x02;
1313const std::uint8_t EndByte = 0x03 ;
1414const std::uint8_t EscByte = 0x10 ;
1515
16- std::size_t findMsgStart (const std::uint8_t * buf, std::size_t bufLen)
17- {
18- std::size_t pos = 0 ;
19- bool escaped = false ;
20- while (pos < bufLen) {
21- auto ch = buf[pos];
22- if ((ch == StartByte) && (!escaped)) {
23- break ;
24- }
25-
26- ++pos;
27-
28- if ((ch == EscByte) && (!escaped)) {
29- escaped = true ;
30- continue ;
31- }
32-
33- escaped = false ;
34- }
35-
36- return pos;
37- }
38-
39- std::size_t findMsgEnd (const std::uint8_t * buf, std::size_t bufLen, std::size_t from)
40- {
41- auto pos = from;
42- bool escaped = false ;
43- while (pos < bufLen) {
44- auto ch = buf[pos];
45- if ((ch == EndByte) && (!escaped)) {
46- break ;
47- }
48-
49- ++pos;
50-
51- if ((ch == EscByte) && (!escaped)) {
52- escaped = true ;
53- continue ;
54- }
55-
56- escaped = false ;
57- }
58-
59- return pos;
60- }
61-
62- void copyMsg (
63- const std::uint8_t * buf,
64- std::size_t startPos,
65- std::size_t endPos,
66- CommonSessionBase::MsgBuf& outBuf)
67- {
68- auto pos = startPos + 1 ;
69- bool escaped = false ;
70- while (pos < endPos) {
71- auto ch = buf[pos];
72- ++pos;
73-
74- if ((ch == EscByte) && (!escaped)) {
75- escaped = true ;
76- continue ;
77- }
78-
79- escaped = false ;
80- outBuf.push_back (ch);
81- }
82- }
83-
84- }
85-
86- std::size_t CommonSessionBase::preProcessInput (
87- const std::uint8_t * buf,
88- std::size_t bufLen,
89- MsgBuf& outBuf)
90- {
91- outBuf.clear ();
92- auto startPos = findMsgStart (buf, bufLen);
93- if (bufLen <= startPos) {
94- // No start has been found, report consumed all input data
95- return bufLen;
96- }
97-
98- auto endPos = findMsgEnd (buf, bufLen, startPos);
99- if (bufLen <= endPos) {
100- // No end has been found, report consumed until start
101- return startPos;
102- }
103-
104- outBuf.reserve (endPos - startPos);
105- copyMsg (buf, startPos, endPos, outBuf);
106- return endPos + 1 ;
107- }
108-
109- void CommonSessionBase::postProcessOutput (MsgBuf& buf)
110- {
111- MsgBuf newBuf;
112- newBuf.push_back (StartByte);
113- for (auto ch : buf) {
114- static const std::uint8_t Special[] = {
115- StartByte,
116- EndByte,
117- EscByte
118- };
119-
120- auto specialIter = std::find (std::begin (Special), std::end (Special), ch);
121- if (specialIter != std::end (Special)) {
122- newBuf.push_back (EscByte);
123- }
124-
125- newBuf.push_back (ch);
126- }
127- newBuf.push_back (EndByte);
128- buf.swap (newBuf);
12916}
13017
13118void CommonSessionBase::dropEscapes (const std::uint8_t * buf, std::size_t bufLen, MsgBuf& outBuf)
0 commit comments