• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 STREAM_PACKETIZER_H
17 #define STREAM_PACKETIZER_H
18 
19 #include "i_stream.h"
20 
21 namespace Communication {
22 namespace SoftBus {
23 class StreamPacketizer {
24 public:
StreamPacketizer(int streamType,std::unique_ptr<IStream> data)25     StreamPacketizer(int streamType, std::unique_ptr<IStream> data)
26     {
27         originData_ = std::move(data);
28         streamType_ = streamType;
29     }
30     virtual ~StreamPacketizer() = default;
31 
32     ssize_t CalculateHeaderSize() const;
33     ssize_t CalculateExtSize(ssize_t extSize) const;
34 
35     std::unique_ptr<char[]> PacketizeStream();
GetPacketLen()36     ssize_t GetPacketLen() const
37     {
38         return hdrSize_ + dataSize_ + extSize_;
39     }
40 
GetHeaderLen()41     ssize_t GetHeaderLen() const
42     {
43         return hdrSize_ + extSize_;
44     }
45 
46 private:
47     ssize_t hdrSize_ = 0;
48     ssize_t dataSize_ = 0;
49     ssize_t extSize_ = 0;
50     std::unique_ptr<IStream> originData_ = nullptr;
51     int streamType_ = INVALID;
52 };
53 } // namespace SoftBus
54 } // namespace Communication
55 
56 #endif
57