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 CheckStatus();
42     Status OpenConnect(const ISocketListener *listener);
43     Status SendData(const DataInfo &dataInfo, const ISocketListener *listener);
44     bool operator==(int32_t socket) const;
45     bool operator==(const std::string &deviceId) const;
46     uint32_t GetMtuSize() const;
47     uint32_t GetTimeout() const;
48     Time GetExpireTime() const;
49     int32_t GetSocket() const;
50     uint32_t GetQoSType() const;
51     void UpdateExpireTime();
52     int32_t GetSoftBusError();
53 
54 private:
55     int32_t 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     int32_t softBusError_ = 0;
89 };
90 } // namespace OHOS::AppDistributedKv
91 
92 #endif // DISTRIBUTEDDATAMGR_DATAMGR_SERVICE_SOFTBUS_CLIENT_H