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