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 "event_handler.h" 22 #include "session_data_header.h" 23 #include <memory> 24 #include <functional> 25 #include <thread> 26 27 namespace OHOS { 28 namespace DistributedCollab { 29 30 class DataSenderReceiver { 31 public: DataSenderReceiver(const int32_t socketId)32 explicit DataSenderReceiver(const int32_t socketId) 33 : socketId_(socketId) {}; 34 ~DataSenderReceiver(); 35 36 int32_t SendBytesData(const std::shared_ptr<AVTransDataBuffer>& sendData); 37 int32_t PackRecvPacketData(const uint8_t* header, const uint32_t dataLen); 38 std::shared_ptr<AVTransDataBuffer> GetPacketedData(); 39 40 int32_t SendStreamData(const std::shared_ptr<AVTransStreamData>& sendData); 41 int32_t SendMessageData(const std::shared_ptr<AVTransDataBuffer>& sendData); 42 int32_t SendFileData(const std::vector<std::string>& sFiles, 43 const std::vector<std::string>& dFiles); 44 45 private: 46 int32_t SendUnpackData(const std::shared_ptr<AVTransDataBuffer>& sendData, const int32_t dataType); 47 int32_t SendAllPackets(const std::shared_ptr<AVTransDataBuffer> sendData, const int32_t dataType); 48 int32_t DoSendPacket(SessionDataHeader& headerPara, const uint8_t* dataHeader, const uint32_t dataLen); 49 50 int32_t CheckRecvSessionHeader(const SessionDataHeader& headerPara); 51 int32_t ProcessAllPacketRecv(const uint8_t* data, const uint32_t dataLen, 52 const SessionDataHeader& headerPara); 53 int32_t ProcessStartPacketRecv(const uint8_t* data, const uint32_t dataLen, 54 const SessionDataHeader& headerPara); 55 int32_t ProcessMidPacketRecv(const uint8_t* data, const uint32_t dataLen, 56 const SessionDataHeader& headerPara); 57 int32_t ProcessEndPacketRecv(const uint8_t* data, const uint32_t dataLen, 58 const SessionDataHeader& headerPara); 59 int32_t WriteRecvBytesDataToBuffer(const uint8_t* data, const uint32_t dataLen, 60 const SessionDataHeader& headerPara); 61 62 bool isDataReady(); 63 int64_t GetNowTimeStampUs(); 64 void ResetFlag(); 65 66 void Init(); 67 void DeInit(); 68 void StartEvent(); 69 70 private: 71 static constexpr uint32_t MAX_SEND_MESSAGE_SIZE = 4 * 1024; 72 73 private: 74 int32_t socketId_ = 0; 75 // waiting for packet with end flag 76 bool isWaiting_ = false; 77 uint32_t nowSeqNum_ = 0; 78 uint32_t nowSubSeq_ = 0; 79 uint32_t nowTotalLen_ = 0; 80 std::unique_ptr<AVTransDataBuffer> packBuffer_ = nullptr; 81 uint8_t* currentPos = nullptr; 82 83 std::mutex eventMutex_; 84 std::thread eventThread_; 85 std::shared_ptr<OHOS::AppExecFwk::EventHandler> eventHandler_; 86 std::condition_variable eventCon_; 87 std::once_flag initFlag_; 88 }; 89 } // namespace DistributedCollab 90 } // namespace OHOS 91 #endif