1 /* 2 * Copyright (c) 2024 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 OHOS_DSCHED_COLLAB_DATA_SENDER_RECEIVER_H 17 #define OHOS_DSCHED_COLLAB_DATA_SENDER_RECEIVER_H 18 19 #include "av_trans_data_buffer.h" 20 #include "av_trans_stream_data.h" 21 #include "session_data_header.h" 22 #include <atomic> 23 #include <memory> 24 25 namespace OHOS { 26 namespace DistributedCollab { 27 28 class DataSenderReceiver { 29 public: DataSenderReceiver(const int32_t socketId)30 explicit DataSenderReceiver(const int32_t socketId) 31 : socketId_(socketId) {}; 32 ~DataSenderReceiver() = default; 33 34 int32_t SendBytesData(const std::shared_ptr<AVTransDataBuffer>& sendData); 35 int32_t PackRecvPacketData(const uint8_t* header, const uint32_t dataLen); 36 std::shared_ptr<AVTransDataBuffer> GetPacketedData(); 37 38 int32_t SendStreamData(const std::shared_ptr<AVTransStreamData>& sendData); 39 int32_t SendMessageData(const std::shared_ptr<AVTransDataBuffer>& sendData); 40 int32_t SendFileData(const std::vector<std::string>& sFiles, 41 const std::vector<std::string>& dFiles); 42 43 private: 44 int32_t SendUnpackData(const std::shared_ptr<AVTransDataBuffer>& sendData, const int32_t dataType); 45 int32_t SendAllPackets(const std::shared_ptr<AVTransDataBuffer> sendData, const int32_t dataType); 46 int32_t DoSendPacket(SessionDataHeader& headerPara, const uint8_t* dataHeader, const uint32_t dataLen); 47 48 int32_t CheckRecvSessionHeader(const SessionDataHeader& headerPara); 49 int32_t ProcessAllPacketRecv(const uint8_t* data, const uint32_t dataLen, 50 const SessionDataHeader& headerPara); 51 int32_t ProcessStartPacketRecv(const uint8_t* data, const uint32_t dataLen, 52 const SessionDataHeader& headerPara); 53 int32_t ProcessMidPacketRecv(const uint8_t* data, const uint32_t dataLen, 54 const SessionDataHeader& headerPara); 55 int32_t ProcessEndPacketRecv(const uint8_t* data, const uint32_t dataLen, 56 const SessionDataHeader& headerPara); 57 int32_t WriteRecvBytesDataToBuffer(const uint8_t* data, const uint32_t dataLen, 58 const SessionDataHeader& headerPara); 59 60 bool isDataReady(); 61 int64_t GetNowTimeStampUs(); 62 void ResetFlag(); 63 64 private: 65 static constexpr uint32_t MAX_SEND_MESSAGE_SIZE = 4 * 1024; 66 67 private: 68 int32_t socketId_ = 0; 69 // waiting for packet with end flag 70 bool isWaiting_ = false; 71 uint32_t nowSeqNum_ = 0; 72 uint32_t nowSubSeq_ = 0; 73 uint32_t nowTotalLen_ = 0; 74 std::unique_ptr<AVTransDataBuffer> packBuffer_ = nullptr; 75 uint8_t* currentPos = nullptr; 76 }; 77 } // namespace DistributedCollab 78 } // namespace OHOS 79 #endif