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 #include "stream_common_data.h"
17 
18 #include "common_inner.h"
19 
20 namespace Communication {
21 namespace SoftBus {
MakeCommonStream(StreamData & data,const StreamFrameInfo & info)22 std::unique_ptr<IStream> IStream::MakeCommonStream(StreamData &data, const StreamFrameInfo &info)
23 {
24     auto stream = std::make_unique<StreamCommonData>(info.streamId, info.seqNum, info);
25     stream->InitStreamData(std::move(data.buffer), data.bufLen, std::move(data.extBuffer), data.extLen);
26 
27     return stream;
28 }
29 
StreamCommonData(uint32_t streamId,uint16_t seq,const StreamFrameInfo & frameInfo)30 StreamCommonData::StreamCommonData(uint32_t streamId, uint16_t seq, const StreamFrameInfo& frameInfo)
31 {
32     curSeqNum_ = seq;
33     curStreamId_ = streamId;
34     streamFrameInfo_ = frameInfo;
35 }
36 
InitStreamData(std::unique_ptr<char[]> inputBuf,ssize_t bufSize,std::unique_ptr<char[]> inputExt,ssize_t extSize)37 int StreamCommonData::InitStreamData(std::unique_ptr<char[]> inputBuf, ssize_t bufSize,
38     std::unique_ptr<char[]> inputExt, ssize_t extSize)
39 {
40     if (inputBuf == nullptr) {
41         TRANS_LOGE(TRANS_STREAM, "InitStreamData: Stream MUST not be null");
42         return -1;
43     }
44     streamData_ = std::move(inputBuf);
45     streamLen_ = bufSize;
46 
47     if (inputExt == nullptr) {
48         extBuf_ = nullptr;
49         extBufLen_ = 0;
50     } else {
51         extBuf_ = std::move(inputExt);
52         extBufLen_ = extSize;
53     }
54 
55     return 0;
56 }
57 } // namespace SoftBus
58 } // namespace Communication
59