1 /*
2 * Copyright (c) 2023 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 SOCKET_SESSION_MANAGER_H
17 #define SOCKET_SESSION_MANAGER_H
18
19 #include <functional>
20 #include <map>
21
22 #include "nocopyable.h"
23
24 #include "app_mgr_interface.h"
25 #include "application_state_observer_stub.h"
26
27 #include "epoll_manager.h"
28 #include "i_epoll_event_source.h"
29 #include "i_socket_session_manager.h"
30 #include "socket_session.h"
31
32 namespace OHOS {
33 namespace Msdp {
34 namespace DeviceStatus {
35 class SocketSessionManager final : public ISocketSessionManager, public IEpollEventSource {
36 public:
37 SocketSessionManager() = default;
38 ~SocketSessionManager() = default;
39 DISALLOW_COPY_AND_MOVE(SocketSessionManager);
40
41 int32_t Init();
42
43 void AddSessionDeletedCallback(int32_t pid, std::function<void(SocketSessionPtr)> callback) override;
44 void RemoveSessionDeletedCallback(int32_t pid) override;
45 int32_t AllocSocketFd(const std::string& programName, int32_t moduleType, int32_t tokenType,
46 int32_t uid, int32_t pid, int32_t& clientFd) override;
47 SocketSessionPtr FindSessionByPid(int32_t pid) const override;
48
49 int32_t GetFd() const override;
50 void Dispatch(const struct epoll_event &ev) override;
51 void RegisterApplicationState() override;
52 void DeleteCollaborationServiceByName() override;
53
54 private:
55 class AppStateObserver final : public AppExecFwk::ApplicationStateObserverStub {
56 public:
AppStateObserver(SocketSessionManager & socketSessionManager)57 explicit AppStateObserver(SocketSessionManager &socketSessionManager)
58 : socketSessionManager_(socketSessionManager) {}
59 ~AppStateObserver() = default;
60 void OnProcessDied(const AppExecFwk::ProcessData &processData) override;
61 private:
62 SocketSessionManager &socketSessionManager_;
63 };
64
65 private:
66 bool SetBufferSize(int32_t sockFd, int32_t bufSize);
67 void DispatchOne();
68 void ReleaseSession(int32_t fd);
69 void ReleaseSessionByPid(int32_t pid);
70 std::shared_ptr<SocketSession> FindSession(int32_t fd) const;
71 sptr<AppExecFwk::IAppMgr> GetAppMgr();
72 bool AddSession(std::shared_ptr<SocketSession> session);
73 void DumpSession(const std::string& title) const;
74 void NotifySessionDeleted(std::shared_ptr<SocketSession> sessionPtr);
75
76 static std::recursive_mutex mutex_;
77 EpollManager epollMgr_;
78 std::map<int32_t, std::shared_ptr<SocketSession>> sessions_;
79 std::map<int32_t, std::function<void(SocketSessionPtr)>> callbacks_;
80 sptr<AppStateObserver> appStateObserver_ { nullptr };
81 };
82
GetFd()83 inline int32_t SocketSessionManager::GetFd() const
84 {
85 return epollMgr_.GetFd();
86 }
87 } // namespace DeviceStatus
88 } // namespace Msdp
89 } // namespace OHOS
90 #endif // SOCKET_SESSION_MANAGER_H