1bae4d13cSopenharmony_ci/*
2bae4d13cSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3bae4d13cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4bae4d13cSopenharmony_ci * you may not use this file except in compliance with the License.
5bae4d13cSopenharmony_ci * You may obtain a copy of the License at
6bae4d13cSopenharmony_ci *
7bae4d13cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8bae4d13cSopenharmony_ci *
9bae4d13cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10bae4d13cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11bae4d13cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bae4d13cSopenharmony_ci * See the License for the specific language governing permissions and
13bae4d13cSopenharmony_ci * limitations under the License.
14bae4d13cSopenharmony_ci */
15bae4d13cSopenharmony_ci
16bae4d13cSopenharmony_ci#ifndef STREAM_SOCKET_H
17bae4d13cSopenharmony_ci#define STREAM_SOCKET_H
18bae4d13cSopenharmony_ci
19bae4d13cSopenharmony_ci#include <functional>
20bae4d13cSopenharmony_ci
21bae4d13cSopenharmony_ci#include <sys/socket.h>
22bae4d13cSopenharmony_ci#include <unistd.h>
23bae4d13cSopenharmony_ci
24bae4d13cSopenharmony_ci#include "nocopyable.h"
25bae4d13cSopenharmony_ci
26bae4d13cSopenharmony_ci#include "circle_stream_buffer.h"
27bae4d13cSopenharmony_ci#include "net_packet.h"
28bae4d13cSopenharmony_ci#ifdef OHOS_BUILD_ENABLE_RUST
29bae4d13cSopenharmony_ci#include "rust_binding.h"
30bae4d13cSopenharmony_ci#endif // OHOS_BUILD_ENABLE_RUST
31bae4d13cSopenharmony_ci
32bae4d13cSopenharmony_cinamespace OHOS {
33bae4d13cSopenharmony_cinamespace Sensors {
34bae4d13cSopenharmony_ciclass StreamSocket {
35bae4d13cSopenharmony_cipublic:
36bae4d13cSopenharmony_ci    using PacketCallBackFun = std::function<void(NetPacket &)>;
37bae4d13cSopenharmony_ci    StreamSocket();
38bae4d13cSopenharmony_ci    virtual ~StreamSocket();
39bae4d13cSopenharmony_ci    void Close();
40bae4d13cSopenharmony_ci    int32_t GetFd() const;
41bae4d13cSopenharmony_ci#ifndef OHOS_BUILD_ENABLE_RUST
42bae4d13cSopenharmony_ci    void OnReadPackets(CircleStreamBuffer &buf, PacketCallBackFun callbackFun);
43bae4d13cSopenharmony_ci#endif // OHOS_BUILD_ENABLE_RUST
44bae4d13cSopenharmony_ci    DISALLOW_COPY_AND_MOVE(StreamSocket);
45bae4d13cSopenharmony_ciprotected:
46bae4d13cSopenharmony_ci#ifdef OHOS_BUILD_ENABLE_RUST
47bae4d13cSopenharmony_ci    struct RustDelete {
48bae4d13cSopenharmony_ci        void operator() (RustStreamSocket* raw)
49bae4d13cSopenharmony_ci        {
50bae4d13cSopenharmony_ci            StreamSocketDelete(raw);
51bae4d13cSopenharmony_ci        }
52bae4d13cSopenharmony_ci    };
53bae4d13cSopenharmony_ci    std::unique_ptr<RustStreamSocket, RustDelete> streamSocketPtr_ { StreamSocketCreate() };
54bae4d13cSopenharmony_ci#else
55bae4d13cSopenharmony_ci    int32_t fd_ { -1 };
56bae4d13cSopenharmony_ci    int32_t epollFd_ { -1 };
57bae4d13cSopenharmony_ci#endif // OHOS_BUILD_ENABLE_RUST
58bae4d13cSopenharmony_ci};
59bae4d13cSopenharmony_ci} // namespace Sensors
60bae4d13cSopenharmony_ci} // namespace OHOS
61bae4d13cSopenharmony_ci#endif // STREAM_SOCKET_H
62