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 SOFTBUS_DISTRIBUTED_DATA_MANAGER_H 17 #define SOFTBUS_DISTRIBUTED_DATA_MANAGER_H 18 19 #include <mutex> 20 21 #include "softbus_session_manager.h" 22 #include "softbus_session_server.h" 23 #include "softbus_session_proxy.h" 24 25 namespace OHOS::AVSession { 26 class SoftbusDistributedDataManager : public std::enable_shared_from_this<SoftbusDistributedDataManager> { 27 class SSListener : public SoftbusSessionListener { 28 public: SSListener(std::weak_ptr<SoftbusDistributedDataManager> ptr)29 explicit SSListener(std::weak_ptr<SoftbusDistributedDataManager> ptr) 30 { 31 ptr_ = ptr; 32 } 33 OnBind(int32_t socket,PeerSocketInfo info)34 void OnBind(int32_t socket, PeerSocketInfo info) override 35 { 36 std::shared_ptr<SoftbusDistributedDataManager> manager = ptr_.lock(); 37 if (manager != nullptr) { 38 manager->SessionOpened(socket, info); 39 } 40 } 41 OnShutdown(int32_t socket,ShutdownReason reason)42 void OnShutdown(int32_t socket, ShutdownReason reason) override 43 { 44 std::shared_ptr<SoftbusDistributedDataManager> manager = ptr_.lock(); 45 if (manager != nullptr) { 46 manager->SessionClosed(socket); 47 } 48 } 49 OnMessage(int32_t socket,const void * data,int32_t dataLen)50 void OnMessage(int32_t socket, const void *data, int32_t dataLen) override 51 { 52 std::shared_ptr<SoftbusDistributedDataManager> manager = ptr_.lock(); 53 std::string msg = std::string(static_cast<const char*>(data), dataLen); 54 if (manager != nullptr) { 55 manager->MessageReceived(socket, msg); 56 } 57 } 58 OnBytes(int32_t socket,const void * data,int32_t dataLen)59 void OnBytes(int32_t socket, const void *data, int32_t dataLen) override 60 { 61 std::shared_ptr<SoftbusDistributedDataManager> manager = ptr_.lock(); 62 std::string msg = std::string(static_cast<const char*>(data), dataLen); 63 if (manager != nullptr) { 64 manager->BytesReceived(socket, msg); 65 } 66 } 67 68 std::weak_ptr<SoftbusDistributedDataManager> ptr_; 69 }; 70 71 public: 72 SoftbusDistributedDataManager(); 73 74 ~SoftbusDistributedDataManager(); 75 76 void Init(); 77 78 void InitSessionServer(const std::string &pkg); 79 80 void CreateServer(const std::shared_ptr<SoftbusSessionServer> &server); 81 82 void DestroySessionServer(const std::string &pkg); 83 84 void ReleaseServer(const std::shared_ptr<SoftbusSessionServer> &server); 85 86 bool CreateProxy(const std::shared_ptr<SoftbusSessionProxy> &proxy, 87 const std::string &peerNetworkId, const std::string &packageName); 88 89 bool ReleaseProxy(const std::shared_ptr<SoftbusSessionProxy> &proxy, const std::string &peerNetworkId); 90 91 private: 92 void SessionOpened(int32_t socket, PeerSocketInfo info); 93 void SessionClosed(int32_t socket); 94 void MessageReceived(int32_t socket, const std::string &data); 95 void BytesReceived(int32_t socket, const std::string &data); 96 void OnSessionServerOpened(); 97 void OnSessionServerClosed(int32_t socket); 98 void OnMessageHandleReceived(int32_t socket, const std::string &data); 99 void OnBytesServerReceived(int32_t socket, const std::string &data); 100 int32_t ConnectRemoteDevice(const std::string &peerNetworkId, 101 const std::string &packageName, int32_t retryCount); 102 void OnSessionProxyOpened(int32_t socket); 103 void OnSessionProxyClosed(int32_t socket); 104 void OnBytesProxyReceived(int32_t socket, const std::string &data); 105 106 std::map<int32_t, std::shared_ptr<SoftbusSessionServer>> serverMap_; 107 std::shared_ptr<SSListener> ssListener_; 108 std::recursive_mutex softbusDistributedDataLock_; 109 std::map<const std::string, int32_t> mServerSocketMap_; 110 111 static constexpr const int MESSAGE_CODE_CONNECT_SERVER = 1; 112 113 PeerSocketInfo peerSocketInfo = { 114 .name = nullptr, 115 .networkId = nullptr, 116 .pkgName = nullptr, 117 .dataType = DATA_TYPE_BYTES, 118 }; 119 120 bool isServer_ = true; 121 std::map<const std::string, int32_t> mProxySocketMap_; 122 std::map<const std::string, std::map<int32_t, std::shared_ptr<SoftbusSessionProxy>>> mDeviceToProxyMap_; 123 static constexpr const int softbusLinkMaxRetryTimes = 5; 124 static constexpr int retryIntervalTime = 500; 125 std::map<const std::string, int32_t> mMap_; 126 }; 127 } // namespace OHOS::AVSession 128 129 #endif // SOFTBUS_DISTRIBUTED_DATA_MANAGER_H