• 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 #include "soft_bus_server_socket.h"
17 
18 #include "remote_connect_listener_manager.h"
19 
20 #define LOG_TAG "USER_AUTH_SA"
21 namespace OHOS {
22 namespace UserIam {
23 namespace UserAuth {
ServerSocket(const int32_t socketId)24 ServerSocket::ServerSocket(const int32_t socketId)
25     : BaseSocket(socketId)
26 {
27     IAM_LOGI("server socket id is %{public}d.", socketId);
28 }
29 
SendMessage(const std::string & connectionName,const std::string & srcEndPoint,const std::string & destEndPoint,const std::shared_ptr<Attributes> & attributes,MsgCallback & callback)30 ResultCode ServerSocket::SendMessage(const std::string &connectionName, const std::string &srcEndPoint,
31     const std::string &destEndPoint, const std::shared_ptr<Attributes> &attributes, MsgCallback &callback)
32 {
33     IAM_LOGI("start.");
34     int32_t socketId = GetSocketIdByClientConnectionName(connectionName);
35     if (socketId == INVALID_SOCKET_ID) {
36         IAM_LOGE("socket id is invalid");
37         return GENERAL_ERROR;
38     }
39 
40     const ConnectionInfo connectionInfo = {
41         .socketId = socketId,
42         .connectionName = connectionName,
43         .srcEndPoint = srcEndPoint,
44         .destEndPoint = destEndPoint,
45         .attributes = attributes,
46         .callback = callback
47     };
48     return SendRequest(connectionInfo);
49 }
50 
OnBind(int32_t socketId,PeerSocketInfo info)51 void ServerSocket::OnBind(int32_t socketId, PeerSocketInfo info)
52 {
53     IAM_LOGI("start, socket id is %{public}d", socketId);
54     if (socketId <= INVALID_SOCKET_ID) {
55         IAM_LOGE("socket id invalid.");
56         return;
57     }
58 
59     if (info.networkId == nullptr) {
60         IAM_LOGE("networkId is invalid.");
61         return;
62     }
63 
64     std::string peerNetworkId(info.networkId);
65     AddServerSocket(socketId, peerNetworkId);
66 }
67 
OnShutdown(int32_t socketId,ShutdownReason reason)68 void ServerSocket::OnShutdown(int32_t socketId, ShutdownReason reason)
69 {
70     IAM_LOGI("start, socket id is %{public}d", socketId);
71     std::string connectionName = GetClientConnectionName(socketId);
72     if (!connectionName.empty()) {
73         RemoteConnectListenerManager::GetInstance().OnConnectionDown(connectionName);
74     }
75     DeleteServerSocket(socketId);
76     DeleteClientConnection(socketId);
77 }
78 
OnBytes(int32_t socketId,const void * data,uint32_t dataLen)79 void ServerSocket::OnBytes(int32_t socketId, const void *data, uint32_t dataLen)
80 {
81     IAM_LOGI("start, socket id is %{public}d", socketId);
82     std::string networkId = GetNetworkIdBySocketId(socketId);
83     if (networkId.empty()) {
84         IAM_LOGE("networkId id is null, socketId:%{public}d.", socketId);
85         return;
86     }
87 
88     std::shared_ptr<SoftBusMessage> softBusMessage = ParseMessage(networkId, const_cast<void *>(data), dataLen);
89     if (softBusMessage == nullptr) {
90         IAM_LOGE("serverSocket parse message fail.");
91         return;
92     }
93 
94     bool ack = softBusMessage->GetAckFlag();
95     std::string connectionName = softBusMessage->GetConnectionName();
96     if (ack == false && !connectionName.empty()) {
97         AddClientConnection(socketId, connectionName);
98     }
99 
100     ResultCode ret = ProcDataReceive(socketId, softBusMessage);
101     if (ret != SUCCESS) {
102         IAM_LOGE("HandleDataReceive fail, socketId:%{public}d.", socketId);
103         return;
104     }
105 }
106 
OnQos(int32_t socketId,QoSEvent eventId,const QosTV * qos,uint32_t qosCount)107 void ServerSocket::OnQos(int32_t socketId, QoSEvent eventId, const QosTV *qos, uint32_t qosCount)
108 {
109     IAM_LOGI("start, socket id is %{public}d", socketId);
110 }
111 
AddServerSocket(const int32_t socketId,const std::string & networkId)112 void ServerSocket::AddServerSocket(const int32_t socketId, const std::string &networkId)
113 {
114     IAM_LOGI("start, socketId %{public}d.", socketId);
115     IF_FALSE_LOGE_AND_RETURN(socketId != INVALID_SOCKET_ID);
116 
117     std::lock_guard<std::recursive_mutex> lock(socketMutex_);
118     auto iter = serverSocketBindMap_.find(socketId);
119     if (iter == serverSocketBindMap_.end()) {
120         serverSocketBindMap_.insert(std::pair<int32_t, std::string>(socketId, networkId));
121     } else {
122         iter->second = networkId;
123     }
124 }
125 
DeleteServerSocket(const int32_t socketId)126 void ServerSocket::DeleteServerSocket(const int32_t socketId)
127 {
128     IAM_LOGI("start, socketId %{public}d.", socketId);
129     IF_FALSE_LOGE_AND_RETURN(socketId != INVALID_SOCKET_ID);
130 
131     std::lock_guard<std::recursive_mutex> lock(socketMutex_);
132     auto iter = serverSocketBindMap_.find(socketId);
133     if (iter != serverSocketBindMap_.end()) {
134         serverSocketBindMap_.erase(iter);
135     }
136 }
137 
GetNetworkIdBySocketId(int32_t socketId)138 std::string ServerSocket::GetNetworkIdBySocketId(int32_t socketId)
139 {
140     IF_FALSE_LOGE_AND_RETURN_VAL(socketId != INVALID_SOCKET_ID, "");
141 
142     std::lock_guard<std::recursive_mutex> lock(socketMutex_);
143     std::string networkId;
144     auto iter = serverSocketBindMap_.find(socketId);
145     if (iter != serverSocketBindMap_.end()) {
146         networkId = iter->second;
147     }
148     return networkId;
149 }
150 
AddClientConnection(const int32_t socketId,const std::string & connectionName)151 void ServerSocket::AddClientConnection(const int32_t socketId, const std::string &connectionName)
152 {
153     IAM_LOGI("add socketId %{public}d connectionName %{public}s.", socketId, connectionName.c_str());
154     IF_FALSE_LOGE_AND_RETURN(socketId != INVALID_SOCKET_ID);
155 
156     std::lock_guard<std::recursive_mutex> lock(connectionMutex_);
157     auto iter = clientConnectionMap_.find(socketId);
158     if (iter == clientConnectionMap_.end()) {
159         clientConnectionMap_.insert(std::pair<int32_t, std::string>(socketId, connectionName));
160     }
161 }
162 
DeleteClientConnection(const int32_t socketId)163 void ServerSocket::DeleteClientConnection(const int32_t socketId)
164 {
165     IAM_LOGI("start, socketId %{public}d.", socketId);
166     IF_FALSE_LOGE_AND_RETURN(socketId != INVALID_SOCKET_ID);
167 
168     std::lock_guard<std::recursive_mutex> lock(connectionMutex_);
169     auto iter = clientConnectionMap_.find(socketId);
170     if (iter != clientConnectionMap_.end()) {
171         std::string connectionName = iter->second;
172         IAM_LOGI("delete socketId %{public}d connectionName %{public}s.", socketId, connectionName.c_str());
173         clientConnectionMap_.erase(iter);
174     }
175 }
176 
GetClientConnectionName(const int32_t socketId)177 std::string ServerSocket::GetClientConnectionName(const int32_t socketId)
178 {
179     IF_FALSE_LOGE_AND_RETURN_VAL(socketId != INVALID_SOCKET_ID, "");
180 
181     std::lock_guard<std::recursive_mutex> lock(connectionMutex_);
182     std::string ConnectionName;
183     auto iter = clientConnectionMap_.find(socketId);
184     if (iter != clientConnectionMap_.end()) {
185         ConnectionName = iter->second;
186     }
187     return ConnectionName;
188 }
189 
GetSocketIdByClientConnectionName(const std::string & connectionName)190 int32_t ServerSocket::GetSocketIdByClientConnectionName(const std::string &connectionName)
191 {
192     std::lock_guard<std::recursive_mutex> lock(connectionMutex_);
193     int32_t socketId = INVALID_SOCKET_ID;
194     for (const auto &iter : clientConnectionMap_) {
195         if (iter.second == connectionName) {
196             socketId = iter.first;
197             break;
198         }
199     }
200 
201     return socketId;
202 }
203 
GetConnectionName()204 std::string ServerSocket::GetConnectionName()
205 {
206     return "";
207 }
208 
GetNetworkId()209 std::string ServerSocket::GetNetworkId()
210 {
211     return "";
212 }
213 } // namespace UserAuth
214 } // namespace UserIam
215 } // namespace OHOS