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 <map> 21 #include <mutex> 22 23 #include "commu_types.h" 24 #include "executor_pool.h" 25 #include "session.h" 26 #include "socket.h" 27 #include "softbus_bus_center.h" 28 namespace OHOS::AppDistributedKv { 29 class SoftBusClient : public std::enable_shared_from_this<SoftBusClient> { 30 public: 31 enum QoSType { 32 QOS_BR, 33 QOS_HML, 34 QOS_BUTT 35 }; 36 SoftBusClient(const PipeInfo &pipeInfo, const DeviceId &deviceId, uint32_t type = QOS_HML); 37 ~SoftBusClient(); 38 39 using Time = std::chrono::steady_clock::time_point; 40 using Duration = std::chrono::steady_clock::duration; 41 Status SendData(const DataInfo &dataInfo, const ISocketListener *listener); 42 bool operator==(int32_t socket) const; 43 bool operator==(const std::string &deviceId) const; 44 uint32_t GetMtuSize() const; 45 uint32_t GetTimeout() const; 46 Time GetExpireTime() const; 47 int32_t GetSocket() const; 48 uint32_t GetQoSType() const; 49 void UpdateExpireTime(); 50 51 private: 52 Status OpenConnect(const ISocketListener *listener); 53 Status Open(int32_t socket, const QosTV qos[], const ISocketListener *listener); 54 std::pair<int32_t, uint32_t> GetMtu(int32_t socket); 55 Time CalcExpireTime() const; 56 57 static constexpr int32_t INVALID_SOCKET_ID = -1; 58 static constexpr uint32_t DEFAULT_TIMEOUT = 30 * 1000; 59 static constexpr uint32_t DEFAULT_MTU_SIZE = 4096u; 60 static constexpr Duration BR_CLOSE_DELAY = std::chrono::seconds(5); 61 static constexpr Duration HML_CLOSE_DELAY = std::chrono::seconds(3); 62 static constexpr Duration MAX_DELAY = std::chrono::seconds(60 * 60 * 24 * 365); 63 static constexpr uint32_t QOS_COUNT = 3; 64 static constexpr QosTV QOS_INFOS[QOS_BUTT][QOS_COUNT] = { 65 { // BR QOS 66 QosTV{ .qos = QOS_TYPE_MIN_BW, .value = 0x5a5a5a5a }, 67 QosTV{ .qos = QOS_TYPE_MAX_LATENCY, .value = 15000 }, 68 QosTV{ .qos = QOS_TYPE_MIN_LATENCY, .value = 1600 } 69 }, 70 { // HML QOS 71 QosTV{ .qos = QOS_TYPE_MIN_BW, .value = 64 * 1024 }, 72 QosTV{ .qos = QOS_TYPE_MAX_LATENCY, .value = 15000 }, 73 QosTV{ .qos = QOS_TYPE_MIN_LATENCY, .value = 1600 } 74 } 75 }; 76 std::atomic_bool isOpening_ = false; 77 mutable std::mutex mutex_; 78 uint32_t type_ = QOS_HML; 79 PipeInfo pipe_; 80 DeviceId device_; 81 uint32_t mtu_; 82 Time expireTime_ = std::chrono::steady_clock::now() + MAX_DELAY; 83 84 int32_t socket_ = INVALID_SOCKET_ID; 85 int32_t bindState_ = -1; 86 }; 87 } // namespace OHOS::AppDistributedKv 88 89 #endif // DISTRIBUTEDDATAMGR_DATAMGR_SERVICE_SOFTBUS_CLIENT_H