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