• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "socket.h"
26 #include "softbus_bus_center.h"
27 namespace OHOS::AppDistributedKv {
28 class SoftBusClient : public std::enable_shared_from_this<SoftBusClient> {
29 public:
30     enum QoSType {
31         QOS_BR,
32         QOS_HML,
33         QOS_REUSE,
34         QOS_BUTT
35     };
36     SoftBusClient(const PipeInfo &pipeInfo, const DeviceId &deviceId, const std::string& networkId,
37         uint32_t type = QOS_HML);
38     ~SoftBusClient();
39 
40     using Time = std::chrono::steady_clock::time_point;
41     using Duration = std::chrono::steady_clock::duration;
42     Status CheckStatus();
43     Status OpenConnect(const ISocketListener *listener);
44     Status SendData(const DataInfo &dataInfo, const ISocketListener *listener);
45     bool operator==(int32_t socket) const;
46     bool operator==(const std::string &deviceId) const;
47     uint32_t GetMtuSize() const;
48     uint32_t GetTimeout() const;
49     Time GetExpireTime() const;
50     int32_t GetSocket() const;
51     uint32_t GetQoSType() const;
52     void UpdateExpireTime(bool async = true);
53     int32_t GetSoftBusError();
54     Status ReuseConnect(const ISocketListener *listener);
55     const std::string& GetNetworkId() const;
56 
57 private:
58     int32_t Open(int32_t socket, uint32_t type, const ISocketListener *listener, bool async = true);
59     std::pair<int32_t, uint32_t> GetMtu(int32_t socket);
60     Time CalcExpireTime() const;
61     int32_t CreateSocket() const;
62     void UpdateBindInfo(int32_t socket, uint32_t mtu, int32_t status, bool async = true);
63 
64     static constexpr int32_t INVALID_SOCKET_ID = -1;
65     static constexpr uint32_t DEFAULT_TIMEOUT = 30 * 1000;
66     static constexpr uint32_t DEFAULT_MTU_SIZE = 4096 * 1024u;
67     static constexpr Duration BR_CLOSE_DELAY = std::chrono::seconds(5);
68     static constexpr Duration HML_CLOSE_DELAY = std::chrono::seconds(3);
69     static constexpr Duration MAX_DELAY = std::chrono::seconds(20);
70     static constexpr uint32_t QOS_COUNT = 3;
71     static constexpr uint32_t BR_QOS_COUNT = 3;
72     static constexpr uint32_t HML_QOS_COUNT = 2;
73     static constexpr uint32_t REUSE_QOS_COUNT = 1;
74     static constexpr QosTV QOS_INFOS[QOS_BUTT][QOS_COUNT] = {
75         { // BR QOS
76             QosTV{ .qos = QOS_TYPE_MIN_BW, .value = 0x5a5a5a5a },
77             QosTV{ .qos = QOS_TYPE_MAX_LATENCY, .value = 15000 },
78             QosTV{ .qos = QOS_TYPE_MIN_LATENCY, .value = 1600 }
79         },
80         { // HML QOS
81             QosTV{ .qos = QOS_TYPE_MAX_LATENCY, .value = 10000 },
82             QosTV{ .qos = QOS_TYPE_MIN_LATENCY, .value = 2000 }
83         },
84         { // REUSE_QOS
85             QosTV{ .qos = QOS_TYPE_REUSE_BE, .value = 1 }
86         }
87     };
88     static constexpr uint32_t QOS_COUNTS[QOS_BUTT] = { BR_QOS_COUNT, HML_QOS_COUNT, REUSE_QOS_COUNT };
89     std::atomic_bool isOpening_ = false;
90     mutable std::mutex mutex_;
91     uint32_t type_ = QOS_HML;
92     PipeInfo pipe_;
93     DeviceId device_;
94     uint32_t mtu_;
95     Time expireTime_ = std::chrono::steady_clock::now() + MAX_DELAY;
96 
97     int32_t socket_ = INVALID_SOCKET_ID;
98     int32_t bindState_ = -1;
99     int32_t softBusError_ = 0;
100     const std::string networkId_;
101 };
102 } // namespace OHOS::AppDistributedKv
103 
104 #endif // DISTRIBUTEDDATAMGR_DATAMGR_SERVICE_SOFTBUS_CLIENT_H