1 /*
2  * Copyright (c) 2021-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 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     int32_t CreateSessionServer(const std::string &pkgName, const std::string &sessionName,
36         std::shared_ptr<ISessionListener> listener) override;
37 
38     int32_t 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 &peerNetworkId, const std::string &groupId, int32_t flags) override;
42 
43     int32_t CloseSession(std::shared_ptr<Session> session) override;
44 
45     int32_t GrantPermission(int32_t uid, int32_t pid, const std::string &busName) override;
46 
47     int32_t RemovePermission(const std::string &busName) override;
48 
49     int32_t OpenSessionCallback(int32_t sessionId);
50 
51     void CloseSessionCallback(int32_t sessionId);
52 
53     void BytesReceivedCallback(int32_t sessionId, const void *data, uint32_t len);
54 
55     void MessageReceivedCallback(int32_t sessionId, const void *data, uint32_t len);
56 
57 private:
58     static std::mutex listenerMutex_;
59     static std::map<std::string, std::shared_ptr<ISessionListener>> listenerMap_;
60 
61     static std::mutex sessionMutex_;
62     static std::map<int32_t, std::shared_ptr<Session>> sessionMap_;
63 
64     int32_t GetSessionListener(int32_t sessionId, std::shared_ptr<ISessionListener> &listener,
65         std::shared_ptr<Session> &session);
66 
67     int32_t GetSessionListenerOnSessionOpened(int32_t sessionId,
68         std::shared_ptr<ISessionListener> &listener, std::shared_ptr<Session> &session);
69 
70     int32_t CreateSession(int32_t sessionId, const std::shared_ptr<Session> &session);
71 };
72 } // namespace SoftBus
73 } // namespace Communication
74 
75 #endif // SESSION_SERVICE_IMPL_H
76