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