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 #ifndef SESSION_SERVICE_IMPL_H 17 #define SESSION_SERVICE_IMPL_H 18 19 #include "Session.h" 20 21 #include <map> 22 #include <memory> 23 #include <string> 24 #include "ISessionListener.h" 25 #include "ISessionService.h" 26 27 namespace Communication { 28 namespace SoftBus { 29 class SessionServiceImpl : public ISessionService, public std::enable_shared_from_this<SessionServiceImpl> { 30 public: 31 SessionServiceImpl() = default; 32 33 ~SessionServiceImpl() override = default; 34 35 int CreateSessionServer(const std::string &pkgName, const std::string &sessionName, 36 std::shared_ptr<ISessionListener> listener) override; 37 38 int RemoveSessionServer(const std::string &pkgName, const std::string &sessionName) override; 39 40 std::shared_ptr<Session> OpenSession(const std::string &mySessionName, const std::string &peerSessionName, 41 const std::string &peerDeviceId, const std::string &groupId, int flags) override; 42 43 int CloseSession(std::shared_ptr<Session> session) override; 44 45 int GrantPermission(int uid, int pid, const std::string &busName) override; 46 47 int OpenSessionCallback(int sessionId); 48 49 void CloseSessionCallback(int sessionId); 50 51 void BytesReceivedCallback(int sessionId, const void *data, unsigned int len); 52 53 void MessageReceivedCallback(int sessionId, const void *data, unsigned int len); 54 55 private: 56 static std::mutex listenerMutex_; 57 static std::map<std::string, std::shared_ptr<ISessionListener>> listenerMap_; 58 59 static std::mutex sessionMutex_; 60 static std::map<int, std::shared_ptr<Session>> sessionMap_; 61 62 int GetSessionListener(int sessionId, std::shared_ptr<ISessionListener> &listener, 63 std::shared_ptr<Session> &session); 64 65 int GetSessionListenerOnSessionOpened(int sessionId, 66 std::shared_ptr<ISessionListener> &listener, std::shared_ptr<Session> &session); 67 68 int CreatNewSession(int sessionId); 69 }; 70 } // namespace SoftBus 71 } // namespace Communication 72 73 #endif // SESSION_SERVICE_IMPL_H 74