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