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_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 OnUserUnlocked() override; 58 59 private: 60 void CreateServer(); 61 void RemoveServer(); 62 63 std::shared_ptr<SoftbusSession> CreateSession(SoftbusSession::DataType type, const std::string &peerDeviceId); 64 std::shared_ptr<SoftbusSession> GetSendSession(SoftbusSession::DataType type, const std::string &peerDeviceId); 65 void CacheSendSession(std::shared_ptr<SoftbusSession> session); 66 void RemoveSendSession(int sessionId); 67 68 bool IsSessionOpened(int sessionId); 69 bool IsDeviceIdVailid(const std::string &peerDeviceId); 70 71 std::mutex sessionVecMutex_; 72 std::vector<std::shared_ptr<SoftbusSession>> sendSessionVec_; 73 std::shared_ptr<RecieveDataHandler> dataHandler_; 74 bool SetFileRecvListenerFlag_{true}; 75 }; 76 77 class RecieveDataHandler { 78 public: 79 virtual ~RecieveDataHandler() = default; 80 virtual void OnMessageHandle(const std::string &senderNetworkId, 81 int receiverSessionId, 82 const void *data, 83 unsigned int dataLen) = 0; 84 virtual void OnFileRecvHandle(const std::string &senderNetworkId, const char *filePath, int result) = 0; 85 virtual void OnSessionClosed(); 86 }; 87 } // namespace OHOS::FileManagement::CloudSync 88 89 #endif // OHOS_FILEMGMT_SESSION_MANAGER_H 90