• 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 <map>
20 #include <mutex>
21 
22 #include "commu_types.h"
23 #include "communication_strategy.h"
24 #include "executor_pool.h"
25 #include "session.h"
26 #include "softbus_bus_center.h"
27 namespace OHOS::AppDistributedKv {
28 class SoftBusClient {
29 public:
30     SoftBusClient(const PipeInfo &pipeInfo, const DeviceId &deviceId,
31         const std::function<int32_t(int32_t)> &getConnStatus);
32     ~SoftBusClient();
33 
34     using Strategy = CommunicationStrategy::Strategy;
35     Status Send(const DataInfo &dataInfo, uint32_t totalLength);
36     bool operator==(int32_t connId) const;
37     bool operator==(const std::string &deviceId) const;
38     uint32_t GetMtuSize() const;
39     void AfterStrategyUpdate(Strategy strategy);
40 private:
41     enum class ConnectStatus : int32_t {
42         CONNECT_OK,
43         DISCONNECT,
44     };
45 
46     using Time = std::chrono::steady_clock::time_point;
47     Status OpenConnect(uint32_t totalLength);
48     Status SwitchChannel(uint32_t totalLength);
49     Status CreateChannel(uint32_t totalLength);
50     Status Open(SessionAttribute attr);
51     SessionAttribute GetSessionAttribute(bool isP2p);
52     void RestoreDefaultValue();
53     void UpdateMtuSize();
54     void CloseP2pSessions();
55     void UpdateP2pFinishTime(int32_t connId, uint32_t dataLength);
56 
57     static constexpr int32_t INVALID_CONNECT_ID = -1;
58     static constexpr uint32_t WAIT_MAX_TIME = 10;
59     static constexpr uint32_t DEFAULT_MTU_SIZE = 4096u;
60     static constexpr uint32_t P2P_SIZE_THRESHOLD = 0x10000u; // 64KB
61     static constexpr uint32_t P2P_TRANSFER_PER_MICROSECOND = 10; // 10 bytes per microsecond
62     static constexpr float SWITCH_DELAY_FACTOR = 0.6f;
63     static constexpr std::chrono::steady_clock::duration P2P_CLOSE_DELAY = std::chrono::seconds(3);
64     int32_t connId_ = INVALID_CONNECT_ID;
65     int32_t routeType_ = RouteType::INVALID_ROUTE_TYPE;
66     Strategy strategy_ = Strategy::DEFAULT;
67     ConnectStatus status_ = ConnectStatus::DISCONNECT;
68     std::mutex mutex_;
69     std::mutex taskMutex_;
70     PipeInfo pipe_;
71     DeviceId device_;
72     uint32_t mtu_;
73     ConcurrentMap<int32_t, Time> p2pFinishTime_;
74     ExecutorPool::TaskId closeP2pTaskId_ = ExecutorPool::INVALID_TASK_ID;
75     std::function<int32_t(int32_t)> getConnStatus_;
76 };
77 }
78 
79 
80 #endif // DISTRIBUTEDDATAMGR_DATAMGR_SERVICE_SOFTBUS_CLIENT_H
81