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