author | František Kučera <franta-hg@frantovo.cz> |
Sun, 20 Jun 2021 21:06:02 +0200 | |
branch | v_0 |
changeset 14 | 02725d301010 |
parent 13 | d5e2cb9e31f1 |
permissions | -rw-r--r-- |
10 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, version 3 of the License. |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
17 |
#pragma once |
|
18 |
||
19 |
#include <string> |
|
20 |
#include <exception> |
|
21 |
#include <cstring> |
|
22 |
||
23 |
#include "AbstractParser.h" |
|
24 |
||
25 |
namespace relpipe { |
|
26 |
namespace in { |
|
27 |
namespace asn1 { |
|
28 |
namespace lib { |
|
29 |
||
30 |
class TransactionalBuffer { |
|
31 |
private: |
|
13
d5e2cb9e31f1
switch TransactionalBuffer from char to uint8_t + partial parser of tags and lengths
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
32 |
uint8_t* buffer; |
10 | 33 |
size_t bufferSize; |
34 |
bool hasData = false; |
|
35 |
size_t writePosition = 0; |
|
36 |
// size_t writePositionCommited = 0; // TODO: transactions also on the writing side? |
|
37 |
size_t readPosition = 0; |
|
38 |
size_t readPositionCommited = 0; |
|
39 |
||
14
02725d301010
somehow parse nested SEQUENCEs and SETs + support definite long form of lengths (multiple length octets)
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
40 |
size_t bytesRead = 0; |
02725d301010
somehow parse nested SEQUENCEs and SETs + support definite long form of lengths (multiple length octets)
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
41 |
size_t bytesReadCommited = 0; |
02725d301010
somehow parse nested SEQUENCEs and SETs + support definite long form of lengths (multiple length octets)
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
42 |
|
10 | 43 |
size_t availableForReading() { |
44 |
if (readPosition < writePosition) return writePosition - readPosition; |
|
45 |
else if (readPosition > writePosition) return bufferSize - readPosition + writePosition; |
|
46 |
else if (hasData) return bufferSize; |
|
47 |
else return 0; |
|
48 |
} |
|
49 |
||
50 |
size_t availableForWriting() { |
|
51 |
if (readPositionCommited < writePosition) return bufferSize - writePosition + readPositionCommited; |
|
52 |
else if (readPositionCommited > writePosition) return readPositionCommited - writePosition; |
|
53 |
else if (hasData) return 0; |
|
54 |
else return bufferSize; |
|
55 |
} |
|
56 |
||
57 |
public: |
|
58 |
||
13
d5e2cb9e31f1
switch TransactionalBuffer from char to uint8_t + partial parser of tags and lengths
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
59 |
/** |
10 | 60 |
* Is thrown from read() and peak() methods if there are not enough data. |
61 |
* Interrupts current update() cycle and causes rollback. |
|
62 |
*/ |
|
63 |
class ReadBufferUnderflowException { |
|
64 |
// TODO: common super-class for exceptions, hidden implementation |
|
65 |
}; |
|
66 |
||
67 |
/** |
|
68 |
* Is thrown from write() method of the buffer when there is not enough space. |
|
69 |
* Interrupts whole process. |
|
70 |
*/ |
|
71 |
class WriteBufferOverflowException { |
|
72 |
}; |
|
73 |
||
74 |
TransactionalBuffer(size_t initialSize = 4096) : bufferSize(initialSize) { |
|
75 |
// TODO: initial size + resize + hard upper limit |
|
13
d5e2cb9e31f1
switch TransactionalBuffer from char to uint8_t + partial parser of tags and lengths
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
76 |
buffer = (uint8_t*) malloc(bufferSize); |
10 | 77 |
} |
78 |
||
79 |
virtual ~TransactionalBuffer() { |
|
80 |
free(buffer); |
|
81 |
} |
|
82 |
||
13
d5e2cb9e31f1
switch TransactionalBuffer from char to uint8_t + partial parser of tags and lengths
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
83 |
void write(const uint8_t* inputBuffer, const size_t length) { |
10 | 84 |
if (length == 0) return; |
85 |
if (length > availableForWriting()) throw WriteBufferOverflowException(); // TODO: optional resize |
|
86 |
||
87 |
hasData = true; |
|
88 |
||
89 |
const size_t a = std::min(length, bufferSize - writePosition); |
|
90 |
const size_t b = length - a; |
|
91 |
||
92 |
// TODO: map buffer twice in the memory in order to create a continuous area and avoid the second memcpy()? |
|
93 |
memcpy(buffer + writePosition, inputBuffer, a); |
|
94 |
memcpy(buffer, inputBuffer + a, b); |
|
95 |
||
96 |
writePosition = (writePosition + length) % bufferSize; |
|
97 |
} |
|
98 |
||
13
d5e2cb9e31f1
switch TransactionalBuffer from char to uint8_t + partial parser of tags and lengths
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
99 |
void read(uint8_t* outputBuffer, const size_t length) { |
10 | 100 |
if (length == 0) return; |
101 |
||
102 |
peek(outputBuffer, length); |
|
103 |
readPosition = (readPosition + length) % bufferSize; |
|
104 |
hasData = readPosition != writePosition; |
|
14
02725d301010
somehow parse nested SEQUENCEs and SETs + support definite long form of lengths (multiple length octets)
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
105 |
bytesRead += length; |
10 | 106 |
} |
107 |
||
13
d5e2cb9e31f1
switch TransactionalBuffer from char to uint8_t + partial parser of tags and lengths
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
108 |
void peek(uint8_t* outputBuffer, const size_t length) { |
10 | 109 |
if (length == 0) return; |
110 |
if (length > availableForReading()) throw ReadBufferUnderflowException(); |
|
111 |
||
112 |
const size_t a = std::min(length, bufferSize - readPosition); |
|
113 |
const size_t b = length - a; |
|
114 |
||
115 |
memcpy(outputBuffer, buffer + readPosition, a); |
|
116 |
memcpy(outputBuffer + a, buffer, b); |
|
117 |
} |
|
118 |
||
119 |
void commitRead() { |
|
120 |
readPositionCommited = readPosition; |
|
14
02725d301010
somehow parse nested SEQUENCEs and SETs + support definite long form of lengths (multiple length octets)
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
121 |
bytesReadCommited = bytesRead; |
10 | 122 |
} |
123 |
||
124 |
void rollbackRead() { |
|
125 |
readPosition = readPositionCommited; |
|
14
02725d301010
somehow parse nested SEQUENCEs and SETs + support definite long form of lengths (multiple length octets)
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
126 |
bytesRead = bytesReadCommited; |
10 | 127 |
} |
14
02725d301010
somehow parse nested SEQUENCEs and SETs + support definite long form of lengths (multiple length octets)
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
128 |
|
02725d301010
somehow parse nested SEQUENCEs and SETs + support definite long form of lengths (multiple length octets)
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
129 |
size_t getBytesRead() { |
02725d301010
somehow parse nested SEQUENCEs and SETs + support definite long form of lengths (multiple length octets)
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
130 |
return bytesRead; |
02725d301010
somehow parse nested SEQUENCEs and SETs + support definite long form of lengths (multiple length octets)
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
131 |
} |
02725d301010
somehow parse nested SEQUENCEs and SETs + support definite long form of lengths (multiple length octets)
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
132 |
|
10 | 133 |
}; |
134 |
||
135 |
||
136 |
} |
|
137 |
} |
|
138 |
} |
|
139 |
} |