1 /* 2 * Copyright (c) 2022 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 DISTRIBUTEDDATAMGR_DATAMGR_SERVICE_SOFTBUS_CLIENT_H 17 #define DISTRIBUTEDDATAMGR_DATAMGR_SERVICE_SOFTBUS_CLIENT_H 18 19 #include <atomic> 20 #include <mutex> 21 22 #include "commu_types.h" 23 #include "serializable/serializable.h" 24 #include "socket.h" 25 26 namespace OHOS::AppDistributedKv { 27 class SoftBusClient : public std::enable_shared_from_this<SoftBusClient> { 28 public: 29 struct AccessExtraInfo final : public DistributedData::Serializable { 30 std::string bundleName = ""; 31 std::string accountId = ""; 32 std::string storeId = ""; 33 AccessExtraInfofinal34 AccessExtraInfo() {}; ~AccessExtraInfofinal35 ~AccessExtraInfo() {}; Marshalfinal36 bool Marshal(json &node) const override 37 { 38 SetValue(node[GET_NAME(bundleName)], bundleName); 39 SetValue(node[GET_NAME(accountId)], accountId); 40 SetValue(node[GET_NAME(storeId)], storeId); 41 return true; 42 }; Unmarshalfinal43 bool Unmarshal(const json &node) override 44 { 45 GetValue(node, GET_NAME(bundleName), bundleName); 46 GetValue(node, GET_NAME(accountId), accountId); 47 GetValue(node, GET_NAME(storeId), storeId); 48 return true; 49 }; 50 }; 51 52 enum QoSType { 53 QOS_BR, 54 QOS_HML, 55 QOS_REUSE, 56 QOS_BUTT 57 }; 58 59 SoftBusClient(const PipeInfo &pipeInfo, const DeviceId &deviceId, const std::string& networkId, 60 uint32_t type = QOS_HML, const SessionAccessInfo &accessInfo = {}); 61 ~SoftBusClient(); 62 63 using Time = std::chrono::steady_clock::time_point; 64 using Duration = std::chrono::steady_clock::duration; 65 Status CheckStatus(); 66 Status OpenConnect(const ISocketListener *listener); 67 Status SendData(const DataInfo &dataInfo); 68 bool operator==(int32_t socket) const; 69 bool operator==(const std::string &deviceId) const; 70 uint32_t GetMtuBuffer() const; 71 uint32_t GetTimeout() const; 72 Time GetExpireTime() const; 73 int32_t GetSocket() const; 74 uint32_t GetQoSType() const; 75 int32_t GetSoftBusError(); 76 Status ReuseConnect(const ISocketListener *listener); 77 std::string GetNetworkId() const; 78 void UpdateNetworkId(const std::string &networkId); 79 80 private: 81 int32_t Open(int32_t socket, uint32_t type, const ISocketListener *listener, bool async = true); 82 std::pair<int32_t, uint32_t> GetMtu(int32_t socket); 83 Time CalcExpireTime() const; 84 int32_t CreateSocket() const; 85 void UpdateBindInfo(int32_t socket, uint32_t mtu, int32_t status, bool async = true); 86 87 static constexpr int32_t INVALID_SOCKET_ID = -1; 88 static constexpr uint32_t DEFAULT_TIMEOUT = 30 * 1000; 89 static constexpr uint32_t DEFAULT_MTU_SIZE = 4096 * 1024u; 90 static constexpr Duration BR_CLOSE_DELAY = std::chrono::seconds(5); 91 static constexpr Duration HML_CLOSE_DELAY = std::chrono::seconds(3); 92 static constexpr Duration MAX_DELAY = std::chrono::seconds(20); 93 static constexpr uint32_t QOS_COUNT = 3; 94 static constexpr uint32_t BR_QOS_COUNT = 3; 95 static constexpr uint32_t HML_QOS_COUNT = 2; 96 static constexpr uint32_t REUSE_QOS_COUNT = 1; 97 static constexpr QosTV QOS_INFOS[QOS_BUTT][QOS_COUNT] = { 98 { // BR QOS 99 QosTV{ .qos = QOS_TYPE_MIN_BW, .value = 0x5a5a5a5a }, 100 QosTV{ .qos = QOS_TYPE_MAX_LATENCY, .value = 15000 }, 101 QosTV{ .qos = QOS_TYPE_MIN_LATENCY, .value = 1600 } 102 }, 103 { // HML QOS 104 QosTV{ .qos = QOS_TYPE_MAX_LATENCY, .value = 10000 }, 105 QosTV{ .qos = QOS_TYPE_MIN_LATENCY, .value = 2000 } 106 }, 107 { // REUSE_QOS 108 QosTV{ .qos = QOS_TYPE_REUSE_BE, .value = 1 } 109 } 110 }; 111 static constexpr uint32_t QOS_COUNTS[QOS_BUTT] = { BR_QOS_COUNT, HML_QOS_COUNT, REUSE_QOS_COUNT }; 112 std::atomic_bool isOpening_ = false; 113 mutable std::mutex mutex_; 114 mutable std::mutex networkIdMutex_; 115 uint32_t type_ = QOS_HML; 116 PipeInfo pipe_; 117 DeviceId device_; 118 uint32_t mtu_; 119 Time expireTime_ = std::chrono::steady_clock::now() + MAX_DELAY; 120 121 int32_t socket_ = INVALID_SOCKET_ID; 122 int32_t bindState_ = -1; 123 int32_t softBusError_ = 0; 124 std::string networkId_; 125 SessionAccessInfo accessInfo_; 126 }; 127 } // namespace OHOS::AppDistributedKv 128 #endif // DISTRIBUTEDDATAMGR_DATAMGR_SERVICE_SOFTBUS_CLIENT_H