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
53 private:
54 class AppStateObserver final : public AppExecFwk::ApplicationStateObserverStub {
55 public:
AppStateObserver(SocketSessionManager & socketSessionManager)56 explicit AppStateObserver(SocketSessionManager &socketSessionManager)
57 : socketSessionManager_(socketSessionManager) {}
58 ~AppStateObserver() = default;
59 void OnProcessDied(const AppExecFwk::ProcessData &processData) override;
60 private:
61 SocketSessionManager &socketSessionManager_;
62 };
63
64 private:
65 bool SetBufferSize(int32_t sockFd, int32_t bufSize);
66 void DispatchOne();
67 void ReleaseSession(int32_t fd);
68 void ReleaseSessionByPid(int32_t pid);
69 std::shared_ptr<SocketSession> FindSession(int32_t fd) const;
70 sptr<AppExecFwk::IAppMgr> GetAppMgr();
71 bool AddSession(std::shared_ptr<SocketSession> session);
72 void DumpSession(const std::string& title) const;
73 void NotifySessionDeleted(std::shared_ptr<SocketSession> sessionPtr);
74
75 EpollManager epollMgr_;
76 std::map<int32_t, std::shared_ptr<SocketSession>> sessions_;
77 std::map<int32_t, std::function<void(SocketSessionPtr)>> callbacks_;
78 sptr<AppStateObserver> appStateObserver_ { nullptr };
79 };
80
GetFd()81 inline int32_t SocketSessionManager::GetFd() const
82 {
83 return epollMgr_.GetFd();
84 }
85 } // namespace DeviceStatus
86 } // namespace Msdp
87 } // namespace OHOS
88 #endif // SOCKET_SESSION_MANAGER_H