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