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();
39 DISALLOW_COPY_AND_MOVE(SocketSessionManager);
40
41 int32_t Enable();
42 void Disable();
43
44 void AddSessionDeletedCallback(int32_t pid, std::function<void(SocketSessionPtr)> callback) override;
45 void RemoveSessionDeletedCallback(int32_t pid) override;
46 int32_t AllocSocketFd(const std::string& programName, int32_t moduleType, int32_t tokenType,
47 int32_t uid, int32_t pid, int32_t& clientFd) override;
48 SocketSessionPtr FindSessionByPid(int32_t pid) const override;
49
50 int32_t GetFd() const override;
51 void Dispatch(const struct epoll_event &ev) override;
52 void RegisterApplicationState() override;
53 void DeleteCollaborationServiceByName() override;
54
55 private:
56 class AppStateObserver final : public AppExecFwk::ApplicationStateObserverStub {
57 public:
AppStateObserver(SocketSessionManager & socketSessionManager)58 explicit AppStateObserver(SocketSessionManager &socketSessionManager)
59 : socketSessionManager_(socketSessionManager) {}
60 ~AppStateObserver() = default;
61 void OnProcessDied(const AppExecFwk::ProcessData &processData) override;
62 private:
63 SocketSessionManager &socketSessionManager_;
64 };
65
66 private:
67 bool SetBufferSize(int32_t sockFd, int32_t bufSize);
68 void DispatchOne();
69 void OnEpollIn(IEpollEventSource &source);
70 void ReleaseSession(int32_t fd);
71 void ReleaseSessionByPid(int32_t pid);
72 std::shared_ptr<SocketSession> FindSession(int32_t fd) const;
73 sptr<AppExecFwk::IAppMgr> GetAppMgr();
74 bool AddSession(std::shared_ptr<SocketSession> session);
75 void DumpSession(const std::string& title) const;
76 void NotifySessionDeleted(std::shared_ptr<SocketSession> sessionPtr);
77
78 mutable std::recursive_mutex mutex_;
79 EpollManager epollMgr_;
80 std::map<int32_t, std::shared_ptr<SocketSession>> sessions_;
81 std::map<int32_t, std::function<void(SocketSessionPtr)>> callbacks_;
82 sptr<AppStateObserver> appStateObserver_ { nullptr };
83 };
84
GetFd()85 inline int32_t SocketSessionManager::GetFd() const
86 {
87 return epollMgr_.GetFd();
88 }
89 } // namespace DeviceStatus
90 } // namespace Msdp
91 } // namespace OHOS
92 #endif // SOCKET_SESSION_MANAGER_H