1 /*
2  * Copyright (c) 2021-2022 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 UDS_SERVER_H
17 #define UDS_SERVER_H
18 
19 #include <functional>
20 #include <list>
21 #include <map>
22 #include <mutex>
23 
24 #include "nocopyable.h"
25 
26 #include "circle_stream_buffer.h"
27 #include "uds_socket.h"
28 
29 #include "i_uds_server.h"
30 
31 namespace OHOS {
32 namespace MMI {
33 enum EpollEventType {
34     EPOLL_EVENT_BEGIN = 0,
35     EPOLL_EVENT_INPUT = EPOLL_EVENT_BEGIN,
36     EPOLL_EVENT_SOCKET,
37     EPOLL_EVENT_SIGNAL,
38     EPOLL_EVENT_ETASK,
39     EPOLL_EVENT_END,
40 };
41 
42 struct mmi_epoll_event {
43     int32_t fd{ 0 };
44     EpollEventType event_type{ EPOLL_EVENT_BEGIN };
45 };
46 
47 using MsgServerFunCallback = std::function<void(SessionPtr, NetPacket&)>;
48 class UDSServer : public UDSSocket, public IUdsServer {
49 public:
50     UDSServer() = default;
51     DISALLOW_COPY_AND_MOVE(UDSServer);
52     virtual ~UDSServer();
53     void UdsStop();
54     bool SendMsg(int32_t fd, NetPacket& pkt);
55     void Multicast(const std::vector<int32_t>& fdList, NetPacket& pkt);
56     void Dump(int32_t fd, const std::vector<std::string> &args);
57     int32_t GetClientFd(int32_t pid) const;
58     int32_t GetClientPid(int32_t fd) const;
59     void AddSessionDeletedCallback(std::function<void(SessionPtr)> callback);
60     int32_t AddSocketPairInfo(const std::string& programName, const int32_t moduleType, const int32_t uid,
61         const int32_t pid, int32_t& serverFd, int32_t& toReturnClientFd, int32_t& tokenType) override;
62 
63     SessionPtr GetSession(int32_t fd) const;
64     SessionPtr GetSessionByPid(int32_t pid) const override;
65 
66     void AddEpollEvent(int32_t fd, std::shared_ptr<mmi_epoll_event> epollEvent);
67     void RemoveEpollEvent(int32_t fd);
68 
69 protected:
70     virtual void OnConnected(SessionPtr s);
71     virtual void OnDisconnected(SessionPtr s);
72     virtual int32_t AddEpoll(EpollEventType type, int32_t fd);
73 
74     void SetRecvFun(MsgServerFunCallback fun);
75     void ReleaseSession(int32_t fd, epoll_event& ev);
76     void OnPacket(int32_t fd, NetPacket& pkt);
77     void OnEpollRecv(int32_t fd, epoll_event& ev);
78     void OnEpollEvent(epoll_event& ev);
79     bool AddSession(SessionPtr ses);
80     void DelSession(int32_t fd);
81     void DumpSession(const std::string& title);
82     void NotifySessionDeleted(SessionPtr ses);
83     int32_t SetFdProperty(int32_t& tokenType, int32_t& serverFd, int32_t& toReturnClientFd);
84 
85 protected:
86     MsgServerFunCallback recvFun_ { nullptr };
87     std::map<int32_t, SessionPtr> sessionsMap_;
88     std::map<int32_t, int32_t> idxPidMap_;
89     std::map<int32_t, CircleStreamBuffer> circleBufMap_;
90     std::list<std::function<void(SessionPtr)>> callbacks_;
91     std::map<int32_t, std::shared_ptr<mmi_epoll_event>> epollEventMap_;
92     mutable int32_t pid_ { -1 };
93 };
94 } // namespace MMI
95 } // namespace OHOS
96 #endif // UDS_SERVER_H