1 /* 2 * Copyright (c) 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 20 #include <sys/socket.h> 21 #include <unistd.h> 22 23 #include "circle_stream_buffer.h" 24 #include "net_packet.h" 25 #ifdef OHOS_BUILD_ENABLE_RUST 26 #include "rust_binding.h" 27 #endif // OHOS_BUILD_ENABLE_RUST 28 29 namespace OHOS { 30 namespace Sensors { 31 class StreamSocket { 32 public: 33 using PacketCallBackFun = std::function<void(NetPacket &)>; 34 StreamSocket(); 35 virtual ~StreamSocket(); 36 void Close(); 37 int32_t GetFd() const; 38 #ifndef OHOS_BUILD_ENABLE_RUST 39 void OnReadPackets(CircleStreamBuffer &buf, PacketCallBackFun callbackFun); 40 #endif // OHOS_BUILD_ENABLE_RUST 41 DISALLOW_COPY_AND_MOVE(StreamSocket); 42 protected: 43 #ifdef OHOS_BUILD_ENABLE_RUST 44 struct RustDelete { operatorRustDelete45 void operator() (RustStreamSocket* raw) 46 { 47 StreamSocketDelete(raw); 48 } 49 }; 50 std::unique_ptr<RustStreamSocket, RustDelete> streamSocketPtr_ { StreamSocketCreate() }; 51 #else 52 int32_t fd_ { -1 }; 53 int32_t epollFd_ { -1 }; 54 #endif // OHOS_BUILD_ENABLE_RUST 55 }; 56 } // namespace Sensors 57 } // namespace OHOS 58 #endif // STREAM_SOCKET_H 59