1 /* 2 * Copyright (c) 2023 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 55 int SendBytes(int sessionId, const void *data, unsigned int dataLen); 56 int SendFile(int sessionId, const std::vector<std::string> &sFileList, const std::vector<std::string> &dFileList); 57 std::string GetSessionNameFromMap(int sessionId); 58 std::string GetPeerNetworkId(int sessionId); 59 int32_t GetSocketNameFromMap(std::string sessionAndPack); 60 bool IsFromServer(int sessionId); 61 62 void RegisterSessionListener(std::string sessionName, std::shared_ptr<ISoftbusListener> listener); 63 void UnRegisterSessionListener(std::string sessionName); 64 65 std::shared_ptr<ISoftbusListener> GetListener(std::string sessionName); 66 bool IsSessionOpened(int sessionId); 67 void AcceptSesion(int sessionId, const std::string &sessionName, const std::string &networkId); 68 void RemoveSesion(int sessionId); 69 70 private: 71 SoftbusAdapter() = default; 72 std::unordered_map<std::string, std::shared_ptr<ISoftbusListener>> listeners_; 73 std::shared_mutex listenerMutex_; 74 75 std::mutex sessionMutex_; 76 std::unordered_map<int32_t, bool> sessionOpenedMap_; 77 std::unordered_map<int32_t, std::string> sessionNameMap_; 78 std::unordered_map<int32_t, std::string> networkIdMap_; 79 std::unordered_map<int32_t, std::string> sessionAndPackageMap_; 80 static constexpr const int QOS_COUNT = 3; 81 }; 82 } // namespace OHOS::FileManagement::CloudSync 83 84 #endif // OHOS_FILEMGMT_SOFTBUS_ADAPTER_H