/src/opendnp3/cpp/lib/src/link/ShiftableBuffer.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2013-2022 Step Function I/O, LLC |
3 | | * |
4 | | * Licensed to Green Energy Corp (www.greenenergycorp.com) and Step Function I/O |
5 | | * LLC (https://stepfunc.io) under one or more contributor license agreements. |
6 | | * See the NOTICE file distributed with this work for additional information |
7 | | * regarding copyright ownership. Green Energy Corp and Step Function I/O LLC license |
8 | | * this file to you under the Apache License, Version 2.0 (the "License"); you |
9 | | * may not use this file except in compliance with the License. You may obtain |
10 | | * a copy of the License at: |
11 | | * |
12 | | * http://www.apache.org/licenses/LICENSE-2.0 |
13 | | * |
14 | | * Unless required by applicable law or agreed to in writing, software |
15 | | * distributed under the License is distributed on an "AS IS" BASIS, |
16 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17 | | * See the License for the specific language governing permissions and |
18 | | * limitations under the License. |
19 | | */ |
20 | | #ifndef OPENDNP3_SHIFTABLEBUFFER_H |
21 | | #define OPENDNP3_SHIFTABLEBUFFER_H |
22 | | |
23 | | #include <ser4cpp/container/SequenceTypes.h> |
24 | | |
25 | | namespace opendnp3 |
26 | | { |
27 | | |
28 | | /** @section DESCRIPTION |
29 | | Implements a buffer that can shift its contents as it is read */ |
30 | | class ShiftableBuffer |
31 | | { |
32 | | public: |
33 | | /** |
34 | | * Construct the facade over the specified underlying buffer |
35 | | */ |
36 | | ShiftableBuffer(uint8_t* pBuffer_, size_t size); |
37 | | |
38 | | // ------- Functions related to reading ----------- |
39 | | |
40 | | size_t NumBytesRead() const |
41 | 588k | { |
42 | 588k | return writePos - readPos; |
43 | 588k | } |
44 | | |
45 | | /// @return Pointer to the next byte to be read in the buffer |
46 | | ser4cpp::rseq_t ReadBuffer() const |
47 | 283k | { |
48 | 283k | return ser4cpp::rseq_t(pBuffer + readPos, NumBytesRead()); |
49 | 283k | } |
50 | | |
51 | | /// Signal that some bytes don't have to be stored any longer. They'll be recovered during the next shift operation. |
52 | | void AdvanceRead(size_t aNumBytes); |
53 | | |
54 | | // ------- Functions related to writing ----------- |
55 | | |
56 | | /// Shift the buffer back to front, writing over bytes that have already been read. The objective |
57 | | /// being to free space for further writing. |
58 | | void Shift(); |
59 | | |
60 | | /// Reset the buffer to its initial state, empty |
61 | | void Reset(); |
62 | | |
63 | | /// @return Bytes of available for writing |
64 | | size_t NumWriteBytes() const |
65 | 23.1k | { |
66 | 23.1k | return M_SIZE - writePos; |
67 | 23.1k | } |
68 | | |
69 | | /// @return Pointer to the position in the buffer available for writing |
70 | | uint8_t* WriteBuff() const |
71 | 11.5k | { |
72 | 11.5k | return pBuffer + writePos; |
73 | 11.5k | } |
74 | | |
75 | | /// Signal to the buffer bytes were written to the current write position |
76 | | void AdvanceWrite(size_t numBytes); |
77 | | |
78 | | //////////////////////////////////////////// |
79 | | // Other functions |
80 | | //////////////////////////////////////////// |
81 | | |
82 | | /// Searches the read subsequence for 0x0564 sync bytes |
83 | | /// @return true if both sync bytes were found in the buffer. |
84 | | bool Sync(size_t& skipCount); |
85 | | |
86 | | private: |
87 | | uint8_t* pBuffer; |
88 | | const size_t M_SIZE; |
89 | | size_t writePos; |
90 | | size_t readPos; |
91 | | }; |
92 | | |
93 | | } // namespace opendnp3 |
94 | | |
95 | | #endif |