• 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 "softbus_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         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "SessionServiceImpl:CreateSessionServer, 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         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "SessionServiceImpl:RemoveSessionServer, 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     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "SessionServiceImpl:RemoveSessionServer, not find session server");
78     return SOFTBUS_ERR;
79 }
80 
OpenSession(const std::string & mySessionName,const std::string & peerSessionName,const std::string & peerDeviceId,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 &peerDeviceId, const std::string &groupId, int flags)
83 {
84     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "SessionServiceImpl::OpenSession");
85     if (mySessionName.empty() || peerSessionName.empty() || peerDeviceId.empty()) {
86         return nullptr;
87     }
88     int sessionId = OpenSessionInner(mySessionName.c_str(), peerSessionName.c_str(),
89         peerDeviceId.c_str(), groupId.c_str(), flags);
90     if (sessionId <= 0) {
91         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "SessionServiceImpl:OpenSession, 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         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "SessionServiceImpl::Session Find");
101     }
102     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "SessionServiceImpl::OpenSession 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         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "SessionServiceImpl:CloseSession, invalid parameter");
110         return SOFTBUS_ERR;
111     }
112     int sessionId = session->GetSessionId();
113     if (sessionId <= 0) {
114         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "SessionServiceImpl:OpenSession, invalid 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         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "SessionServiceImpl:GrantPermission, invalid parameter");
130         return SOFTBUS_ERR;
131     }
132     return SOFTBUS_OK;
133 }
134 
CreatNewSession(int sessionId)135 int SessionServiceImpl::CreatNewSession(int sessionId)
136 {
137     std::shared_ptr<Session> session = std::make_shared<SessionImpl>();
138     session->SetSessionId(sessionId);
139     char str[SESSION_NAME_SIZE_MAX];
140     int ret = GetMySessionNameInner(sessionId, str, SESSION_NAME_SIZE_MAX);
141     if (ret != SOFTBUS_OK) {
142         return ret;
143     }
144     std::string mySessionName(str);
145     session->SetMySessionName(mySessionName);
146     ret = GetPeerSessionNameInner(sessionId, str, SESSION_NAME_SIZE_MAX);
147     if (ret != SOFTBUS_OK) {
148         return ret;
149     }
150     std::string peerSessionName(str);
151     session->SetPeerSessionName(peerSessionName);
152     ret = GetPeerDeviceIdInner(sessionId, str, SESSION_NAME_SIZE_MAX);
153     if (ret != SOFTBUS_OK) {
154         return ret;
155     }
156     std::string peerDevId(str);
157     session->SetPeerDeviceId(peerDevId);
158     session->SetIsServer(true);
159     std::lock_guard<std::mutex> autoLock(sessionMutex_);
160     sessionMap_.insert(std::pair<int, std::shared_ptr<Session>>(sessionId, session));
161     return SOFTBUS_OK;
162 }
163 
OpenSessionCallback(int sessionId)164 int SessionServiceImpl::OpenSessionCallback(int sessionId)
165 {
166     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "SessionServiceImpl::OpenSessionCallback");
167     int isServer;
168     if (IsServerSideInner(sessionId, &isServer) != SOFTBUS_OK) {
169         return SOFTBUS_ERR;
170     }
171 
172     std::shared_ptr<Session> session = std::make_shared<SessionImpl>();
173     session->SetSessionId(sessionId);
174     char str[SESSION_NAME_SIZE_MAX];
175     if (GetMySessionNameInner(sessionId, str, SESSION_NAME_SIZE_MAX) != SOFTBUS_OK) {
176         return SOFTBUS_ERR;
177     }
178     std::string mySessionName(str);
179     session->SetMySessionName(mySessionName);
180 
181     if (GetPeerSessionNameInner(sessionId, str, SESSION_NAME_SIZE_MAX) != SOFTBUS_OK) {
182         return SOFTBUS_ERR;
183     }
184     std::string peerSessionName(str);
185     session->SetPeerSessionName(peerSessionName);
186 
187     char deviceId[DEVICE_ID_SIZE_MAX];
188     if (GetPeerDeviceIdInner(sessionId, deviceId, DEVICE_ID_SIZE_MAX) != SOFTBUS_OK) {
189         return SOFTBUS_ERR;
190     }
191     std::string peerDeviceId(deviceId);
192     session->SetPeerDeviceId(peerDeviceId);
193     session->SetIsServer(isServer);
194 
195     std::lock_guard<std::mutex> autoLock(sessionMutex_);
196     sessionMap_.insert(std::pair<int, std::shared_ptr<Session>>(sessionId, session));
197 
198     std::shared_ptr<ISessionListener> listener;
199     if (GetSessionListenerOnSessionOpened(sessionId, listener, session) != SOFTBUS_OK) {
200         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "OpenSessionCallback get session listener failed");
201         return SOFTBUS_ERR;
202     }
203 
204     NodeBasicInfo info;
205     char pkgName[PKG_NAME_SIZE_MAX];
206     if (GetPkgNameInner(sessionId, pkgName, PKG_NAME_SIZE_MAX) != SOFTBUS_OK) {
207         return SOFTBUS_ERR;
208     }
209     if (GetLocalNodeDeviceInfo(pkgName, &info) != SOFTBUS_OK) {
210         return SOFTBUS_ERR;
211     }
212     session->SetDeviceId(info.networkId);
213 
214     int tmp;
215     if (GetPeerUidInner(sessionId, &tmp) != SOFTBUS_OK) {
216         return SOFTBUS_ERR;
217     }
218     session->SetPeerUid(static_cast<uid_t>(tmp));
219     if (GetPeerPidInner(sessionId, &tmp) != SOFTBUS_OK) {
220         return SOFTBUS_ERR;
221     }
222     session->SetPeerPid(static_cast<pid_t>(tmp));
223     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "SessionServiceImpl::OpenSessionCallback Ok");
224     return listener->OnSessionOpened(session);
225 }
226 
CloseSessionCallback(int sessionId)227 void SessionServiceImpl::CloseSessionCallback(int sessionId)
228 {
229     std::shared_ptr<ISessionListener> listener;
230     std::shared_ptr<Session> session;
231     if (GetSessionListener(sessionId, listener, session) != SOFTBUS_OK) {
232         return;
233     }
234     listener->OnSessionClosed(session);
235 }
236 
BytesReceivedCallback(int sessionId,const void * data,unsigned int len)237 void SessionServiceImpl::BytesReceivedCallback(int sessionId, const void *data, unsigned int len)
238 {
239     std::shared_ptr<ISessionListener> listener;
240     std::shared_ptr<Session> session;
241     if (GetSessionListener(sessionId, listener, session) != SOFTBUS_OK) {
242         return;
243     }
244     const char *msg = static_cast<const char *>(data);
245     ssize_t lenMsg = static_cast<ssize_t>(len);
246     listener->OnBytesReceived(session, msg, lenMsg);
247 }
248 
MessageReceivedCallback(int sessionId,const void * data,unsigned int len)249 void SessionServiceImpl::MessageReceivedCallback(int sessionId, const void *data, unsigned int len)
250 {
251     std::shared_ptr<ISessionListener> listener;
252     std::shared_ptr<Session> session;
253     if (GetSessionListener(sessionId, listener, session) != SOFTBUS_OK) {
254         return;
255     }
256     const char *msg = static_cast<const char *>(data);
257     ssize_t lenMsg = static_cast<ssize_t>(len);
258     listener->OnMessageReceived(session, msg, lenMsg);
259 }
260 
GetSessionListener(int sessionId,std::shared_ptr<ISessionListener> & listener,std::shared_ptr<Session> & session)261 int SessionServiceImpl::GetSessionListener(int sessionId, std::shared_ptr<ISessionListener> &listener,
262     std::shared_ptr<Session> &session)
263 {
264     std::lock_guard<std::mutex> autoLock(sessionMutex_);
265     auto iter = sessionMap_.find(sessionId);
266     if (iter != sessionMap_.end()) {
267         session = iter->second;
268         std::lock_guard<std::mutex> autoLock(listenerMutex_);
269         auto iterListener = listenerMap_.find(session->GetMySessionName());
270         if (iterListener != listenerMap_.end()) {
271             listener = iterListener->second;
272             return SOFTBUS_OK;
273         }
274     }
275     return SOFTBUS_ERR;
276 }
277 
GetSessionListenerOnSessionOpened(int sessionId,std::shared_ptr<ISessionListener> & listener,std::shared_ptr<Session> & session)278 int SessionServiceImpl::GetSessionListenerOnSessionOpened(int sessionId,
279     std::shared_ptr<ISessionListener> &listener, std::shared_ptr<Session> &session)
280 {
281     (void)session;
282     char str[SESSION_NAME_SIZE_MAX];
283     if (GetMySessionNameInner(sessionId, str, SESSION_NAME_SIZE_MAX) != SOFTBUS_OK) {
284         return SOFTBUS_ERR;
285     }
286     std::string mySessionName(str);
287 
288     std::lock_guard<std::mutex> autoLock(listenerMutex_);
289     auto iterListener = listenerMap_.find(mySessionName);
290     if (iterListener != listenerMap_.end()) {
291         listener = iterListener->second;
292         return SOFTBUS_OK;
293     }
294     return SOFTBUS_ERR;
295 }
296 } // namespace SoftBus
297 } // namespace Communication
298