• 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 #include <functional>
18 #include <list>
19 #include <map>
20 #include <mutex>
21 #include <thread>
22 
23 #include "nocopyable.h"
24 
25 #include "circle_stream_buffer.h"
26 #include "i_uds_server.h"
27 #include "uds_session.h"
28 #include "uds_socket.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 
38     EPOLL_EVENT_END,
39 };
40 
41 struct StreamBufData {
42     bool isOverflow = false;
43     OHOS::MMI::StreamBuffer sBuf;
44 };
45 using MsgServerFunCallback = std::function<void(SessionPtr, NetPacket&)>;
46 class UDSServer : public UDSSocket, public IUdsServer {
47 public:
48     UDSServer();
49     DISALLOW_COPY_AND_MOVE(UDSServer);
50     virtual ~UDSServer();
51     void UdsStop();
52     bool SendMsg(int32_t fd, NetPacket& pkt);
53     void Broadcast(NetPacket& pkt);
54     void Multicast(const std::vector<int32_t>& fdList, NetPacket& pkt);
55     void Dump(int32_t fd);
56     int32_t GetClientFd(int32_t pid);
57     int32_t GetClientPid(int32_t fd);
58     void AddSessionDeletedCallback(std::function<void(SessionPtr)> callback);
59 
60 public:
61     virtual int32_t AddSocketPairInfo(const std::string& programName, const int32_t moduleType, const int32_t uid,
62                                       const int32_t pid, int32_t& serverFd, int32_t& toReturnClientFd);
63     SessionPtr GetSession(int32_t fd) const;
64 
65 protected:
66     virtual void OnConnected(SessionPtr s);
67     virtual void OnDisconnected(SessionPtr s);
68     virtual int32_t AddEpoll(EpollEventType type, int32_t fd);
69 
70     void SetRecvFun(MsgServerFunCallback fun);
71     void ReleaseSession(int32_t fd, epoll_event& ev);
72     void OnPacket(int32_t fd, NetPacket& pkt);
73     void OnEpollRecv(int32_t fd, epoll_event& ev);
74     void OnEpollEvent(epoll_event& ev);
75     bool AddSession(SessionPtr ses);
76     void DelSession(int32_t fd);
77     void DumpSession(const std::string& title);
78     bool ClearDeadSessionInMap(const int32_t serverFd, const int32_t clientFd);
79 
80     void NotifySessionDeleted(SessionPtr ses);
81     void AddPermission(SessionPtr sess);
82 
83 protected:
84     std::mutex mux_;
85     bool isRunning_ = false;
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 };
92 } // namespace MMI
93 } // namespace OHOS
94 #endif // UDS_SERVER_H