• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 FILEMANAGEMENT_DFS_SERVICE_CHANNEL_MANAGER_H
17 #define FILEMANAGEMENT_DFS_SERVICE_CHANNEL_MANAGER_H
18 
19 #include "control_cmd_parser.h"
20 #include "dfs_error.h"
21 #include "event_handler.h"
22 #include "single_instance.h"
23 #include "socket.h"
24 
25 #include <atomic>
26 #include <map>
27 #include <memory>
28 #include <mutex>
29 #include <optional>
30 #include <shared_mutex>
31 #include <string>
32 #include <thread>
33 #include <vector>
34 
35 namespace OHOS {
36 namespace Storage {
37 namespace DistributedFile {
38 
39 class ChannelManager final {
40     DECLARE_SINGLE_INSTANCE_BASE(ChannelManager);
41 
42 public:
43     int32_t Init();
44     void DeInit();
45 
46     int32_t CreateClientChannel(const std::string &networkId);
47     int32_t DestroyClientChannel(const std::string &networkId);
48     bool HasExistChannel(const std::string &networkId);
49 
50     int32_t SendRequest(
51         const std::string &networkId, ControlCmd &request, ControlCmd &response, bool needResponse = false);
52     int32_t SendBytes(const std::string &networkId, const std::string &data);
53 
54     void OnSocketError(int32_t socketId, const int32_t errorCode);
55     void OnSocketConnected(int32_t socketId, const PeerSocketInfo &info);
56     void OnSocketClosed(int32_t socketId, const ShutdownReason &reason);
57     void OnBytesReceived(int32_t socketId, const void *data, const uint32_t dataLen);
58     bool OnNegotiate2(int32_t socket, PeerSocketInfo info, SocketAccessInfo *peerInfo, SocketAccessInfo *localInfo);
59 
60 private:
61     explicit ChannelManager() = default;
62     ~ChannelManager();
63 
64     void StartEvent();
65     void StartCallbackEvent();
66     int32_t PostTask(const AppExecFwk::InnerEvent::Callback &callback, const AppExecFwk::EventQueue::Priority priority);
67     int32_t PostCallbackTask(const AppExecFwk::InnerEvent::Callback &callback,
68                              const AppExecFwk::EventQueue::Priority priority);
69 
70     int32_t CreateServerSocket();
71     int32_t CreateClientSocket(const std::string &peerNetworkId);
72 
73     int32_t DoSendBytes(const std::int32_t socketId, const std::string &data);
74 
75     void HandleRemoteBytes(const std::string &jsonStr, int32_t socketId);
76     void DoSendBytesAsync(const ControlCmd &request, const std::string &networkId);
77 
78 private:
79     std::mutex initMutex_;
80     int32_t serverSocketId_ = -1;
81     std::string ownerName_ = "dfs_channel_manager";
82 
83     // networkId -> socketId (client)
84     std::shared_mutex clientMutex_;
85     std::map<std::string, int32_t> clientNetworkSocketMap_;
86 
87     // networkId -> socketId (server)
88     std::shared_mutex serverMutex_;
89     std::map<std::string, int32_t> serverNetworkSocketMap_;
90 
91     // for send data
92     std::mutex eventMutex_;
93     std::thread eventThread_;
94     std::shared_ptr<OHOS::AppExecFwk::EventHandler> eventHandler_;
95     std::condition_variable eventCon_;
96 
97     // for receive data and softbus callback
98     std::mutex callbackEventMutex_;
99     std::thread callbackEventThread_;
100     std::shared_ptr<OHOS::AppExecFwk::EventHandler> callbackEventHandler_;
101     std::condition_variable callbackEventCon_;
102 
103     struct ResponseWaiter {
104         std::mutex mutex;
105         std::condition_variable cv;
106         ControlCmd response;
107         bool received = false;
108     };
109 
110     // for control cmd, msgId -> ResponseWaiter
111     std::mutex mtx_;
112     std::condition_variable cmdEventCon_;
113     std::unordered_map<int32_t, std::shared_ptr<ResponseWaiter>> pendingResponses_;
114 };
115 } // namespace DistributedFile
116 } // namespace Storage
117 } // namespace OHOS
118 #endif // FILEMANAGEMENT_DFS_SERVICE_CHANNEL_MANAGER_H