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 CLIENT_TRANS_UDP_STREAM_ADAPTOR_H_ 17 #define CLIENT_TRANS_UDP_STREAM_ADAPTOR_H_ 18 19 #include <atomic> 20 #include <sys/types.h> 21 22 #include "client_trans_udp_stream_interface.h" 23 #include "i_stream_manager.h" 24 #include "softbus_adapter_crypto.h" 25 #include "stream_common.h" 26 27 namespace OHOS { 28 class StreamAdaptor : public std::enable_shared_from_this<StreamAdaptor> { 29 public: 30 StreamAdaptor() = delete; 31 explicit StreamAdaptor(const std::string &pkgName); 32 ~StreamAdaptor() = default; 33 34 static ssize_t Encrypt(const void *in, ssize_t inLen, void *out, ssize_t outLen, const char* sessionKey); 35 static ssize_t Decrypt(const void *in, ssize_t inLen, void *out, ssize_t outLen, const char *sessionKey); 36 static ssize_t GetEncryptOverhead(); 37 int GetStreamType(); 38 const char *GetSessionKey(); 39 int64_t GetChannelId(); 40 const IStreamListener *GetListenerCallback(); 41 std::shared_ptr<Communication::SoftBus::IStreamManager> GetStreamManager(); 42 void SetAliveState(bool state); 43 void InitAdaptor(int32_t channelId, const VtpStreamOpenParam *param, bool isServerSide, 44 const IStreamListener *callback); 45 void ReleaseAdaptor(); 46 bool GetAliveState(); 47 48 private: 49 int64_t channelId_ = -1; 50 std::atomic<bool> aliveState_ = {false}; 51 std::shared_ptr<Communication::SoftBus::IStreamManager> streamManager_ = nullptr; 52 int streamType_ = StreamType::INVALID; 53 bool serverSide_; 54 std::string pkgName_ {}; 55 std::string sessionKey_; 56 const IStreamListener *callback_ = nullptr; 57 std::atomic<bool> enableState_ = {false}; 58 }; 59 } // namespace OHOS 60 61 #endif // !defined(CLIENT_TRANS_UDP_STREAM_ADAPTOR_H_