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_SESSION_MANAGER_H 17 #define OHOS_FILEMGMT_SESSION_MANAGER_H 18 19 #include <functional> 20 #include <map> 21 #include <memory> 22 #include <mutex> 23 #include <stdexcept> 24 #include <string> 25 #include <vector> 26 27 #include "i_softbus_listener.h" 28 #include "sync_rule/i_user_status_observer.h" 29 #include "softbus_session.h" 30 namespace OHOS::FileManagement::CloudSync { 31 class RecieveDataHandler; 32 class SessionManager : public ISoftbusListener, public std::enable_shared_from_this<SessionManager>, 33 public IUserStatusObserver { 34 public: 35 SessionManager() = default; 36 ~SessionManager(); 37 38 void Init(); 39 40 int32_t SendData(const std::string &peerNetworkId, const void *data, uint32_t dataLen); 41 int32_t SendData(int sessionId, const void *data, uint32_t dataLen); 42 int32_t SendFile(const std::string &peerNetworkId, 43 const std::vector<std::string> &sFileList, 44 const std::vector<std::string> &dFileList); 45 46 void ReleaseSession(SoftbusSession::DataType type, const std::string &peerDeviceId); 47 48 void OnSessionOpened(int socket, int result) override; 49 void OnSessionClosed(int socket) override; 50 void OnDataReceived(const std::string &senderNetworkId, 51 int receiverSessionId, 52 const void *data, 53 unsigned int dataLen) override; 54 void OnFileReceived(const std::string &senderNetworkId, const char *filePath, int result) override; 55 56 void RegisterDataHandler(std::shared_ptr<RecieveDataHandler> handler); 57 void SetFileRecvPath(const std::string &bundleName, int32_t userId); 58 void OnUserUnlocked() override; 59 60 private: 61 void CreateServer(); 62 void RemoveServer(); 63 64 std::shared_ptr<SoftbusSession> CreateSession(SoftbusSession::DataType type, const std::string &peerDeviceId); 65 std::shared_ptr<SoftbusSession> GetSendSession(SoftbusSession::DataType type, const std::string &peerDeviceId); 66 void CacheSendSession(std::shared_ptr<SoftbusSession> session); 67 void RemoveSendSession(int sessionId); 68 69 bool IsSessionOpened(int sessionId); 70 bool IsDeviceIdVailid(const std::string &peerDeviceId); 71 72 std::mutex sessionVecMutex_; 73 std::vector<std::shared_ptr<SoftbusSession>> sendSessionVec_; 74 std::shared_ptr<RecieveDataHandler> dataHandler_; 75 bool SetFileRecvListenerFlag_{true}; 76 }; 77 78 class RecieveDataHandler { 79 public: 80 virtual ~RecieveDataHandler() = default; 81 virtual void OnMessageHandle(const std::string &senderNetworkId, 82 int receiverSessionId, 83 const void *data, 84 unsigned int dataLen) = 0; 85 virtual void OnFileRecvHandle(const std::string &senderNetworkId, const char *filePath, int result) = 0; 86 virtual void OnSessionClosed(); 87 }; 88 } // namespace OHOS::FileManagement::CloudSync 89 90 #endif // OHOS_FILEMGMT_SESSION_MANAGER_H 91