• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "session_service_impl.h"
17 
18 #include "session_impl.h"
19 #include "session_mock.h"
20 #include "softbus_bus_center.h"
21 #include "softbus_def.h"
22 #include "softbus_errcode.h"
23 #include "trans_log.h"
24 
25 namespace Communication {
26 namespace SoftBus {
27 std::shared_mutex ISessionService::instanceMutex_;
28 std::shared_ptr<ISessionService> ISessionService::instance_ = nullptr;
29 std::mutex SessionServiceImpl::listenerMutex_;
30 std::map<std::string, std::shared_ptr<ISessionListener>> SessionServiceImpl::listenerMap_;
31 std::mutex SessionServiceImpl::sessionMutex_;
32 std::map<int, std::shared_ptr<Session>> SessionServiceImpl::sessionMap_;
33 
GetInstance()34 std::shared_ptr<ISessionService> ISessionService::GetInstance()
35 {
36     std::shared_ptr<ISessionService> tmp = instance_;
37     if (tmp == nullptr) {
38         std::unique_lock<std::shared_mutex> instanceLock(instanceMutex_);
39         tmp = instance_;
40         if (tmp == nullptr) {
41             tmp = std::make_shared<SessionServiceImpl>();
42             instance_ = tmp;
43         }
44     }
45     return instance_;
46 }
47 
CreateSessionServer(const std::string & pkgName,const std::string & sessionName,std::shared_ptr<ISessionListener> listener)48 int SessionServiceImpl::CreateSessionServer(const std::string &pkgName, const std::string &sessionName,
49     std::shared_ptr<ISessionListener> listener)
50 {
51     if (pkgName.empty() || sessionName.empty() || listener == nullptr) {
52         TRANS_LOGW(TRANS_SDK, "invalid parameter");
53         return SOFTBUS_ERR;
54     }
55 
56     std::lock_guard<std::mutex> autoLock(listenerMutex_);
57     int ret = CreateSessionServerInner(pkgName.c_str(), sessionName.c_str());
58     if (ret == SOFTBUS_OK) {
59         listenerMap_.insert(std::pair<std::string, std::shared_ptr<ISessionListener>>(sessionName, listener));
60     }
61     return ret;
62 }
63 
RemoveSessionServer(const std::string & pkgName,const std::string & sessionName)64 int SessionServiceImpl::RemoveSessionServer(const std::string &pkgName, const std::string &sessionName)
65 {
66     if (pkgName.empty() || sessionName.empty()) {
67         TRANS_LOGW(TRANS_SDK, "invalid parameter");
68         return SOFTBUS_ERR;
69     }
70 
71     std::lock_guard<std::mutex> autoLock(listenerMutex_);
72     auto iter = listenerMap_.find(sessionName);
73     if (iter != listenerMap_.end()) {
74         listenerMap_.erase(iter);
75         return RemoveSessionServerInner(pkgName.c_str(), sessionName.c_str());
76     }
77     TRANS_LOGE(TRANS_SDK, "not find session server");
78     return SOFTBUS_ERR;
79 }
80 
OpenSession(const std::string & mySessionName,const std::string & peerSessionName,const std::string & peerNetworkId,const std::string & groupId,int flags)81 std::shared_ptr<Session> SessionServiceImpl::OpenSession(const std::string &mySessionName,
82     const std::string &peerSessionName, const std::string &peerNetworkId, const std::string &groupId, int flags)
83 {
84     TRANS_LOGD(TRANS_SDK, "enter.");
85     if (mySessionName.empty() || peerSessionName.empty() || peerNetworkId.empty()) {
86         return nullptr;
87     }
88     int sessionId = OpenSessionInner(mySessionName.c_str(), peerSessionName.c_str(),
89         peerNetworkId.c_str(), groupId.c_str(), flags);
90     if (sessionId <= 0) {
91         TRANS_LOGE(TRANS_SDK, "invalid sessionId.");
92         return nullptr;
93     }
94 
95     std::shared_ptr<Session> session;
96     std::lock_guard<std::mutex> autoLock(sessionMutex_);
97     auto iter = sessionMap_.find(sessionId);
98     if (iter != sessionMap_.end()) {
99         session = iter->second;
100         TRANS_LOGE(TRANS_SDK, "Session find");
101     }
102     TRANS_LOGD(TRANS_SDK, "ok");
103     return session;
104 }
105 
CloseSession(std::shared_ptr<Session> session)106 int SessionServiceImpl::CloseSession(std::shared_ptr<Session> session)
107 {
108     if (session == nullptr) {
109         TRANS_LOGW(TRANS_SDK, "invalid parameter");
110         return SOFTBUS_ERR;
111     }
112     int sessionId = session->GetSessionId();
113     if (sessionId <= 0) {
114         TRANS_LOGE(TRANS_SDK, "invalid sessionId. sessionId=%{public}d", sessionId);
115         return SOFTBUS_ERR;
116     }
117     CloseSessionInner(sessionId);
118     std::lock_guard<std::mutex> autoLock(sessionMutex_);
119     auto iter = sessionMap_.find(sessionId);
120     if (iter != sessionMap_.end()) {
121         sessionMap_.erase(sessionId);
122     }
123     return SOFTBUS_OK;
124 }
125 
GrantPermission(int uid,int pid,const std::string & busName)126 int SessionServiceImpl::GrantPermission(int uid, int pid, const std::string &busName)
127 {
128     if (uid < 0 || pid < 0 || busName.empty()) {
129         TRANS_LOGW(TRANS_SDK, "invalid parameter");
130         return SOFTBUS_ERR;
131     }
132     return GrantPermissionInner(uid, pid, busName.c_str());
133 }
134 
RemovePermission(const std::string & busName)135 int SessionServiceImpl::RemovePermission(const std::string &busName)
136 {
137     if (busName.empty()) {
138         TRANS_LOGW(TRANS_SDK, "invalid parameter");
139         return SOFTBUS_ERR;
140     }
141     return RemovePermissionInner(busName.c_str());
142 }
143 
OpenSessionCallback(int sessionId)144 int SessionServiceImpl::OpenSessionCallback(int sessionId)
145 {
146     TRANS_LOGD(TRANS_SDK, "enter.");
147     int isServer;
148     if (IsServerSideInner(sessionId, &isServer) != SOFTBUS_OK) {
149         return SOFTBUS_ERR;
150     }
151 
152     std::shared_ptr<Session> session = std::make_shared<SessionImpl>();
153     session->SetSessionId(sessionId);
154     char str[SESSION_NAME_SIZE_MAX];
155     if (GetMySessionNameInner(sessionId, str, SESSION_NAME_SIZE_MAX) != SOFTBUS_OK) {
156         return SOFTBUS_ERR;
157     }
158     std::string mySessionName(str);
159     session->SetMySessionName(mySessionName);
160 
161     if (GetPeerSessionNameInner(sessionId, str, SESSION_NAME_SIZE_MAX) != SOFTBUS_OK) {
162         return SOFTBUS_ERR;
163     }
164     std::string peerSessionName(str);
165     session->SetPeerSessionName(peerSessionName);
166 
167     char networkId[DEVICE_ID_SIZE_MAX];
168     if (GetPeerDeviceIdInner(sessionId, networkId, DEVICE_ID_SIZE_MAX) != SOFTBUS_OK) {
169         return SOFTBUS_ERR;
170     }
171     std::string peerNetworkId(networkId);
172     session->SetPeerDeviceId(peerNetworkId);
173     session->SetIsServer(isServer);
174 
175     std::lock_guard<std::mutex> autoLock(sessionMutex_);
176     sessionMap_.insert(std::pair<int, std::shared_ptr<Session>>(sessionId, session));
177 
178     std::shared_ptr<ISessionListener> listener;
179     if (GetSessionListenerOnSessionOpened(sessionId, listener, session) != SOFTBUS_OK) {
180         TRANS_LOGE(TRANS_SDK, "OpenSessionCallback get session listener failed");
181         return SOFTBUS_ERR;
182     }
183 
184     NodeBasicInfo info;
185     char pkgName[PKG_NAME_SIZE_MAX];
186     if (GetPkgNameInner(sessionId, pkgName, PKG_NAME_SIZE_MAX) != SOFTBUS_OK) {
187         return SOFTBUS_ERR;
188     }
189     if (GetLocalNodeDeviceInfo(pkgName, &info) != SOFTBUS_OK) {
190         return SOFTBUS_ERR;
191     }
192     session->SetDeviceId(info.networkId);
193 
194     int tmp;
195     if (GetPeerUidInner(sessionId, &tmp) != SOFTBUS_OK) {
196         return SOFTBUS_ERR;
197     }
198     session->SetPeerUid(static_cast<uid_t>(tmp));
199     if (GetPeerPidInner(sessionId, &tmp) != SOFTBUS_OK) {
200         return SOFTBUS_ERR;
201     }
202     session->SetPeerPid(static_cast<pid_t>(tmp));
203     TRANS_LOGI(TRANS_SDK, "Ok");
204     return listener->OnSessionOpened(session);
205 }
206 
CloseSessionCallback(int sessionId)207 void SessionServiceImpl::CloseSessionCallback(int sessionId)
208 {
209     std::shared_ptr<ISessionListener> listener;
210     std::shared_ptr<Session> session;
211     if (GetSessionListener(sessionId, listener, session) != SOFTBUS_OK) {
212         return;
213     }
214     listener->OnSessionClosed(session);
215 }
216 
BytesReceivedCallback(int sessionId,const void * data,unsigned int len)217 void SessionServiceImpl::BytesReceivedCallback(int sessionId, const void *data, unsigned int len)
218 {
219     std::shared_ptr<ISessionListener> listener;
220     std::shared_ptr<Session> session;
221     if (GetSessionListener(sessionId, listener, session) != SOFTBUS_OK) {
222         return;
223     }
224     const char *msg = static_cast<const char *>(data);
225     ssize_t lenMsg = static_cast<ssize_t>(len);
226     listener->OnBytesReceived(session, msg, lenMsg);
227 }
228 
MessageReceivedCallback(int sessionId,const void * data,unsigned int len)229 void SessionServiceImpl::MessageReceivedCallback(int sessionId, const void *data, unsigned int len)
230 {
231     std::shared_ptr<ISessionListener> listener;
232     std::shared_ptr<Session> session;
233     if (GetSessionListener(sessionId, listener, session) != SOFTBUS_OK) {
234         return;
235     }
236     const char *msg = static_cast<const char *>(data);
237     ssize_t lenMsg = static_cast<ssize_t>(len);
238     listener->OnMessageReceived(session, msg, lenMsg);
239 }
240 
GetSessionListener(int sessionId,std::shared_ptr<ISessionListener> & listener,std::shared_ptr<Session> & session)241 int SessionServiceImpl::GetSessionListener(int sessionId, std::shared_ptr<ISessionListener> &listener,
242     std::shared_ptr<Session> &session)
243 {
244     std::lock_guard<std::mutex> autoLock(sessionMutex_);
245     auto iter = sessionMap_.find(sessionId);
246     if (iter != sessionMap_.end()) {
247         session = iter->second;
248         std::lock_guard<std::mutex> autoLock(listenerMutex_);
249         auto iterListener = listenerMap_.find(session->GetMySessionName());
250         if (iterListener != listenerMap_.end()) {
251             listener = iterListener->second;
252             return SOFTBUS_OK;
253         }
254     }
255     return SOFTBUS_ERR;
256 }
257 
GetSessionListenerOnSessionOpened(int sessionId,std::shared_ptr<ISessionListener> & listener,std::shared_ptr<Session> & session)258 int SessionServiceImpl::GetSessionListenerOnSessionOpened(int sessionId,
259     std::shared_ptr<ISessionListener> &listener, std::shared_ptr<Session> &session)
260 {
261     (void)session;
262     char str[SESSION_NAME_SIZE_MAX];
263     if (GetMySessionNameInner(sessionId, str, SESSION_NAME_SIZE_MAX) != SOFTBUS_OK) {
264         return SOFTBUS_ERR;
265     }
266     std::string mySessionName(str);
267 
268     std::lock_guard<std::mutex> autoLock(listenerMutex_);
269     auto iterListener = listenerMap_.find(mySessionName);
270     if (iterListener != listenerMap_.end()) {
271         listener = iterListener->second;
272         return SOFTBUS_OK;
273     }
274     return SOFTBUS_ERR;
275 }
276 } // namespace SoftBus
277 } // namespace Communication
278