1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef DISTRIBUTEDDATAMGR_DATA_BUFFER_H
17 #define DISTRIBUTEDDATAMGR_DATA_BUFFER_H
18 
19 #include <cstdint>
20 #include <string>
21 
22 namespace OHOS {
23 namespace AppDistributedKv {
24 struct HeaderInfo {
25     uint32_t type;
26     uint32_t length;
27     uint32_t version;
28     uint32_t sequence;
29 } __attribute__((packed));
30 
31 class DataBuffer {
32 public:
33     DataBuffer();
34 
35     ~DataBuffer();
36 
37     bool Init(size_t size);
38 
39     const char *GetBufPtr() const;
40 
41     size_t GetBufSize() const;
42 
43     void SetBufUsed(size_t used);
44 
45     size_t GetBufUsed() const;
46 
47     // header length
48     static const uint32_t HEADER_LEN = sizeof(HeaderInfo);
49 
50 // max size for transferring data using pipe is 5M
51     static constexpr size_t MAX_DATA_LEN = 1024 * 1024 * 5;
52 
53 // 5M; max size for transfer using pipe (12 is header length, rest is data wait for transferring)
54     static constexpr uint32_t MAX_TRANSFER_SIZE = 1024 * 1024 * 5 - HEADER_LEN;
55 
56     static constexpr uint32_t VERSION = 0;
57 
58     static constexpr uint32_t TYPE = 0;
59 
60 private:
61     char *buf_;
62     size_t size_;
63     size_t used_;
64     static int sequence_;
65 };
66 
67 union Head {
68     HeaderInfo headerInfo;
69     uint8_t headArray[DataBuffer::HEADER_LEN];
70 } __attribute__((packed));
71 } // namespace AppDistributedKv
72 } // namespace OHOS
73 #endif // DISTRIBUTEDDATAMGR_DATA_BUFFER_H
74