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