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