• 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_SOCKET_H
16 #define UDS_SOCKET_H
17 
18 #include <atomic>
19 #include <string>
20 
21 #include <fcntl.h>
22 #include <sys/epoll.h>
23 #include <sys/socket.h>
24 #include <sys/types.h>
25 #include <sys/un.h>
26 #include <unistd.h>
27 
28 #include "nocopyable.h"
29 
30 #include "circle_stream_buffer.h"
31 #include "define_multimodal.h"
32 #include "net_packet.h"
33 
34 namespace OHOS {
35 namespace MMI {
36 class UDSSocket {
37 public:
38     UDSSocket();
39     DISALLOW_COPY_AND_MOVE(UDSSocket);
40     virtual ~UDSSocket();
41     using PacketCallBackFun = std::function<void(NetPacket&)>;
42 
43     int32_t EpollCreat(int32_t size);
44     int32_t EpollCtl(int32_t fd, int32_t op, struct epoll_event &event, int32_t epollFd = -1);
45     int32_t EpollWait(struct epoll_event &events, int32_t maxevents, int32_t timeout, int32_t epollFd = -1);
46     int32_t SetNonBlockMode(int32_t fd, bool isNonBlock = true);
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