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_SOCKET_H
17 #define UDS_SOCKET_H
18 
19 #include <atomic>
20 #include <string>
21 
22 #include <fcntl.h>
23 #include <sys/epoll.h>
24 #include <sys/socket.h>
25 #include <sys/types.h>
26 #include <sys/un.h>
27 #include <unistd.h>
28 
29 #include "nocopyable.h"
30 
31 #include "circle_stream_buffer.h"
32 #include "define_multimodal.h"
33 #include "net_packet.h"
34 
35 namespace OHOS {
36 namespace MMI {
37 class UDSSocket {
38 public:
39     UDSSocket();
40     DISALLOW_COPY_AND_MOVE(UDSSocket);
41     virtual ~UDSSocket();
42     using PacketCallBackFun = std::function<void(NetPacket&)>;
43 
44     int32_t EpollCreate(int32_t size);
45     int32_t EpollCtl(int32_t fd, int32_t op, struct epoll_event &event, int32_t epollFd = -1);
46     int32_t EpollWait(struct epoll_event &events, int32_t maxevents, int32_t timeout, int32_t epollFd = -1);
47     void OnReadPackets(CircleStreamBuffer &buf, PacketCallBackFun callbackFun);
48     void EpollClose();
49     void Close();
50 
GetFd()51     int32_t GetFd() const
52     {
53         return fd_;
54     }
GetEpollFd()55     int32_t GetEpollFd() const
56     {
57         return epollFd_;
58     }
59 
60 protected:
61     int32_t fd_ { -1 };
62     int32_t epollFd_ { -1 };
63 };
64 } // namespace MMI
65 } // namespace OHOS
66 #endif // UDS_SOCKET_H