• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 OHOS_FILEMGMT_SOFTBUS_ADAPTER_H
17 #define OHOS_FILEMGMT_SOFTBUS_ADAPTER_H
18 
19 #include <memory>
20 #include <shared_mutex>
21 #include <string>
22 #include <unordered_map>
23 #include <vector>
24 
25 #include "nocopyable.h"
26 #include "session.h"
27 #include "socket.h"
28 
29 #include "i_softbus_listener.h"
30 
31 namespace OHOS::FileManagement::CloudSync {
32 class SoftbusAdapter : public NoCopyable {
33 public:
34     static SoftbusAdapter &GetInstance();
35 
36     int32_t CreateSessionServer(const char *packageName, const char *sessionName);
37     int32_t RemoveSessionServer(const char *packageName, const char *sessionName);
38 
39     int OpenSession(char *sessionName,
40                     char *peerDeviceId,
41                     char *groupId,
42                     TransDataType dataType);
43 
44     int OpenSessionByP2P(char *sessionName, char *peerDeviceId, char *groupId, bool isFileType);
45     void CloseSession(int sessionId);
46 
47     static void OnBind(int socket, PeerSocketInfo info);
48     static void OnShutdown(int32_t socket, ShutdownReason reason);
49     static void OnFile(int32_t socket, FileEvent *event);
50     static void OnBytes(int sessionId, const void *data, unsigned int dataLen);
51     static int OnReceiveFileProcess(int sessionId, const char *firstFile, uint64_t bytesUpload, uint64_t bytesTotal);
52     static void OnReceiveFileFinished(int sessionId, const char *files, int fileCnt);
53     static const char* GetRecvPath();
54     static void UpdateFileRecvPath(const std::string &bundleName, int32_t userId);
55 
56     int SendBytes(int sessionId, const void *data, unsigned int dataLen);
57     int SendFile(int sessionId, const std::vector<std::string> &sFileList, const std::vector<std::string> &dFileList);
58     std::string GetSessionNameFromMap(int sessionId);
59     std::string GetPeerNetworkId(int sessionId);
60     int32_t GetSocketNameFromMap(std::string sessionAndPack);
61     bool IsFromServer(int sessionId);
62 
63     void RegisterSessionListener(std::string sessionName, std::shared_ptr<ISoftbusListener> listener);
64     void UnRegisterSessionListener(std::string sessionName);
65 
66     std::shared_ptr<ISoftbusListener> GetListener(std::string sessionName);
67     bool IsSessionOpened(int sessionId);
68     void AcceptSesion(int sessionId, const std::string &sessionName, const std::string &networkId);
69     void RemoveSesion(int sessionId);
70 
71 private:
72     SoftbusAdapter() = default;
73     std::unordered_map<std::string, std::shared_ptr<ISoftbusListener>> listeners_;
74     std::shared_mutex listenerMutex_;
75 
76     std::mutex sessionMutex_;
77     std::unordered_map<int32_t, bool> sessionOpenedMap_;
78     std::unordered_map<int32_t, std::string> sessionNameMap_;
79     std::unordered_map<int32_t, std::string> networkIdMap_;
80     std::unordered_map<int32_t, std::string> sessionAndPackageMap_;
81     static constexpr const int QOS_COUNT = 3;
82     static inline std::string pathDir_;
83 };
84 } // namespace OHOS::FileManagement::CloudSync
85 
86 #endif // OHOS_FILEMGMT_SOFTBUS_ADAPTER_H