1 /* 2 * Copyright (C) 2024 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_IPC_DBINDER_DATABUS_SOCKET_LISTENER_H 17 #define OHOS_IPC_DBINDER_DATABUS_SOCKET_LISTENER_H 18 19 #include <list> 20 #include <unordered_map> 21 22 #include "singleton.h" 23 24 #include "dbinder_softbus_client.h" 25 #include "ipc_types.h" 26 27 namespace OHOS { 28 static constexpr QosTV QOS_TV[] = { 29 { .qos = QOS_TYPE_MIN_BW, .value = RPC_QOS_MIN_BW }, 30 { .qos = QOS_TYPE_MAX_LATENCY, .value = RPC_QOS_MAX_LATENCY }, 31 { .qos = QOS_TYPE_MIN_LATENCY, .value = RPC_QOS_MIN_LATENCY } 32 }; 33 static constexpr uint32_t QOS_COUNT = static_cast<uint32_t>(sizeof(QOS_TV) / sizeof(QosTV)); 34 35 static const std::string DBINDER_PKG_NAME = "DBinderBus"; 36 static const std::string DBINDER_SOCKET_NAME_PREFIX = "DBinder"; 37 38 class DBinderSocketInfo { 39 public: 40 DBinderSocketInfo(const std::string &ownName, const std::string &peerName, const std::string &networkId); 41 DBinderSocketInfo() = default; 42 virtual ~DBinderSocketInfo() = default; 43 std::string GetOwnName() const; 44 std::string GetPeerName() const; 45 std::string GetNetworkId() const; 46 47 bool operator==(const DBinderSocketInfo &info) const 48 { 49 return (info.GetOwnName().compare(ownName_) == 0 && 50 info.GetPeerName().compare(peerName_) == 0 && 51 info.GetNetworkId().compare(networkId_) == 0); 52 } 53 54 private: 55 std::string ownName_; 56 std::string peerName_; 57 std::string networkId_; 58 }; 59 60 struct SocketInfoHash { operatorSocketInfoHash61 size_t operator()(const DBinderSocketInfo &info) const 62 { 63 return std::hash<std::string>()(info.GetOwnName()) ^ 64 std::hash<std::string>()(info.GetPeerName()) ^ 65 std::hash<std::string>()(info.GetNetworkId()); 66 } 67 }; 68 69 class DatabusSocketListener { 70 DECLARE_DELAYED_SINGLETON(DatabusSocketListener) 71 public: 72 int32_t StartServerListener(const std::string &ownName); 73 int32_t CreateClientSocket(const std::string &ownName, const std::string &peerName, 74 const std::string &networkId); 75 void ShutdownSocket(int32_t socket); 76 77 static void ServerOnBind(int32_t socket, PeerSocketInfo info); 78 static void ServerOnShutdown(int32_t socket, ShutdownReason reason); 79 static void ClientOnBind(int32_t socket, PeerSocketInfo info); 80 static void ClientOnShutdown(int32_t socket, ShutdownReason reason); 81 static void OnBytesReceived(int32_t socket, const void *data, uint32_t dataLen); 82 static void EraseDeviceLock(DBinderSocketInfo info); 83 static void RemoveSessionName(void); 84 85 private: 86 std::shared_ptr<std::mutex> QueryOrNewInfoMutex(DBinderSocketInfo socketInfo); 87 88 ISocketListener clientListener_ {}; 89 ISocketListener serverListener_ {}; 90 91 static inline std::mutex socketInfoMutex_; 92 static inline std::mutex deviceMutex_; 93 static inline std::unordered_map<DBinderSocketInfo, std::shared_ptr<std::mutex>, SocketInfoHash> infoMutexMap_ {}; 94 static inline std::unordered_map<DBinderSocketInfo, int32_t, SocketInfoHash> socketInfoMap_ {}; 95 }; 96 } // namespace OHOS 97 #endif // OHOS_IPC_DBINDER_DATABUS_SOCKET_LISTENER_H 98